diff --git a/Code/Control/Laptop_Code/main.py b/Code/Control/Laptop_Code/main.py
index 2a51fa0302f4e2524f77d7b3e2c1dda3032f9f19..853428b4dbf5dee5286224653f52825b878cd214 100644
--- a/Code/Control/Laptop_Code/main.py
+++ b/Code/Control/Laptop_Code/main.py
@@ -70,7 +70,6 @@ def serial_port_out(serial_port, pwm1, pwm2, pwm3, pwm4, dir1, dir2, dir3, dir4)
Output:
None
'''
-
output_message = ''
for pwm_itr in [pwm1, pwm2, pwm3, pwm4]:
@@ -89,9 +88,7 @@ def serial_port_out(serial_port, pwm1, pwm2, pwm3, pwm4, dir1, dir2, dir3, dir4)
print(output_message)
-
-
-# ====== supporting function in main control ====
+# ====== Logic-directing Functions ====
def ball_detect(gbx, gby):
'''
return True if green ball is detected
@@ -119,7 +116,7 @@ def ball_capture(LIDAR_dist):
else:
return False
-
+# ======= Motion-based Functions ======
def stop_all():
pwm1, pwm2, pwm3, pwm4 = 0 , 0 , 0 , 0
dir1, dir2, dir3, dir4 = '+', '-', '+', '-'
@@ -139,11 +136,52 @@ def move2goal(tx, ty):
Output:
pwm1, pwm2, pwm3, pwm4
dir1, dir2, dir3, dir4
- """
- pass
- # return int(pwm1), int(pwm2), int(pwm3), int(pwm4), dir1, dir2, dir3, dir4
-
-
+ """
+ inputx = tx / 1.00
+ inputy = ty / 1.00
+
+ # April Tag Center
+ setpoint_x = 160
+ setpoint_y = 120
+
+ pid_x = PID(kdx,kix,kpx,setpoint = setpoint_x)
+ pid_y = PID(kdy,kiy,kpy,setpoint = setpoint_y)
+
+ pid_x.auto_mode = True
+ pid_x.set_auto_mode(True, last_output = 8.0)
+ pid_x.output_limits = (-255,255)
+ pid_y.output_limits = (-255,255)
+
+ outputx = pid_x(inputx)
+ outputy = pid_y(inputy)
+
+ # Vertical
+ pwm1 = abs(outputy)
+ pwm2 = abs(outputy)
+
+ if(outputy > 0):
+ dir1 = '+'
+ dir2 = '+'
+ else:
+ dir1 = '-'
+ dir2 = '-'
+
+ # Horizontal
+ lspeed = -1 * outputx + base_speed
+ rspeed = 1 * outputx + base_speed
+ pwm3 = abs(lspeed)
+ pwm4 = abs(rspeed)
+ if (lspeed > 0):
+ dir3 = '+'
+ else:
+ dir3 = '-'
+ if (rspeed > 0):
+ dir4 = '+'
+ else:
+ dir4 = '-'
+
+ return int(pwm1), int(pwm2), int(pwm3), int(pwm4), dir1, dir2, dir3, dir4
+
def seeking():
"""
@@ -178,39 +216,36 @@ def move2ball(gbx,gby,gb_dist):
pwm1, pwm2, pwm3, pwm4
dir1, dir2, dir3, dir4
"""
-
inputx = gbx / 1.00
inputy = gby / 1.00
- # ESP 32 Cam Center
+ # ESP-Cam Center
setpoint_x = 400
setpoint_y = 300
-
- pid_x = PID(kdx,kix,kpx,setpoint=setpoint_x)
- pid_y = PID(kdy,kiy,kpy,setpoint=setpoint_y)
-
-
+
+ pid_x = PID(kdx,kix,kpx,setpoint = setpoint_x)
+ pid_y = PID(kdy,kiy,kpy,setpoint = setpoint_y)
+
pid_x.auto_mode = True
- pid_x.set_auto_mode(True, last_output=8.0)
+ pid_x.set_auto_mode(True, last_output = 8.0)
pid_x.output_limits = (-255,255)
pid_y.output_limits = (-255,255)
-
-
+
outputx = pid_x(inputx)
outputy = pid_y(inputy)
-
- # vertical
+
+ # Vertical
pwm1 = abs(outputy)
pwm2 = abs(outputy)
-
+
if(outputy > 0):
dir1 = '+'
dir2 = '+'
else:
dir1 = '-'
dir2 = '-'
-
- # horizontal
+
+ # Horizontal
lspeed = -1 * outputx + base_speed
rspeed = 1 * outputx + base_speed
pwm3 = abs(lspeed)
@@ -219,18 +254,14 @@ def move2ball(gbx,gby,gb_dist):
dir3 = '+'
else:
dir3 = '-'
-
if (rspeed > 0):
dir4 = '+'
else:
dir4 = '-'
-
-
+
return int(pwm1), int(pwm2), int(pwm3), int(pwm4), dir1, dir2, dir3, dir4
-
-
# =========== main control ===========
def main_control(gbx, gby, gb_dist, tx, ty, tz, rx, ry, rz, LIDAR_dist, debugM):
@@ -256,7 +287,6 @@ def main_control(gbx, gby, gb_dist, tx, ty, tz, rx, ry, rz, LIDAR_dist, debugM):
goalDetect = goal_detect(tx,ty)
if ballCapture: # Ball captured
-
if goalDetect: # Goal detected
pwm1, pwm2, pwm3, pwm4, dir1, dir2, dir3, dir4 = move2goal(tx, ty)
else: # Goal not detectedpwm1, pwm2, pwm3, pwm4, dir1, dir2, dir3, dir4 =
@@ -306,7 +336,7 @@ if __name__ == '__main__':
# =========== LOOP FOREVER===========
while True:
gbx, gby, gb_dist = ball_detection.detectLive(model, minDetectionScore, showSight = True)
- print("gbx,gby:{},{}".format(gbx,gby))
+ print("gbx, gby: {}, {}".format(gbx, gby))
line = serial_port.readline()
if line == b'SERIAL_IN_START\r\n':
@@ -318,11 +348,11 @@ if __name__ == '__main__':
# ===== STEP 3: FEED ALL OUTPUT =====
serial_port_out(serial_port, pwm1, pwm2, pwm3, pwm4, dir1, dir2, dir3, dir4)
+
+ time.sleep(waitTime)
if count == 0:
# first time calling (call once)
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)
- # count +=1
-
- time.sleep(waitTime)
+ # count +=1
\ No newline at end of file