Control servo motors
The digital outputs DO2 and DO3 can control positional servo motors that accept a pulse-width signal. For instance, it can be used to move a lick spout in and out of reach, or open and close a door. Refer to the connections article to set up the servo motor on DO2, which we will use for the rest of these examples.
This article covers how to configure the servo, enable and disable the pulse-width signal, and adjust the position in Bonsai.
The complete workflow is shown below. Copy and paste it into Bonsai to get started immediately, or build each section by following the step-by-step instructions in the following sections.
Configure the servo
To configure the servo, set the period and pulse-width registers for the digital output port (e.g. ServoMotor2Period and ServoMotor2Pulse for DO2) in microseconds. The period determines the servo update rate (i.e. how often the servo receives a position command), which can affect how smoothly and reliably it responds. The pulse width determines the servo's target angle.
- Insert a
KeyDownoperator and set theFilterproperty toA. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectServoMotor2PeriodPayload.ServoMotor2Period- Set the period of the servo control pulse in µs (e.g.20000for a 50 Hz servo update rate, typical of positional servos).
- Insert a
MulticastSubjectoperator namedBehavior Commands.
In a separate branch:
- Insert a
KeyDownoperator and set theFilterproperty toS. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectServoMotor2PulsePayload.ServoMotor2Pulse- Set the pulse width in µs (e.g.1500for the center position of a positional servo, the range is typically 1000–2000 µs).
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow, press A to set the servo update rate and S to set the servo starting angle. The two commands are bound to separate keys, so you can reset the angle later without reconfiguring the period. Check your servo's datasheet for its period and pulse width range if you are unsure about which values to use.
Enable and disable servos
Use the EnableServos and DisableServos registers to turn on and off the servo:
- Insert a
KeyDownoperator and set theFilterproperty toD. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectEnableServosPayload.EnableServos- Select the servo output to activate (e.g.ServoOutput2).
- Insert a
MulticastSubjectoperator namedBehavior Commands.
In a separate branch:
- Insert a
KeyDownoperator and set theFilterproperty toF. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectDisableServosPayload.DisableServos- Select the servo output to deactivate (e.g.ServoOutput2).
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow, press D to enable the servo and F to stop. The servo moves to the starting position which was configured previously.
Adjust the position
While the servo is enabled, writing a new pulse width moves it immediately:
- Insert a
KeyDownoperator and set theFilterproperty toG. - Insert a
CreateMessageoperator and configure the following properties:Payload- SelectServoMotor2PulsePayload.ServoMotor2Pulse- Set a new pulse width in µs (e.g.2000).
- Insert a
MulticastSubjectoperator namedBehavior Commands.
Run the workflow, then press G to move the servo to a new angle. Press S to move it back to the starting angle.
Documentation for release v3.4.0