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_servo.py and rc.py py files on your CIRCUITPY
lib folder.
import board
import time
from rc import RCReceiver
from arcade_drive_servo import Drive
rc = RCReceiver(ch1=board.D10, ch2=board.D11, ch3=None, ch4=None, ch5=board.D12, ch6=board.D13)
drive = Drive(left_pin=board.D2, right_pin=board.D3, left_stop=0.0, right_stop=0.0)
channels = [1,2,5,6]
# 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)
print("spin", spin, "throttle", throttle) # move our motors arcade drive style
rc.ensure_cycle() # Maintains sync with our 20ms cycle every loop iteration
import board
import time
from rc import RCReceiver
from arcade_drive_servo 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.D4, ch2=board.D5, ch3=None, ch4=None, ch5=board.D6, ch6=board.D7)
drive = Drive(left_pin=board.D2, right_pin=board.D3, left_stop=0.0, right_stop=0.0)
code.py
file.left_stop
and right_stop
values are set to zero. You can calibrate these values if you find they stop value of 0.0
is not actually causing your servo to stop. [servo stop calibration tutorial]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
rc.ensure_cycle() # 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 servo motors. It’s crucial to ensure that the pins and channels are correctly configured to match your hardware setup.