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

make kd non-negative #25

Merged
merged 1 commit into from
Oct 12, 2024
Merged
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
54 changes: 42 additions & 12 deletions actuator/rust/robstride/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,9 +1111,15 @@ impl MotorsSupervisor {

pub fn get_position(&self, motor_id: u8) -> Result<f32, std::io::Error> {
let target_params = self.target_params.lock().unwrap();
target_params.get(&motor_id)
target_params
.get(&motor_id)
.map(|params| params.position)
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, format!("Motor ID {} not found", motor_id)))
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Motor ID {} not found", motor_id),
)
})
}

pub fn set_velocity(&self, motor_id: u8, velocity: f32) {
Expand All @@ -1125,37 +1131,55 @@ impl MotorsSupervisor {

pub fn get_velocity(&self, motor_id: u8) -> Result<f32, std::io::Error> {
let target_params = self.target_params.lock().unwrap();
target_params.get(&motor_id)
target_params
.get(&motor_id)
.map(|params| params.velocity)
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, format!("Motor ID {} not found", motor_id)))
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Motor ID {} not found", motor_id),
)
})
}

pub fn set_kp(&self, motor_id: u8, kp: f32) {
let mut target_params = self.target_params.lock().unwrap();
if let Some(params) = target_params.get_mut(&motor_id) {
params.kp = kp;
params.kp = kp.clamp(0.0, kp); // Clamp kp to be non-negative.
}
}

pub fn get_kp(&self, motor_id: u8) -> Result<f32, std::io::Error> {
let target_params = self.target_params.lock().unwrap();
target_params.get(&motor_id)
target_params
.get(&motor_id)
.map(|params| params.kp)
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, format!("Motor ID {} not found", motor_id)))
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Motor ID {} not found", motor_id),
)
})
}

pub fn set_kd(&self, motor_id: u8, kd: f32) {
let mut target_params = self.target_params.lock().unwrap();
if let Some(params) = target_params.get_mut(&motor_id) {
params.kd = kd;
params.kd = kd.clamp(0.0, kd); // Clamp kd to be non-negative.
}
}

pub fn get_kd(&self, motor_id: u8) -> Result<f32, std::io::Error> {
let target_params = self.target_params.lock().unwrap();
target_params.get(&motor_id)
target_params
.get(&motor_id)
.map(|params| params.kd)
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, format!("Motor ID {} not found", motor_id)))
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Motor ID {} not found", motor_id),
)
})
}

pub fn set_torque(&self, motor_id: u8, torque: f32) {
Expand All @@ -1167,9 +1191,15 @@ impl MotorsSupervisor {

pub fn get_torque(&self, motor_id: u8) -> Result<f32, std::io::Error> {
let target_params = self.target_params.lock().unwrap();
target_params.get(&motor_id)
target_params
.get(&motor_id)
.map(|params| params.torque)
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, format!("Motor ID {} not found", motor_id)))
.ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("Motor ID {} not found", motor_id),
)
})
}

pub fn add_motor_to_zero(&self, motor_id: u8) {
Expand Down
Loading