2022-04-07 17:57:38 +01:00
|
|
|
import time
|
|
|
|
from machine import Pin
|
|
|
|
from pimoroni import Analog, AnalogMux, Button
|
2022-04-26 12:52:28 +01:00
|
|
|
from motor import motor2040
|
2022-04-07 17:57:38 +01:00
|
|
|
|
|
|
|
"""
|
2022-04-26 12:52:28 +01:00
|
|
|
Shows how to initialise and read the 2 external
|
|
|
|
and 6 internal sensors of Motor 2040.
|
2022-04-07 17:57:38 +01:00
|
|
|
|
|
|
|
Press "Boot" to exit the program.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Set up the shared analog inputs
|
2022-04-26 12:52:28 +01:00
|
|
|
sen_adc = Analog(motor2040.SHARED_ADC)
|
|
|
|
vol_adc = Analog(motor2040.SHARED_ADC, motor2040.VOLTAGE_GAIN)
|
|
|
|
cur_adc = Analog(motor2040.SHARED_ADC, motor2040.CURRENT_GAIN,
|
|
|
|
motor2040.SHUNT_RESISTOR, motor2040.CURRENT_OFFSET)
|
2022-04-07 17:57:38 +01:00
|
|
|
|
|
|
|
# Set up the analog multiplexer, including the pin for controlling pull-up/pull-down
|
2022-04-26 12:52:28 +01:00
|
|
|
mux = AnalogMux(motor2040.ADC_ADDR_0, motor2040.ADC_ADDR_1, motor2040.ADC_ADDR_2,
|
|
|
|
muxed_pin=Pin(motor2040.SHARED_ADC))
|
2022-04-07 17:57:38 +01:00
|
|
|
|
|
|
|
# Set up the sensor addresses and have them pulled down by default
|
2022-04-26 12:52:28 +01:00
|
|
|
sensor_addrs = list(range(motor2040.SENSOR_1_ADDR, motor2040.SENSOR_2_ADDR + 1))
|
2022-04-07 17:57:38 +01:00
|
|
|
for addr in sensor_addrs:
|
|
|
|
mux.configure_pull(addr, Pin.PULL_DOWN)
|
|
|
|
|
2022-04-26 12:52:28 +01:00
|
|
|
# Set up the pull-up for the fault sense
|
|
|
|
mux.configure_pull(motor2040.FAULT_SENSE_ADDR, Pin.PULL_UP)
|
|
|
|
|
2022-04-07 17:57:38 +01:00
|
|
|
# Create the user button
|
2022-04-26 12:52:28 +01:00
|
|
|
user_sw = Button(motor2040.USER_SW)
|
2022-04-07 17:57:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Read sensors until the user button is pressed
|
2022-06-17 14:24:23 +01:00
|
|
|
while not user_sw.raw():
|
2022-04-07 17:57:38 +01:00
|
|
|
|
|
|
|
# Read each sensor in turn and print its voltage
|
|
|
|
for i in range(len(sensor_addrs)):
|
|
|
|
mux.select(sensor_addrs[i])
|
|
|
|
print("S", i + 1, " = ", round(sen_adc.read_voltage(), 3), sep="", end=", ")
|
|
|
|
|
|
|
|
# Read the voltage sense and print the value
|
2022-04-26 12:52:28 +01:00
|
|
|
mux.select(motor2040.VOLTAGE_SENSE_ADDR)
|
2022-04-07 17:57:38 +01:00
|
|
|
print("Voltage =", round(vol_adc.read_voltage(), 4), end=", ")
|
|
|
|
|
2022-04-26 12:52:28 +01:00
|
|
|
# Read the current sense's of each motor and print the value
|
|
|
|
for i in range(motor2040.NUM_MOTORS):
|
|
|
|
mux.select(i + motor2040.CURRENT_SENSE_A_ADDR)
|
2022-04-28 21:23:18 +01:00
|
|
|
print("C", i + 1, " = ", cur_adc.read_current(), sep="", end=", ")
|
2022-04-26 12:52:28 +01:00
|
|
|
|
|
|
|
# Read the fault sense and print the value
|
|
|
|
mux.select(motor2040.FAULT_SENSE_ADDR)
|
|
|
|
print("Fault =", not mux.read())
|
2022-04-07 17:57:38 +01:00
|
|
|
|
|
|
|
time.sleep(0.5)
|