From 605b0afa9b77e7e97273e7008dcab9937694091d Mon Sep 17 00:00:00 2001 From: Danny Staple Date: Tue, 15 Nov 2022 21:24:59 +0000 Subject: [PATCH] Put into a function. break down the large function --- .../robot/code.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ch-13/4-moving-poses-with-sensors-turning/robot/code.py b/ch-13/4-moving-poses-with-sensors-turning/robot/code.py index 400c798..1a54581 100644 --- a/ch-13/4-moving-poses-with-sensors-turning/robot/code.py +++ b/ch-13/4-moving-poses-with-sensors-turning/robot/code.py @@ -13,6 +13,13 @@ class Simulation: for n in range(population_size): self.poses[n] = random.uniform(0, arena.width), random.uniform(0, arena.height), random.uniform(0, 360) + def move_poses(self, speed_in_mm, heading_change): + for pose in self.poses: + pose[2] += heading_change + pose[2] = pose[2] % 360 + pose[0] += speed_in_mm * np.cos(np.radians(pose[2])) + pose[1] += speed_in_mm * np.sin(np.radians(pose[2])) + async def run(self): try: for _ in range(5): @@ -27,11 +34,7 @@ class Simulation: speed_in_mm = robot.ticks_to_m * ((left_movement + right_movement) / 2) * 1000 new_heading = robot.imu.euler[0] heading_change = starting_heading - new_heading - for pose in self.poses: - pose[2] += heading_change - pose[2] = pose[2] % 360 - pose[0] += speed_in_mm * np.cos(np.radians(pose[2])) - pose[1] += speed_in_mm * np.sin(np.radians(pose[2])) + self.move_poses(speed_in_mm, heading_change) finally: robot.stop()