Skip to content
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

Refinements for package #5

Merged
merged 10 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 133 additions & 10 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish Python Packages
name: Publish Python and Rust Packages

on:
release:
Expand All @@ -14,10 +14,13 @@ concurrency:
cancel-in-progress: true

jobs:
publish-python:
build-wheels:
strategy:
matrix:
os: [ubuntu-latest]
name: Build and publish Python package (${{ matrix.os }})
timeout-minutes: 10
name: Build and publish Python package
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}

steps:
- name: Checkout code
Expand All @@ -28,18 +31,138 @@ jobs:
with:
python-version: "3.10"

- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y libudev-dev pkg-config
- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build wheel
pip install cibuildwheel
shell: bash

- name: Build package
run: python -m build --sdist --outdir dist/ .
env:
CIBW_SKIP: "pp* *-musllinux*" # Skip PyPy and musllinux builds
CIBW_BEFORE_ALL_LINUX: |
yum install -y libudev-devel pkgconfig
CIBW_BEFORE_ALL_MACOS: |
brew install openssl pkg-config
CIBW_BEFORE_BUILD: |
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source $HOME/.cargo/env
pip install setuptools-rust
CIBW_ENVIRONMENT: |
PATH="/usr/local/bin:$HOME/.cargo/bin:$PATH"
CARGO_NET_GIT_FETCH_WITH_CLI=true
run: |
cibuildwheel --output-dir dist

- name: Upload wheel artifacts
uses: actions/upload-artifact@v3
with:
name: wheels-${{ matrix.os }}
path: |
dist/*.whl

build-source-dist:
name: Build and publish Python package (source distribution)
timeout-minutes: 10
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.11"

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install build
shell: bash

- name: Build source distribution
run: |
python -m build --sdist --outdir dist

- name: Upload source distribution
uses: actions/upload-artifact@v3
with:
name: source-dist
path: |
dist/*.tar.gz

publish-wheels:
needs: [build-wheels, build-source-dist]
name: Publish Python wheels
timeout-minutes: 10
runs-on: ubuntu-latest

steps:
- name: Download all wheels
uses: actions/download-artifact@v3
with:
path: dist

- name: Move wheels to dist directory
run: |
mkdir -p final_dist
find dist -name "*.whl" -exec mv {} final_dist/ \;
find dist -name "*.tar.gz" -exec mv {} final_dist/ \;

- name: Publish package
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: final_dist/

publish-rust:
name: Build and publish Rust package
timeout-minutes: 10
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y libudev-dev pkg-config

- name: Cache Cargo registry
uses: actions/cache@v2
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry
restore-keys: |
${{ runner.os }}-cargo-registry

- name: Cache Cargo index
uses: actions/cache@v2
with:
path: ~/.cargo/git
key: ${{ runner.os }}-cargo-index
restore-keys: |
${{ runner.os }}-cargo-index

- name: Publish imu and hexmove packages to crates.io
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: |
cargo publish -p imu
cargo publish -p hexmove
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ __pycache__/
.pytest_cache/
.ruff_cache/
.dmypy.json
*.pyi

# Databases
*.db
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ resolver = "2"

[workspace.package]

version = "0.1.2"
version = "0.1.3"
authors = ["Wesley Maa <wesley@kscale.dev>"]
edition = "2021"
description = "IMU package"
Expand Down
16 changes: 12 additions & 4 deletions examples/hexmove.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@ def main() -> None:
print(f"Failed to initialize IMU reader: {e}")
return

imu_reader.zero_imu(duration_ms=1000)

try:
while True:
# Get the current IMU data
data = imu_reader.get_data()
# # Get the current IMU data
# data = imu_reader.get_data()

# angles = [data.x_angle, data.y_angle, data.z_angle]
# velocities = [data.x_velocity, data.y_velocity, data.z_velocity]

angles = imu_reader.get_angles()
velocities = imu_reader.get_velocities()
print(
f"Angular Position: X={data.x_angle}°, Y={data.y_angle}°, Z={data.z_angle}° | "
f"Angular Velocity: X={data.x_velocity}°/s, Y={data.y_velocity}°/s, Z={data.z_velocity}°/s"
f"Angular Position: X={angles[0]}°, Y={angles[1]}°, Z={angles[2]}° | "
f"Angular Velocity: X={velocities[0]}°/s, Y={velocities[1]}°/s, Z={velocities[2]}°/s"
)

# Sleep for a short duration to avoid spamming the console
Expand Down
2 changes: 0 additions & 2 deletions imu/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Defines the top-level API for the IMU package."""

__version__ = "0.0.2"

from .bindings import (
PyHexmoveImuData as HexmoveImuData,
PyHexmoveImuReader as HexmoveImuReader,
Expand Down
1 change: 0 additions & 1 deletion imu/bindings.pyi

This file was deleted.

21 changes: 0 additions & 21 deletions imu/bindings/bindings.pyi

This file was deleted.

34 changes: 34 additions & 0 deletions imu/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,31 @@ impl PyHexmoveImuReader {
Ok(PyHexmoveImuData::from(data))
}

fn get_angles(&self) -> PyResult<(f32, f32, f32)> {
let imu_reader = self.inner.lock().unwrap();
let (x, y, z) = imu_reader.get_angles();
Ok((x, y, z))
}

fn get_velocities(&self) -> PyResult<(f32, f32, f32)> {
let imu_reader = self.inner.lock().unwrap();
let (x, y, z) = imu_reader.get_velocities();
Ok((x, y, z))
}

#[pyo3(signature = (duration_ms=None, max_retries=None, max_variance=None))]
fn zero_imu(
&self,
duration_ms: Option<u64>,
max_retries: Option<u32>,
max_variance: Option<f32>,
) -> PyResult<()> {
let imu_reader = self.inner.lock().unwrap();
imu_reader
.zero_imu(duration_ms, max_retries, max_variance)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e))
}

fn stop(&self) -> PyResult<()> {
let imu_reader = self.inner.lock().unwrap();
imu_reader.stop();
Expand All @@ -51,6 +76,12 @@ struct PyHexmoveImuData {
y_velocity: f32,
#[pyo3(get)]
z_velocity: f32,
#[pyo3(get)]
x_angle_offset: f32,
#[pyo3(get)]
y_angle_offset: f32,
#[pyo3(get)]
z_angle_offset: f32,
}

impl From<HexmoveImuData> for PyHexmoveImuData {
Expand All @@ -62,6 +93,9 @@ impl From<HexmoveImuData> for PyHexmoveImuData {
x_velocity: data.x_velocity,
y_velocity: data.y_velocity,
z_velocity: data.z_velocity,
x_angle_offset: data.x_angle_offset,
y_angle_offset: data.y_angle_offset,
z_angle_offset: data.z_angle_offset,
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions imu/hexmove/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Hexmove

This crate contains the code for interacting with Hexmove IMUs.

Hexmove IMUs operate over a CAN bus. To set up the CAN interface for Hexmove IMUs, run the following command (where can0 is the name of the CAN interface):

```bash
sudo ip link set can0 up type can bitrate 500000
```
Loading
Loading