Last active
October 13, 2021 04:20
-
-
Save BrianHenryIE/a48672370fd91b1c2d0ab2c766a9282f to your computer and use it in GitHub Desktop.
Donkey Car differential steering with dual DS3502 Digital Potentiometer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DS3502_DIGITAL_POTENTIOMETER(object): | |
''' | |
DS3502 Digital Potentiometer to emulate throttle driving ebike ESC. | |
https://www.adafruit.com/product/4286 | |
https://learn.adafruit.com/ds3502-i2c-potentiometer/arduino | |
https://cdn-learn.adafruit.com/downloads/pdf/ds3502-i2c-potentiometer.pdf | |
https://youtu.be/II0kozLDRyI | |
pip install adafruit-circuitpython-ds3502 | |
https://github.com/adafruit/Adafruit_CircuitPython_DS3502 | |
''' | |
def __init__(self, aaddress, busnum=1): | |
import board | |
import busio | |
import adafruit_ds3502 | |
if busnum is not None: | |
from Adafruit_GPIO import I2C | |
# replace the get_bus function with our own | |
def get_bus(): | |
return busnum | |
I2C.get_default_bus = get_bus | |
i2c_bus = busio.I2C(board.SCL, board.SDA) | |
self.ds3502 = adafruit_ds3502.DS3502(i2c_bus, address=aaddress) | |
self.speed = 0 | |
self.throttle = 0 | |
self.ds3502.wiper = 0 | |
def run(self, speed): | |
''' | |
Update the speed of the motor where 1 is full forward and | |
-1 is full backwards. | |
DAC uses range 0-127 | |
''' | |
if speed > 1 or speed < -1: | |
raise ValueError("Speed must be between 1(forward) and -1(reverse)") | |
self.speed = speed | |
self.throttle = int(speed * 127) | |
if self.throttle > 0: | |
self.ds3502.wiper = self.throttle | |
elif self.throttle <= 0: | |
self.ds3502.wiper = 0 | |
def shutdown(self): | |
self.ds3502.wiper = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
elif cfg.DRIVE_TRAIN_TYPE == "DUAL_DS3502_DIGITAL_POTENTIOMETER_STEER_THROTTLE": | |
from donkeycar.parts.actuator import TwoWheelSteeringThrottle, DS3502_DIGITAL_POTENTIOMETER | |
left_motor = DS3502_DIGITAL_POTENTIOMETER(cfg.LEFT_DS3502_DIGITAL_POTENTIOMETER_I2C_ADDRESS, cfg.LEFT_DS3502_DIGITAL_POTENTIOMETER_I2C_BUS) | |
right_motor = DS3502_DIGITAL_POTENTIOMETER(cfg.RIGHT_DS3502_DIGITAL_POTENTIOMETER_I2C_ADDRESS, cfg.RIGHT_DS3502_DIGITAL_POTENTIOMETER_I2C_BUS) | |
two_wheel_control = TwoWheelSteeringThrottle() | |
V.add(two_wheel_control, | |
inputs=['angle', 'throttle'], | |
outputs=['left_motor_speed', 'right_motor_speed']) | |
V.add(left_motor, inputs=['left_motor_speed']) | |
V.add(right_motor, inputs=['right_motor_speed']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://youtu.be/II0kozLDRyI | |
DRIVE_TRAIN_TYPE = "DUAL_DS3502_DIGITAL_POTENTIOMETER_STEER_THROTTLE" | |
LEFT_DS3502_DIGITAL_POTENTIOMETER_I2C_ADDRESS=0x28 # Default address 0x28 | |
RIGHT_DS3502_DIGITAL_POTENTIOMETER_I2C_ADDRESS=0x29 | |
LEFT_DS3502_DIGITAL_POTENTIOMETER_I2C_BUS=1 # I2C bus 1 uses Nano GPIO pins 3,5 for SDA,SCL. | |
RIGHT_DS3502_DIGITAL_POTENTIOMETER_I2C_BUS=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment