From 0284507041c447dfc1bc3d0985a4bde643bb90f5 Mon Sep 17 00:00:00 2001
From: Zhaoliang <zhz03@g.ucla.edu>
Date: Mon, 25 Oct 2021 16:33:45 -0700
Subject: [PATCH] test keyboard code

---
 Code/Control/Laptop_Code/main_keyboard.py | 66 +++++++++++++++++++----
 1 file changed, 56 insertions(+), 10 deletions(-)

diff --git a/Code/Control/Laptop_Code/main_keyboard.py b/Code/Control/Laptop_Code/main_keyboard.py
index a7dd3db..3b0af13 100644
--- a/Code/Control/Laptop_Code/main_keyboard.py
+++ b/Code/Control/Laptop_Code/main_keyboard.py
@@ -1,11 +1,16 @@
 import time
 import serial
-import ball_detection.ball_detection as ball_detection
+import pygame
 import simple_pid.PID as PID
 import cv2
 
 from constants import *
 
+global ml
+ml = 0
+if ml == 1:
+    import ball_detection.ball_detection as ball_detection
+
 # ========= Serial Port I/O ===========
 
 def serial_port_in(serial_port):
@@ -283,7 +288,7 @@ def main_control(gbx, gby, gb_dist, tx, ty, tz, rx, ry, rz, LIDAR_dist, debugM):
     ballDetect  = ball_detect(gbx, gby)
     ballCapture = ball_capture(LIDAR_dist)
     goalDetect  = goal_detect(tx, ty)
-
+    ballCapture = 1
     if ballCapture: # Ball captured
         if goalDetect:  # Goal detected
             # stop_all()  # Debug
@@ -328,7 +333,11 @@ 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)
+    if ml == 1:
+        gbx, gby, gb_dist = ball_detection.detectLive(model, minDetectionScore, showSight=True)
+    else:
+        gbx, gby = -1, -1
+        gb_dist = -1
     line = serial_port.readline()
 
     if line == b'SERIAL_IN_START\r\n':
@@ -357,6 +366,17 @@ def decode_ctl(Ctl_com):
     dir4 = Ctl_com[7]
     return pwm1, pwm2, pwm3, pwm4, dir1, dir2, dir3, dir4
 
+def manual_command(val1,val2,val3,val4,sign1,sign2,sign3,sign4):
+    pwm1 = val1
+    pwm2 = val2
+    pwm3 = val3
+    pwm4 = val4
+    dir1 = sign1
+    dir2 = sign2
+    dir3 = sign3
+    dir4 = sign4
+    return pwm1,pwm2,pwm3,pwm4,dir1,dir2,dir3,dir4
+
 def manual_control(Ctl_com,serial_port):
 
     if get_key("w"):
@@ -392,14 +412,32 @@ def manual_control(Ctl_com,serial_port):
     time.sleep(waitTime)
 
 
-def variables_change():
-    pass
+def dynamic_variable(str_name_v):
+
+    global kpx,kix,kdx,start_speed
+    if str_name_v == "kpx":
+        kpx = input("Enter your value: ")
+        print("kpx:{}".format(kpx))
+    elif str_name_v == "kix":
+        kix = input("Enter your value: ")
+        print("kix:{}".format(kix))
+    elif str_name_v == "kdx":
+        kdx = input("Enter your value: ")
+        print("kdx:{}".format(kdx))
+    elif str_name_v == "stsp":
+        start_speed = input("Enter your value: ")
+        print("start_speed:{}".format(start_speed))
+
+def variables_change_once():
+
+    str_name = input("Enter your variable: ")
+    dynamic_variable(str_name)
 
 # ===== Main Function =====
 if __name__ == '__main__':
     # =========== SET UP ============
     # Defining Variables for ESP 32 Serial I/O
-    PORT = "COM6" # for Alienware
+    PORT = "COM20" # Based on your own serial port number
     serial_port = serial.Serial(PORT, 115200)
     serial_port.close()
     serial_port.open()
@@ -408,10 +446,12 @@ if __name__ == '__main__':
     waitTime = 0.05
 
     # Loading the PyTorch ML model for ball detection
-    model = ball_detection.returnModel(device, labelSet, modelLoc, modelFile)
+    if ml == 1:
+        model = ball_detection.returnModel(device, labelSet, modelLoc, modelFile)
 
     # =========== DECLARE VARIABLES ===========
     # ESP CAM In
+    global gbx,gby,gb_dist
     gbx, gby  = -1, -1   # by default (-1 means no found green ball)
     gb_dist = -1         # by default (-1 means no found green ball)
 
@@ -426,13 +466,17 @@ if __name__ == '__main__':
     dir1, dir2, dir3, dir4 = '+', '+', '+', '+'
 
     # Trigger the ESP32_SLAVE to talk first
-    gbx, gby, gb_dist = ball_detection.detectLive(model, minDetectionScore, showSight = True)
+    if ml == 1:
+        gbx, gby, gb_dist = ball_detection.detectLive(model, minDetectionScore, showSight = True)
     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
+    global start_speed
+    start_speed = 70
+
     init()
     # =========== LOOP FOREVER===========
     while True:
@@ -456,8 +500,10 @@ if __name__ == '__main__':
         elif get_key('v'):
             flag = 3
             while (flag == 3):
-                variables_change()
-                flag, print_count = keyboard_stop(flag,print_count)
+                variables_change_once()
+                flag = 0
+                print_count = 1
+
         elif get_key('k'):
             break
 
-- 
GitLab