def interactiveControl(): setupInteractiveControl() clock = pygame.time.Clock() with picamera.PiCamera() as camera: camera.resolution = (WIDTH, HEIGHT) camera.start_preview(fullscreen=False, window=(500, 50, CP_WIDTH, CP_HEIGHT)) command = 'idle' while (True): up_key, down, left, right, change, stop = getKeys() getDistance() if stop: break if change: command = 'idle' if up_key: command = 'forward' forward(FB_TIME) elif down: command = 'reverse' reverse(FB_TIME) elif left: command = 'left' lef(LR_TIME) elif right: command = 'right' righ(LR_TIME) print(command) if (command in ('forward', 'right', 'left')): input_img = save_image_with_direction(command) camera.capture(input_img, use_video_port=True) clock.tick(10) pygame.quit()
def main(): wm=wimote.connect(Led) while True: button=wimote.whichbutton(wm) time.sleep(0.05) wm.rumble=False #Moving Forwards if button==3: distance_front = usonic.reading(Trigger_front,Echo_front) print distance_front if distance_front < Collision: wm.rumble=True #led.off(Led) motor.stop() elif distance_front >= Collision: #led.on(Led) motor.forward() #Reverse if button==4: motor.reverse() if button==7: motor.cleanup() led.cleanup() usonic.cleanup() sys.exit() if button==None: led.off(Led) motor.stop()
def func_sensor(t, hu, p, counter, max_num, move_time, stabilizer, current_rotate_position, rotate_time, temp_threshold, pressure_threshold, humid_threshold): # bool to ensure only one time mvement each time we run this function unmove = True if t > temp_threshold and counter < max_num and unmove: current_rotate_position = angle_fix(current_rotate_position, rotate_time) reverse( move_time ) # set move_time manually here to control the time of operation counter = counter + 1 unmove = False elif hu > humid_threshold and counter < max_num and stabilizer and unmove: current_rotate_position = angle_fix(current_rotate_position, rotate_time) reverse( move_time ) # set x move_time manually here to control the time of operation counter = counter + 1 stabilizer = False unmove = False elif hu > 140 and counter < max_num and unmove: # humidity toward dangerous level,backward without waiting current_rotate_position = angle_fix(current_rotate_position, rotate_time) reverse( move_time ) # set x move_time manually here to control the time of operation counter = counter + 1 stabilizer = False unmove = False elif p > pressure_threshold and counter < max_num and unmove: current_rotate_position = angle_fix(current_rotate_position, rotate_time) reverse( move_time ) # set move_time manually here to control the time of operation counter = counter + 1 unmove = False else: pass return stabilizer, counter, current_rotate_position
def reverse(): motor.reverse(1)
#!/usr/bin/python #coding: utf8 import motor import define import time if __name__ == '__main__': motor = MoterClass() motor.forward() sleep(3) motor.reverse() sleep(3) motor.left() sleep(3) motor.right() sleep(3) motor.stop() sleep(3)
def interactiveControl(): setupInteractiveControl() clock = pygame.time.Clock() with picamera.PiCamera() as camera: camera.resolution = (HEIGHT, WIDTH) camera.start_preview(fullscreen=False, window=(200, 0, CP_WIDTH, CP_HEIGHT)) command = 'idle' start_time = time.time() now = 0 while (now <= TRAIN_TIME): now = time.time() - start_time desired_target = np.array([[1, 0, 0]]) input_img = np.empty((WIDTH, HEIGHT, 3), dtype=np.uint8) camera.capture(input_img, 'bgr', use_video_port=True) input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY) input_img = input_img.reshape([-1, WIDTH, HEIGHT, CHANNEL]) up_key, down, left, right, change, stop = getKeys() getDistance() if stop: break if change: command = 'idle' if up_key: command = 'forward' desired_target = np.array([[1, 0, 0]]) t1 = Thread(target=forward, args=(FB_TIME, )) t1.start() model.fit(x=input_img, y=desired_target, epochs=1, verbose=0) t1.join() elif down: command = 'reverse' reverse(FB_TIME) append = lambda x: command + '_' + x if command != 'idle' else x if left: command = append('left') desired_target = np.array([[0, 0, 1]]) t2 = Thread(target=lef, args=(LR_TIME, )) t2.start() model.fit(x=input_img, y=desired_target, epochs=1, verbose=0) t2.join() elif right: command = append('right') desired_target = np.array([[0, 1, 0]]) t3 = Thread(target=righ, args=(LR_TIME, )) t3.start() model.fit(x=input_img, y=desired_target, epochs=1, verbose=0) t3.join() print(command) print('Time left : ', (TRAIN_TIME - now), ' s') clock.tick(0) pygame.quit()
for i in range(hats): hat = joystick.get_hat(i) textPrint.print(screen, "Hat {} value: {}".format(i, str(hat))) textPrint.unindent() textPrint.unindent() if joystick.get_axis(4) > float(0) and joystick.get_axis(5) > float(0): pass elif joystick.get_axis(4) > float(0): motor.forward(1 / 4) elif joystick.get_axis(5) > float(0): motor.reverse(1 / 4) if joystick.get_button(3) == 1: done = True # ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT # Go ahead and update the screen with what we've drawn. pygame.display.flip() # Limit to 20 frames per second clock.tick(20) # Close the window and quit. # If you forget this line, the program will 'hang' # on exit if running from IDLE.