Skip to content

Commit

Permalink
0.0.22 fixes (#36)
Browse files Browse the repository at this point in the history
* centralize robstride configs in rust land

* lint

* change to bound

* revert cursor

* Bump version to 0.0.23
  • Loading branch information
WT-MM authored Oct 22, 2024
1 parent 21ec337 commit eb089af
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 120 deletions.
4 changes: 3 additions & 1 deletion actuator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Defines the top-level API for the actuator package."""

__version__ = "0.0.22"
__version__ = "0.0.23"

from .rust.bindings import (
PyRobstrideMotorConfig as RobstrideMotorConfig,
PyRobstrideMotorControlParams as RobstrideMotorControlParams,
PyRobstrideMotorFeedback as RobstrideMotorFeedback,
PyRobstrideMotors as RobstrideMotors,
PyRobstrideMotorsSupervisor as RobstrideMotorsSupervisor,
PyRobstrideMotorType as RobstrideMotorType,
)
34 changes: 34 additions & 0 deletions actuator/rust/bindings/bindings.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@

import typing

class PyRobstrideMotorConfig:
p_min: float
p_max: float
v_min: float
v_max: float
kp_min: float
kp_max: float
kd_min: float
kd_max: float
t_min: float
t_max: float
zero_on_init: bool
can_timeout_command: int
can_timeout_factor: float

class PyRobstrideMotorControlParams:
position: float
velocity: float
Expand All @@ -29,6 +44,21 @@ class PyRobstrideMotorFeedback:
...


class PyRobstrideMotorType:
def __repr__(self) -> str:
...

@staticmethod
def from_str(s:str) -> PyRobstrideMotorType:
...

def __hash__(self) -> int:
...

def __eq__(self, other:typing.Any) -> bool:
...


class PyRobstrideMotors:
def __new__(cls,port_name,motor_infos,verbose = ...): ...
def send_get_mode(self) -> dict[int, str]:
Expand All @@ -46,6 +76,10 @@ class PyRobstrideMotors:
def __repr__(self) -> str:
...

@staticmethod
def get_default_configs() -> dict[PyRobstrideMotorType, PyRobstrideMotorConfig]:
...


class PyRobstrideMotorsSupervisor:
total_commands: int
Expand Down
151 changes: 149 additions & 2 deletions actuator/rust/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use pyo3_stub_gen::define_stub_info_gatherer;
use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pymethods};
use robstride::{
motor_mode_from_str as robstride_motor_mode_from_str,
motor_type_from_str as robstride_motor_type_from_str,
motor_type_from_str as robstride_motor_type_from_str, MotorConfig as RobstrideMotorConfig,
MotorControlParams as RobstrideMotorControlParams, MotorFeedback as RobstrideMotorFeedback,
MotorType as RobstrideMotorType, Motors as RobstrideMotors,
MotorsSupervisor as RobstrideMotorsSupervisor,
MotorsSupervisor as RobstrideMotorsSupervisor, ROBSTRIDE_CONFIGS as RobstrideDefaultConfigs,
};
use std::collections::HashMap;
use std::hash::{Hash, Hasher};

#[gen_stub_pyclass]
#[pyclass]
Expand Down Expand Up @@ -78,6 +79,14 @@ impl PyRobstrideMotors {
fn __repr__(&self) -> PyResult<String> {
Ok(format!("PyRobstrideMotors"))
}

#[staticmethod]
fn get_default_configs() -> PyResult<HashMap<PyRobstrideMotorType, PyRobstrideMotorConfig>> {
Ok(RobstrideDefaultConfigs
.iter()
.map(|(motor_type, config)| ((*motor_type).into(), (*config).into()))
.collect())
}
}

#[gen_stub_pyclass]
Expand Down Expand Up @@ -390,12 +399,150 @@ impl PyRobstrideMotorsSupervisor {
}
}

#[gen_stub_pyclass]
#[pyclass]
#[derive(Clone)]
struct PyRobstrideMotorConfig {
#[pyo3(get)]
p_min: f32,
#[pyo3(get)]
p_max: f32,
#[pyo3(get)]
v_min: f32,
#[pyo3(get)]
v_max: f32,
#[pyo3(get)]
kp_min: f32,
#[pyo3(get)]
kp_max: f32,
#[pyo3(get)]
kd_min: f32,
#[pyo3(get)]
kd_max: f32,
#[pyo3(get)]
t_min: f32,
#[pyo3(get)]
t_max: f32,
#[pyo3(get)]
zero_on_init: bool,
#[pyo3(get)]
can_timeout_command: u16,
#[pyo3(get)]
can_timeout_factor: f32,
}

impl From<RobstrideMotorConfig> for PyRobstrideMotorConfig {
fn from(config: RobstrideMotorConfig) -> Self {
PyRobstrideMotorConfig {
p_min: config.p_min,
p_max: config.p_max,
v_min: config.v_min,
v_max: config.v_max,
kp_min: config.kp_min,
kp_max: config.kp_max,
kd_min: config.kd_min,
kd_max: config.kd_max,
t_min: config.t_min,
t_max: config.t_max,
zero_on_init: config.zero_on_init,
can_timeout_command: config.can_timeout_command,
can_timeout_factor: config.can_timeout_factor,
}
}
}

#[gen_stub_pyclass]
#[pyclass]
#[derive(Copy, Clone, Debug, Eq)]
struct PyRobstrideMotorType {
value: u8,
}

impl Hash for PyRobstrideMotorType {
fn hash<H: Hasher>(&self, state: &mut H) {
self.value.hash(state);
}
}

impl PartialEq for PyRobstrideMotorType {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}

#[gen_stub_pymethods]
#[pymethods]
impl PyRobstrideMotorType {
#[classattr]
const TYPE01: Self = PyRobstrideMotorType { value: 0 };
#[classattr]
const TYPE02: Self = PyRobstrideMotorType { value: 1 };
#[classattr]
const TYPE03: Self = PyRobstrideMotorType { value: 2 };
#[classattr]
const TYPE04: Self = PyRobstrideMotorType { value: 3 };

fn __repr__(&self) -> PyResult<String> {
let type_name = match self.value {
0 => "TYPE01",
1 => "TYPE02",
2 => "TYPE03",
3 => "TYPE04",
_ => "Unknown",
};
Ok(format!("PyRobstrideMotorType::{}", type_name))
}

#[staticmethod]
fn from_str(s: &str) -> PyResult<Self> {
let motor_type = robstride_motor_type_from_str(s)?;
Ok(PyRobstrideMotorType::from(motor_type))
}

fn __hash__(&self) -> PyResult<isize> {
Ok(self.value as isize)
}

fn __eq__(&self, other: &Bound<'_, PyAny>) -> PyResult<bool> {
if let Ok(other) = other.extract::<PyRobstrideMotorType>() {
Ok(self.value == other.value)
} else {
Ok(false)
}
}
}

impl From<RobstrideMotorType> for PyRobstrideMotorType {
fn from(motor_type: RobstrideMotorType) -> Self {
match motor_type {
RobstrideMotorType::Type01 => PyRobstrideMotorType::TYPE01,
RobstrideMotorType::Type02 => PyRobstrideMotorType::TYPE02,
RobstrideMotorType::Type03 => PyRobstrideMotorType::TYPE03,
RobstrideMotorType::Type04 => PyRobstrideMotorType::TYPE04,
}
}
}

impl From<PyRobstrideMotorType> for RobstrideMotorType {
fn from(py_motor_type: PyRobstrideMotorType) -> Self {
match py_motor_type.value {
0 => RobstrideMotorType::Type01,
1 => RobstrideMotorType::Type02,
2 => RobstrideMotorType::Type03,
3 => RobstrideMotorType::Type04,
_ => RobstrideMotorType::Type04,
}
}
}

#[pymodule]
fn bindings(m: &Bound<PyModule>) -> PyResult<()> {
m.add_class::<PyRobstrideMotors>()?;
m.add_class::<PyRobstrideMotorFeedback>()?;
m.add_class::<PyRobstrideMotorsSupervisor>()?;
m.add_class::<PyRobstrideMotorControlParams>()?;
m.add_class::<PyRobstrideMotorConfig>()?;
m.add_class::<PyRobstrideMotorType>()?;
Ok(())
}

Expand Down
3 changes: 2 additions & 1 deletion actuator/rust/robstride/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ pub const CAN_ID_DEBUG_UI: u8 = 0xFD;
pub const BAUD_RATE: nix::sys::termios::BaudRate = nix::sys::termios::BaudRate::B921600;

// WARNING: NOT A VALID BAUDRATE
// This is just a configuration to build on MacOS
// This is just a configuration to build without errors on MacOS
#[cfg(target_os = "macos")]
pub const BAUD_RATE: nix::sys::termios::BaudRate = nix::sys::termios::BaudRate::B115200;

#[derive(Debug, Clone, Copy)]
pub struct MotorConfig {
pub p_min: f32,
pub p_max: f32,
Expand Down
2 changes: 1 addition & 1 deletion actuator/rust/robstride/src/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::time::Duration;
pub const BAUD_RATE: nix::sys::termios::BaudRate = nix::sys::termios::BaudRate::B921600;

// WARNING: NOT A VALID BAUDRATE
// This is just a configuration to build on MacOS
// This is just a configuration to build without errors on MacOS
#[cfg(target_os = "macos")]
pub const BAUD_RATE: nix::sys::termios::BaudRate = nix::sys::termios::BaudRate::B115200;

Expand Down
Loading

0 comments on commit eb089af

Please sign in to comment.