Metadata-Version: 2.4
Name: servomotor
Version: 0.13.0
Summary: A Python package for controlling M17 Servomotors, the 4-in-1 (motor + drive + motion control + encoder) integrated servomotors from Gearotons
Author-email: Tom Rodinger <tom@gearotons.com>
License: MIT License
        
        Copyright (c) 2024 Move the Needle Limited (Hong Kong)
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Project-URL: Homepage, https://gearotons.com
Project-URL: Source, https://github.com/tomrodinger/servomotor
Project-URL: Changelog, https://github.com/tomrodinger/servomotor/blob/main/python_programs/PyPi_distribution/CHANGELOG.md
Project-URL: Issues, https://tutorial.gearotons.com/feedback
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENCE
Dynamic: license-file


## Usage

Here's a basic example of how to use the servomotor package:

```python
import servomotor

# Open the intended adapter explicitly. Use a motor unique ID on shared racks.
servomotor.set_serial_port("/dev/ttyUSB0")
servomotor.open_serial_port()
motor = servomotor.M3("0123456789ABCDEF", position_unit="encoder_counts")
try:
    kp, ki, kd = motor.get_pid_constants()  # firmware 0.16.0.0 or later
    print(kp, ki, kd)
finally:
    servomotor.close_serial_port()
```

Firmware 0.17.0.0 adds `set_immediate_position_target(targetPosition)` and
`set_immediate_position_target_and_get_position(targetPosition)`. Both require
closed-loop position mode, enabled MOSFETs, an empty trajectory queue and zero
commanded/planned velocity. Targets must satisfy the configured position and
deviation limits. These commands step the reference immediately; trajectory
velocity/acceleration limits do not profile the resulting catch-up motion.

The combined method returns the latest stored measured Hall position, which may
come from the preceding control tick. It does not wait for arrival or sample the
ADC again. With `position_unit="encoder_counts"`, Python integer targets and
responses preserve the full signed 64-bit value. For other units, conversions use
floating point. The measured position may differ from the requested target.

Broadcast calls return an empty list because motors do not reply. Use a unique
ID to obtain a measured position or PID constants. Invalid keyword arguments,
nonfinite numeric inputs and out-of-range integer fields raise exceptions before
transmitting. Device fatal errors raise `servomotor.FatalError` and require a
motor reset. Do not retry motion automatically after a communication failure.

For more detailed usage instructions, please refer to the documentation.

## Choosing the serial port

The command line tools work out which serial port to use in this order, highest
priority first:

1. `-p PORT` on the command line (`-P` instead pops up a menu of detected ports)
2. the `SERVOMOTOR_PORT` environment variable
3. the port saved from the last successful run
4. an interactive menu, if none of the above apply

So a port can be set once for a whole shell session:

```bash
export SERVOMOTOR_PORT=/dev/ttyUSB0        # Linux
export SERVOMOTOR_PORT=/dev/cu.usbserial-110   # macOS
set SERVOMOTOR_PORT=COM3                   # Windows (cmd)
$env:SERVOMOTOR_PORT = "COM3"              # Windows (PowerShell)
```

`-p` still overrides it for a single command, and `-P` always shows the menu.

After a port is opened successfully it is remembered, in a per-user location:

| Platform | Saved port file |
|----------|-----------------|
| Linux    | `$XDG_CONFIG_HOME/servomotor/serial_device.txt`, else `~/.config/servomotor/serial_device.txt` |
| macOS    | `~/Library/Application Support/servomotor/serial_device.txt` |
| Windows  | `%LOCALAPPDATA%\servomotor\serial_device.txt` |

Delete that file to forget the remembered port.

### Serial port permissions on Linux

Serial devices usually belong to the `dialout` group (`uucp` on some
distributions). If you get a permission error, add yourself to it:

```bash
sudo usermod -a -G dialout $USER
```

then log out and back in for the change to take effect.

## Features

- Control Gearotons servomotors via serial communication
- Easy-to-use interface for sending commands and receiving responses
- Automatic handling of serial port selection and management

## Requirements

- Python 3.8+

That is the whole list. pyserial is BUNDLED inside this package
(`servomotor/vendor/serial/`) and imported from there, so `pip install servomotor`
pulls in no other dependencies and you do not need to install pyserial yourself.

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

## License

This project is licensed under the MIT License - see the [LICENCE](LICENCE) file for details.

## Contact

Tom Rodinger - tom.rodinger@alumni.utoronto.ca

Project Link: [https://gearotons.com](https://gearotons.com)
