From 929f36dc8c85d199d45dce94603fe6c3a4f0a73d Mon Sep 17 00:00:00 2001 From: Danny Staple Date: Sun, 20 Mar 2022 22:32:06 +0000 Subject: [PATCH] Level down clean up --- ch-10/2-graphing/pc/live_graph.py | 81 +++++++++++++++------------- ch-10/3-wall-follow/pc/live_graph.py | 81 +++++++++++++++------------- ch-5/4-pwm/with_helpers/robot.py | 4 +- ch-5/4-pwm/with_pwm/robot.py | 2 +- 4 files changed, 89 insertions(+), 79 deletions(-) diff --git a/ch-10/2-graphing/pc/live_graph.py b/ch-10/2-graphing/pc/live_graph.py index f384218..8fb9a3f 100644 --- a/ch-10/2-graphing/pc/live_graph.py +++ b/ch-10/2-graphing/pc/live_graph.py @@ -4,52 +4,57 @@ import requests import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation -url = 'http://192.168.1.128' +url = "http://192.168.1.128" class SensorStream: - def __init__(self) -> None: - self.reset() + def __init__(self) -> None: + self.reset() - def reset(self): - self.error_values = [] - self.pid_outputs = [] - self.times = [] + def reset(self): + self.error_values = [] + self.pid_outputs = [] + self.times = [] - def sensor_stream(self, frame): - try: - response = requests.get(url, timeout=1) - except (requests.exceptions.ConnectTimeout, requests.exceptions.ReadTimeout, requests.exceptions.ConnectionError): - print("Waiting...") - return - print(f"Content: {response.content}") - print(f"status: {response.status_code}") + def sensor_stream(self, frame): + try: + response = requests.get(url, timeout=1) + except ( + requests.exceptions.ConnectTimeout, + requests.exceptions.ReadTimeout, + requests.exceptions.ConnectionError, + ): + print("Waiting...") + return + print(f"Content: {response.content}") + print(f"status: {response.status_code}") - item = response.json() - - print(f"Received: {item}") - if self.times and item['time'] < self.times[-1]: - self.reset() - self.times.append(item['time']) - self.error_values.append(item['last_value']) - self.pid_outputs.append(item['pid_output']) + item = response.json() - if len(self.times) > 100: - self.times = self.times[-100:] - self.error_values = self.error_values[-100:] - self.pid_outputs = self.pid_outputs[-100:] + print(f"Received: {item}") + if self.times and item["time"] < self.times[-1]: + self.reset() + self.times.append(item["time"]) + self.error_values.append(item["last_value"]) + self.pid_outputs.append(item["pid_output"]) - plt.cla() # clear axes. - # plot the items - plt.plot(self.times, self.error_values, label="error") - plt.plot(self.times, self.pid_outputs, label="pid") - - plt.legend(loc='upper right') + if len(self.times) > 100: + self.times = self.times[-100:] + self.error_values = self.error_values[-100:] + self.pid_outputs = self.pid_outputs[-100:] + + plt.cla() # clear axes. + # plot the items + plt.plot(self.times, self.error_values, label="error") + plt.plot(self.times, self.pid_outputs, label="pid") + + plt.legend(loc="upper right") + + def start(self): + # Create the animation. GFC - get current figure. random_stream - callback func. + self.ani = FuncAnimation(plt.gcf(), self.sensor_stream, interval=200) + plt.tight_layout() + plt.show() - def start(self): - # Create the animation. GFC - get current figure. random_stream - callback func. - self.ani = FuncAnimation(plt.gcf(), self.sensor_stream, interval=200) - plt.tight_layout() - plt.show() SensorStream().start() diff --git a/ch-10/3-wall-follow/pc/live_graph.py b/ch-10/3-wall-follow/pc/live_graph.py index f384218..8fb9a3f 100644 --- a/ch-10/3-wall-follow/pc/live_graph.py +++ b/ch-10/3-wall-follow/pc/live_graph.py @@ -4,52 +4,57 @@ import requests import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation -url = 'http://192.168.1.128' +url = "http://192.168.1.128" class SensorStream: - def __init__(self) -> None: - self.reset() + def __init__(self) -> None: + self.reset() - def reset(self): - self.error_values = [] - self.pid_outputs = [] - self.times = [] + def reset(self): + self.error_values = [] + self.pid_outputs = [] + self.times = [] - def sensor_stream(self, frame): - try: - response = requests.get(url, timeout=1) - except (requests.exceptions.ConnectTimeout, requests.exceptions.ReadTimeout, requests.exceptions.ConnectionError): - print("Waiting...") - return - print(f"Content: {response.content}") - print(f"status: {response.status_code}") + def sensor_stream(self, frame): + try: + response = requests.get(url, timeout=1) + except ( + requests.exceptions.ConnectTimeout, + requests.exceptions.ReadTimeout, + requests.exceptions.ConnectionError, + ): + print("Waiting...") + return + print(f"Content: {response.content}") + print(f"status: {response.status_code}") - item = response.json() - - print(f"Received: {item}") - if self.times and item['time'] < self.times[-1]: - self.reset() - self.times.append(item['time']) - self.error_values.append(item['last_value']) - self.pid_outputs.append(item['pid_output']) + item = response.json() - if len(self.times) > 100: - self.times = self.times[-100:] - self.error_values = self.error_values[-100:] - self.pid_outputs = self.pid_outputs[-100:] + print(f"Received: {item}") + if self.times and item["time"] < self.times[-1]: + self.reset() + self.times.append(item["time"]) + self.error_values.append(item["last_value"]) + self.pid_outputs.append(item["pid_output"]) - plt.cla() # clear axes. - # plot the items - plt.plot(self.times, self.error_values, label="error") - plt.plot(self.times, self.pid_outputs, label="pid") - - plt.legend(loc='upper right') + if len(self.times) > 100: + self.times = self.times[-100:] + self.error_values = self.error_values[-100:] + self.pid_outputs = self.pid_outputs[-100:] + + plt.cla() # clear axes. + # plot the items + plt.plot(self.times, self.error_values, label="error") + plt.plot(self.times, self.pid_outputs, label="pid") + + plt.legend(loc="upper right") + + def start(self): + # Create the animation. GFC - get current figure. random_stream - callback func. + self.ani = FuncAnimation(plt.gcf(), self.sensor_stream, interval=200) + plt.tight_layout() + plt.show() - def start(self): - # Create the animation. GFC - get current figure. random_stream - callback func. - self.ani = FuncAnimation(plt.gcf(), self.sensor_stream, interval=200) - plt.tight_layout() - plt.show() SensorStream().start() diff --git a/ch-5/4-pwm/with_helpers/robot.py b/ch-5/4-pwm/with_helpers/robot.py index f1a9a2b..9ec5868 100755 --- a/ch-5/4-pwm/with_helpers/robot.py +++ b/ch-5/4-pwm/with_helpers/robot.py @@ -8,7 +8,7 @@ motor_B2 = pwmio.PWMOut(board.GP19) right_motor = motor_A1, motor_A2 left_motor = motor_B1, motor_B2 -max_speed = 2**16-1 +max_speed = 2 ** 16 - 1 def stop(): @@ -25,7 +25,7 @@ def set_speed(motor, speed): speed = -speed else: direction = motor - speed = min(speed, 1) # limit to 1.0 + speed = min(speed, 1) # limit to 1.0 direction[0].duty_cycle = int(max_speed * speed) direction[1].duty_cycle = 0 diff --git a/ch-5/4-pwm/with_pwm/robot.py b/ch-5/4-pwm/with_pwm/robot.py index 7bbff7a..22587a6 100755 --- a/ch-5/4-pwm/with_pwm/robot.py +++ b/ch-5/4-pwm/with_pwm/robot.py @@ -6,9 +6,9 @@ motor_A2 = pwmio.PWMOut(board.GP16) motor_B1 = pwmio.PWMOut(board.GP18) motor_B2 = pwmio.PWMOut(board.GP19) + def stop(): motor_A1.duty_cycle = 0 motor_A2.duty_cycle = 0 motor_B1.duty_cycle = 0 motor_B2.duty_cycle = 0 -