Skip to content

Commit ee2bc59

Browse files
WeixuanZjr-brown
andcommitted
Improve passive collision avoidance and motion
Co-authored-by: Jason Brown <[email protected]>
1 parent dccfb70 commit ee2bc59

9 files changed

+241
-94
lines changed

driver/driver.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
"""This is the driver script of the controller
55
"""
66
from robot import IDPRobot
7+
from modules.utils import print_if_debug
8+
9+
DEBUG_OBJECTIVE = False
710

811
robot = IDPRobot()
912
complete = False
@@ -12,7 +15,7 @@
1215
while robot.step(robot.timestep) != -1:
1316

1417
# Check if we are finished
15-
if robot.target_cache.num_collected >= 4 or robot.targeting_handler.num_scans >= 10:
18+
if robot.target_cache.num_collected >= 4 or robot.targeting_handler.num_scans >= 15:
1619
if robot.distance_from_bot(robot.home) <= 0.1:
1720
robot.do("hold")
1821
if not complete:
@@ -24,10 +27,14 @@
2427
# If we have no action
2528
if robot.execute_next_action():
2629
# If we have a target go to it, else scan
27-
if robot.get_best_target():
30+
if target := robot.get_best_target():
31+
print_if_debug(f"{robot.color}, objective: Collecting block at {target.position}",
32+
debug_flag=DEBUG_OBJECTIVE)
2833
robot.do("collect")
2934
else:
3035
other_bot_collected = robot.radio.get_other_bot_collected()
3136
if other_bot_collected is not None and len(other_bot_collected) == 4:
3237
robot.target_cache.update_flipped(robot.color)
38+
print_if_debug(f"{robot.color}, objective: No target, scanning",
39+
debug_flag=DEBUG_OBJECTIVE)
3340
robot.do("scan")

driver/modules/motion.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@ class MotionCS:
1515
To be used via robot.motors.velocities = MotionControlStrategies.some_method(*args)
1616
"""
1717

18-
# Some characteristics of the robot used for motion calcs
19-
max_f_speed = 1.0
20-
max_r_speed = 11.2
21-
f_drive_speed_ratio = 1 / max_f_speed
22-
r_drive_speed_ratio = 1 / max_r_speed
18+
# Overwritten in robot.py
19+
max_f_speed = None
2320

2421
@staticmethod
2522
def combine_and_scale(forward, rotation, angle=None):
@@ -103,8 +100,8 @@ def linear_dual_pid(distance=None, forward_speed=None, distance_pid=None, forwar
103100
req_forward_speed *= attenuation_factor
104101

105102
# Convert from actual robot velocities to drive fraction equivalent
106-
required_forward_drive = MotionCS.f_drive_speed_ratio * req_forward_speed
107-
forward_speed_drive_eq = None if forward_speed is None else MotionCS.f_drive_speed_ratio * forward_speed
103+
required_forward_drive = req_forward_speed / MotionCS.max_f_speed
104+
forward_speed_drive_eq = None if forward_speed is None else forward_speed / MotionCS.max_f_speed
108105

109106
return MotionCS.dual_pid(prime_quantity=distance, deriv_quantity=forward_speed_drive_eq,
110107
prime_pid=distance_pid, derivative_pid=forward_speed_pid,

driver/modules/pid.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,11 @@ def plot_history(self, *args):
173173
{f" d_weight_decay_half_life={self.d_weight_decay_half_life} " if self.d_weight_decay_half_life is not None else ""}"""
174174

175175
if not args:
176-
plot_args = ["output", "error"] + (["p"] * bool(self.k_p)) + (["cumulative_error", "i"] * bool(self.k_i))\
177-
+ (["error_change", "d"] * bool(self.k_d)) # Now this is pod-racing
176+
if self.custom_function is None:
177+
plot_args = ["output", "error"] + (["p"] * bool(self.k_p)) + (["cumulative_error", "i"] * bool(self.k_i))\
178+
+ (["error_change", "d"] * bool(self.k_d)) # Now this is pod-racing
179+
else:
180+
plot_args = ["output", "error", "cumulative_error", "error_change"]
178181
else:
179182
plot_args = args
180183

driver/pid_tuning.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# Test script for drive_to_pos
22

33
import numpy as np
4-
from modules.utils import fire_and_forget
54

65
tau = np.pi * 2
76

@@ -13,8 +12,7 @@ def manual(robot):
1312

1413
# Actions for our robot
1514
action_queue = [
16-
("hold", 2),
17-
("move", [0.5, 0])
15+
("move", [-0.5, -0.4])
1816
]
1917

2018
robot.action_queue = action_queue
@@ -23,7 +21,4 @@ def manual(robot):
2321
while robot.step(timestep) != -1:
2422
robot.execute_next_action()
2523

26-
fire_and_forget(robot.plot_motion_history)
27-
fire_and_forget(robot.pid_f_velocity.plot_history)
28-
fire_and_forget(robot.pid_distance.plot_history)
29-
fire_and_forget(robot.pid_angle.plot_history)
24+
robot.plot_all_graphs()

driver/robot.py

+192-72
Large diffs are not rendered by default.

driver/tests/collision_avoidance.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Test script for drive_to_pos
2+
3+
def main(robot):
4+
# Setup
5+
timestep = int(robot.getBasicTimeStep())
6+
7+
# Actions for our robot
8+
action_queue = [
9+
("move", [0, 0]),
10+
"scan",
11+
("move", [0.9, 0.9]),
12+
("move", [0.9, -0.9]),
13+
("move", [-0.9, 0.9]),
14+
("move", [-0.9, -0.9]),
15+
("move", [0, 0])
16+
]
17+
18+
robot.action_queue = action_queue
19+
20+
# Main loop, perform simulation steps until Webots is stopping the controller
21+
while robot.step(timestep) != -1:
22+
robot.execute_next_action()

driver/tests/manual_test.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ def main(robot):
1212
# Main loop, perform simulation steps until Webots is stopping the controller
1313
while robot.step(timestep) != -1:
1414

15-
robot.motors.velocities = np.array([0.5, -0.5])
15+
robot.motors.velocities = np.array([1, -1])
1616
robot.update_motion_history(time=robot.time, linear_speed=robot.linear_speed,
1717
angular_velocity=robot.angular_velocity)
18-
print(robot.linear_speed)
1918

2019
robot.plot_motion_history()

driver/tests/motion_test.py

+2
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ def main(robot):
2323
# Main loop, perform simulation steps until Webots is stopping the controller
2424
while robot.step(timestep) != -1:
2525
robot.execute_next_action()
26+
27+
robot.plot_all_graphs()

driver/tests_extern.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
from pid_tuning import manual as pid_tuning
1919
from tests.manual_test import main as manual_test
2020
from tests.color_test import main as color_test
21+
from tests.collision_avoidance import main as collision_avoidance_test
2122

2223
if __name__ == '__main__':
2324
robot = IDPRobot()
2425
# com_test(robot)
25-
# motion_test(robot)
26+
motion_test(robot)
2627
# camera_test(robot)
2728
# gate_test(robot)
2829
# object_processing_test(robot)
@@ -31,6 +32,7 @@
3132
# sensor_bounds_ultrasonic_test(robot)
3233
# sensor_bounds_ir_test(robot)
3334
# greedy_collect(robot)
34-
pid_tuning(robot)
35+
# pid_tuning(robot)
3536
# manual_test(robot)
3637
# color_test(robot)
38+
collision_avoidance_test(robot)

0 commit comments

Comments
 (0)