-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working python binding draft for hiwonder IMU
- Loading branch information
1 parent
9f13b24
commit 3c7dbc8
Showing
10 changed files
with
105 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -5,11 +5,11 @@ members = [ | |
"imu/bindings", | ||
"imu/hexmove", | ||
"imu/hiwonder", | ||
"imu/bindings_hiwonder", | ||
] | ||
resolver = "2" | ||
|
||
[workspace.package] | ||
|
||
version = "0.1.6" | ||
authors = ["Wesley Maa <[email protected]>"] | ||
edition = "2021" | ||
|
This file contains 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,21 @@ | ||
from bindings_hiwonder import HiwonderImu | ||
import time | ||
|
||
def main(): | ||
# Create IMU instance | ||
imu = HiwonderImu("/dev/ttyUSB0", 9600) | ||
|
||
try: | ||
while True: | ||
if data := imu.read_data(): | ||
acc, gyro, angle = data | ||
print(f"\033[2J\033[H") # Clear screen | ||
print(f"Acceleration (m/s²): {acc}") | ||
print(f"Gyroscope (deg/s): {gyro}") | ||
print(f"Angle (degrees): {angle}") | ||
time.sleep(0.1) # Add small delay to make output readable | ||
except KeyboardInterrupt: | ||
print("\nExiting...") | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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 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,16 @@ | ||
[package] | ||
name = "bindings_hiwonder" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["Aaron Xie <[email protected]>"] | ||
description = "Python bindings for Hiwonder IMU" | ||
license = "MIT" | ||
|
||
[lib] | ||
name = "bindings_hiwonder" | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[dependencies] | ||
pyo3 = { version = ">= 0.21.0", features = ["extension-module"] } | ||
pyo3-stub-gen = ">= 0.6.0" | ||
hiwonder = { path = "../hiwonder" } |
This file contains 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,8 @@ | ||
[build-system] | ||
requires = ["maturin>=1.1,<2.0"] | ||
build-backend = "maturin" | ||
|
||
[project] | ||
name = "bindings_hiwonder" | ||
version = "0.1.0" | ||
requires-python = ">=3.9" |
This file contains 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,7 @@ | ||
use pyo3_stub_gen::Result; | ||
|
||
fn main() -> Result<()> { | ||
let stub = bindings_hiwonder::stub_info()?; | ||
stub.generate()?; | ||
Ok(()) | ||
} |
This file contains 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,40 @@ | ||
use hiwonder::IMU; | ||
use pyo3::prelude::*; | ||
use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pymethods}; | ||
use pyo3_stub_gen::define_stub_info_gatherer; | ||
use std::sync::{Arc, Mutex}; | ||
|
||
#[gen_stub_pyclass] | ||
#[pyclass(name = "HiwonderImu")] | ||
struct PyHiwonderImu { | ||
inner: Arc<Mutex<IMU>>, | ||
} | ||
|
||
#[gen_stub_pymethods] | ||
#[pymethods] | ||
impl PyHiwonderImu { | ||
#[new] | ||
fn new(interface: String, baud_rate: u32) -> PyResult<Self> { | ||
let imu = IMU::new(&interface, baud_rate) | ||
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?; | ||
Ok(PyHiwonderImu { | ||
inner: Arc::new(Mutex::new(imu)), | ||
}) | ||
} | ||
|
||
fn read_data(&mut self) -> PyResult<Option<([f32; 3], [f32; 3], [f32; 3])>> { | ||
let mut imu = self.inner | ||
.lock() | ||
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?; | ||
imu.read_data() | ||
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string())) | ||
} | ||
} | ||
|
||
#[pymodule] | ||
fn bindings_hiwonder(py: Python<'_>, m: &Bound<PyModule>) -> PyResult<()> { | ||
m.add("HiwonderImu", py.get_type::<PyHiwonderImu>())?; | ||
Ok(()) | ||
} | ||
|
||
define_stub_info_gatherer!(stub_info); |
This file contains 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,3 @@ | ||
TODO | ||
- [ ] Add std write for updating the data output hz | ||
- [ ] Add python binding |
This file contains 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,5 @@ | ||
"""Defines the top-level API for the Hiwonder IMU package.""" | ||
|
||
from bindings_hiwonder import HiwonderImu # Changed from relative import | ||
|
||
__all__ = ['HiwonderImu'] |
This file contains 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