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

Fix dynamic import error, also added explicit run_main_loop call whic… #61

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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: 2 additions & 0 deletions actuator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .bindings import (
PyRobstrideActuator as RobstrideActuator,
PyRobstrideActuatorConfig as RobstrideActuatorConfig,
PyRobstrideActuatorCommand as RobstrideActuatorCommand,
PyRobstrideConfigureRequest as RobstrideConfigureRequest,
get_version,
)

Expand Down
80 changes: 62 additions & 18 deletions actuator/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::Arc;
use std::time::Duration;
use tokio::runtime::Runtime;
use tokio::sync::Mutex;

use tokio::time;
struct ErrReportWrapper(eyre::Report);

impl From<eyre::Report> for ErrReportWrapper {
Expand Down Expand Up @@ -49,12 +49,17 @@ struct PyRobstrideActuatorCommand {
#[pymethods]
impl PyRobstrideActuatorCommand {
#[new]
fn new(actuator_id: u32) -> Self {
fn new(
actuator_id: u32,
position: Option<f64>,
velocity: Option<f64>,
torque: Option<f64>,
) -> Self {
Self {
actuator_id,
position: None,
velocity: None,
torque: None,
position: position,
velocity: velocity,
torque: torque,
}
}
}
Expand Down Expand Up @@ -83,15 +88,23 @@ struct PyRobstrideConfigureRequest {
#[pymethods]
impl PyRobstrideConfigureRequest {
#[new]
fn new(actuator_id: u32) -> Self {
fn new(
actuator_id: u32,
kp: Option<f64>,
kd: Option<f64>,
max_torque: Option<f64>,
torque_enabled: Option<bool>,
zero_position: Option<bool>,
new_actuator_id: Option<u32>,
) -> Self {
Self {
actuator_id,
kp: None,
kd: None,
max_torque: None,
torque_enabled: None,
zero_position: None,
new_actuator_id: None,
kp: kp,
kd: kd,
max_torque: max_torque,
torque_enabled: torque_enabled,
zero_position: zero_position,
new_actuator_id: new_actuator_id,
}
}
}
Expand Down Expand Up @@ -153,17 +166,16 @@ impl PyRobstrideActuator {
fn new(
ports: Vec<String>,
py_actuators_config: Vec<(u8, PyRobstrideActuatorConfig)>,
polling_interval: f64,
) -> PyResult<Self> {
let actuators_config: Vec<(u8, ActuatorConfiguration)> = py_actuators_config
.into_iter()
.map(|(id, config)| (id, config.into()))
.collect();

let rt = Runtime::new().map_err(|e| ErrReportWrapper(e.into()))?;
let rt: Runtime = Runtime::new().map_err(|e| ErrReportWrapper(e.into()))?;

let supervisor = rt.block_on(async {
let mut supervisor =
let mut supervisor: Supervisor =
Supervisor::new(Duration::from_secs(1)).map_err(|e| ErrReportWrapper(e))?;

for port in &ports {
Expand Down Expand Up @@ -194,13 +206,13 @@ impl PyRobstrideActuator {
.scan_bus(0xFD, port, &actuators_config)
.await
.map_err(|e| ErrReportWrapper(e))?;

for (motor_id, _) in &actuators_config {
if !discovered_ids.contains(motor_id) {
tracing::warn!("Configured motor not found - ID: {}", motor_id);
}
}
}

Ok(supervisor)
})?;

Expand All @@ -210,6 +222,39 @@ impl PyRobstrideActuator {
})
}

fn run_main_loop(&self, interval_ms: u64) -> PyResult<bool> {
self.rt.block_on(async {
let mut interval = time::interval(Duration::from_millis(interval_ms));
let supervisor_clone = self.supervisor.clone();
self.rt.spawn(async move {
loop {
interval.tick().await;
let mut clone: tokio::sync::MutexGuard<'_, Supervisor> =
supervisor_clone.lock().await;
if let Err(e) = clone.run_update_and_control().await {
tracing::error!("Error in run_update_and_control: {:?}", e);
}
drop(clone);
}
});
return Ok(true);
})
}

fn disable(&self, id: u8) -> PyResult<bool> {
self.rt.block_on(async {
let mut supervisor = self.supervisor.lock().await;
Ok(supervisor.disable(id, true).await.is_ok())
})
}

fn enable(&self, id: u8) -> PyResult<bool> {
self.rt.block_on(async {
let mut supervisor = self.supervisor.lock().await;
Ok(supervisor.enable(id).await.is_ok())
})
}

fn command_actuators(&self, commands: Vec<PyRobstrideActuatorCommand>) -> PyResult<Vec<bool>> {
self.rt.block_on(async {
let mut results = vec![];
Expand Down Expand Up @@ -289,7 +334,6 @@ impl PyRobstrideActuator {
self.rt.block_on(async {
let mut responses = vec![];
let supervisor = self.supervisor.lock().await;

for id in actuator_ids {
if let Ok(Some((feedback, ts))) = supervisor.get_feedback(id as u8).await {
responses.push(PyRobstrideActuatorState {
Expand Down Expand Up @@ -326,7 +370,7 @@ impl From<PyRobstrideActuatorConfig> for robstride::ActuatorConfiguration {
}

#[pymodule]
fn robstride_bindings(m: &Bound<PyModule>) -> PyResult<()> {
fn bindings(py: Python, m: &Bound<PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(get_version, m)?)?;
m.add_class::<PyRobstrideActuator>()?;
m.add_class::<PyRobstrideActuatorCommand>()?;
Expand Down
Loading
Loading