ECEN5458/Software/python/hwtest.py

83 lines
2.5 KiB
Python
Raw Normal View History

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-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
from bokeh.models import ColumnDataSource, Slider, TextInput
from bokeh.plotting import figure
2020-02-26 10:47:21 -07:00
if __name__ == "__main__":
2020-03-02 13:54:49 -07:00
mux_io = [None] * 4
2020-03-02 13:48:35 -07:00
mux_io[0] = digitalio.DigitalInOut(board.D17)
mux_io[1] = digitalio.DigitalInOut(board.D27)
mux_io[2] = digitalio.DigitalInOut(board.D22)
mux_io[3] = digitalio.DigitalInOut(board.D23)
2020-03-02 13:57:55 -07:00
for ii, io in enumerate(mux_io):
2020-03-02 13:48:35 -07:00
io.switch_to_output()
2020-03-02 11:39:07 -07:00
2020-03-02 13:40:42 -07:00
i2c = busio.I2C(board.SCL, board.SDA)
adc = ADS.ADS1015(i2c)
2020-03-02 11:39:07 -07:00
adc_mux = AnalogIn(adc, ADS.P0)
def get_reflectivity(chan):
chan = int(chan)
2020-03-02 13:54:49 -07:00
global mux_io
2020-03-02 11:43:30 -07:00
global adc_mux
2020-03-02 13:44:55 -07:00
mux = 1-np.array(list(f"{chan:04b}"), dtype=int)
2020-03-02 13:57:55 -07:00
for ii, io in enumerate(mux_io):
2020-03-02 13:54:49 -07:00
io.value = mux[ii]
2020-03-02 11:43:30 -07:00
return adc_mux.voltage
2020-03-02 14:07:21 -07:00
input("White calibration, press ENTER to continue...")
white_cal = [get_reflectivity(c) for c in range(8)]
2020-03-02 14:09:16 -07:00
2020-03-02 14:07:21 -07:00
input("Black calibration, press ENTER to continue...")
black_cal = [get_reflectivity(c) for c in range(8)]
def get_normalized_reflectivity(chan):
global white_cal
global black_cal
2020-03-02 14:10:05 -07:00
return (get_reflectivity(chan) - black_cal[chan]) / (white_cal[chan] - black_cal[chan])
2020-03-02 11:39:07 -07:00
2020-03-02 14:45:16 -07:00
brightness_idx = np.arange(8)
brightness = [get_normalized_reflectivity(c) for c in range(8)]
plt_source = ColumnDataSource(data=dict(x=brightness_idx, y=brightness))
# Set up plot
plot = figure(plot_height=400, plot_width=400, title="my sine wave",
tools="crosshair,pan,reset,save,wheel_zoom",
x_range=[0, 4*np.pi], y_range=[-2.5, 2.5])
plot.line('x', 'y', source=plt_source, line_width=3, line_alpha=0.6)
def update_data(attrname, old, new):
brightness = [get_normalized_reflectivity(c) for c in range(8)]
plt_source.data = dict(x=brightness_idx, y=brightness)
curdoc().add_root(plot)
curdoc().title = "test"
2020-03-02 11:39:07 -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-02-26 10:47:21 -07:00