def main(): global flag,ser ,distance_cm, model, graph, angle stream = io.BytesIO() model_loaded = glob.glob('model/*.h5') for single_mod in model_loaded: model = load_model(single_mod) graph = tf.get_default_graph() print('ok') # open camera camera = picamera.PiCamera(resolution=(160, 120)) camera.brightness = 60 ser = serial.Serial("/dev/ttyAMA0", 115200, timeout=0.5) dis_thread1 = threading.Thread(target=hc_1, args=()) dis_thread1.setDaemon(True) ang_thread = threading.Thread(target=get_thread, args=()) ang_thread.setDaemon(True) ang_thread.start() dis_thread1.start() print('set') event1.set() time.sleep(3) print(angle[2]) try: while True: print(distance_cm) if distance_cm<27 and distance_cm>2: car_control.car_stop() time.sleep(0.5) while distance_cm<13 or distance_cm >100: car_control.slow_backward() car_control.car_stop() time.sleep(0.1) event1.clear() flag += 1 if flag == 3: time.sleep(3) print('stop') camera.capture(stream, format='jpeg') judge(stream) distance_cm = 40 event1.set() else: smart_forward() finally: camera.close() GPIO.cleanup() stream.close()
def smart_go_back(): global dire_angle,distance_cm,event1 dire_angle = 180 while abs(angle[2]-dire_angle) > 18: car_control.car_cycle_right() car_control.car_cycle_left() time.sleep(0.05) car_control.car_stop() distance_cm = 40
def smart_go_left(): global dire_angle,distance_cm,event1 dire_angle = dire_angle+90 if dire_angle == 270.0: dire_angle = -90.0 while abs(angle[2]-dire_angle) > 15: car_control.car_cycle_left() car_control.car_cycle_right() time.sleep(0.05) car_control.car_stop() distance_cm = 40
def control_car(action_num): global distance_cm if action_num == 0: print('left') smart_go_left() elif action_num == 1: print('right') smart_go_right() elif action_num == 2: print('bibi') car_control.car_stop() time.sleep(2)
def control_car(action_num): """out put the char and call car_control(we used before)""" if action_num == 0: print("Left") car_control.car_turn_left() time.sleep(0.25) elif action_num == 1: print("Right") car_control.car_turn_right() time.sleep(0.25) elif action_num == 2: car_control.car_move_forward() print('Forward') elif action_num == 3: car_control.car_move_backward() print('Backward') else: car_control.car_stop() print('Stop')
def car_drive(): global is_capture_running, key key = 4 pygame.init() pygame.display.set_mode((1, 1)) car_control.car_stop() while is_capture_running: # 获取人输入的驾驶指令 events = pygame.event.get() for evt in events: if evt.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]) if key_input[pygame.K_w] and not key_input[ pygame.K_a] and not key_input[pygame.K_d]: print('Forward') key = 2 car_control.car_move_forward() elif key_input[pygame.K_a]: print('Left') car_control.car_turn_left() sleep(0.1) key = 0 elif key_input[pygame.K_d]: print('Right') car_control.car_turn_right() sleep(0.1) key = 1 elif key_input[pygame.K_s]: print('Backward') car_control.car_move_backward() sleep(0.1) key = 3 elif key_input[pygame.K_q] or key_input[pygame.K_e]: print("Barricade") car_control.car_stop() key = 5 elif evt.type == pygame.KEYUP: key_input = pygame.key.get_pressed() if key_input[pygame.K_w] and not key_input[ pygame.K_a] and not key_input[pygame.K_d]: print('Forward') key = 2 car_control.car_turn_straight() car_control.car_move_forward() elif key_input[pygame.K_s] and not key_input[ pygame.K_a] and not key_input[pygame.K_d]: print('Backward') key = 3 car_control.car_move_backward() else: print('Stop') car_control.car_stop() # car_control.clean_GPIO() car_control.clean_GPIO()
def my_car_control(): global is_capture_running, key key = 4 pygame.init() pygame.display.set_mode((1, 1)) 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]) if key_input[pygame.K_w] and not key_input[ pygame.K_a] and not key_input[pygame.K_d]: print("Forward") key = 2 car_control.car_move_forward() elif key_input[pygame.K_a]: print("Left") car_control.car_turn_left() sleep(0.1) key = 0 elif key_input[pygame.K_d]: print("Right") car_control.car_turn_right() sleep(0.1) key = 1 elif key_input[pygame.K_s]: print("Backward") car_control.car_move_backward() key = 3 elif key_input[pygame.K_k]: car_control.car_stop() elif event.type == pygame.KEYUP: key_input = pygame.key.get_pressed() if key_input[pygame.K_w] and not key_input[ pygame.K_a] and not key_input[pygame.K_d]: print("Forward") key = 2 car_control.car_turn_straight() car_control.car_move_forward() elif key_input[pygame.K_s] and not key_input[ pygame.K_a] and not key_input[pygame.K_d]: print("Backward") key = 3 car_control.car_move_backward() else: print("Stop") car_control.car_stop() #car_control.cleanGPIO() car_control.cleanGPIO()
if action_num = 0: print("Left") car_control.car_turn_left() time.sleep(0.25) elif action_num == 1: print("Right") car_control.car_turn_right() time.sleep(0.25) elif action_num == 2: car_control.car_move_forward() print('Forward') elif action_num == 3: car_control.car_move_backward() print('Backward') elif action_num == 5: car_control.car_stop() print("Barricade") else: car_control.car_stop() print('Stop') class ImageProcessor(threading.Thread): """docstring for ImageProcessor""" def __init__(self, owner): super(ImageProcessor, self).__init__() self.stream = io.BytesIO() self.event = threading.Event() self.terminated = False self.owner = owner self.start()
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!") angle = 90 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") angle = angle - 5 zth_car_control.car_turn_left(angle) sleep(0.1) key = 0 # 按下d右键,保存图片以1开头 elif key_input[pygame.K_d]: print("Right") angle = angle + 5 zth_car_control.car_turn_right(angle) 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键抬起 if 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键抬起 if 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()