Servos
Servos are similar to motors, except their position is fixed rather than continuously rotating. This makes them ideal for mechanisms such as arms, grabbers, or anything that needs clearly defined positions.
Servo Overview
Unlike motors, servos are typically moved to specific positions rather than being driven forwards or backwards indefinitely. Once commanded, the servo moves to the requested position and holds it.
Servo Position
Servo positions are expressed as a percentage. Valid values range from
-100 to 100, where 0 represents the
neutral (default) position.
Negative values move the servo in one direction, while positive values move it in the opposite direction.
Hardware Setup
Servos are plugged into the Servo ports on the BrainBox. The number printed next to each port corresponds directly to the index used in your code.
Python Control
Servos are controlled using the servos property of the
Robot object. Before setting a servo position, its mode must
be set to PWM_SERVO.
R.servos[0].mode = robot.PWM_SERVO
R.servos[0] = 50
This sets servo 0 to the 50% position.
To control a different servo, replace servos[0] with the
appropriate index (for example servos[3]).
Worked Example
The following example demonstrates controlling two servos, moving them to different positions and then returning them to their default state.
import robot
import time
R = robot.Robot()
R.servos[0].mode = robot.PWM_SERVO
R.servos[1].mode = robot.PWM_SERVO
# set servo 0 to the 50% position
R.servos[0] = 50
time.sleep(1)
# set servo 1 to the -100% position
R.servos[1] = -100
time.sleep(1)
# return both servos to their default positions
R.servos[0] = 0
R.servos[1] = 0
Tips
Add delays using time.sleep() to give servos enough time to
physically reach their new positions before issuing the next command.
Remember that servo indexes start at 0, matching the numbering
on the BrainBox.