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
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()
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'])
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:
self.times = self.times[-100:]
self.error_values = self.error_values[-100:]
self.pid_outputs = self.pid_outputs[-100:]
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.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')
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()

View File

@ -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()
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'])
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:
self.times = self.times[-100:]
self.error_values = self.error_values[-100:]
self.pid_outputs = self.pid_outputs[-100:]
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.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')
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()

View File

@ -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

View File

@ -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