2020-02-26 10:47:21 -07:00
|
|
|
import time
|
|
|
|
import numpy as np
|
2020-04-08 14:23:01 -06:00
|
|
|
from scipy import signal
|
|
|
|
from scipy.signal import TransferFunction
|
2020-03-02 11:39:07 -07:00
|
|
|
|
|
|
|
import board
|
2020-03-02 13:40:42 -07:00
|
|
|
import busio
|
2020-03-02 11:39:07 -07:00
|
|
|
import digitalio
|
2020-02-26 10:47:21 -07:00
|
|
|
from adafruit_servokit import ServoKit
|
2020-03-02 13:39:37 -07:00
|
|
|
import adafruit_ads1x15.ads1015 as ADS
|
2020-03-02 11:39:07 -07:00
|
|
|
from adafruit_ads1x15.analog_in import AnalogIn
|
2020-03-02 16:06:58 -07:00
|
|
|
import threading
|
2020-03-04 00:18:18 -07:00
|
|
|
import os
|
2020-02-26 10:47:21 -07:00
|
|
|
|
2020-03-02 14:45:16 -07:00
|
|
|
from bokeh.io import curdoc
|
|
|
|
from bokeh.layouts import column, row
|
2020-03-04 00:18:18 -07:00
|
|
|
from bokeh.models import ColumnDataSource, Slider, TextInput, Button, Paragraph
|
2020-03-02 14:45:16 -07:00
|
|
|
from bokeh.plotting import figure
|
|
|
|
|
2020-03-02 21:57:43 -07:00
|
|
|
DEBUG = False
|
2020-03-04 00:26:48 -07:00
|
|
|
BAT_MUX_CHAN = 9
|
2020-03-04 00:18:18 -07:00
|
|
|
VBAT_THRESHOLD = 11.0
|
2020-03-02 16:27:41 -07:00
|
|
|
|
2020-03-02 20:57:31 -07:00
|
|
|
# Configure MUX for ADC
|
2020-03-02 15:59:59 -07:00
|
|
|
mux_io = [None] * 4
|
2020-03-02 18:56:42 -07:00
|
|
|
mux_io[0] = digitalio.DigitalInOut(board.D23)
|
|
|
|
mux_io[1] = digitalio.DigitalInOut(board.D22)
|
|
|
|
mux_io[2] = digitalio.DigitalInOut(board.D27)
|
|
|
|
mux_io[3] = digitalio.DigitalInOut(board.D17)
|
2020-03-02 15:59:59 -07:00
|
|
|
for ii, io in enumerate(mux_io):
|
|
|
|
io.switch_to_output()
|
|
|
|
|
2020-03-02 20:57:31 -07:00
|
|
|
# Configure ADC
|
2020-03-02 15:59:59 -07:00
|
|
|
i2c = busio.I2C(board.SCL, board.SDA)
|
|
|
|
adc = ADS.ADS1015(i2c)
|
|
|
|
adc_mux = AnalogIn(adc, ADS.P0)
|
2020-03-02 20:57:31 -07:00
|
|
|
adc_lock = threading.Lock()
|
|
|
|
|
|
|
|
# Configure Servo Driver
|
|
|
|
servos = ServoKit(channels=16).continuous_servo
|
|
|
|
servos[0].throttle = 0
|
|
|
|
servos[1].throttle = 0
|
|
|
|
servos[2].throttle = 0
|
|
|
|
|
|
|
|
# Initialize calibration
|
2020-03-02 22:18:32 -07:00
|
|
|
try:
|
|
|
|
white_cal = np.loadtxt('cal_white.txt')
|
2020-03-02 22:19:21 -07:00
|
|
|
except IOError:
|
2020-03-02 22:18:32 -07:00
|
|
|
white_cal = [0]*8
|
|
|
|
|
|
|
|
try:
|
|
|
|
black_cal = np.loadtxt('cal_black.txt')
|
2020-03-02 22:19:21 -07:00
|
|
|
except IOError:
|
2020-03-02 22:18:32 -07:00
|
|
|
black_cal = [5]*8
|
2020-03-02 15:59:59 -07:00
|
|
|
|
2020-03-04 00:18:18 -07:00
|
|
|
def get_mux_adc(chan):
|
2020-03-02 15:59:59 -07:00
|
|
|
global mux_io
|
|
|
|
global adc_mux
|
2020-03-02 16:24:01 -07:00
|
|
|
global adc_lock
|
2020-03-02 20:57:31 -07:00
|
|
|
chan = int(chan)
|
2020-03-02 15:59:59 -07:00
|
|
|
mux = 1-np.array(list(f"{chan:04b}"), dtype=int)
|
2020-03-02 16:24:01 -07:00
|
|
|
adc_lock.acquire()
|
2020-03-02 13:57:55 -07:00
|
|
|
for ii, io in enumerate(mux_io):
|
2020-03-02 15:59:59 -07:00
|
|
|
io.value = mux[ii]
|
2020-03-02 16:24:01 -07:00
|
|
|
voltage = adc_mux.voltage
|
|
|
|
adc_lock.release()
|
|
|
|
return voltage
|
2020-03-02 15:22:01 -07:00
|
|
|
|
2020-03-02 15:59:59 -07:00
|
|
|
def get_normalized_reflectivity(chan):
|
|
|
|
global white_cal
|
|
|
|
global black_cal
|
2020-03-04 00:18:18 -07:00
|
|
|
return (get_mux_adc(chan) - black_cal[chan]) / (white_cal[chan] - black_cal[chan])
|
2020-03-02 15:22:01 -07:00
|
|
|
|
2020-03-02 21:06:44 -07:00
|
|
|
# Initialize brightness data
|
|
|
|
brightness_idx = np.arange(8)
|
|
|
|
brightness = [get_normalized_reflectivity(c) for c in range(8)]
|
|
|
|
|
|
|
|
# Initialize time data
|
2020-04-08 14:57:35 -06:00
|
|
|
time_data = np.empty((0, 3)) # [[t, e, u]]
|
2020-03-02 21:06:44 -07:00
|
|
|
|
2020-03-02 20:57:31 -07:00
|
|
|
# Create sources for plots
|
2020-03-02 19:45:04 -07:00
|
|
|
brightness_plot_source = ColumnDataSource(data=dict(sensor=brightness_idx, brightness=brightness))
|
2020-04-08 14:57:35 -06:00
|
|
|
time_plot_source = ColumnDataSource(data=dict(t=time_data[:,0], e=time_data[:,1], u=time_data[:,2]))
|
2020-03-02 14:45:16 -07:00
|
|
|
|
2020-03-02 19:45:04 -07:00
|
|
|
# Set up plots
|
2020-03-02 22:09:25 -07:00
|
|
|
brightness_plot = figure(plot_height=150, plot_width=400, x_range=[0, 7], y_range=[0, 1], tools="save")
|
2020-03-02 19:45:04 -07:00
|
|
|
brightness_plot.line('sensor', 'brightness', source=brightness_plot_source, line_width=3)
|
|
|
|
brightness_plot.circle('sensor', 'brightness', source=brightness_plot_source, size=8, fill_color="white", line_width=2)
|
2020-03-02 14:45:16 -07:00
|
|
|
|
2020-03-02 22:25:48 -07:00
|
|
|
time_plot = figure(plot_height=400, plot_width=800, y_range=[-1, 1], tools="pan,reset,save,wheel_zoom")
|
2020-03-02 21:21:02 -07:00
|
|
|
time_plot.line('t', 'e', source=time_plot_source, line_width=3, line_alpha=0.6, legend_label="e(t)")
|
2020-04-08 14:57:35 -06:00
|
|
|
time_plot.line('t', 'u', source=time_plot_source, line_width=3, line_alpha=0.6, legend_label="u(t)", line_color = "green")
|
2020-03-02 15:24:40 -07:00
|
|
|
|
2020-03-02 21:50:07 -07:00
|
|
|
# Controller thread
|
2020-03-02 21:53:10 -07:00
|
|
|
control_thread_run = False
|
2020-03-02 15:59:59 -07:00
|
|
|
|
2020-03-02 21:53:10 -07:00
|
|
|
def controller():
|
2020-03-02 16:32:55 -07:00
|
|
|
global brightness
|
2020-03-02 20:00:17 -07:00
|
|
|
global time_data
|
2020-03-02 20:57:31 -07:00
|
|
|
global servos
|
2020-03-02 21:55:53 -07:00
|
|
|
global control_thread_run
|
2020-04-08 14:29:42 -06:00
|
|
|
global D # controller model
|
2020-03-02 20:57:31 -07:00
|
|
|
|
2020-03-02 21:31:35 -07:00
|
|
|
# TODO: make these parameters editable via network interface
|
2020-04-08 14:29:42 -06:00
|
|
|
if D.dt is None:
|
|
|
|
sample_interval = 0.01
|
|
|
|
else:
|
|
|
|
sample_interval = D.dt
|
2020-03-02 20:57:31 -07:00
|
|
|
base_speed = 0.1
|
2020-04-08 14:54:03 -06:00
|
|
|
iir_taps = np.append(D.num[1:], 0)
|
2020-04-08 15:07:54 -06:00
|
|
|
fir_taps = np.append(D.num, 0)
|
|
|
|
fir_taps = np.concatenate([np.zeros(len(iir_taps)-len(fir_taps) + 1), fir_taps])
|
2020-04-08 15:09:47 -06:00
|
|
|
|
|
|
|
iir_taps = np.flip(iir_taps)
|
|
|
|
fir_taps = np.flip(fir_taps)
|
2020-04-08 14:29:42 -06:00
|
|
|
# fir_taps = [1, 0, 0]
|
|
|
|
# iir_taps = [0, 0]
|
|
|
|
|
2020-03-02 23:43:40 -07:00
|
|
|
motor_directions = [1, -1, 0]
|
2020-03-02 21:41:43 -07:00
|
|
|
steering_sign = 1
|
2020-03-02 20:57:31 -07:00
|
|
|
|
2020-03-02 22:24:36 -07:00
|
|
|
print("INFO: Controller started")
|
2020-03-02 22:25:48 -07:00
|
|
|
|
2020-03-04 00:18:18 -07:00
|
|
|
# Initialize
|
|
|
|
time_data = np.zeros((max(len(fir_taps), len(iir_taps)), time_data.shape[1]))
|
|
|
|
|
2020-03-02 21:41:43 -07:00
|
|
|
# Precompute
|
|
|
|
this_time = 0
|
2020-04-08 14:57:35 -06:00
|
|
|
new_u = 0
|
2020-03-02 21:41:43 -07:00
|
|
|
motor_speed = np.array(motor_directions) * base_speed
|
2020-03-02 19:45:04 -07:00
|
|
|
|
2020-03-02 21:55:53 -07:00
|
|
|
while control_thread_run:
|
2020-03-02 20:57:31 -07:00
|
|
|
# Read error
|
2020-03-02 19:29:40 -07:00
|
|
|
brightness = np.clip([get_normalized_reflectivity(c) for c in range(8)], 0, 1)
|
2020-03-02 20:57:31 -07:00
|
|
|
line_position = np.sum((1 - brightness) * (np.arange(8) - 3.5)) / np.sum(1-brightness) / 3.5
|
2020-03-02 20:17:17 -07:00
|
|
|
if np.isnan(line_position):
|
|
|
|
line_position = 0
|
2020-03-02 19:45:04 -07:00
|
|
|
|
2020-03-02 20:57:31 -07:00
|
|
|
# Calculate output
|
2020-04-08 14:57:35 -06:00
|
|
|
new_u += fir_taps[0] * line_position
|
|
|
|
motor_speed += steering_sign * new_u
|
2020-03-02 20:57:31 -07:00
|
|
|
|
|
|
|
# Update motors
|
2020-03-02 23:40:24 -07:00
|
|
|
for ii in range(3):
|
2020-04-08 14:54:03 -06:00
|
|
|
servos[ii].throttle = np.clip(motor_speed[ii], -1, 1)
|
2020-03-02 20:57:31 -07:00
|
|
|
|
|
|
|
# Log data
|
2020-04-08 14:57:35 -06:00
|
|
|
new_time_data = [[this_time, line_position, new_u]]
|
2020-03-02 20:00:46 -07:00
|
|
|
time_data = np.concatenate((time_data, new_time_data))
|
2020-03-02 19:45:04 -07:00
|
|
|
|
2020-03-02 20:57:31 -07:00
|
|
|
# Print data
|
2020-03-02 16:27:41 -07:00
|
|
|
if DEBUG:
|
|
|
|
for b in brightness:
|
|
|
|
print(f"{b:1.2f}\t", end="")
|
2020-03-02 19:29:40 -07:00
|
|
|
print(f"{line_position:+1.2f}", end="")
|
2020-03-02 16:27:41 -07:00
|
|
|
print()
|
2020-03-02 16:17:34 -07:00
|
|
|
|
2020-03-02 21:41:43 -07:00
|
|
|
# Precompute for next iteration
|
|
|
|
this_time = time_data[-1, 0] + sample_interval
|
2020-04-08 14:57:35 -06:00
|
|
|
u = time_data[:,2]
|
2020-03-02 21:41:43 -07:00
|
|
|
e = time_data[:,1]
|
2020-04-08 14:57:35 -06:00
|
|
|
new_u = np.sum(fir_taps[1:] * e[-len(fir_taps)+1:]) - np.sum(iir_taps * u[-len(iir_taps):])
|
2020-03-02 21:41:43 -07:00
|
|
|
motor_speed = np.array(motor_directions) * base_speed
|
|
|
|
|
|
|
|
# TODO: replace sleep statement with something that doesn't depend on execution time of loop
|
|
|
|
time.sleep(sample_interval)
|
2020-03-02 22:24:36 -07:00
|
|
|
|
2020-03-02 23:45:07 -07:00
|
|
|
for ii in range(3):
|
|
|
|
servos[ii].throttle = 0
|
2020-03-04 00:18:18 -07:00
|
|
|
|
2020-03-02 22:24:36 -07:00
|
|
|
print("INFO: Controller stopped")
|
2020-03-02 21:41:43 -07:00
|
|
|
|
2020-03-02 21:55:06 -07:00
|
|
|
control_thread = None
|
2020-03-02 16:11:21 -07:00
|
|
|
|
2020-03-02 21:50:07 -07:00
|
|
|
# Callback functions
|
|
|
|
def update_plots(attrname=None, old=None, new=None):
|
|
|
|
global brightness
|
|
|
|
global time_data
|
|
|
|
global brightness_plot_source
|
2020-03-02 22:02:50 -07:00
|
|
|
global control_thread_run
|
|
|
|
if control_thread_run:
|
|
|
|
brightness_plot_source.data = dict(sensor=brightness_idx, brightness=brightness)
|
2020-04-08 15:01:20 -06:00
|
|
|
time_plot_source.data = dict(t=time_data[:,0], e=time_data[:,1], u=time_data[:,2])
|
2020-03-02 22:02:50 -07:00
|
|
|
else:
|
|
|
|
brightness = np.clip([get_normalized_reflectivity(c) for c in range(8)], 0, 1)
|
|
|
|
brightness_plot_source.data = dict(sensor=brightness_idx, brightness=brightness)
|
2020-03-02 21:50:07 -07:00
|
|
|
|
|
|
|
def cal_white(attrname=None, old=None, new=None):
|
|
|
|
global white_cal
|
2020-03-04 00:18:18 -07:00
|
|
|
white_cal = [get_mux_adc(c) for c in range(8)]
|
2020-03-02 22:18:32 -07:00
|
|
|
np.savetxt('cal_white.txt', white_cal)
|
2020-03-02 21:50:07 -07:00
|
|
|
update_plots()
|
|
|
|
|
|
|
|
def cal_black(attrname=None, old=None, new=None):
|
|
|
|
global black_cal
|
2020-03-04 00:18:18 -07:00
|
|
|
black_cal = [get_mux_adc(c) for c in range(8)]
|
2020-03-02 22:18:32 -07:00
|
|
|
np.savetxt('cal_black.txt', black_cal)
|
2020-03-02 21:50:07 -07:00
|
|
|
update_plots()
|
|
|
|
|
|
|
|
def start_controller(attrname=None, old=None, new=None):
|
2020-03-02 21:51:03 -07:00
|
|
|
global control_thread
|
2020-03-02 21:53:10 -07:00
|
|
|
global control_thread_run
|
2020-03-02 21:51:03 -07:00
|
|
|
control_thread_run = True
|
2020-03-02 21:55:06 -07:00
|
|
|
control_thread = threading.Thread(target=controller)
|
2020-03-02 21:59:54 -07:00
|
|
|
control_thread.daemon = True
|
2020-03-02 21:51:03 -07:00
|
|
|
control_thread.start()
|
2020-03-02 21:50:07 -07:00
|
|
|
|
|
|
|
def stop_controller(attrname=None, old=None, new=None):
|
2020-03-02 21:51:03 -07:00
|
|
|
global control_thread
|
2020-03-02 21:53:10 -07:00
|
|
|
global control_thread_run
|
2020-03-02 21:55:06 -07:00
|
|
|
try:
|
|
|
|
control_thread_run = False
|
|
|
|
control_thread.join()
|
|
|
|
control_thread = None
|
|
|
|
except:
|
|
|
|
pass
|
2020-03-02 21:50:07 -07:00
|
|
|
|
2020-04-08 13:45:34 -06:00
|
|
|
def update_models(attrname=None, old=None, new=None):
|
2020-04-08 13:55:07 -06:00
|
|
|
stop_controller()
|
2020-04-08 13:59:44 -06:00
|
|
|
try:
|
2020-04-08 14:23:01 -06:00
|
|
|
exec("global D\n" + controller_model_text.value)
|
2020-04-08 13:59:44 -06:00
|
|
|
print("INFO: controller model updated")
|
|
|
|
print(D)
|
|
|
|
except:
|
|
|
|
print("WARN: invalid controller model")
|
2020-04-08 13:45:34 -06:00
|
|
|
|
2020-03-04 00:18:18 -07:00
|
|
|
|
2020-03-02 21:50:07 -07:00
|
|
|
# GUI elements
|
2020-03-04 00:18:18 -07:00
|
|
|
vbat_text = Paragraph(text="Battery Voltage: ? V")
|
|
|
|
|
2020-03-02 21:50:07 -07:00
|
|
|
cal_white_button = Button(label="Cal White")
|
|
|
|
cal_white_button.on_click(cal_white)
|
|
|
|
cal_black_button = Button(label="Cal Black")
|
|
|
|
cal_black_button.on_click(cal_black)
|
|
|
|
|
|
|
|
start_button = Button(label="Start")
|
|
|
|
start_button.on_click(start_controller)
|
|
|
|
stop_button = Button(label="Stop")
|
|
|
|
stop_button.on_click(stop_controller)
|
|
|
|
|
2020-04-08 13:45:34 -06:00
|
|
|
# plant_model_text =
|
2020-04-08 13:53:29 -06:00
|
|
|
controller_model_text = TextInput(value="D = TransferFunction([1], [1], dt=0.01)")
|
|
|
|
update_models_button = Button(label="Update models")
|
|
|
|
update_models_button.on_click(update_models)
|
2020-04-08 14:29:42 -06:00
|
|
|
update_models()
|
2020-04-08 13:45:34 -06:00
|
|
|
|
2020-03-04 00:18:18 -07:00
|
|
|
def update_battery_voltage(attrname=None, old=None, new=None):
|
|
|
|
global VBAT_THRESHOLD
|
|
|
|
global vbat_text
|
|
|
|
vadc = get_mux_adc(BAT_MUX_CHAN)
|
2020-03-04 00:27:46 -07:00
|
|
|
# vbat = vadc * (10+1)/1
|
|
|
|
vbat = vadc * 12.21/1.022
|
2020-03-04 00:18:18 -07:00
|
|
|
vbat_text.text = f"Battery Voltage: {vbat:2.1f}V"
|
|
|
|
if vbat < VBAT_THRESHOLD:
|
|
|
|
stop_controller()
|
|
|
|
print("ERROR: Battery Critically Low")
|
|
|
|
# os.system("sudo poweroff")
|
|
|
|
|
|
|
|
# sample_interval = 0.01
|
|
|
|
# base_speed = 0.1
|
|
|
|
# fir_taps = [1, 0, 0]
|
|
|
|
# iir_taps = [0, 0]
|
|
|
|
# controller_sample_interval = TextInput(title="Sample Interval", value=str(sample_interval))
|
|
|
|
# controller_base_speed = Slider(title="Base Speed", value=base_speed, start=0, end=0.8, step=0.01)
|
|
|
|
# controller_fir_taps = TextInput(title="FIR taps", value=str(fir_taps))
|
|
|
|
# controller_iir_taps = TextInput(title="IIR taps", value=str(iir_taps))
|
|
|
|
|
|
|
|
controls = column(vbat_text, cal_white_button, cal_black_button, start_button, stop_button)
|
2020-04-08 13:53:29 -06:00
|
|
|
controller_model = row(controller_model_text, update_models_button)
|
2020-03-04 00:18:18 -07:00
|
|
|
# controller_settings = column(controller_sample_interval, controller_base_speed, controller_fir_taps, controller_iir_taps)
|
2020-04-08 13:45:34 -06:00
|
|
|
curdoc().add_root(column(row(controls, brightness_plot, width=800), time_plot, controller_model))#, controller_settings))
|
2020-03-02 21:50:07 -07:00
|
|
|
curdoc().title = "TriangleBot Control Panel"
|
|
|
|
curdoc().add_periodic_callback(update_plots, 250)
|
2020-03-04 00:18:18 -07:00
|
|
|
curdoc().add_periodic_callback(update_battery_voltage, 1000)
|