Skip to content

Commit f718a89

Browse files
authored
Remove Body Control Loop (#48)
* remove control loop on control board as a test * add battery monitoring
1 parent 485e1df commit f718a89

2 files changed

Lines changed: 25 additions & 42 deletions

File tree

control/drivers/motion/src/motion_control.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ impl MotionControl {
9696
}
9797

9898
/// Convert a body velocity into duty cycles for the robot
99-
pub fn body_to_wheels(&self, body_velocity: Vector3<f32>) -> Vector4<f32> {
100-
self.bot_to_wheel * body_velocity
99+
pub fn body_to_wheels(&self, body_velocity: Vector3<f32>) -> Vector4<i32> {
100+
(self.bot_to_wheel * body_velocity).map(|v| MotionControl::meters_to_ticks(v))
101101
}
102102

103103
/// Converts a wheel's duty cycles into velocity for the robot

control/src/main.rs

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ mod app {
916916
initialized: bool = false,
917917
iteration: u32 = 0,
918918
last_time: u32 = 0
919+
last_body_velocities: Vector3<f32> = Vector3::new(0.0, 0.0, 0.0),
919920
],
920921
priority = 1,
921922
)]
@@ -936,14 +937,6 @@ mod app {
936937
None => (Vector3::new(0.0, 0.0, 0.0), 0),
937938
});
938939

939-
let (gyro, accel_x, accel_y) = ctx.shared.imu.lock(|imu| {
940-
let gyro = imu.gyro_z().unwrap_or(0.0);
941-
let accel_x = imu.accel_x().unwrap_or(0.0);
942-
let accel_y = imu.accel_y().unwrap_or(0.0);
943-
944-
(gyro, accel_x, accel_y)
945-
});
946-
947940
let now = ctx.shared.gpt.lock(|gpt| gpt.count());
948941
let delta = now - *ctx.local.last_time;
949942
*ctx.local.last_time = now;
@@ -958,36 +951,26 @@ mod app {
958951
log::info!("DEAD: {}", elapsed_time);
959952
}
960953

961-
let last_encoders = (
962-
ctx.shared.motor_one_velocity,
963-
ctx.shared.motor_two_velocity,
964-
ctx.shared.motor_three_velocity,
965-
ctx.shared.motor_four_velocity,
966-
)
967-
.lock(|one, two, three, four| Vector4::new(*one, *two, *three, *four));
954+
if body_velocities != *ctx.local.last_body_velocities {
955+
*ctx.local.last_body_velocities = body_velocities;
956+
let wheel_velocities = ctx.local.motion_controller.body_to_wheels(body_velocities);
968957

969-
let wheel_velocities = ctx.local.motion_controller.control_update(
970-
Vector3::new(-accel_y, -accel_x, -gyro),
971-
last_encoders,
972-
body_velocities,
973-
delta,
974-
);
975-
976-
ctx.shared
977-
.dribbler_uart
978-
.lock(|uart| send_command(dribbler_speed as i32, ctx.local.dribbler_tx, uart, 0));
979-
ctx.shared
980-
.motor_one_uart
981-
.lock(|uart| send_command(wheel_velocities[0], ctx.local.motor_one_tx, uart, 0));
982-
ctx.shared
983-
.motor_two_uart
984-
.lock(|uart| send_command(wheel_velocities[1], ctx.local.motor_two_tx, uart, 0));
985-
ctx.shared
986-
.motor_three_uart
987-
.lock(|uart| send_command(wheel_velocities[2], ctx.local.motor_three_tx, uart, 0));
988-
ctx.shared
989-
.motor_four_uart
990-
.lock(|uart| send_command(wheel_velocities[3], ctx.local.motor_four_tx, uart, 0));
958+
ctx.shared
959+
.dribbler_uart
960+
.lock(|uart| send_command(dribbler_speed as i32, ctx.local.dribbler_tx, uart, 0));
961+
ctx.shared
962+
.motor_one_uart
963+
.lock(|uart| send_command(wheel_velocities[0], ctx.local.motor_one_tx, uart, 0));
964+
ctx.shared
965+
.motor_two_uart
966+
.lock(|uart| send_command(wheel_velocities[1], ctx.local.motor_two_tx, uart, 0));
967+
ctx.shared
968+
.motor_three_uart
969+
.lock(|uart| send_command(wheel_velocities[2], ctx.local.motor_three_tx, uart, 0));
970+
ctx.shared
971+
.motor_four_uart
972+
.lock(|uart| send_command(wheel_velocities[3], ctx.local.motor_four_tx, uart, 0));
973+
}
991974

992975
#[cfg(feature = "debug")]
993976
log::info!("Moving at {:?}", wheel_velocities);
@@ -1060,9 +1043,9 @@ mod app {
10601043
});
10611044

10621045
// // Battery is under voltaged so we should die
1063-
// if battery_voltage < MIN_BATTERY_VOLTAGE {
1064-
// kill_self::spawn().ok();
1065-
// }
1046+
if battery_voltage < MIN_BATTERY_VOLTAGE {
1047+
kill_self::spawn().ok();
1048+
}
10661049

10671050
// TODO: Display robot status on display
10681051
}

0 commit comments

Comments
 (0)