Ejemplo n.º 1
0
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()
		data = tcpCliSock.recv(BUFSIZ)    # Receive data sent from the client. 
		# 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'
Ejemplo n.º 3
0
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
        time_now = time()
        if time_now >= time_next:
            time_next = time_now + time_delay

            frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
            frame_small = cv2.resize(frame_gray, (128, 128))

            if video_output:
                cv2.imshow('video', frame_small)

            output_value = process_frame(frame_small)
            print(output_value)
            if output_value > 0.1:
                car_dir.turn_left()
            elif output_value < -0.1:
                car_dir.turn_right()
            else:
                car_dir.home()

            motor.forward()
            #sock.sendto('ai: %.6f' % output_value, (udp_host, udp_port))

        ch = cv2.waitKey(key_wait_time) & 0xFF
        if ch == 27:
            break
        if ch == ord('q'):
            break

capture.release()
cv2.destroyAllWindows()
Ejemplo n.º 5
0
 def turn_right(self):
     """Similar to turn_left()"""
     car_dir.turn_right()