Circuit Python tutorials in Robotics II
View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python
There are two classic ways of controlling a 2 or 4 wheeled rover that has a tank drive drivetrain.
From xiaoxiae on Github:
Tank drive is a method of controlling the motors of a tank drive drivetrain using two axes of a controller, where each of the axes operate motors on one side of the robot.
In contrast, Arcade drive is a method of controlling the motors of a tank drive drivetrain using two axes of a controller, where one of the axes controls the speed (throttle) of the robot, and the other the steering (spin) of the robot.
Your rover needs the following hardware:
Before moving forward, ensure you have the following arcade_drive_dc.py and rc.py py files on your CIRCUITPY
lib folder.
import time
import board
from rc import RCReceiver
from arcade_drive_dc import Drive
rc = RCReceiver(ch1=board.D10, ch2=board.D11, ch3=None, ch4=None, ch5=board.D12, ch6=board.D13)
drive = Drive(left=board.D0, right=board.D1)
# Main code
while True:
# Read joystick channels
spin = rc.read_channel(1) # spin
throttle = rc.read_channel(2) # throttle
if spin is not None and throttle is not None: # must not be None to do something with the output
drive.drive(spin,throttle) # move our motors arcade drive style
time.sleep(0.02) # keep timer in sync with flysky receiver
import board
import time
from rc import RCReceiver
from arcade_drive_dc import Drive
sleep
.RCReceiver
is used to handle the input from the RC receiver, while Drive
is used to control the driving mechanism of the robot.rc = RCReceiver(ch1=board.D10, ch2=board.D11, ch3=None, ch4=None, ch5=board.D12, ch6=board.D13)
drive = Drive(left=board.D0, right=board.D1)
code.py
file.The code enters an infinite loop, constantly checking for input from the RC receiver and updating the drive system accordingly.
while True:
# Read joystick channels
spin = rc.read_channel(1) # spin
throttle = rc.read_channel(2) # throttle
if spin is not None and throttle is not None: # Ensures valid input
drive.drive(spin, throttle) # Moves the motors based on input
print("spin", spin, "throttle", throttle) # Displays the current input values
time.sleep(0.02) # Maintains sync with our 20ms cycle every loop iteration
spin
and channel 2 for throttle
to get joystick input.None
(meaning valid and connected inputs).drive
function is called to control the motors according to the joystick’s direction and throttle.spin
and throttle
to the console for debugging purposes.This code effectively allows for controlling a robot using an RC receiver and DC motors. It’s crucial to ensure that the pins and channels are correctly configured to match your hardware setup.