def control_car(action_num): if action_num == 0: print("Left") zth_car_control.car_turn_left() time.sleep(0.25) elif action_num == 1: print("Right") zth_car_control.car_turn_right() time.sleep(0.25) elif action_num == 2: print("Forward") zth_car_control.car_move_forward() elif action_num == 3: zth_car_control.car_move_backward() print("Backward") else: zth_car_control.car_stop() print('stop')
def my_car_control(): global is_capture_running, key key = 4 pygame.init() pygame.display.set_mode((1, 1)) # 窗口 zth_car_control.car_stop() sleep(0.1) print("Start control!") while is_capture_running: # get input from human driver # for event in pygame.event.get(): # 判断事件是不是按键按下的事件 if event.type == pygame.KEYDOWN: key_input = pygame.key.get_pressed() # 可以同时检测多个按键 print(key_input[pygame.K_w], key_input[pygame.K_a], key_input[pygame.K_d]) # 按下前进,保存图片以2开头 if key_input[pygame.K_w] and not key_input[ pygame.K_a] and not key_input[pygame.K_d]: print("Forward") key = 2 zth_car_control.car_move_forward() # 按下左键,保存图片以0开头 elif key_input[pygame.K_a]: print("Left") zth_car_control.car_turn_left() sleep(0.1) key = 0 # 按下d右键,保存图片以1开头 elif key_input[pygame.K_d]: print("Right") zth_car_control.car_turn_right() sleep(0.1) key = 1 # 按下s后退键,保存图片为3开头 elif key_input[pygame.K_s]: print("Backward") zth_car_control.car_move_backward() key = 3 # 按下k停止键,停止 elif key_input[pygame.K_k]: zth_car_control.car_stop() # 检测按键是不是抬起 elif event.type == pygame.KEYUP: key_input = pygame.key.get_pressed() # w键抬起,轮子回正 if key_input[pygame.K_w] and not key_input[ pygame.K_a] and not key_input[pygame.K_d]: print("Forward") key = 2 zth_car_control.car_turn_straight() zth_car_control.car_move_forward() # s键抬起 elif key_input[pygame.K_s] and not key_input[ pygame.K_a] and not key_input[pygame.K_d]: print("Backward") key = 3 zth_car_control.car_move_backward() else: print("Stop") zth_car_control.car_stop() # car_control.cleanGPIO() zth_car_control.clean_GPIO()
# s键抬起 elif key_input[pygame.K_s] and not key_input[ pygame.K_a] and not key_input[pygame.K_d]: print("Backward") key = 3 zth_car_control.car_move_backward() else: print("Stop") zth_car_control.car_stop() # car_control.cleanGPIO() zth_car_control.clean_GPIO() if __name__ == '__main__': global train_labels, train_img, key print("capture thread") print('-' * 50) capture_thread = threading.Thread(target=pi_capture, args=()) # 开启线程 capture_thread.setDaemon(True) capture_thread.start() my_car_control() while is_capture_running: pass print("Done!") zth_car_control.car_stop() zth_car_control.clean_GPIO()