This repository contains examples for how to program LX-16A bus servomotors on Raspberry Pi.
It also contains LX16A class taken from PyLX-16A package. This class is free from any Qt or other unnecessary dependencies. These examples can run on Raspberry Pi OS, any other Linux, or macOS. Perhaps, should work on Windows, but not tested on it.
- Compatible with 3-pin LX-16A bus servo motors, and possibly with other servos of this family
- No need to install Qt, only pyserial
- Contains examples with smooth motion (easing in and out) in servo and motor modes: see
osc.py,smoothmotor.py,smoothservo.py.
- Raspberry Pi 4 or 5 [with GPIO]
- BusLinker, also known as the TTL/USB Debug Board for Serial Bus Servo
- External power for the motor
Put the files from this repository into your project directory on Raspberry Pi.
Install pyserial package (for Debian 12 / Raspberry Pi OS):
sudo apt install python3-serialor (if you are using virtual environments):
pip3 install pyserialInitialize the BusLinker:
LX16A.initialize(port: str, timeout: float = 0.02) -> None
On Raspberry Pi, the port is typically "/dev/ttyUSB0".
Create servo instances:
motor_obj = LX16A(id_: int, disable_torque: bool = False) -> None
Set the ID of the servo:
motor_obj.set_id(id_: int) -> None
Rotate to the given angle (for LX-16A from 0 to 240):
motor_obj.move(angle: float, time: int = 0, relative: bool = False, wait: bool = False) -> None
Enable torque so that it does not relax (prevents moving due to gravity or other forces):
motor_obj.enable_torque() -> None
Disable torque so that you can rotate it, for instance, with your hands:
motor_obj.disable_torque() -> None
Read the physical angle at which the servo is currently positioned:
motor_obj.get_physical_angle() -> float
Read the last commanded angle (not necessarily the same as physical angle):
motor_obj.get_commanded_angle() -> float
Set the servo to the motor mode (continuous rotation without positioning):
motor_obj.motor_mode(speed: int) -> None
Set the servo to the servo mode (positioning, no continuous rotation):
motor_obj.servo_mode() -> None
Turn on internal servo LED:
motor_obj.led_power_on() -> None
Turn off internal servo LED:
motor_obj.led_power_off() -> None
Check if the servo is in the motor mode:
motor_obj.is_motor_mode(poll_hardware: bool = False) -> bool
Check if torque is enabled:
motor_obj.is_torque_enabled(poll_hardware: bool = False) -> bool
Check if internal LED is on:
motor_obj.is_led_power_on(poll_hardware: bool = False) -> bool
Read the current temperature at the servo in °C:
motor_obj.get_temp(self) -> int
Read supplied voltage in millivolts:
motor_obj.get_vin(self) -> int
See more examples and get smooth motion classes in other files in this repository.
import time
from lx16a import LX16A
# Setup:
try:
LX16A.initialize("/dev/ttyUSB0")
except Exception as e:
print("Initialisation ERROR")
print(e)
quit()
motor1 = LX16A(1)
motor2 = LX16A(2)
motor1.enable_torque()
motor1.servo_mode()
motor2.enable_torque()
motor2.servo_mode()
# Main loop:
try:
while True:
angle = motor1.get_physical_angle()
time.sleep(0.05)
print(f"angle: {angle:.2f}")
motor1.move(240)
time.sleep(1)
motor2.move(240)
time.sleep(2)
angle = motor1.get_physical_angle()
time.sleep(0.05)
print(f"angle: {angle:.2f}")
motor1.move(0)
time.sleep(1)
motor2.move(0)
time.sleep(2)
except KeyboardInterrupt:
print("Program stopped.")If you find this useful, consider supporting the developer.