Circuit Python tutorials in Robotics II
View the Project on GitHub MrPrattASH/Robotics-II-Circuit-Python
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.
A servo has 3 wires:
all timers already in use for this pin
Serial output. If this happens, try a new pin.You’ll find this Servo-Shroud to x3 Male jumper cable helpful when connecting your servo to your breadboard.
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)
import time
import board
import pwmio
from adafruit_motor import servo
These imports bring in the necessary libraries:
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:
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
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:
0.1
is stop-0.18
is stop0.3
and -0.3
may be stop.Here is a handy code you can use to calibrate your stop position of a servo. Each servo will need to be individually calibrated.
throttle.value
causes your servo to stop.# 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)