-
Notifications
You must be signed in to change notification settings - Fork 6
v1.2.0 #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
v1.2.0 #5
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
94242e4
Add comprehensive error classes and improve ST3215
alessiodam c61a012
only run release workflow on main branch
alessiodam 11c5d4c
Fixed _sync_read.
2bffa43
Fixed signed-word issues.
3f96304
add support for passing a ser object (for networked purposes)
alessiodam 99d2c71
Fixed errors pointed out by Copilot.
thvoigtmann e833030
Merge pull request #7 from thvoigtmann/dev
alessiodam de9a2fe
prep v1.2.0: add docstrings, fix ServoStatusError, enforce BroadcastO…
alessiodam c978a2f
Merge branch 'dev' of github.com:alessiodam/python-st3215 into dev
alessiodam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| name: Build and Publish to PyPi | ||
|
|
||
| on: push | ||
| on: | ||
| push: | ||
| branches: [main] | ||
|
|
||
| jobs: | ||
| build: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| """ | ||
| Scan for all servos on the bus and display their information. | ||
| Supports either a direct serial device (e.g. /dev/ttyUSB0) or a network | ||
| socket URL (e.g. socket://<IP>:<PORT>). | ||
|
|
||
| Env vars: | ||
| - ST3215_URL : full pyserial URL (takes precedence) | ||
| - ST3215_HOST : IP/hostname for socket connection (default: 100.122.96.71) | ||
| - ST3215_PORT : TCP port for socket connection (default: 2000) | ||
| - ST3215_DEV : fallback serial device (default: /dev/ttyUSB0) | ||
|
|
||
| Run this on the host connected to the ST3215 driver board (make sure you have socat installed and set up udev rules for /dev/ttyACM0): | ||
| `stty -F /dev/ttyACM0 1000000 raw -echo && socat -d -d TCP4-LISTEN:2000,bind=0.0.0.0,reuseaddr,fork,nodelay FILE:/dev/ttyACM0,b1000000,raw,echo=0` | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
| import serial | ||
|
|
||
| from python_st3215 import ST3215 | ||
|
|
||
| url_env = os.environ.get("ST3215_URL") | ||
| host = os.environ.get("ST3215_HOST", "st3215-host") | ||
| port = os.environ.get("ST3215_PORT", "2000") | ||
|
|
||
| if url_env: | ||
| TARGET_URL = url_env | ||
| elif host and port: | ||
| TARGET_URL = f"socket://{host}:{port}" | ||
| else: | ||
| sys.exit(1) | ||
|
|
||
| print(f"Connecting to ST3215 via: {TARGET_URL}") | ||
|
|
||
| ser = serial.serial_for_url(TARGET_URL, timeout=0.02) | ||
| controller = ST3215(ser=ser, read_timeout=0.02) | ||
|
|
||
| try: | ||
| print("Scanning for servos...\n") | ||
| servos = controller.list_servos(timeout=0.02) | ||
|
|
||
| if not servos: | ||
| print("No servos found!") | ||
| else: | ||
| print(f"Found {len(servos)} servo(s)\n") | ||
| print("=" * 80) | ||
|
|
||
| mode_names = {0: "Position", 1: "Constant Speed", 2: "PWM", 3: "Stepper"} | ||
|
|
||
| for servo_id in servos: | ||
| servo = controller.wrap_servo(servo_id) | ||
|
|
||
| print(f"\nServo ID: {servo_id}") | ||
| print( | ||
| f" Firmware: v{servo.eeprom.read_firmware_major_version()}.{servo.eeprom.read_firmware_minor_version()}" | ||
| ) | ||
| print(f" Position: {servo.sram.read_current_location()}") | ||
| print(f" Temperature: {servo.sram.read_current_temperature()}°C") | ||
| print(f" Voltage: {servo.sram.read_current_voltage() / 10:.1f}V") | ||
| print( | ||
| f" Min/Max Angle: {servo.eeprom.read_min_angle_limit()} / {servo.eeprom.read_max_angle_limit()}" | ||
| ) | ||
|
|
||
| mode = servo.eeprom.read_operating_mode() | ||
| print(f" Operating Mode: {mode_names.get(mode, 'Unknown')}") | ||
|
|
||
| print("\n" + "=" * 80) | ||
| finally: | ||
| controller.close() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,34 @@ | ||
| from .st3215 import ST3215 | ||
| from .errors import ST3215Error, ServoNotRespondingError, InvalidInstructionError | ||
| from .errors import ( | ||
| BroadcastOperationError, | ||
| ChecksumError, | ||
| CommunicationTimeoutError, | ||
| InvalidIDError, | ||
| InvalidInstructionError, | ||
| InvalidParameterError, | ||
| ServoAngleLimitError, | ||
| ServoLockedError, | ||
| ServoNotRespondingError, | ||
| ServoStatusError, | ||
| ST3215Error, | ||
| ) | ||
| from .servo import Servo | ||
| from .st3215 import ST3215 | ||
|
|
||
| __version__ = "1.1.0" | ||
| __version__ = "1.2.0" | ||
|
|
||
| __all__ = [ | ||
| "__version__", | ||
| "ST3215", | ||
| "ST3215Error", | ||
| "ServoNotRespondingError", | ||
| "InvalidInstructionError", | ||
| "ChecksumError", | ||
| "InvalidParameterError", | ||
| "ServoAngleLimitError", | ||
| "CommunicationTimeoutError", | ||
| "ServoLockedError", | ||
| "BroadcastOperationError", | ||
| "InvalidIDError", | ||
| "ServoStatusError", | ||
| "Servo", | ||
| "__version__", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,40 +12,22 @@ def validate_servo_id(func: Callable[..., Any]) -> Callable[..., Any]: | |||||||||||||
| @wraps(func) | ||||||||||||||
| def wrapper(self: Any, servo_id: int, *args: Any, **kwargs: Any) -> Any: | ||||||||||||||
| if servo_id == 254: | ||||||||||||||
| from .errors import ST3215Error | ||||||||||||||
| from .errors import BroadcastOperationError | ||||||||||||||
|
|
||||||||||||||
| raise ST3215Error(f"Cannot {func.__name__} broadcast servo ID 254.") | ||||||||||||||
| return func(self, servo_id, *args, **kwargs) | ||||||||||||||
|
|
||||||||||||||
| return wrapper | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def validate_broadcast_only(func: Callable[..., Any]) -> Callable[..., Any]: | ||||||||||||||
| """ | ||||||||||||||
| Decorator to ensure operation is only used with broadcast servo (ID 254). | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| @wraps(func) | ||||||||||||||
| def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any: | ||||||||||||||
| if self.servo.id != 254: | ||||||||||||||
| from .errors import ST3215Error | ||||||||||||||
|
|
||||||||||||||
| raise ST3215Error( | ||||||||||||||
| f"{func.__name__} can only be used with broadcast servo (ID 254)." | ||||||||||||||
| raise BroadcastOperationError( | ||||||||||||||
| f"{func.__name__} cannot be used with broadcast ID 254." | ||||||||||||||
| ) | ||||||||||||||
| return func(self, *args, **kwargs) | ||||||||||||||
| return func(self, servo_id, *args, **kwargs) | ||||||||||||||
|
|
||||||||||||||
| return wrapper | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def validate_value_range( | ||||||||||||||
| min_val: int, max_val: int | ||||||||||||||
| ) -> Callable[[Callable[..., Any]], Callable[..., Any]]: | ||||||||||||||
| def validate_value_range(min_val: int, max_val: int) -> Callable[[F], F]: | ||||||||||||||
| """ | ||||||||||||||
| Decorator to validate that a value parameter is within the specified range. | ||||||||||||||
| """ | ||||||||||||||
|
|
||||||||||||||
| def decorator(func: Callable[..., Any]) -> Callable[..., Any]: | ||||||||||||||
| def decorator(func: F) -> F: | ||||||||||||||
| @wraps(func) | ||||||||||||||
| def wrapper(self: Any, value: int, *args: Any, **kwargs: Any) -> Any: | ||||||||||||||
| if not (min_val <= value <= max_val): | ||||||||||||||
|
|
@@ -54,27 +36,27 @@ def wrapper(self: Any, value: int, *args: Any, **kwargs: Any) -> Any: | |||||||||||||
| ) | ||||||||||||||
| return func(self, value, *args, **kwargs) | ||||||||||||||
|
|
||||||||||||||
| return wrapper | ||||||||||||||
| return wrapper # type: ignore[return-value] | ||||||||||||||
|
|
||||||||||||||
| return decorator | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def encode_signed_word(value: int) -> tuple[int, int]: | ||||||||||||||
| """ | ||||||||||||||
| Encode a signed 16-bit value to low and high bytes. | ||||||||||||||
| Encoding uses most-significant bit as sign bit. | ||||||||||||||
|
|
||||||||||||||
| Args: | ||||||||||||||
| value: Signed integer (-32768 to 32767) | ||||||||||||||
| value: Signed integer (-32767 to 32767) | ||||||||||||||
|
|
||||||||||||||
| Returns: | ||||||||||||||
| Tuple of (low_byte, high_byte) | ||||||||||||||
| """ | ||||||||||||||
| if value < -32767 or value > 32767: | ||||||||||||||
| raise ValueError | ||||||||||||||
|
||||||||||||||
| raise ValueError | |
| from .errors import InvalidParameterError | |
| raise InvalidParameterError( | |
| f"encode_signed_word: value {value} out of range [-32767, 32767]" | |
| ) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This docstring documents ST3215_HOST default as "100.122.96.71" and mentions an ST3215_DEV serial fallback, but the script defaults host to "st3215-host" and never uses ST3215_DEV (it always builds a socket:// URL when host/port defaults are present). Align the documented defaults with the code and either implement the ST3215_DEV fallback or remove it from the docstring.