Python bindings for piservod - software PWM daemon for controlling servos on Raspberry Pi
Project description
piservod-python
Python bindings for piservod - a software PWM daemon for controlling servos on Raspberry Pi.
NOTE: Requires piservod to be installed and running on the system!
Installation
Install via pip:
pip install piservod
Prerequisites
First, install and start the piservod daemon:
# Clone and build piservod
git clone https://github.com/stylesuxx/piservod.git
cd piservod
make
sudo make install
# Start the daemon
sudo piservod
Usage
Basic Example
from piservod import Servo
# Connect to daemon (shared connection for all servos)
Servo.connect()
# Create and control a servo
servo = Servo(channel=0, gpio=5)
servo.enable()
servo.set_pulse(1500) # Center position
servo.disable()
# Disconnect when done
Servo.disconnect()
Multiple Servos
from piservod import Servo
Servo.connect()
# Create multiple servos sharing the same connection
servo1 = Servo(channel=0, gpio=5)
servo2 = Servo(channel=1, gpio=6)
servo3 = Servo(channel=2, gpio=7)
# Control them independently
servo1.enable()
servo2.enable()
servo3.enable()
servo1.set_pulse(1000)
servo2.set_pulse(1500)
servo3.set_pulse(2000)
Servo.disconnect()
Using Context Managers
from piservod import Servo
Servo.connect()
# Servo automatically enabled on enter, disabled on exit
with Servo(channel=0, gpio=17) as servo:
servo.set_pulse(1500)
servo.center() # Move to center position
Servo.disconnect()
Custom Pulse Ranges
from piservod import Servo
Servo.connect()
# Some servos use different pulse ranges
servo = Servo(channel=0, gpio=17, min_pulse=600, max_pulse=2400)
servo.enable()
servo.center() # Uses the custom range
Servo.disconnect()
Low-Level API
For more control, use the PiServoD class directly:
from piservod import PiServoD
daemon = PiServoD()
daemon.connect()
# Setup channel
daemon.setup(channel=0, gpio=5)
daemon.set_range(channel=0, min_pulse=1000, max_pulse=2000)
# Control servo
daemon.enable(channel=0)
daemon.set_pulse(channel=0, pulse=1500)
# Query state
pulse = daemon.get_pulse(channel=0)
state = daemon.get_state(channel=0)
print(f"Current pulse: {pulse}μs")
print(f"Enabled: {state['enabled']}")
daemon.disconnect()
Error Handling
from piservod import Servo, PiServoDError, PulseOutOfRangeError
try:
Servo.connect()
servo = Servo(channel=0, gpio=5)
servo.enable()
servo.set_pulse(3000) # Out of range
except PulseOutOfRangeError as e:
print(f"Pulse out of range: {e}")
except PiServoDError as e:
print(f"Error: {e}")
finally:
Servo.disconnect()
API Reference
Servo Class
Class Methods
connect(socket_path='/tmp/piservod.sock', timeout=1.0)- Connect to daemondisconnect()- Disconnect from daemonis_connected()- Check connection status
Instance Methods
enable()- Enable PWM outputdisable()- Disable PWM outputset_pulse(pulse)- Set pulse width in microsecondsset_range(min_pulse, max_pulse)- Set pulse rangeget_pulse()- Get current pulse widthget_range()- Get pulse range as tupleget_state()- Get servo state (gpio, enabled)is_enabled()- Check if servo is enabledcenter()- Move to center position
PiServoD Class
Lower-level daemon interface with the same methods as Servo, but requires explicit channel parameter:
connect()/disconnect()/is_connected()setup(channel, gpio)- Setup a channelenable(channel)/disable(channel)set_pulse(channel, pulse)/get_pulse(channel)set_range(channel, min_pulse, max_pulse)/get_range(channel)get_state(channel)
Exception Types
PiServoDError- Base exceptionNotConnectedError- Not connected to daemonInvalidChannelError- Invalid channel number (0-7)InvalidGPIOError- Invalid GPIO pinChannelNotConfiguredError- Channel not setupPulseOutOfRangeError- Pulse value out of rangeInvalidRangeError- Invalid range values
Development
Setup
Clone, setup venv and install dev dependencies:
git clone https://github.com/stylesuxx/piservod-python.git
cd piservod-python
python -m venv ./.venv
source .venv/bin/activate
pip install -e ".[dev]"
Testing
pytest
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
Releases
Pushing a new tag will create a new release on PyPI:
git tag v0.1.0
git push origin v0.1.0
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file piservod-0.1.2.tar.gz.
File metadata
- Download URL: piservod-0.1.2.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
527b682ca605def8a70408af0b9fd5ed2f4e2a9d47aa00cd56f77dd6a877510b
|
|
| MD5 |
f3d4734622a473d028dafe4a4d9bd33f
|
|
| BLAKE2b-256 |
33915bee2c121f8cb6a6804682b5258d19d0279780b0c21b02cccf5095a4466b
|
Provenance
The following attestation bundles were made for piservod-0.1.2.tar.gz:
Publisher:
publish.yml on stylesuxx/piservod-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
piservod-0.1.2.tar.gz -
Subject digest:
527b682ca605def8a70408af0b9fd5ed2f4e2a9d47aa00cd56f77dd6a877510b - Sigstore transparency entry: 582648253
- Sigstore integration time:
-
Permalink:
stylesuxx/piservod-python@3166aea7da9c582b7a3c33ed707cac6071066f20 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/stylesuxx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3166aea7da9c582b7a3c33ed707cac6071066f20 -
Trigger Event:
push
-
Statement type:
File details
Details for the file piservod-0.1.2-py3-none-any.whl.
File metadata
- Download URL: piservod-0.1.2-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
54c793be16a57d826b471117f5d46c8d1dd4c90d44471b6298166992b71d2562
|
|
| MD5 |
7403d5576f6c9f941eeae905702748cb
|
|
| BLAKE2b-256 |
a573f4108d546ee15643fcfeba88cb44860416400186bf0369ac303de4324211
|
Provenance
The following attestation bundles were made for piservod-0.1.2-py3-none-any.whl:
Publisher:
publish.yml on stylesuxx/piservod-python
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
piservod-0.1.2-py3-none-any.whl -
Subject digest:
54c793be16a57d826b471117f5d46c8d1dd4c90d44471b6298166992b71d2562 - Sigstore transparency entry: 582648255
- Sigstore integration time:
-
Permalink:
stylesuxx/piservod-python@3166aea7da9c582b7a3c33ed707cac6071066f20 -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/stylesuxx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3166aea7da9c582b7a3c33ed707cac6071066f20 -
Trigger Event:
push
-
Statement type: