Last active
September 26, 2021 14:17
-
-
Save BrianHenryIE/f411228cf27064f482d4c7bb64f1b696 to your computer and use it in GitHub Desktop.
Donkey Car differential steering with dual MCP4725 DAC
This file contains hidden or 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 MCP4725_DAC(object): | |
''' | |
MCP4725 Digital to Analog Convertor to emulate throttle driving ebike ESC. | |
''' | |
def __init__(self, aaddress, busnum=1): | |
import board | |
import busio | |
import Adafruit_MCP4725 | |
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 = I2C | |
self.dac = Adafruit_MCP4725.MCP4725(address=aaddress, i2c=i2c) | |
self.speed = 0 | |
self.throttle = 0 | |
self.dac.set_voltage(0) | |
def run(self, speed): | |
''' | |
Update the speed of the motor where 1 is full forward and | |
-1 is full backwards. | |
DAC uses max 12-bit number: 0xFFF = 4095 | |
''' | |
if speed > 1 or speed < -1: | |
raise ValueError("Speed must be between 1(forward) and -1(reverse)") | |
self.speed = speed | |
self.throttle = int(speed * 4095) | |
if self.throttle > 0: | |
self.dac.set_voltage(self.throttle) | |
elif self.throttle <= 0: | |
self.dac.set_voltage(0) | |
def shutdown(self): | |
self.dac.set_voltage(0) |
This file contains hidden or 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_MCP4725_DAC_STEER_THROTTLE": | |
from donkeycar.parts.actuator import TwoWheelSteeringThrottle, MCP4725_DAC | |
left_motor = MCP4725_DAC(cfg.LEFT_MCP4725_DAC_I2C_ADDRESS, cfg.LEFT_MCP4725_DAC_I2C_BUS) | |
right_motor = MCP4725_DAC(cfg.RIGHT_MCP4725_DAC_I2C_ADDRESS, cfg.RIGHT_MCP4725_DAC_I2C_BUS) | |
two_wheel_control = TwoWheelSteeringThrottle() | |
V.add(two_wheel_control, | |
inputs=['angle', 'throttle'], | |
outputs=['right_motor_speed', 'left_motor_speed']) | |
V.add(left_motor, inputs=['left_motor_speed']) | |
V.add(right_motor, inputs=['right_motor_speed']) |
This file contains hidden or 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
# sudo pip3 install adafruit-circuitpython-mcp4725 | |
# pip3 install adafruit-blinka | |
# pip install adafruit_mcp4725 | |
# https://www.youtube.com/watch?v=4rC7EPV7wis | |
DRIVE_TRAIN_TYPE = "DUAL_MCP4725_DAC_STEER_THROTTLE" | |
LEFT_MCP4725_DAC_I2C_ADDRESS=0x60 # Sparkfun MCP4725 default address 0x60; Adafruit default is 0x62. | |
RIGHT_MCP4725_DAC_I2C_ADDRESS=0x61 | |
LEFT_MCP4725_DAC_I2C_BUS=1 # I2C bus 1 uses Nano GPIO pins 3,5 for SDA,SCL. | |
RIGHT_MCP4725_DAC_I2C_BUS=1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment