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

better api #20

Merged
merged 3 commits into from
Oct 11, 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
2 changes: 1 addition & 1 deletion actuator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.0.11"
__version__ = "0.0.12"

from .rust.bindings import (
PyRobstrideMotorFeedback as RobstrideMotorFeedback,
Expand Down
32 changes: 29 additions & 3 deletions actuator/rust/bindings/bindings.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@

import typing

class PyRobstrideMotorControlParams:
position: float
velocity: float
kp: float
kd: float
torque: float
def __new__(cls,position:float, velocity:float, kp:float, kd:float, torque:float): ...
def __repr__(self) -> str:
...


class PyRobstrideMotorFeedback:
can_id: int
position: float
Expand All @@ -28,7 +39,7 @@ class PyRobstrideMotors:
def send_start(self) -> dict[int, PyRobstrideMotorFeedback]:
...

def send_torque_controls(self, torque_sets:typing.Mapping[int, float]) -> dict[int, PyRobstrideMotorFeedback]:
def send_motor_controls(self, motor_controls:typing.Mapping[int, PyRobstrideMotorControlParams]) -> dict[int, PyRobstrideMotorFeedback]:
...

def get_latest_feedback(self) -> dict[int, PyRobstrideMotorFeedback]:
Expand All @@ -43,10 +54,19 @@ class PyRobstrideMotors:

class PyRobstrideMotorsSupervisor:
def __new__(cls,port_name:str, motor_infos:typing.Mapping[int, str]): ...
def set_target_position(self, motor_id:int, position:float) -> None:
def set_position(self, motor_id:int, position:float) -> None:
...

def set_velocity(self, motor_id:int, velocity:float) -> None:
...

def set_kp_kd(self, motor_id:int, kp:float, kd:float) -> None:
def set_kp(self, motor_id:int, kp:float) -> None:
...

def set_kd(self, motor_id:int, kd:float) -> None:
...

def set_torque(self, motor_id:int, torque:float) -> None:
...

def set_sleep_duration(self, sleep_duration:float) -> None:
Expand All @@ -58,10 +78,16 @@ class PyRobstrideMotorsSupervisor:
def get_latest_feedback(self) -> dict[int, PyRobstrideMotorFeedback]:
...

def toggle_pause(self) -> None:
...

def stop(self) -> None:
...

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

def set_params(self, motor_id:int, params:PyRobstrideMotorControlParams) -> None:
...


119 changes: 111 additions & 8 deletions actuator/rust/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use pyo3::prelude::*;
use pyo3_stub_gen::define_stub_info_gatherer;
use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pymethods};
use robstride::{
motor_type_from_str as robstride_motor_type_from_str, MotorFeedback as RobstrideMotorFeedback,
motor_type_from_str as robstride_motor_type_from_str,
MotorControlParams as RobstrideMotorControlParams, MotorFeedback as RobstrideMotorFeedback,
MotorType as RobstrideMotorType, Motors as RobstrideMotors,
MotorsSupervisor as RobstrideMotorsSupervisor,
};
Expand Down Expand Up @@ -72,12 +73,17 @@ impl PyRobstrideMotors {
.collect()
}

fn send_torque_controls(
fn send_motor_controls(
&mut self,
torque_sets: HashMap<u8, f32>,
motor_controls: HashMap<u8, PyRobstrideMotorControlParams>,
) -> PyResult<HashMap<u8, PyRobstrideMotorFeedback>> {
let motor_controls: HashMap<u8, RobstrideMotorControlParams> = motor_controls
.into_iter()
.map(|(k, v)| (k, v.into()))
.collect();

self.inner
.send_torque_controls(&torque_sets)
.send_motor_controls(&motor_controls)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?
.into_iter()
.map(|(k, v)| Ok((k, v.into())))
Expand Down Expand Up @@ -147,6 +153,68 @@ impl From<RobstrideMotorFeedback> for PyRobstrideMotorFeedback {
}
}

#[gen_stub_pyclass]
#[pyclass]
#[derive(FromPyObject)]
struct PyRobstrideMotorControlParams {
#[pyo3(get, set)]
position: f32,
#[pyo3(get, set)]
velocity: f32,
#[pyo3(get, set)]
kp: f32,
#[pyo3(get, set)]
kd: f32,
#[pyo3(get, set)]
torque: f32,
}

#[gen_stub_pymethods]
#[pymethods]
impl PyRobstrideMotorControlParams {
#[new]
fn new(position: f32, velocity: f32, kp: f32, kd: f32, torque: f32) -> Self {
PyRobstrideMotorControlParams {
position,
velocity,
kp,
kd,
torque,
}
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!(
"PyRobstrideMotorControlParams(position={:.2}, velocity={:.2}, kp={:.2}, kd={:.2}, torque={:.2})",
self.position, self.velocity, self.kp, self.kd, self.torque
))
}
}

impl From<PyRobstrideMotorControlParams> for RobstrideMotorControlParams {
fn from(params: PyRobstrideMotorControlParams) -> Self {
RobstrideMotorControlParams {
position: params.position,
velocity: params.velocity,
kp: params.kp,
kd: params.kd,
torque: params.torque,
}
}
}

impl From<RobstrideMotorControlParams> for PyRobstrideMotorControlParams {
fn from(params: RobstrideMotorControlParams) -> Self {
PyRobstrideMotorControlParams {
position: params.position,
velocity: params.velocity,
kp: params.kp,
kd: params.kd,
torque: params.torque,
}
}
}

#[gen_stub_pyclass]
#[pyclass]
struct PyRobstrideMotorsSupervisor {
Expand All @@ -172,13 +240,28 @@ impl PyRobstrideMotorsSupervisor {
Ok(PyRobstrideMotorsSupervisor { inner: controller })
}

fn set_target_position(&self, motor_id: u8, position: f32) -> PyResult<()> {
self.inner.set_target_position(motor_id, position);
fn set_position(&self, motor_id: u8, position: f32) -> PyResult<()> {
self.inner.set_position(motor_id, position);
Ok(())
}

fn set_velocity(&self, motor_id: u8, velocity: f32) -> PyResult<()> {
self.inner.set_velocity(motor_id, velocity);
Ok(())
}

fn set_kp(&self, motor_id: u8, kp: f32) -> PyResult<()> {
self.inner.set_kp(motor_id, kp);
Ok(())
}

fn set_kd(&self, motor_id: u8, kd: f32) -> PyResult<()> {
self.inner.set_kd(motor_id, kd);
Ok(())
}

fn set_kp_kd(&self, motor_id: u8, kp: f32, kd: f32) -> PyResult<()> {
self.inner.set_kp_kd(motor_id, kp, kd);
fn set_torque(&self, motor_id: u8, torque: f32) -> PyResult<()> {
self.inner.set_torque(motor_id, torque);
Ok(())
}

Expand All @@ -201,6 +284,11 @@ impl PyRobstrideMotorsSupervisor {
.collect()
}

fn toggle_pause(&self) -> PyResult<()> {
self.inner.toggle_pause();
Ok(())
}

fn stop(&self) -> PyResult<()> {
self.inner.stop();
Ok(())
Expand All @@ -213,13 +301,28 @@ impl PyRobstrideMotorsSupervisor {
motor_count
))
}

fn set_params(&self, motor_id: u8, params: &PyRobstrideMotorControlParams) -> PyResult<()> {
self.inner.set_params(
motor_id,
RobstrideMotorControlParams {
position: params.position,
velocity: params.velocity,
kp: params.kp,
kd: params.kd,
torque: params.torque,
},
);
Ok(())
}
}

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

Expand Down
139 changes: 0 additions & 139 deletions actuator/rust/robstride/src/bin/motors.rs

This file was deleted.

Loading
Loading