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

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_servo.py and rc.py py files on your CIRCUITPY lib folder.


Example Code

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

Code Breakdown

Importing Necessary Libraries


import board
import time
from rc import RCReceiver
from arcade_drive_servo import Drive

Setting Up the RC Receiver and Drive

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)

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

    rc.ensure_cycle()  # Maintains sync with our 20ms cycle every loop iteration

Conclusion

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.