def move(direction): # Choose the direction of the request if direction == 'camleft': video_dir.move_decrease_x() elif direction == 'camright': video_dir.move_increase_x() elif direction == 'camup': video_dir.move_increase_y() elif direction == 'camdown': video_dir.move_decrease_y() elif direction == 'camhome': video_dir.home_x_y() elif direction == 'record': subprocess.call(['motion'], shell=True) elif direction == 'stoprecord': subprocess.call(['./stop.sh'], shell=True) elif direction == 'forward': motor.forward() elif direction == 'reverse': motor.backward() elif direction == 'left': car_dir.turn_left() elif direction == 'right': car_dir.turn_right() elif direction == 'stop': motor.ctrl(0) elif direction == 'home': car_dir.home()
def run_mode(request): video_dir.setup() car_dir.setup() motor.setup() video_dir.home_x_y() car_dir.home() motor.setSpeed(50) return HttpResponse("Run mode start")
def mover(): sRobotDirection = Direction() rospy.init_node('mover', anonymous=True) rospy.Subscriber("motioncommand", String, sRobotDirection.direction_callback) video_dir.setup(busnum=busnum) car_dir.setup(busnum=busnum) motor.setup( busnum=busnum ) # Initialize the Raspberry Pi GPIO connected to the DC motor. video_dir.home_x_y() car_dir.home() #data = sRobotDirection.directionMsg rospy.spin()
busnum = 1 # Edit busnum to 0, if you uses Raspberry Pi 1 or 0 HOST = '' # The variable of HOST is null, so the function bind( ) can be bound to all valid addresses. PORT = 21565 BUFSIZ = 1024 # Size of the buffer ADDR = (HOST, PORT) tcpSerSock = socket(AF_INET, SOCK_STREAM) # Create a socket. tcpSerSock.bind(ADDR) # Bind the IP address and port number of the server. tcpSerSock.listen(5) # The parameter of listen() defines the number of connections permitted at one time. Once the # connections are full, others will be rejected. video_dir.setup(busnum=busnum) car_dir.setup(busnum=busnum) motor.setup(busnum=busnum) # Initialize the Raspberry Pi GPIO connected to the DC motor. video_dir.home_x_y() car_dir.home() 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 :'+ str(addr)) # Print the IP address of the client connected with the server. while True: data = '' data = tcpCliSock.recv(BUFSIZ) # Receive data sent from the client. # Analyze the command received and control the car accordingly. if not data:
else: setSpeed(60) turn_right() forward() time.sleep(1.2) ######################## forward() home() motor1(forward1) time.sleep(1.2) stop() time.sleep(5)''' setup() US_turn.setup() US_turn.home_x_y() stop() distance() distance() time.sleep(2) while True: try: #''' setSpeed(50) home() forward() #stop() D_straight = distance() print("Straight distance = %f" % D_straight) if D_straight < 40:
busnum = 1 # Edit busnum to 0, if you uses Raspberry Pi 1 or 0 HOST = '' # The variable of HOST is null, so the function bind( ) can be bound to all valid addresses. PORT = 21567 BUFSIZ = 1024 # Size of the buffer ADDR = (HOST, PORT) tcpSerSock = socket(AF_INET, SOCK_STREAM) # Create a socket. tcpSerSock.bind(ADDR) # Bind the IP address and port number of the server. tcpSerSock.listen(5) # The parameter of listen() defines the number of connections permitted at one time. Once the # connections are full, others will be rejected. video_dir.setup(busnum=busnum) car_dir.setup(busnum=busnum) motor.setup(busnum=busnum) # Initialize the Raspberry Pi GPIO connected to the DC motor. video_dir.home_x_y() car_dir.home() 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 = '' data = tcpCliSock.recv(BUFSIZ) # Receive data sent from the client. # Analyze the command received and control the car accordingly. if not data:
def camera_home(request): video_dir.home_x_y() return HttpResponse("Camera back to defaultrequest")
def run_server(key): #!/usr/bin/env python import RPi.GPIO as GPIO import video_dir import car_dir import motor from time import ctime # Import necessary modules ctrl_cmd = [ 'forward', 'backward', 'left', 'right', 'stop', 'read cpu_temp', 'home', 'distance', 'x+', 'x-', 'y+', 'y-', 'xy_home' ] busnum = 1 # Edit busnum to 0, if you uses Raspberry Pi 1 or 0 HOST = '' # The variable of HOST is null, so the function bind( ) can be bound to all valid addresses. PORT = 21567 BUFSIZ = 1024 # Size of the buffer ADDR = (HOST, PORT) tcpSerSock = socket(AF_INET, SOCK_STREAM) # Create a socket. tcpSerSock.bind(ADDR) # Bind the IP address and port number of the server. tcpSerSock.listen( 5 ) # The parameter of listen() defines the number of connections permitted at one time. Once the # connections are full, others will be rejected. #video_dir.setup(busnum=busnum) #car_dir.setup(busnum=busnum) #motor.setup(busnum=busnum) # Initialize the Raspberry Pi GPIO connected to the DC motor. #video_dir.home_x_y() #car_dir.home() 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: try: data = '' data = tcpCliSock.recv( BUFSIZ) # Receive data sent from the client. print "\nEncrypted command recieved from client = ,", eval( data)[2] data = crypto.AES_decrypt(eval(data), key) except: print "INCOMPLETE PACKET ERROR - try to input the command again" # Analyze the command received and control the car accordingly. if not data: 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 == ctrl_cmd[8]: print 'recv x+ cmd' video_dir.move_increase_x() elif data == ctrl_cmd[9]: print 'recv x- cmd' video_dir.move_decrease_x() elif data == ctrl_cmd[10]: print 'recv y+ cmd' video_dir.move_increase_y() elif data == ctrl_cmd[11]: print 'recv y- cmd' video_dir.move_decrease_y() elif data == ctrl_cmd[12]: print 'home_x_y' video_dir.home_x_y() elif data[0:5] == 'speed': # Change the 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:5] == 'turn=': #Turning Angle print 'data =', data angle = data.split('=')[1] try: angle = int(angle) car_dir.turn(angle) except: print 'Error: angle =', angle elif data[0:8] == 'forward=': print 'data =', data spd = data[8:] try: spd = int(spd) motor.forward(spd) except: print 'Error speed =', spd elif data[0:9] == 'backward=': print 'data =', data spd = data.split('=')[1] try: spd = int(spd) motor.backward(spd) except: print 'ERROR, speed =', spd else: #print 'Command Error! Cannot recognize command: ' + data print "COMMAND ERROR - Unable to interpret recieved command" tcpSerSock.close()
def on_message(ws, data): print data # Analyze the command received and control the car accordingly. #if not data: 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 == ctrl_cmd[8]: print 'recv x+ cmd' video_dir.move_increase_x() elif data == ctrl_cmd[9]: print 'recv x- cmd' video_dir.move_decrease_x() elif data == ctrl_cmd[10]: print 'recv y+ cmd' video_dir.move_increase_y() elif data == ctrl_cmd[11]: print 'recv y- cmd' video_dir.move_decrease_y() elif data == ctrl_cmd[12]: print 'home_x_y' video_dir.home_x_y() elif data[0:12] == ctrl_cmd[13]: # Change the 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(30) elif data[0:5] == 'turn=': #Turning Angle print 'data =', data angle = data.split('=')[1] try: angle = int(angle) car_dir.turn(angle) except: print 'Error: angle =', angle elif data[0:8] == 'forward=': print 'data =', data spd = 30 try: spd = int(spd) motor.forward(spd) except: print 'Error speed =', spd elif data[0:9] == 'backward=': print 'data =', data spd = data.split('=')[1] try: spd = int(spd) motor.backward(spd) except: print 'ERROR, speed =', spd else: print 'Command Error! Cannot recognize command: ' + data
""" if __name__ == '__main__': # Obstacle Detection sleep_time = 0.3 GPIO.setmode(GPIO.BOARD) # Settings initialization busnum = 1 # Edit busnum to 0, if you uses Raspberry Pi 1 or 0 video_dir.setup(busnum=busnum) car_dir.setup(busnum=busnum) motor.setup(busnum=busnum) # Initialize the Raspberry Pi GPIO connected to the DC motor. motor.setSpeed(50) video_dir.home_x_y() car_dir.home() motor.ctrl(0) # Load classifier model_path = os.path.join('..', '..', 'models', 'model_aug_bright.h5') model = load_model(model_path) ctrl_cmd = ['forward', 'backward', 'left', 'right', 'stop', 'read cpu_temp',
def home_pan_tilt(self): sfpantilt.home_x_y() return True
def process_request(data): # Analyze the command received and control the car accordingly. if not data: return "cmd not understood" 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 == ctrl_cmd[8]: print('recv x+ cmd') video_dir.move_increase_x() elif data == ctrl_cmd[9]: print('recv x- cmd') video_dir.move_decrease_x() elif data == ctrl_cmd[10]: print('recv y+ cmd') video_dir.move_increase_y() elif data == ctrl_cmd[11]: print('recv y- cmd') video_dir.move_decrease_y() elif data == ctrl_cmd[12]: print('home_x_y') video_dir.home_x_y() elif data[0:5] == 'speed': # Change the 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:5] == 'turn=': # Turning Angle print('data =', data) angle = data.split('=')[1] try: angle = int(angle) car_dir.turn(angle) except: print('Error: angle =', angle) elif data[0:8] == 'forward=': print('data =', data) spd = data[8:] try: spd = int(spd) motor.forward(spd) except: print('Error speed =', spd) elif data[0:9] == 'backward=': print('data =', data) spd = data.split('=')[1] try: spd = int(spd) motor.backward(spd) except: print('ERROR, speed =', spd) else: print('Command Error! Cannot recognize command: ' + data)
def run(self): while True: print 'Waiting for connection...' tcpCliSock, addr = tcpSerSock.accept() print '...connected from :', addr while True: data = '' data = tcpCliSock.recv(BUFSIZ) if not data: 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 == ctrl_cmd[8]: print 'recv x+ cmd' video_dir.move_increase_x() elif data == ctrl_cmd[9]: print 'recv x- cmd' video_dir.move_decrease_x() elif data == ctrl_cmd[10]: print 'recv y+ cmd' video_dir.move_increase_y() elif data == ctrl_cmd[11]: print 'recv y- cmd' video_dir.move_decrease_y() elif data == ctrl_cmd[12]: print 'home_x_y' video_dir.home_x_y() 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:5] == 'turn=': print 'data =', data angle = data.split('=')[1] try: angle = int(angle) car_dir.turn(angle) except: print 'Error: angle =', angle elif data[0:8] == 'forward=': print 'data =', data spd = data[8:] try: spd = int(spd) motor.forward(spd) except: print 'Error speed =', spd elif data[0:9] == 'backward=': print 'data =', data spd = data.split('=')[1] try: spd = int(spd) motor.backward(spd) except: print 'ERROR, speed =', spd else: print 'Command Error! Cannot recognize command: ' + data tcpSerSock.close()