This is a Python 3 library implementing the LewanSoul Servo Bus Communication Protocol, which is used by the following products:
This library supports all the commands necessary to control the servo positions, as well as commands to set parameters and read servo states such as position, temperature, and supply voltage.
from lewansoul_servo_bus import ServoBus
servo_bus = ServoBus('/dev/ttyUSB0')
# Move servo with ID 1 to 90 degrees in 1.0 seconds
servo_bus.move_time_write(1, 90, 1.0)
# Move servo with ID 2 to 180 degrees in 2.0 seconds
servo_2 = servo_bus.get_servo(2)
servo_2.move_time_write(180, 2.0)
To talk to the servos, you will need a way to interface the computer with the servo bus. Most serial interfaces are full-duplex, but the servo bus is half-duplex. The following circuit provides a simple way to convert full-duplex signals to half-duplex:
- If you want to isolate the computer power system from the servo power system, check out this schematic.
- Notice that this configuration will cause all data sent by the computer to be "echoed" back to the computer.
ServoBus
discards echoed data by default, but that behavior can be disabled withServoBus(discard_echo=False)
. - This library is for direct communication with the bus servos; it is not intended for communication with the servos through e.g. the Hiwonder Serial Bus Servo Controller. This allows for faster communication (115200 baud vs 9600), access to all the protocol functions, and less hardware.
- You may want to create a Python 3 virtualenv at the root of this repository.
- Run:
pip install -r requirements.txt
cd src/python
python3 lewansoul_servo_bus.py
- Of course, you can also import
lewansoul_servo_bus.py
in your own project and use theServoBus
class within to control your servos. Just make sure thesrc/python
directory is in yourPYTHONPATH
.
from lewansoul_servo_bus import ServoBus
def main():
with ServoBus('/dev/ttyUSB0') as servo_bus:
pan_servo = servo_bus.get_servo(1)
tilt_servo = servo_bus.get_servo(2)
# Do things with pan/tilt_servo...