def setup(): global offset_x, offset_y, offset, forward0, forward1 offset_x = 0 offset_y = 0 offset = 0 forward0 = 'True' forward1 = 'False' try: for line in open('config'): if line[0:8] == 'offset_x': offset_x = int(line[11:-1]) print 'offset_x =', offset_x if line[0:8] == 'offset_y': offset_y = int(line[11:-1]) print 'offset_y =', offset_y if line[0:8] == 'offset =': offset = int(line[9:-1]) print 'offset =', offset if line[0:8] == "forward0": forward0 = line[11:-1] print 'turning0 =', forward0 if line[0:8] == "forward1": forward1 = line[11:-1] print 'turning1 =', forward1 except: print 'no config file, set config to original' video_dir.setup(busnum=busnum) car_dir.setup(busnum=busnum) motor.setup(busnum=busnum) video_dir.calibrate(offset_x, offset_y) car_dir.calibrate(offset)
def calibrate_turning(request, direction, in_offset): global offset offset = int(in_offset) if direction == '-': offset = 0 - offset car_dir.calibrate(offset) text = "offset:", offset 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 setup(): global offset_x, offset_y, offset, forward0, forward1, backward0, backward1 offset_x = 0 offset_y = 0 offset = 0 forward0 = 'True' forward1 = 'False' # Read calibration value from config file try: for line in open('config'): if line[0:8] == 'offset_x': offset_x = int(line[11:-1]) print 'offset_x =', offset_x if line[0:8] == 'offset_y': offset_y = int(line[11:-1]) print 'offset_y =', offset_y if line[0:8] == 'offset =': offset = int(line[9:-1]) print 'offset =', offset if line[0:8] == "forward0": forward0 = line[11:-1] print 'turning0 =', forward0 if line[0:8] == "forward1": forward1 = line[11:-1] print 'turning1 =', forward1 except: print 'no config file, set config to original' video_dir.setup(busnum=busnum) car_dir.setup(busnum=busnum) motor.setup(busnum=busnum) video_dir.calibrate(offset_x, offset_y) car_dir.calibrate(offset) # Set the motor's true / false value to the opposite. backward0 = REVERSE(forward0) backward1 = REVERSE(forward1)
def setup(): global offset, offset_x, offset_y, forward0, forward1, backward0, backward1 offset_x = 0 offset_y = 0 offset = 0 forward0 = 'True' forward1 = 'False' # Read calibration value from config file try: for line in open('config'): find_line(offset_x, "offset_x", 'offset_x') find_line(offset_y, "offset_y", 'offset_y') find_line(forward0, "forward0", 'turning0') find_line(forward1, "forward1", 'turning1') if line[0:8] == 'offset =': offset = int(line[9:-1]) print 'offset =', offset except: print 'no config file, set config to original' video_dir.setup(busnum=busnum) car_dir.setup(busnum=busnum) motor.setup(busnum=busnum) video_dir.calibrate(offset_x, offset_y) car_dir.calibrate(offset) # Set the motor's true / false value to the opposite. backward0 = REVERSE(forward0) backward1 = REVERSE(forward1)
def calibration_mode(request): video_dir.calibrate(offset_x, offset_y) car_dir.calibrate(offset) return HttpResponse("Calibration mode start")
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
if line[0:8] == 'offset =': offset = int(line[9:-1]) print 'offset =', offset if line[0:8] == "forward0": forward0 = line[11:-1] print 'turning0 =', forward0 if line[0:8] == "forward1": forward1 = line[11:-1] print 'turning1 =', forward1 except: print 'no config file, set config to original' video_dir.setup(busnum=busnum) car_dir.setup(busnum=busnum) motor.setup(busnum=busnum) video_dir.calibrate(offset_x, offset_y) car_dir.calibrate(offset) def REVERSE(x): if x == 'True': return 'False' elif x == 'False': return 'True' 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()
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 setup(): car_dir.setup(busnum=busnum) motor.setup(busnum=busnum) car_dir.calibrate(0) motor.setSpeed(50)
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 cv2 import motor import car_dir import time video_capture = cv2.VideoCapture(-1) video_capture.set(3, 160) video_capture.set(4, 120) busnum = 1 motor.setup(busnum=busnum) car_dir.setup(busnum=busnum) car_dir.calibrate(-72) motor.setSpeed(35) motor.backward() #motor.stop() lastTurn = "Right" while (True): ret, frame = video_capture.read() crop_img = frame if ret is True: hsv = cv2.cvtColor(crop_img, cv2.COLOR_BGR2HSV) else: continue
def CarController(socket): while True: print 'Waiting for connection to car controller service...' # 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. clientSocket, addr = socket.accept() print '...connected to car controller service:', addr # Print the IP address of the client connected with the server. lastCmd = '' while True: msgs = '' recdata = clientSocket.recv(BUFSIZ) # Receive data sent from the client. # Analyze the command received and control the car accordingly. msgs = recdata.split(';') #print("Received", len(msgs), "new messages") for data in msgs: if not data: break if lastCmd == data: print("Last Command:", lastCmd, "Current Data:", data, "Ignoring") break if data == ctrl_cmd[0]: print 'motor moving forward' motor.forward() elif data == ctrl_cmd[1]: print 'recv backward cmd' motor.backward() elif data == ctrl_cmd[2]: print 'recv left cmd' car_dir.turn_left() elif data == ctrl_cmd[3]: print 'recv right cmd' car_dir.turn_right() elif data == ctrl_cmd[6]: print 'recv home cmd' car_dir.home() elif data == ctrl_cmd[4]: print 'recv stop cmd' motor.ctrl(0) elif data == ctrl_cmd[5]: print 'read cpu temp...' temp = cpu_temp.read() tcpCliSock.send('[%s] %0.2f' % (ctime(), temp)) elif data[0:5] == 'speed': #print data numLen = len(data) - len('speed') if numLen == 1 or numLen == 2 or numLen == 3: tmp = data[-numLen:] #print 'tmp(str) = %s' % tmp spd = int(tmp) #print 'spd(int) = %d' % spd if spd < 24: spd = 24 motor.setSpeed(spd) elif data[0:8] == 'network=': print 'network =', data spd = data.split('=')[1] try: spd = int(spd) os.system('sudo tc qdisc del dev wlan0 root') os.system( 'sudo tc qdisc add dev wlan0 root netem delay {0}ms' .format(spd)) except: print 'ERROR , speed =', spd elif data[0:7] == 'offset=': print 'offset called, data = ', data offset = int(data[7:]) + 28 car_dir.calibrate(offset) elif data[0:8] == 'forward=': #print 'data =', data spd = data.split('=')[1] try: spd = int(spd) motor.setSpeed(spd) motor.forward() except: print 'Error speed =', spd elif data[0:9] == 'backward=': #print 'data =', data spd = data.split('=')[1] try: spd = int(spd) motor.setSpeed(spd) motor.backward() except: print 'ERROR , speed =', spd else: print 'Command Error! Cannot recognize command: ' + data