Overview
The Servo Adapter is a USB to Servo bridge that uses off-the-shelf and inexpensive boards such as the
Raspberry Pi Pico, and control it using the python package servo_adapter
.
For example, the diagram below shows the wiring to control a servo using the servo output SERVO0. |
Examples
Random servo positon every 1 second.
1import time
2from random import randrange
3from servo_adaptr import ServoAdapter
4
5# Serial port name. Adapt to your system.
6port = "/dev/tty.usbmodem1101"
7
8# Connect to adapter and enable PWM servo out 0.
9adapter = ServoAdapter(port=port)
10adapter.set_servo_state(0, True)
11
12while True:
13 # Random pulse width in the range 1000us to 2000us.
14 pw_us = 1000 + randrange(1000 + 1)
15 adapter.set_servo_pulse_width(0, pw_us)
16 time.sleep(1.0)
Reading and writing auxiliary I/O pins:
1import time
2from servo_adapter import ServoAdapter, AuxPinMode
3
4# Customize for your system.
5port = "COM18"
6aux_out_pin = 0
7aux_in_pin = 1
8
9# Configure the two aux pins.
10adapter = ServoAdapter(port)
11adapter.set_aux_pin_mode(aux_out_pin, AuxPinMode.OUTPUT)
12adapter.set_aux_pin_mode(aux_in_pin, AuxPinMode.INPUT_PULLUP)
13
14# Access the two pins.
15i = 0
16while True:
17 i += 1
18 adapter.write_aux_pin(aux_out_pin, i % 2) # Generates a square wave
19 in_value = adapter.read_aux_pin(aux_in_pin)
20 print(f"{i:03d}: Input pin value: {in_value}", flush=True)
21 time.sleep(0.5)
Supported Boards
The able below lists the currently supported boards. To make your own Servo Adapter, get one of these boards, and flash it according to the manufacturer’s instructions with the corresponding Servo Adapter firmware from https://github.com/zapta/servo_adapter/tree/main/firmware/release.
- Example:
For the Raspberry Pico and similar RP2040 boards, flash it by connecting the board to your computer while holding the BOOTSEL button. Once your computer recognized the board as a new hard driver, release the button and copy the firmware file to that hard drive.
Board |
Servo pins |
Aux pins |
---|---|---|
GP 8-15 |
GP 0-7 |
|
GP 8-15 |
GP 0-7 |
|
GP 8-15 |
GP 0-7 |
|
GP 8-15 |
GP 0-7 |
Raspberry PI Pico Pinout
The diagram below shows the pinout for the popular Raspberry Pi Pico. For the other supported board, consult the table above.
API Installation
The Python API package is available from PyPi at https://pypi.org/project/servo-adapter and can be installed on your computer using pip:
pip install servo_adapter
- Note:
The Servo Adapter boards appear on the computer as a standard CDC serial port and thus do not require driver installation.
API Reference
The servo_adapter
package provides the API to access Servo Adapter boards. To access an Servo Adapter,
create an object of the class ServoAdapter, and use the methods it provides.
- class servo_adapter.AuxPinMode(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)
Auxilary pin modes.
- class servo_adapter.ServoAdapter(port: str)
Connects to the Servo Adapter at the specified serial port and asserts that the adapter responses as expcted.
- Parameters:
port (str) – The serial port of the Servo Adapter. Servo Adapters appear on the local computer as a standard serial port
- set_servo_pulse_width(pwm_pin: int, pulse_width_us: int) bool
Sets a PWM pin pulse width. This doesn not change the pin state on or off.
- Parameters:
pwm_pin – The PWM pin index in the range [0, 7].
pulse_width_us – The Servo pulse width in us. Must be in the range [500, 2500]. Note that if the PWM pin is off, the pulses are not generated until it’s set to on state.
- Returns:
True if OK, False otherwise.
- Return type:
bool
- set_servo_state(pwm_pin: int, state: bool) bool
Sets a PWM pin on or off.
- Parameters:
pwm_pin – The PWM pin index in the range [0, 7].
state – Indicates if to turn the PWM state on (True) of off (False)
- Returns:
True if OK, False otherwise.
- Return type:
bool
- set_aux_pin_mode(pin: int, pin_mode: AuxPinMode) bool
Sets the mode of an auxilary pin.
- Parameters:
pin (int) – The aux pin index, should be in [0, 7].
pin_mode (AuxPinMode) – The new pin mode.
- Returns:
True if OK, False otherwise.
- Return type:
bool
- read_aux_pins() int | None
Reads the auxilary pins.
- Returns:
The pins value as a 8 bit in value or None if an error.
- Return type:
int | None
- write_aux_pins(values, mask=255) bool
Writes the aux pins.
- Parameters:
values (int) – An 8 bits integer with the bit values to write. In the range [0, 255].
mask (int) – An 8 bits int with mask that indicates which auxilary pins should be written. If the corresponding bits is 1 than the pin is updated otherwise it’s left as is.
- Returns:
True if OK, False otherwise.
- Return type:
bool
- read_aux_pin(aux_pin_index: int) bool | None
Read a single aux pin.
- Parameters:
aux_pin_index (int) – An aux pin index in the range [0, 7]
- Returns:
The boolean value of the pin or None if error.
- Return type:
bool | None
- write_aux_pin(aux_pin_index: int, value: bool | int) bool
Writes a single aux pin.
- Parameters:
aux_pin_index (int) – An aux pin index in the range [0, 7]
value (bool | int) – The value to write.
- Returns:
True if OK, False otherwise.
- Return type:
bool
- test_connection_to_adapter(max_tries: int = 3) bool
Tests connection to the Servo Adapter.
The method tests if the Servo adapter exists and is responding. It is provided for diagnostic purposes and is not needed in typical applications.
- Parameters:
max_tries (int) – Max number of attempts. The default should be good for most case.
- Returns:
True if connection is OK, false otherwise.
- Return type:
bool
The Wire Protocol
The servo_adapter
package communicates with the Servo Adapter board by sending commands
and receiving command responses on a serial connection. The commands and responses are made of a plain sequence of
‘binary’ bytes with no special encoding such as end of line or byte stuffing. For
an updated specification of the commands and their wire representation see the
firmware protocol implementation.
Firmware Development
The firmware is written in C++ and is developed as a platformio project under Visual Studio Code. The following sections summarize the key aspect of the firmware development.
Project Structure
The platformio project resides in the firmware/platformio directory of the Servo Adapter repository https://github.com/zapta/servo_adapter, the project configuration is in the platformio.ini file and the source code is in the src directory.
Setting up the environment
Install Microsoft’s Visual Studio Code (‘VSC’)
In VSC, add the extension ‘platformio’
Clone the Servo Adapter github repository on your computer.
Use VSC’s ‘file | open-folder’, to open the ‘platformio’ directory in your local repository.
After platformio will complete installing the necessary tools, click on the ‘build’ icon in the status bar to verify that the project builds correctly.
Testing a new firmware version
Make the changes in the source code.
Connect a compatible board to your computer.
Select in the status bar the board target that matches your board.
Use the ‘upload’ button in the status bar to build and upload the binary to the board.
Generating new binaries
Run the python script ‘build_env.py’ and it will build binaries for all the targets and will copy them to release directory.
Adding a new board
Board definitions resides in platformio.ini and in src/board.cpp and the amount of refactoring needed to add a board depends how close it is to the existing boards. Adding a typical board includes adding:
A new target to platformio.ini
A new section in src/boards.cpp.
A new row to the documentation’s list.
A new binary to the release.
Contact
Bug reports and contributions are welcome. You can contact the team and fellow users at the gibhub repository at https://github.com/zapta/servo_adapter.