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 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 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 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()
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)
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 = 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 = ''
time.sleep(1.2) 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)
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: data = ''
from socket import * 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'] 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() car_dir.setup() motor.setup() # 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 = ''
def run_server(key): 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) try: tcpSerSock = socket(AF_INET, SOCK_STREAM) # Create a socket. tcpSerSock.bind( ADDR) # Bind the IP address and port number of the server. tcpSerSock.listen( 1 ) # The parameter of listen() defines the number of connections permitted at one time. Once the except: print "#####\nTHE SOCKET ADDRESS IS ALREADY IN USE!\nPlease restart your device and run this program again" 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: print "Session closed by client" 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 __init__(self): self.busnum = 1 # See sunfounder's documentation, should be 1 for RPi3 sfdriving.setup(busnum=self.busnum) sfsteering.setup(busnum=self.busnum) sfpantilt.setup(busnum=self.busnum)