Level down clean up

This commit is contained in:
Danny Staple 2022-03-20 22:32:06 +00:00
parent b595f3be54
commit 929f36dc8c
4 changed files with 89 additions and 79 deletions

View File

@ -4,52 +4,57 @@ import requests
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation from matplotlib.animation import FuncAnimation
url = 'http://192.168.1.128' url = "http://192.168.1.128"
class SensorStream: class SensorStream:
def __init__(self) -> None: def __init__(self) -> None:
self.reset() self.reset()
def reset(self): def reset(self):
self.error_values = [] self.error_values = []
self.pid_outputs = [] self.pid_outputs = []
self.times = [] self.times = []
def sensor_stream(self, frame): def sensor_stream(self, frame):
try: try:
response = requests.get(url, timeout=1) response = requests.get(url, timeout=1)
except (requests.exceptions.ConnectTimeout, requests.exceptions.ReadTimeout, requests.exceptions.ConnectionError): except (
print("Waiting...") requests.exceptions.ConnectTimeout,
return requests.exceptions.ReadTimeout,
print(f"Content: {response.content}") requests.exceptions.ConnectionError,
print(f"status: {response.status_code}") ):
print("Waiting...")
return
print(f"Content: {response.content}")
print(f"status: {response.status_code}")
item = response.json() 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'])
if len(self.times) > 100: print(f"Received: {item}")
self.times = self.times[-100:] if self.times and item["time"] < self.times[-1]:
self.error_values = self.error_values[-100:] self.reset()
self.pid_outputs = self.pid_outputs[-100:] self.times.append(item["time"])
self.error_values.append(item["last_value"])
self.pid_outputs.append(item["pid_output"])
plt.cla() # clear axes. if len(self.times) > 100:
# plot the items self.times = self.times[-100:]
plt.plot(self.times, self.error_values, label="error") self.error_values = self.error_values[-100:]
plt.plot(self.times, self.pid_outputs, label="pid") self.pid_outputs = self.pid_outputs[-100:]
plt.legend(loc='upper right') 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() SensorStream().start()

View File

@ -4,52 +4,57 @@ import requests
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation from matplotlib.animation import FuncAnimation
url = 'http://192.168.1.128' url = "http://192.168.1.128"
class SensorStream: class SensorStream:
def __init__(self) -> None: def __init__(self) -> None:
self.reset() self.reset()
def reset(self): def reset(self):
self.error_values = [] self.error_values = []
self.pid_outputs = [] self.pid_outputs = []
self.times = [] self.times = []
def sensor_stream(self, frame): def sensor_stream(self, frame):
try: try:
response = requests.get(url, timeout=1) response = requests.get(url, timeout=1)
except (requests.exceptions.ConnectTimeout, requests.exceptions.ReadTimeout, requests.exceptions.ConnectionError): except (
print("Waiting...") requests.exceptions.ConnectTimeout,
return requests.exceptions.ReadTimeout,
print(f"Content: {response.content}") requests.exceptions.ConnectionError,
print(f"status: {response.status_code}") ):
print("Waiting...")
return
print(f"Content: {response.content}")
print(f"status: {response.status_code}")
item = response.json() 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'])
if len(self.times) > 100: print(f"Received: {item}")
self.times = self.times[-100:] if self.times and item["time"] < self.times[-1]:
self.error_values = self.error_values[-100:] self.reset()
self.pid_outputs = self.pid_outputs[-100:] self.times.append(item["time"])
self.error_values.append(item["last_value"])
self.pid_outputs.append(item["pid_output"])
plt.cla() # clear axes. if len(self.times) > 100:
# plot the items self.times = self.times[-100:]
plt.plot(self.times, self.error_values, label="error") self.error_values = self.error_values[-100:]
plt.plot(self.times, self.pid_outputs, label="pid") self.pid_outputs = self.pid_outputs[-100:]
plt.legend(loc='upper right') 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() SensorStream().start()

View File

@ -8,7 +8,7 @@ motor_B2 = pwmio.PWMOut(board.GP19)
right_motor = motor_A1, motor_A2 right_motor = motor_A1, motor_A2
left_motor = motor_B1, motor_B2 left_motor = motor_B1, motor_B2
max_speed = 2**16-1 max_speed = 2 ** 16 - 1
def stop(): def stop():
@ -25,7 +25,7 @@ def set_speed(motor, speed):
speed = -speed speed = -speed
else: else:
direction = motor 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[0].duty_cycle = int(max_speed * speed)
direction[1].duty_cycle = 0 direction[1].duty_cycle = 0

View File

@ -6,9 +6,9 @@ motor_A2 = pwmio.PWMOut(board.GP16)
motor_B1 = pwmio.PWMOut(board.GP18) motor_B1 = pwmio.PWMOut(board.GP18)
motor_B2 = pwmio.PWMOut(board.GP19) motor_B2 = pwmio.PWMOut(board.GP19)
def stop(): def stop():
motor_A1.duty_cycle = 0 motor_A1.duty_cycle = 0
motor_A2.duty_cycle = 0 motor_A2.duty_cycle = 0
motor_B1.duty_cycle = 0 motor_B1.duty_cycle = 0
motor_B2.duty_cycle = 0 motor_B2.duty_cycle = 0