Robotics-II-Circuit-Python

Circuit Python tutorials in Robotics II

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

Table of Contents

Continuous Rotational Servos

Continous rotational servos, as their name implies, are servo motors that constantly spin in circles. They can spin in either direction, depending on what direction you set their throttle to.

Video Tutorial


Text Tutorial

Wiring

A servo has 3 wires:

You’ll find this Servo-Shroud to x3 Male jumper cable helpful when connecting your servo to your breadboard. jumpers

Programming

python code here

# SPDX-FileCopyrightText: 2019 Anne Barela for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""CircuitPython Essentials Servo continuous rotation servo example"""
import time
import board
import pwmio
from adafruit_motor import servo

# ----------------- INIT DEVICES -------------------------

# create a PWMOut object on Pin D0.
pwm = pwmio.PWMOut(board.D0, frequency=50)

# Create a servo object, my_servo.
my_servo = servo.ContinuousServo(pwm)

while True:
    print("forward")
    my_servo.throttle = 1.0
    time.sleep(2.0)
    print("stop")
    my_servo.throttle = 0.0
    time.sleep(2.0)
    print("reverse")
    my_servo.throttle = -1.0
    time.sleep(2.0)
    print("stop")
    my_servo.throttle = 0.0
    time.sleep(4.0)

Examining The code

Import Statements

import time
import board
import pwmio
from adafruit_motor import servo

These imports bring in the necessary libraries:

INITIALIZE Devices

pwm = pwmio.PWMOut(board.D0, duty_cycle=2 ** 15, frequency=50) Initializes our servo on Pin Digital 0, with a duty cycle of 2 ** 15, and a frequency of 50.

my_servo = servo.ContinuousServo(pwm) creates a servo object, taking special methods from our servo library for controlling our servo. Note that we initialize a continuous servo.

While True Loop:

while True:
    print("forward")
    my_servo.throttle = 1.0
    time.sleep(2.0)
    print("stop")
    my_servo.throttle = 0.0
    time.sleep(2.0)
    print("reverse")
    my_servo.throttle = -1.0
    time.sleep(2.0)
    print("stop")
    my_servo.throttle = 0.0
    time.sleep(4.0)

This loop controls the servo, printing the current action and setting the throttle:

You can set the throttle to any value between -1.0 and 1.0:

-1.0: Max Reverse Speed
-0.5: 50% Reverse Speed
0.0: Full Stop
0.1: 10% Forward Speed
1.0: Max Forward Speed

Calibrating Your “Stop” Throttle

Continous servos can be tricky, especially servos on the cheaper side. Generally, Servo motors <30 EUR will need to have calibrated stop positions. Likely you noticed that 0.0 throttle didn’t actually stop your servo. Let’s change that through calibration. It’s possible that:

Here is a handy code you can use to calibrate your stop position of a servo. Each servo will need to be individually calibrated.

# SPDX-FileCopyrightText: 2024 Brogan Pratt
#
# SPDX-License-Identifier: MIT

"""ervo continuous rotation calibration example"""
import time
import board
import pwmio
from adafruit_motor import servo

# ----------------- INIT DEVICES -------------------------

# create a PWMOut object on Pin D0.
pwm = pwmio.PWMOut(board.D0, frequency=50)

# Create a servo object, my_servo.
my_servo = servo.ContinuousServo(pwm)

# watch your serial port to see what value causes the servo to stop. 
# note this stop value for each specific motor
while True:
    
    #don't worry about understanding this code yet,
    #simply watch your serial output :) 
    print("starting calibration test")
    i = -3.0
    while i <= 3.0:
        print("Servo Throttle: ", str(i))
        my_servo.throttle = i
        time.sleep(2)
        i+= 1
    print("Calibration completed\nRestarting...")
    time.sleep(2)