Skip to content

Commit 36d9ab0

Browse files
Add python binding
1 parent a68ae52 commit 36d9ab0

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

python/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Rustypot python bindings
2+
3+
## Installation
4+
5+
Rustypot bindings are available on PyPI. You can install them using pip:
6+
7+
```bash
8+
pip install rustypot
9+
```
10+
11+
### Building the bindings
12+
13+
If you want to build the bindings from source, you can clone the repository and run the following command:
14+
15+
```bash
16+
maturin develop --release --features python
17+
```
18+
19+
## Usage
20+
21+
The Python bindings exposes the same API as the Controller API in the rust crate.
22+
23+
You first need to create a Controller object. For instance, to communicate with a serial port to Feetech STS3215 motors, you can do the following:
24+
25+
```python
26+
from rustypot.servo import Sts3215SyncController
27+
28+
c = Sts3215SyncController(serial_port='/dev/ttyUSB0', baudrate=100000, timeout=0.1)
29+
```
30+
31+
Then, you can directly read/write any register of the motor. For instance, to read the present position of the motors with id 1 and 2, you can do:
32+
33+
```python
34+
35+
pos = c.read_present_position([1, 2])
36+
print(pos)
37+
```
38+
39+
You can also write to the motors. For instance, to set the goal position of the motors with id 1 and 2 to 0.0 and 90° respectively, you can do:
40+
41+
```python
42+
import numpy as np
43+
c.write_goal_position([1, 2], [0.0, np.deg2rad(90.0)])
44+
```

python/examples/feetech_sinus.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import time
2+
import numpy as np
3+
4+
from rustypot.servo import Sts3215SyncController
5+
6+
def main():
7+
c = Sts3215SyncController(
8+
serial_port='/dev/tty.usbmodem58FA0822621',
9+
baudrate=1000000,
10+
timeout=0.1,
11+
)
12+
13+
c.write_torque_enable([1, 2], [True, True])
14+
15+
t0 = time.time()
16+
17+
while True:
18+
t = time.time() - t0
19+
pos = np.sin(2 * np.pi * 0.25 * t) * np.deg2rad(45)
20+
21+
c.write_goal_position([1, 2], [pos, pos])
22+
print(c.read_present_position([1, 2]))
23+
24+
time.sleep(0.01)
25+
26+
if __name__ == '__main__':
27+
main()

0 commit comments

Comments
 (0)