Robotics-II-Circuit-Python

Circuit Python tutorials in Robotics II

View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python

Table of Contents

Arcade Drive (DC Motors)

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.

tank

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.

arcade


Rover Setup

Your rover needs the following hardware:

Video Tutorial


Text Tutorial

Required Library

Before moving forward, ensure you have the following arcade_drive_dc.py and rc.py py files on your CIRCUITPY lib folder.


Example Code

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

Code Breakdown

Importing Necessary Libraries


import board
import time
from rc import RCReceiver
from arcade_drive_dc import Drive

Setting Up the RC Receiver and 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 Loop

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

Conclusion

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.