diff --git a/Code/Control/Laptop_Code/main_keyboard.py b/Code/Control/Laptop_Code/main_keyboard.py index b002e5816c7318a5d5deee6ba1d9c1ba1ad52a1a..a7dd3dbbcfee30f6945160082bf6bf9d17d24bf1 100644 --- a/Code/Control/Laptop_Code/main_keyboard.py +++ b/Code/Control/Laptop_Code/main_keyboard.py @@ -324,9 +324,10 @@ def get_key(keyname): pygame.display.update() return ans -def auto_control(): +def auto_control(serial_port): # =================================== tested autonomous control ====================================================== # ===== STEP 1: TAKE ALL INPUT ===== + waitTime = 0.05 gbx, gby, gb_dist = ball_detection.detectLive(model, minDetectionScore, showSight=True) line = serial_port.readline() @@ -345,9 +346,51 @@ def auto_control(): time.sleep(waitTime) # second # ========================================================================================= +def decode_ctl(Ctl_com): + pwm1 = Ctl_com[0] + pwm2 = Ctl_com[1] + pwm3 = Ctl_com[2] + pwm4 = Ctl_com[3] + dir1 = Ctl_com[4] + dir2 = Ctl_com[5] + dir3 = Ctl_com[6] + dir4 = Ctl_com[7] + return pwm1, pwm2, pwm3, pwm4, dir1, dir2, dir3, dir4 + +def manual_control(Ctl_com,serial_port): + + if get_key("w"): + val = start_speed + Ctl_com = manual_command(val, val, 0, 0, "+", "+", "+", "+") + + elif get_key("s"): + val = start_speed + Ctl_com = manual_command(val, val, 0, 0, "-", "-", "+", "+") + + if get_key("UP"): + val = start_speed + + Ctl_com = manual_command(0, 0, val, val, "+", "+", "+", "+") + elif get_key("DOWN"): + val = start_speed + Ctl_com = manual_command(0, 0, val, val, "+", "+", "-", "-") + + elif get_key("LEFT"): + val = start_speed + Ctl_com = manual_command(0, 0, val, val, "+", "+", "-", "+") + + elif get_key("RIGHT"): + val = start_speed + Ctl_com = manual_command(0, 0, val, val, "+", "+", "+", "-") + + pwm1, pwm2, pwm3, pwm4, dir1, dir2, dir3, dir4 = decode_ctl(Ctl_com) + + waitTime = 0.05 + + serial_port_out(serial_port, pwm1, pwm2, pwm3, pwm4, dir1, dir2, dir3, dir4) + + time.sleep(waitTime) -def manual_control(): - pass def variables_change(): pass @@ -387,6 +430,7 @@ if __name__ == '__main__': pwm1, pwm2, pwm3, pwm4, dir1, dir2, dir3, dir4 = main_control(gbx, gby, gb_dist, tx, ty, tz, rx, ry, rz, LIDAR_dist, debugM) serial_port_out(serial_port, pwm1, pwm2, pwm3, pwm4, dir1, dir2, dir3, dir4) + Ctl_com = [0, 0, 0, 0, "+", "+", "+", "+"] flag = 0 print_count = 1 init() @@ -396,11 +440,8 @@ if __name__ == '__main__': if get_key('a'): flag = 1 while (flag == 1): - auto_control() + auto_control(serial_port) flag, print_count = keyboard_stop(flag,print_count) - if flag == 0: - cap.release() - cv2.destroyAllWindows() elif get_key('s'): stop_all() @@ -409,7 +450,7 @@ if __name__ == '__main__': elif get_key('m'): flag = 2 while (flag == 2): - manual_control() + manual_control(Ctl_com,serial_port) flag, print_count = keyboard_stop(flag,print_count) elif get_key('v'):