Skip to content

Commit

Permalink
Remove unwrap() (#53)
Browse files Browse the repository at this point in the history
* crying

* no more unwraps

* return result

* fix bindings

* fix bins

* use result

* ? ;)

* no more

* none remain

* .vscode

* truly all has been conquered

* bump version to 0.2.9
  • Loading branch information
WT-MM authored Nov 15, 2024
1 parent 42f51f5 commit a24ae71
Show file tree
Hide file tree
Showing 8 changed files with 335 additions and 200 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ Cargo.lock

# CSV files
*.csv

# VSCode
.vscode/
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.2.8"
version = "0.2.9"
authors = ["Wesley Maa <[email protected]>", "Pawel Budzianowski <[email protected]>", "Benjamin Bolte <[email protected]>"]
edition = "2021"
description = "Actuator package"
Expand Down
50 changes: 31 additions & 19 deletions actuator/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@ impl PyRobstrideMotorsSupervisor {
.into_iter()
.map(|(k, v)| (k, RobstrideMotorControlParams::from(v)))
.collect();
self.inner.set_all_params(params);
Ok(())
self.inner
.set_all_params(params)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}

fn set_params(&self, motor_id: u8, params: PyRobstrideMotorControlParams) -> PyResult<()> {
Expand Down Expand Up @@ -384,26 +385,29 @@ impl PyRobstrideMotorsSupervisor {
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}

fn get_latest_feedback(&self) -> HashMap<u8, PyRobstrideMotorFeedback> {
self.inner
fn get_latest_feedback(&self) -> PyResult<HashMap<u8, PyRobstrideMotorFeedback>> {
Ok(self.inner
.get_latest_feedback()
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?
.into_iter()
.map(|(k, v)| (k, v.into()))
.collect()
.collect())
}

fn toggle_pause(&self) -> PyResult<()> {
self.inner.toggle_pause();
Ok(())
self.inner
.toggle_pause()
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}

fn stop(&self) -> PyResult<()> {
self.inner.stop();
Ok(())
self.inner
.stop()
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}

fn __repr__(&self) -> PyResult<String> {
let motor_count = self.inner.get_latest_feedback().len();
let motor_count = self.inner.get_latest_feedback().map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?.len();
Ok(format!(
"PyRobstrideMotorsSupervisor(motor_count={})",
motor_count
Expand All @@ -412,7 +416,7 @@ impl PyRobstrideMotorsSupervisor {

#[getter]
fn total_commands(&self) -> PyResult<u64> {
Ok(self.inner.get_total_commands())
self.inner.get_total_commands().map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}

fn failed_commands_for(&self, motor_id: u8) -> PyResult<u64> {
Expand All @@ -422,32 +426,40 @@ impl PyRobstrideMotorsSupervisor {
}

fn reset_command_counters(&self) -> PyResult<()> {
self.inner.reset_command_counters();
Ok(())
self.inner
.reset_command_counters()
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}

fn is_running(&self) -> PyResult<bool> {
Ok(self.inner.is_running())
self.inner
.is_running()
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}

#[setter]
fn max_update_rate(&self, rate: f64) -> PyResult<()> {
self.inner.set_max_update_rate(rate);
Ok(())
self.inner
.set_max_update_rate(rate)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}

#[getter]
fn actual_update_rate(&self) -> PyResult<f64> {
Ok(self.inner.get_actual_update_rate())
self.inner
.get_actual_update_rate()
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}

fn toggle_serial(&self) -> PyResult<bool> {
Ok(self.inner.toggle_serial())
self.inner
.toggle_serial()
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}

#[getter]
fn serial(&self) -> PyResult<bool> {
Ok(self.inner.get_serial())
self.inner.get_serial().map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))
}
}

Expand Down
1 change: 1 addition & 0 deletions actuator/robstride/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ log = "^0.4.22"
serialport = { version = "^4.2.0", optional = true }
clap = { version = "4.3", features = ["derive"] }
serde = { version = "^1.0", features = ["derive"] }
eyre = "^0.6"

[target.'cfg(any(target_os = "macos", target_os = "linux"))'.dependencies]
serialport = "^4.2.0"
Expand Down
12 changes: 6 additions & 6 deletions actuator/robstride/src/bin/multisupervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn sinusoid(

let start = Instant::now();
let mut last_second = start;
controller.reset_command_counters();
controller.reset_command_counters()?;

while start.elapsed() < duration {
let t = start.elapsed().as_secs_f32();
Expand All @@ -53,7 +53,7 @@ fn sinusoid(
if last_second.elapsed() > Duration::from_secs(1) {
println!(
"Commands per second: {}",
total_commands as f32 / start.elapsed().as_secs_f32()
total_commands.unwrap_or(0) as f32 / start.elapsed().as_secs_f32()
);
last_second = Instant::now();
}
Expand Down Expand Up @@ -231,21 +231,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Added motors {:?} to zero list", test_ids);
}
"get_feedback" | "g" => {
let feedback = controller.get_latest_feedback();
let feedback = controller.get_latest_feedback()?;
for (id, fb) in feedback {
println!("Motor {}: {:?}", id, fb);
}
}
"pause" | "w" => {
controller.toggle_pause();
controller.toggle_pause()?;
println!("Toggled pause state");
}
"reset" | "r" => {
controller.reset();
controller.reset()?;
println!("Reset motors");
}
"quit" | "q" => {
controller.stop();
controller.stop()?;
println!("Exiting...");
break;
}
Expand Down
12 changes: 6 additions & 6 deletions actuator/robstride/src/bin/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn sinusoid(

let start = Instant::now();
let mut last_second = start;
controller.reset_command_counters();
controller.reset_command_counters()?;

while start.elapsed() < duration {
let t = start.elapsed().as_secs_f32();
Expand All @@ -44,7 +44,7 @@ fn sinusoid(
if last_second.elapsed() > Duration::from_secs(1) {
println!(
"Commands per second: {}",
total_commands as f32 / start.elapsed().as_secs_f32()
total_commands.unwrap_or(0) as f32 / start.elapsed().as_secs_f32()
);
last_second = Instant::now();
}
Expand Down Expand Up @@ -181,21 +181,21 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Added motor {} to zero list", test_id);
}
"get_feedback" | "g" => {
let feedback = controller.get_latest_feedback();
let feedback = controller.get_latest_feedback()?;
for (id, fb) in feedback {
println!("Motor {}: {:?}", id, fb);
}
}
"pause" | "w" => {
controller.toggle_pause();
controller.toggle_pause()?;
println!("Toggled pause state");
}
"reset" | "r" => {
controller.reset();
controller.reset()?;
println!("Reset motors");
}
"quit" | "q" => {
controller.stop();
controller.stop()?;
println!("Exiting...");
break;
}
Expand Down
9 changes: 5 additions & 4 deletions actuator/robstride/src/motor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,11 @@ impl Motors {
"Failed to get the current mode",
));
}

let single_read_mode = read_mode.values().next().unwrap().clone();
if read_mode.values().all(|&x| x == single_read_mode) {
self.mode = single_read_mode;
if let Some(first_mode) = read_mode.values().next() {
let single_read_mode = first_mode.clone();
if read_mode.values().all(|&x| x == single_read_mode) {
self.mode = single_read_mode;
}
}
}

Expand Down
Loading

0 comments on commit a24ae71

Please sign in to comment.