2020-02-26 10:47:21 -07:00
|
|
|
import time
|
|
|
|
import numpy as np
|
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-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-02 15:16:55 -07:00
|
|
|
from bokeh.models import ColumnDataSource, Slider, TextInput, Button
|
2020-03-02 14:45:16 -07:00
|
|
|
from bokeh.plotting import figure
|
|
|
|
|
2020-03-02 16:27:41 -07:00
|
|
|
DEBUG = True
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
i2c = busio.I2C(board.SCL, board.SDA)
|
|
|
|
adc = ADS.ADS1015(i2c)
|
|
|
|
adc_mux = AnalogIn(adc, ADS.P0)
|
|
|
|
white_cal = [0]*8
|
|
|
|
black_cal = [5]*8
|
|
|
|
|
2020-03-02 16:24:01 -07:00
|
|
|
adc_lock = threading.Lock()
|
|
|
|
|
2020-03-02 15:59:59 -07:00
|
|
|
def get_reflectivity(chan):
|
|
|
|
chan = int(chan)
|
|
|
|
global mux_io
|
|
|
|
global adc_mux
|
2020-03-02 16:24:01 -07:00
|
|
|
global adc_lock
|
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:30:00 -07:00
|
|
|
time.sleep(0.001)
|
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
|
|
|
|
|
|
|
|
return (get_reflectivity(chan) - black_cal[chan]) / (white_cal[chan] - black_cal[chan])
|
2020-03-02 15:22:01 -07:00
|
|
|
|
2020-03-02 15:59:59 -07:00
|
|
|
brightness_idx = np.arange(8)
|
|
|
|
brightness = [get_normalized_reflectivity(c) for c in range(8)]
|
2020-03-02 19:45:04 -07:00
|
|
|
t = np.array([])
|
|
|
|
error = np.array([])
|
2020-03-02 14:45:16 -07:00
|
|
|
|
2020-03-02 19:45:04 -07:00
|
|
|
brightness_plot_source = ColumnDataSource(data=dict(sensor=brightness_idx, brightness=brightness))
|
|
|
|
time_plot_source = ColumnDataSource(data=dict(t=t, e=error))
|
2020-03-02 14:45:16 -07:00
|
|
|
|
2020-03-02 19:45:04 -07:00
|
|
|
# Set up plots
|
|
|
|
brightness_plot = figure(plot_height=150, plot_width=400, title="Reflectivity", x_range=[0, 7], y_range=[0, 1])
|
|
|
|
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 19:45:04 -07:00
|
|
|
time_plot = figure(plot_height=400, plot_width=400, title="Signals")
|
|
|
|
time_plot.line('t', 'e', source=time_plot_source, line_width=3, line_alpha=0.6)
|
2020-03-02 15:24:40 -07:00
|
|
|
|
2020-03-02 19:45:04 -07:00
|
|
|
def update_plots(attrname=None, old=None, new=None):
|
2020-03-02 16:32:55 -07:00
|
|
|
global brightness
|
2020-03-02 19:45:04 -07:00
|
|
|
global error
|
|
|
|
global brightness_plot_source
|
|
|
|
brightness_plot_source.data = dict(sensor=brightness_idx, brightness=brightness)
|
|
|
|
time_plot_source.data = dict(t=t, e=error)
|
|
|
|
|
2020-03-02 15:59:59 -07:00
|
|
|
|
2020-03-02 16:01:28 -07:00
|
|
|
def cal_white(attrname=None, old=None, new=None):
|
2020-03-02 15:59:59 -07:00
|
|
|
global white_cal
|
2020-03-02 16:24:01 -07:00
|
|
|
white_cal = [get_reflectivity(c) for c in range(8)]
|
2020-03-02 19:45:04 -07:00
|
|
|
update_plots()
|
2020-03-02 15:59:59 -07:00
|
|
|
|
2020-03-02 16:01:28 -07:00
|
|
|
def cal_black(attrname=None, old=None, new=None):
|
2020-03-02 15:59:59 -07:00
|
|
|
global black_cal
|
2020-03-02 16:24:01 -07:00
|
|
|
black_cal = [get_reflectivity(c) for c in range(8)]
|
2020-03-02 19:45:04 -07:00
|
|
|
update_plots()
|
2020-03-02 15:59:59 -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)
|
|
|
|
|
2020-03-02 19:03:20 -07:00
|
|
|
controls = column(cal_white_button, cal_black_button)
|
2020-03-02 15:59:59 -07:00
|
|
|
|
2020-03-02 19:45:52 -07:00
|
|
|
curdoc().add_root(row(controls, brightness_plot, time_plot, width=800))
|
2020-03-02 16:20:43 -07:00
|
|
|
curdoc().title = "TriangleBot Control Panel"
|
2020-03-02 19:45:04 -07:00
|
|
|
curdoc().add_periodic_callback(update_plots, 250)
|
2020-03-02 15:59:59 -07:00
|
|
|
|
2020-03-02 16:11:21 -07:00
|
|
|
def control_thread():
|
2020-03-02 16:32:55 -07:00
|
|
|
global brightness
|
2020-03-02 19:45:04 -07:00
|
|
|
global t
|
|
|
|
global error
|
|
|
|
sample_interval = 0.01
|
2020-03-02 16:11:21 -07:00
|
|
|
while True:
|
2020-03-02 19:45:04 -07:00
|
|
|
# TODO: replace sleep statement with something that doesn't depend on execution time of loop
|
|
|
|
time.sleep(sample_interval)
|
|
|
|
if len(time) == 0:
|
|
|
|
this_time = 0
|
|
|
|
else:
|
|
|
|
this_time = t[-1] + sample_interval
|
|
|
|
|
2020-03-02 19:29:40 -07:00
|
|
|
brightness = np.clip([get_normalized_reflectivity(c) for c in range(8)], 0, 1)
|
|
|
|
line_position = np.sum((1 - brightness) * (np.arange(8) - 3.5))/np.sum(1-brightness)
|
2020-03-02 19:45:04 -07:00
|
|
|
# TODO: implement control stuff and drive outputs
|
|
|
|
|
|
|
|
t = np.concatenate((t, this_time))
|
|
|
|
error = np.concatenate((error, line_position))
|
|
|
|
|
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 16:11:21 -07:00
|
|
|
|
|
|
|
t = threading.Thread(target=control_thread)
|
2020-03-02 16:06:58 -07:00
|
|
|
t.start()
|
2020-03-02 15:59:59 -07:00
|
|
|
|
|
|
|
|
|
|
|
# servos = ServoKit(channels=16).continuous_servo
|
|
|
|
# servos[0].throttle = 0
|
|
|
|
# servos[1].throttle = 0
|
|
|
|
# servos[2].throttle = 0
|
|
|
|
# time.sleep(1)
|
|
|
|
# servos[0].throttle = 20
|
|
|
|
# servos[1].throttle = 20
|
|
|
|
# servos[2].throttle = 20
|
|
|
|
# time.sleep(1)
|
|
|
|
# servos[0].throttle = 0
|
|
|
|
# servos[1].throttle = 0
|
|
|
|
# servos[2].throttle = 0
|
2020-03-02 15:24:40 -07:00
|
|
|
|