def go_Forward(speed, running_time): global forward0, forward1 motor.setSpeed(speed) motor.motor0(forward0) motor.motor1(forward1) time.sleep(running_time) motor.stop()
def go_Backward(speed, running_time): global backward0, backward1 motor.setSpeed(speed) motor.motor0(backward0) motor.motor1(backward1) time.sleep(running_time) motor.stop()
def _twist_callback(self, msg): self._cmd_lin_vel = msg.linear.x self._cmd_ang_vel = msg.angular.z print "x: " + str(self._cmd_lin_vel) print "yaw: " + str(self._cmd_ang_vel) # Disable both motors if self._cmd_lin_vel == 0 and self._cmd_ang_vel == 0: motor.motorStop() else: # Forward driving if self._cmd_lin_vel > 0: self._left_motor_dir = 0 self._right_motor_dir = 0 # Reverse driving elif self._cmd_lin_vel < 0: self._left_motor_dir = 1 self._right_motor_dir = 1 # CCW Rotation elif self._cmd_ang_vel < 0: self._left_motor_dir = 1 self._right_motor_dir = 0 # CW Rotation elif self._cmd_ang_vel > 0: self._left_motor_dir = 0 self._right_motor_dir = 1 motor.motor1(1, self._left_motor_dir, 100) motor.motor(1, self._right_motor_dir, 100)
def calibrate_motor_right_reverse(request): global forward1 if forward1 == "True": forward1 = "False" else: forward1 = "True" motor.motor1(forward1) text = 'Right motor reverse to ', forward1 return HttpResponse(text)
def loop(): global offset_x, offset_y, offset, forward0, forward1 while True: print 'Waiting for connection...' # Waiting for connection. Once receiving a connection, the function accept() returns a separate # client socket for the subsequent communication. By default, the function accept() is a blocking # one, which means it is suspended before the connection comes. tcpCliSock, addr = tcpSerSock.accept() print '...connected from :', addr # Print the IP address of the client connected with the server. while True: data = tcpCliSock.recv( BUFSIZ) # Receive data sent from the client. # Analyze the command received and control the car accordingly. if not data: break #--------Motor calibration---------- if data == 'motor_run': print 'motor moving forward' motor.setSpeed(50) motor.motor0(forward0) motor.motor1(forward1) elif data[0:9] == 'leftmotor': forward0 = data[9:] motor.motor0(forward0) elif data[0:10] == 'rightmotor': forward1 = data[10:] motor.motor1(forward1) elif data == 'motor_stop': print 'motor stop' motor.stop() #--------------------------------- #-------Turing calibration------ elif data[0:7] == 'offset=': offset = int(data[7:]) car_dir.calibrate(offset) #-------------------------------- #----------Mount calibration--------- elif data[0:8] == 'offsetx=': offset_x = int(data[8:]) print 'Mount offset x', offset_x video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsety=': offset_y = int(data[8:]) print 'Mount offset y', offset_y video_dir.calibrate(offset_x, offset_y) #---------------------------------------- else: print 'cmd error !'
def loop(): global offset_x, offset_y, offset, forward0, forward1 while True: print 'Waiting for connection...' # Waiting for connection. Once receiving a connection, the function accept() returns a separate # client socket for the subsequent communication. By default, the function accept() is a blocking # one, which means it is suspended before the connection comes. tcpCliSock, addr = tcpSerSock.accept() print '...connected from :', addr # Print the IP address of the client connected with the server. while True: data = tcpCliSock.recv(BUFSIZ) # Receive data sent from the client. # Analyze the command received and control the car accordingly. if not data: break #--------Motor calibration---------- if data == 'motor_run': print 'motor moving forward' motor.setSpeed(50) motor.motor0(forward0) motor.motor1(forward1) elif data[0:9] == 'leftmotor': forward0 = data[9:] motor.motor0(forward0) elif data[0:10] == 'rightmotor': forward1 = data[10:] motor.motor1(forward1) elif data == 'motor_stop': print 'motor stop' motor.stop() #--------------------------------- #-------Turing calibration------ elif data[0:7] == 'offset=': offset = int(data[7:]) car_dir.calibrate(offset) #-------------------------------- #----------Mount calibration--------- elif data[0:8] == 'offsetx=': offset_x = int(data[8:]) print 'Mount offset x', offset_x video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsety=': offset_y = int(data[8:]) print 'Mount offset y', offset_y video_dir.calibrate(offset_x, offset_y) #---------------------------------------- else: print 'cmd error !'
def loop(): global offset_x, offset_y, offset, forward0, forward1 while True: print 'Waiting for connection...' # Waiting for connection. Once receiving a connection, the function accept() returns a separate # client socket for the subsequent communication. By default, the function accept() is a blocking # one, which means it is suspended before the connection comes. tcpCliSock, addr = tcpSerSock.accept() print '...connected from :', addr # Print the IP address of the client connected with the server. while True: data = tcpCliSock.recv(BUFSIZ) # Receive data sent from the client. # Analyze the command received and control the car accordingly. if not data: break #--------Motor calibration---------- if data == 'motor_run': print 'motor moving forward' motor.setSpeed(50) motor.motor0(forward0) motor.motor1(forward1) elif data[0:9] == 'leftmotor': forward0 = data[9:] motor.motor0(forward0)
def loop(): global offset_x, offset_y, offset, forward0, forward1 while True: print 'Waiting for connection...' # Waiting for connection. Once receiving a connection, the function accept() returns a separate # client socket for the subsequent communication. By default, the function accept() is a blocking # one, which means it is suspended before the connection comes. tcpCliSock, addr = tcpSerSock.accept() print '...connected from :', addr # Print the IP address of the client connected with the server. while True: data = tcpCliSock.recv( BUFSIZ) # Receive data sent from the client. # Analyze the command received and control the car accordingly. if not data: break # --------Motor calibration---------- if data == 'motor_run': print 'motor moving forward' motor.setSpeed(50) motor.motor0(forward0) motor.motor1(forward1) elif data[0:9] == 'leftmotor': forward0 = data[9:] motor.motor0(forward0) elif data[0:10] == 'rightmotor': forward1 = data[10:] motor.motor1(forward1) # -------------Added-------------- elif data == 'leftreverse': if forward0 == "True": forward0 = "False" else: forward0 = "True" print "left motor reversed to", forward0 motor.motor0(forward0) elif data == 'rightreverse': if forward1 == "True": forward1 = "False" else: forward1 = "True" print "right motor reversed to", forward1 motor.motor1(forward1) elif data == 'motor_stop': print 'motor stop' motor.stop() # --------------------------------- # -------Turing calibration------ elif data[0:7] == 'offset=': offset = int(data[7:]) car_dir.calibrate(offset) # -------------------------------- # ----------Mount calibration--------- elif data[0:8] == 'offsetx=': offset_x = int(data[8:]) print 'Mount offset x', offset_x video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsety=': offset_y = int(data[8:]) print 'Mount offset y', offset_y video_dir.calibrate(offset_x, offset_y) # ---------------------------------------- # -------Turing calibration 2------ elif data[0:7] == 'offset+': offset = offset + int(data[7:]) print 'Turning offset', offset car_dir.calibrate(offset) elif data[0:7] == 'offset-': offset = offset - int(data[7:]) print 'Turning offset', offset car_dir.calibrate(offset) # -------------------------------- # ----------Mount calibration 2--------- elif data[0:8] == 'offsetx+': offset_x = offset_x + int(data[8:]) print 'Mount offset x', offset_x video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsetx-': offset_x = offset_x - int(data[8:]) print 'Mount offset x', offset_x video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsety+': offset_y = offset_y + int(data[8:]) print 'Mount offset y', offset_y video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsety-': offset_y = offset_y - int(data[8:]) print 'Mount offset y', offset_y video_dir.calibrate(offset_x, offset_y) # ---------------------------------------- # ----------Confirm-------------------- elif data == 'confirm': config = 'offset_x = %s\noffset_y = %s\noffset = %s\nforward0 = %s\nforward1 = %s\n ' % ( offset_x, offset_y, offset, forward0, forward1) print '' print '*********************************' print ' You are setting config file to:' print '*********************************' print config print '*********************************' print '' fd = open('config', 'w') fd.write(config) fd.close() motor.stop() tcpCliSock.close() quit() else: print 'Command Error! Cannot recognize command: ' + data
data = tcpCliSock.recv(BUFSIZ) # Receive data sent from the client. # Analyze the command received and control the car accordingly. if not data: break #--------Motor calibration---------- if data == 'motor_run': print 'motor moving forward' motor.setSpeed(50) motor.motor0(forward0) motor.motor1(forward1) elif data[0:9] == 'leftmotor': forward0 = data[9:] motor.motor0(forward0) elif data[0:10] == 'rightmotor': forward1 = data[10:] motor.motor1(forward1) # -------------Added-------------- elif data == 'leftreverse': if forward0 == "True": forward0 = "False" else: forward0 = "True" print "left motor reversed to", forward0 motor.motor0(forward0) elif data == 'rightreverse': if forward1 == "True": forward1 = "False" else: forward1 = "True" print "right motor reversed to", forward1
def loop(): global offset_x, offset_y, offset, forward0, forward1 while True: print 'Waiting for connection...' # Waiting for connection. Once receiving a connection, the function accept() returns a separate # client socket for the subsequent communication. By default, the function accept() is a blocking # one, which means it is suspended before the connection comes. tcpCliSock, addr = tcpSerSock.accept() print '...connected from :', addr # Print the IP address of the client connected with the server. while True: data = tcpCliSock.recv(BUFSIZ) # Receive data sent from the client. # Analyze the command received and control the car accordingly. if not data: break #--------Motor calibration---------- if data == 'motor_run': print 'motor moving forward' motor.setSpeed(50) motor.motor0(forward0) motor.motor1(forward1) elif data[0:9] == 'leftmotor': forward0 = data[9:] motor.motor0(forward0) elif data[0:10] == 'rightmotor': forward1 = data[10:] motor.motor1(forward1) # -------------Added-------------- elif data == 'leftreverse': if forward0 == "True": forward0 = "False" else: forward0 = "True" print "left motor reversed to", forward0 motor.motor0(forward0) elif data == 'rightreverse': if forward1 == "True": forward1 = "False" else: forward1 = "True" print "right motor reversed to", forward1 motor.motor1(forward1) elif data == 'motor_stop': print 'motor stop' motor.stop() #--------------------------------- #-------Turing calibration------ elif data[0:7] == 'offset=': offset = int(data[7:]) car_dir.calibrate(offset) #-------------------------------- #----------Mount calibration--------- elif data[0:8] == 'offsetx=': offset_x = int(data[8:]) print 'Mount offset x', offset_x video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsety=': offset_y = int(data[8:]) print 'Mount offset y', offset_y video_dir.calibrate(offset_x, offset_y) #---------------------------------------- #-------Turing calibration 2------ elif data[0:7] == 'offset+': offset = offset + int(data[7:]) print 'Turning offset', offset car_dir.calibrate(offset) elif data[0:7] == 'offset-': offset = offset - int(data[7:]) print 'Turning offset', offset car_dir.calibrate(offset) #-------------------------------- #----------Mount calibration 2--------- elif data[0:8] == 'offsetx+': offset_x = offset_x + int(data[8:]) print 'Mount offset x', offset_x video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsetx-': offset_x = offset_x - int(data[8:]) print 'Mount offset x', offset_x video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsety+': offset_y = offset_y + int(data[8:]) print 'Mount offset y', offset_y video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsety-': offset_y = offset_y - int(data[8:]) print 'Mount offset y', offset_y video_dir.calibrate(offset_x, offset_y) #---------------------------------------- #----------Confirm-------------------- elif data == 'confirm': config = 'offset_x = %s\noffset_y = %s\noffset = %s\nforward0 = %s\nforward1 = %s\n ' % (offset_x, offset_y, offset, forward0, forward1) print '' print '*********************************' print ' You are setting config file to:' print '*********************************' print config print '*********************************' print '' fd = open('config', 'w') fd.write(config) fd.close() motor.stop() tcpCliSock.close() quit() else: print 'Command Error! Cannot recognize command: ' + data
def backward(self, speed=50): """Similar to forward()""" motor.setSpeed(speed) motor.motor0('False') motor.motor1('False')
def loop(): global offset_x, offset_y, offset, forward0, forward1 while True: print 'Waiting for connection...' tcpCliSock, addr = tcpSerSock.accept() print '...connected from :', addr # Print the IP address of the client connected with the server. while True: data = tcpCliSock.recv(BUFSIZ) # Reception données du client # Analyser les commandes reçues et contrôler VirJo if not data: break #--------Motor calibration---------- if data == 'motor_run': print 'motor moving forward' motor.setSpeed(50) motor.motor0(forward0) motor.motor1(forward1) elif data[0:9] == 'leftmotor': forward0 = data[9:] motor.motor0(forward0) elif data[0:10] == 'rightmotor': forward1 = data[10:] motor.motor1(forward1) # -------------Added-------------- elif data == 'leftreverse': if forward0 == "True": forward0 = "False" else: forward0 = "True" print "left motor reversed to", forward0 motor.motor0(forward0) elif data == 'rightreverse': if forward1 == "True": forward1 = "False" else: forward1 = "True" print "right motor reversed to", forward1 motor.motor1(forward1) elif data == 'motor_stop': print 'motor stop' motor.stop() #--------------------------------- #-------Turing calibration------ elif data[0:7] == 'offset=': offset = int(data[7:]) car_dir.calibrate(offset) #-------------------------------- #----------Mount calibration--------- elif data[0:8] == 'offsetx=': offset_x = int(data[8:]) print 'Mount offset x', offset_x video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsety=': offset_y = int(data[8:]) print 'Mount offset y', offset_y video_dir.calibrate(offset_x, offset_y) #---------------------------------------- #-------Turing calibration 2------ elif data[0:7] == 'offset+': offset = offset + int(data[7:]) print 'Turning offset', offset car_dir.calibrate(offset) elif data[0:7] == 'offset-': offset = offset - int(data[7:]) print 'Turning offset', offset car_dir.calibrate(offset) #-------------------------------- #----------Mount calibration 2--------- elif data[0:8] == 'offsetx+': offset_x = offset_x + int(data[8:]) print 'Mount offset x', offset_x video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsetx-': offset_x = offset_x - int(data[8:]) print 'Mount offset x', offset_x video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsety+': offset_y = offset_y + int(data[8:]) print 'Mount offset y', offset_y video_dir.calibrate(offset_x, offset_y) elif data[0:8] == 'offsety-': offset_y = offset_y - int(data[8:]) print 'Mount offset y', offset_y video_dir.calibrate(offset_x, offset_y) #---------------------------------------- #----------Confirm-------------------- elif data == 'confirm': config = 'offset_x = %s\noffset_y = %s\noffset = %s\nforward0 = %s\nforward1 = %s\n ' % ( offset_x, offset_y, offset, forward0, forward1) print '' print '*********************************' print ' You are setting config file to:' print '*********************************' print config print '*********************************' print '' fd = open('config', 'w') fd.write(config) fd.close() motor.stop() tcpCliSock.close() quit() else: print 'Command Error! Cannot recognize command: ' + data
import motor import car_dir import time motor.setup(busnum=1) car_dir.setup() motor.setSpeed(50) motor.motor0('True') motor.motor1('True') t = time.time() while time.time() < t + 1: pass car_dir.turn_left() while time.time() < t + 1.5: pass car_dir.home() while time.time() < t + 2.5: pass motor.stop()
def loop(): car_dir.dis_home(pwm1) #Ultrasound initial position time.sleep(0.5) a = ultrasonic.checkdist() #Get the ultrasonic detection distance b = ultrasonic.checkdist() #Get the ultrasonic detection distance c = ultrasonic.checkdist() #Get the ultrasonic detection distance homedis = min(a, b, c) #Get the ultrasonic detection distance print('homedis = %0.2f m' % homedis) motor.motorStop() #Stop the car if homedis > dis: #No obstacles motor.motor(status, forward, b_spd) motor.motor1(status, forward, t_spd) elif homedis < dis: #Obstacles car_dir.dis_left(pwm1) time.sleep(1) a = ultrasonic.checkdist() b = ultrasonic.checkdist() c = ultrasonic.checkdist() leftdis = min(a, b, c) print('leftdis = %0.2f m' % leftdis) car_dir.dis_right(pwm1) time.sleep(1) a = ultrasonic.checkdist() b = ultrasonic.checkdist() c = ultrasonic.checkdist() rightdis = min(a, b, c) print('rightdis = %0.2f m' % rightdis) if leftdis < dis and rightdis < dis: #Judgment left and right if leftdis >= rightdis: #There are obstacles on the right motor.motor(status, backward, b_spd) #motor.motor(status,backward,left) #motor.motor1(status,backward,t_spd) motor.motor1(status, backward, right) time.sleep(1) motor.motor(status, forward, left) motor.motor1(status, forward, b_spd) time.sleep(0.5) else: #There are obstacles on the left motor.motor(status, backward, left) motor.motor1(status, backward, t_spd) time.sleep(1) motor.motor(status, forward, b_spd) motor.motor1(status, forward, right) time.sleep(0.5) elif leftdis > dis and rightdis <= dis: #There are obstacles on the right if homedis < mindis: #Obstacle ahead motor.motor(status, backward, b_spd) motor.motor1(status, backward, t_spd) time.sleep(1) motor.motor(status, forward, left) motor.motor1(status, forward, b_spd) time.sleep(0.5) else: #No obstacle ahead motor.motor(status, forward, left) motor.motor1(status, forward, b_spd) time.sleep(0.5) elif rightdis > dis and leftdis <= dis: #There are obstacles on the left if homedis < mindis: #Obstacle ahead motor.motor(status, backward, b_spd) motor.motor1(status, backward, t_spd) time.sleep(1) motor.motor(status, forward, b_spd) motor.motor1(status, forward, right) time.sleep(0.5) else: #No obstacle ahead motor.motor(status, forward, b_spd) motor.motor1(status, forward, right) time.sleep(0.5) elif rightdis > dis and leftdis > dis: #There are no obstacles if rightdis > leftdis: #The distance to the right is greater than the left if homedis < mindis: #Obstacle ahead motor.motor(status, backward, b_spd) motor.motor1(status, backward, t_spd) time.sleep(1) motor.motor(status, forward, b_spd) motor.motor1(status, forward, right) time.sleep(0.5) else: #No obstacle ahead motor.motor(status, forward, b_spd) motor.motor1(status, forward, right) time.sleep(0.5) elif rightdis < leftdis: #The distance to the left is greater than the right if homedis < mindis: #Obstacle ahead motor.motor(status, backward, b_spd) motor.motor1(status, backward, t_spd) time.sleep(1) motor.motor(status, forward, left) motor.motor1(status, forward, b_spd) time.sleep(0.5) else: #NO Obstacle ahead motor.motor(status, forward, left) motor.motor1(status, forward, b_spd) time.sleep(0.5) elif leftdis == rightdis: motor.motor(status, backward, b_spd) motor.motor1(status, backward, t_spd) time.sleep(1)
def run(): #Main function global ip_con while True: #print('SET %s'%dir_mid+' %s'%dis_mid+' %s'%b_spd+' %s'%t_spd+' %s'%left+' %s'%right) print('waiting for connection...') tcpCliSock, addr = tcpSerSock.accept()#Determine whether to connect print('...connected from :', addr) tcpCliSock.send('SET %s'%dir_mid+' %s'%dis_mid+' %s'%b_spd+' %s'%t_spd+' %s'%left+' %s'%right) break vn=threading.Thread(target=video_net) #Define a thread for connection vn.setDaemon(True) #'True' means it is a front thread,it would close when the mainloop() closes vn.start() #Thread starts while True: data = '' data = tcpCliSock.recv(BUFSIZ).decode()#Get instructions #print(data) if not data: continue elif 'spdset' in data: global spd_ad try: spd_ad=float((str(data))[7:]) #Speed Adjustment except: print('wrong speed value') elif 'EC1set' in data: #Camera Adjustment try: new_EC1=int((str(data))[7:]) replace_num('E_C1:',new_EC1) except: pass elif 'EC2set' in data: #Ultrasonic Adjustment try: new_EC2=int((str(data))[7:]) replace_num('E_C2:',new_EC2) except: pass elif 'EM1set' in data: #Motor A Speed Adjustment try: new_EM1=int((str(data))[7:]) replace_num('E_M1:',new_EM1) except: pass elif 'EM2set' in data: #Motor B Speed Adjustment try: new_EM2=int((str(data))[7:]) replace_num('E_M2:',new_EM2) except: pass elif 'ET1set' in data: #Motor A Turningf Speed Adjustment try: new_ET1=int((str(data))[7:]) replace_num('E_T1:',new_ET1) except: pass elif 'ET2set' in data: #Motor B Turningf Speed Adjustment try: new_ET2=int((str(data))[7:]) replace_num('E_T2:',new_ET2) except: pass elif 'scan' in data: dis_can=scan() #Start Scanning str_list_1=dis_can[0:100] #Divide the list to make it samller to send str_index=' ' #Separate the values by space str_send_1=str_index.join(str_list_1)+' ' tcpCliSock.send(str(str_send_1)) #Send Part 1 #print(str_send_1) time.sleep(0.3) str_list_2=dis_can[101:200] str_send_2=str_index.join(str_list_2)+' ' tcpCliSock.send(str(str_send_2)) #Send Part 2 #print(str_send_2) time.sleep(0.3) str_list_3=dis_can[201:300] str_send_3=str_index.join(str_list_3)+' ' tcpCliSock.send(str(str_send_3)) #Send Part 3 #print(str_send_3) time.sleep(0.3) str_list_4=dis_can[301:408] str_send_4=str_index.join(str_list_4) tcpCliSock.send(str(str_send_4)) #Send Part 4 #print(str_send_4) time.sleep(0.3) tcpCliSock.send('finished') #Send 'finished' tell the client to stop receiving the list of dis_can #print(dis_can) #print(len(dis_can)) elif 'forward' in data: #When server receive "forward" from client,car moves forward global b_spd global t_spd tcpCliSock.send('1') motor.motor(status, forward, b_spd*spd_ad) motor.motor1(status,forward,t_spd*spd_ad) direction = forward elif 'backward' in data: #When server receive "backward" from client,car moves backward tcpCliSock.send('2') motor.motor(status, backward, b_spd*spd_ad) motor.motor1(status, backward, t_spd*spd_ad) direction = backward elif 'left' in data: #When server receive "left" from client,camera turns left tcpCliSock.send('7') car_dir.dir_left(pwm1) continue elif 'right' in data: #When server receive "right" from client,camera turns right tcpCliSock.send('8') car_dir.dir_right(pwm1) continue elif 'on' in data: #When server receive "on" from client,camera looks up tcpCliSock.send('5') car_dir.dir_Left(pwm0) continue elif 'under' in data: #When server receive "under" from client,camera looks down tcpCliSock.send('6') car_dir.dir_Right(pwm0) continue elif 'Left' in data: #When server receive "Left" from client,car turns left tcpCliSock.send('3') motor.motor(status, forward, left*spd_ad) motor.motor1(status, forward, b_spd*spd_ad) #print('LLL') continue elif 'BLe' in data: #When server receive "BLeft" from client,car move back and left tcpCliSock.send('3') motor.motor(status, 1, left*spd_ad) motor.motor1(status, 1, b_spd*spd_ad) #print("BL") continue elif 'Right' in data: #When server receive "Right" from client,car turns right tcpCliSock.send('4') motor.motor(status, forward, b_spd*spd_ad) motor.motor1(status, forward, right*spd_ad) continue elif 'BRi' in data: #When server receive "BRight" from client,car move back and right tcpCliSock.send('4') motor.motor(status, backward, b_spd*spd_ad) motor.motor1(status, backward, right*spd_ad) continue elif 'exit' in data: #When server receive "exit" from client,server shuts down GPIO.cleanup() tcpSerSock.close() os.system('sudo init 0') continue elif 'stop' in data: #When server receive "stop" from client,car stops moving tcpCliSock.send('9') #setup() motor.motorStop() #GPIO.cleanup() #setup() continue elif 'home' in data: #When server receive "home" from client,camera looks forward car_dir.dir_home(pwm0) car_dir.dir_home(pwm1) continue elif 'Stop' in data: #When server receive "Stop" from client,Auto Mode switches off tcpCliSock.send('9') try: st.stop() except: pass motor.motorStop() elif 'auto' in data: #When server receive "auto" from client,start Auto Mode tcpCliSock.send('0') st = Job() st.start() continue elif 'IPCON' in data: try: data=str(data) ip_var=data.split() ip_con=ip_var[1] print(ip_con) vi=threading.Thread(target=video_net) #Define a thread for data receiving vi.setDaemon(True) #'True' means it is a front thread,it would close when the mainloop() closes vi.start() #Thread starts print('start thread') except: pass else: print 'Command Error! Cannot recongnize command: ' +data
def calibrate_motor_run(request): motor.setSpeed(50) motor.motor0(forward0) motor.motor1(forward1) return HttpResponse('Motors Runing')
def run(): #Main function global ip_con, addr st_s = 0 while True: #print('SET %s'%dir_mid+' %s'%dis_mid+' %s'%b_spd+' %s'%t_spd+' %s'%left+' %s'%right) print('waiting for connection...') tcpCliSock, addr = tcpSerSock.accept() #Determine whether to connect print('...connected from :', addr) tcpCliSock.send( ('SET %s' % dir_mid + ' %s' % dis_mid + ' %s' % b_spd + ' %s' % t_spd + ' %s' % left + ' %s' % right).encode()) break fps_threading = threading.Thread( target=FPV_thread) #Define a thread for FPV and OpenCV fps_threading.setDaemon( True ) #'True' means it is a front thread,it would close when the mainloop() closes fps_threading.start() #Thread starts st = Job() while True: data = '' data = tcpCliSock.recv(BUFSIZ).decode() #Get instructions #print(data) if not data: continue elif 'spdset' in data: global spd_ad try: spd_ad = float((str(data))[7:]) #Speed Adjustment except: print('wrong speed value') elif 'EC1set' in data: #Camera Adjustment try: new_EC1 = int((str(data))[7:]) replace_num('E_C1:', new_EC1) except: pass elif 'EC2set' in data: #Ultrasonic Adjustment try: new_EC2 = int((str(data))[7:]) replace_num('E_C2:', new_EC2) except: pass elif 'EM1set' in data: #Motor A Speed Adjustment try: new_EM1 = int((str(data))[7:]) replace_num('E_M1:', new_EM1) except: pass elif 'EM2set' in data: #Motor B Speed Adjustment try: new_EM2 = int((str(data))[7:]) replace_num('E_M2:', new_EM2) except: pass elif 'ET1set' in data: #Motor A Turningf Speed Adjustment try: new_ET1 = int((str(data))[7:]) replace_num('E_T1:', new_ET1) except: pass elif 'ET2set' in data: #Motor B Turningf Speed Adjustment try: new_ET2 = int((str(data))[7:]) replace_num('E_T2:', new_ET2) except: pass elif 'scan' in data: dis_can = scan() #Start Scanning str_list_1 = dis_can #Divide the list to make it samller to send str_index = ' ' #Separate the values by space str_send_1 = str_index.join(str_list_1) + ' ' tcpCliSock.sendall((str(str_send_1)).encode()) #Send Data tcpCliSock.send( 'finished'.encode() ) #Sending 'finished' tell the client to stop receiving the list of dis_can elif 'forward' in data: #When server receive "forward" from client,car moves forward tcpCliSock.send('1'.encode()) motor.motor(status, forward, b_spd * spd_ad) motor.motor1(status, forward, t_spd * spd_ad) direction = forward elif 'backward' in data: #When server receive "backward" from client,car moves backward tcpCliSock.send('2'.encode()) motor.motor(status, backward, b_spd * spd_ad) motor.motor1(status, backward, t_spd * spd_ad) direction = backward elif 'left' in data: #When server receive "left" from client,camera turns left tcpCliSock.send('7'.encode()) car_dir.dir_left(pwm1) #car_dir.dir_right(pwm1) continue elif 'right' in data: #When server receive "right" from client,camera turns right tcpCliSock.send('8'.encode()) car_dir.dir_right(pwm1) #car_dir.dir_left(pwm1) continue elif 'on' in data: #When server receive "on" from client,camera looks up tcpCliSock.send('5'.encode()) car_dir.dir_Left(pwm0) #car_dir.dir_Right(pwm0) continue elif 'under' in data: #When server receive "under" from client,camera looks down tcpCliSock.send('6'.encode()) car_dir.dir_Right(pwm0) #car_dir.dir_Left(pwm0) continue elif 'Left' in data: #When server receive "Left" from client,car turns left tcpCliSock.send('3'.encode()) motor.motor(0, forward, left * spd_ad) motor.motor1(status, forward, b_spd * spd_ad) #print('LLL') continue elif 'BLe' in data: #When server receive "BLeft" from client,car move back and left tcpCliSock.send('3'.encode()) motor.motor(0, backward, left * spd_ad) motor.motor1(status, backward, b_spd * spd_ad) #print("BL") continue elif 'Right' in data: #When server receive "Right" from client,car turns right tcpCliSock.send('4'.encode()) motor.motor(status, forward, b_spd * spd_ad) motor.motor1(0, forward, right * spd_ad) continue elif 'BRi' in data: #When server receive "BRight" from client,car move back and right tcpCliSock.send('4'.encode()) motor.motor(status, backward, b_spd * spd_ad) motor.motor1(0, backward, right * spd_ad) continue elif 'exit' in data: #When server receive "exit" from client,server shuts down GPIO.cleanup() tcpSerSock.close() os.system('sudo init 0') continue elif 'stop' in data: #When server receive "stop" from client,car stops moving tcpCliSock.send('9'.encode()) #setup() motor.motorStop() #GPIO.cleanup() #setup() continue elif 'home' in data: #When server receive "home" from client,camera looks forward car_dir.dir_home(pwm0) car_dir.dir_home(pwm1) continue elif 'Stop' in data: #When server receive "Stop" from client,Auto Mode switches off tcpCliSock.send('9'.encode()) try: st.pause() except: pass motor.motorStop() time.sleep(0.4) motor.motorStop() elif 'auto' in data: #When server receive "auto" from client,start Auto Mode tcpCliSock.send('0'.encode()) if st_s == 0: st.start() st_s = 1 else: st.resume() continue else: pass
def forward(self, speed=50): """Will cause the car to start moving forward. Car will not stop until stop() is called""" motor.setSpeed(speed) motor.motor0('True') motor.motor1('True')