Wall following demo

This commit is contained in:
Danny Staple
2022-03-20 18:32:33 +00:00
parent 4f7f3542bf
commit e06501811b
9 changed files with 134 additions and 103 deletions
+19 -20
View File
@@ -1,6 +1,4 @@
""" Turn JSON data stream into graphs"""
from itertools import count
import requests
import matplotlib.pyplot as plt
@@ -11,39 +9,40 @@ url = 'http://192.168.1.128'
class SensorStream:
def __init__(self) -> None:
self.index = count()
self.reset()
def reset(self):
self.error_values = []
self.sensor_values = []
self.pid_outputs = []
self.x_values = []
self.times = []
self.set_point = requests.get(f"{url}/set_point").json()
def sensor_stream(self, frame):
response = requests.get(url, timeout=1)
print(f"Content: {response.content}")
print(f"status: {response.status_code}")
def sensor_stream(self):
item = requests.get(url).json()
print(f"Received: {item.decode('utf-8')}")
self.x_vals.append(next(self.index))
self.error_values.append(item['error_value'])
self.sensor_values.append(item['sensor_value'])
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.x_vals) > 100:
self.x_vals = self.x_vals[-100:]
if len(self.times) > 100:
self.times = self.times[-100:]
self.error_values = self.error_values[-100:]
self.sensor_values = self.sensor_values[-100:]
self.pid_outputs = self.pid_outputs[-100:]
plt.cla() # clear axes.
# plot the items
plt.plot(self.x_vals, self.error_values, label="error")
plt.plot(self.x_vals, self.sensor_values, label="sensor")
plt.plot(self.x_vals, self.pid_outputs, label="pid")
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):
plt.style.use('fivethirtyeight')
# Create the animation. GFC - get current figure. random_stream - callback func.
self.ani = FuncAnimation(plt.gcf(), self.sensor_stream, interval=200)
plt.tight_layout()