1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
   |  
 
import serial
import math
import time
from threading import Thread, RLock
import numpy as np 
#import motorControler
import RPi.GPIO as GPIO
 
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
 
 
led=23
GPIO.setup(led,GPIO.OUT)
GPIO.output(led,0)
 
PWM_motor=4
GPIO.setup(PWM_motor,GPIO.OUT)
LidarMotor = GPIO.PWM(PWM_motor,50)#50Hz
LidarMotor.start(0)
 
 
 
Start_Scan = "\xA5\x20" #Begins scanning
Force_Scan = "\xA5\x21" #Overrides anything preventing a scan
Health = "\xA5\x52" #Returns the state of the Lidar
Stop_Scan = "\xA5\x25" #Stops the scan
RESET = "\xA5\x40" #Resets the device
 
verrou = RLock()
 
class Lidar(Thread):
	distance = 0
	angle = 0
 
 
	def __init__(self,ser):
		Thread.__init__(self)
 
		self.port = ser  
 
	def run(self): 
		lock = False     
		lock = self.startScan(self.port)
 
		if lock == True:
			self.getPoints(self.port)
		else:
			print "Exiting"
 
 
 
	def startScan(self, port):
 
		print"connecting"
		lock = False
 
		while lock == False:
 
			print"..."
 
			port.write(RESET)
			time.sleep(1)
			port.write(Start_Scan)
			try:
				line=""
				for i in range(0, 250):
					line = port.read(7)
 
					if line[0:2] == "\xa5\x5a" :#flag 1&2
						lock = True
						print"lidar Enable"
						break;
					else:
						line = ""
			except KeyboardInterrupt:
				break
		return lock
 
	def getPoints(self, port):
		memory = 0
		count = 0
		i=0
 
		line = ""  #reset line
 
		while True:
 
			try:
 
 
				line = port.read(5) 
				if (len(line) )== 5 :# wait 5 bytes
 
 
					# if chackData1 = 1 trame is good
					checkData1= int((line[1].encode("hex")), 16)&0x01
 
					#if chackData2 = 1 trame is good
					checkData2= int((line[0].encode("hex")), 16)
					if ((checkData2&0x02)/2) != (checkData2&0x01) :
						checkData2=1
					else:
						checkData2=0
 
 
					if checkData1 ==1 and checkData2 ==1 :#trame is good
 
						Lidar.distance,Lidar.angle=self.point_Polar(line)
 
 
 
 
					else :#trame is not good
						print"ValueError : lidar disable"
						break
 
					line=""
 
			except KeyboardInterrupt:
				break
 
 
	def point_Polar(self,paquet):
		#Get Distance
		distance = paquet[4].encode("hex") + paquet[3].encode("hex")
		distance = int(distance, 16)
		distance = distance / 4 #instructions from data sheet
 
		#Get Angle
		angle = paquet[2].encode("hex") + paquet[1].encode("hex")
		angle = int(angle, 16)
		angle = angle/128 #instruction from data sheet
 
		return(distance, angle)
 
class Affichage(Thread):
	def __init__(self):
		Thread.__init__(self)
		self.u = 0
 
	def run(self):
 
		print("la distance : {0} mm   angle : {1}".format(Lidar.distance,Lidar.angle))
 
 
 
LidarMotor.start(40)
ser = serial.Serial('/dev/ttyAMA0', 115200, timeout = 1)
ser.setDTR(False)
print ser.name
while True:
 
 
	t1 = Lidar(ser)
	t2 = Affichage()
	t1.start()
	t2.start()
 
 
	t1.join()
	t2.join() | 
Partager