core_pb/driving/
motors.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use crate::drive_system::DriveSystem;
use crate::driving::data::SharedRobotData;
use crate::driving::RobotBehavior;
use crate::messages::{
    FrequentServerToRobot, MotorControlStatus, SensorData, Task, VelocityControl,
};
use crate::pure_pursuit::pure_pursuit;
use crate::util::utilization::UtilizationMonitor;
use crate::util::CrossPlatformInstant;
use core::sync::atomic::Ordering;
use core::time::Duration;
#[cfg(feature = "micromath")]
use micromath::F32Ext;
use nalgebra::Vector2;
use pid::Pid;

/// Functionality that robots with motors must support
pub trait RobotMotorsBehavior {
    /// Set PWM for the given pin
    ///
    /// - 0 <= pin < 2*WHEELS
    /// - 0 <= to <= [`robot_definition.pwm_max`]
    async fn set_pwm(&mut self, pin: usize, to: u16);
}

struct MotorsData<const WHEELS: usize, M: RobotMotorsBehavior> {
    motors: M,

    config: FrequentServerToRobot,

    pid_controllers: [Pid<f32>; WHEELS],

    motor_speeds: [f32; 3],
    set_points: [f32; WHEELS],
    pwm: [[u16; 2]; WHEELS],
}

/// The "main" method for the motors task
pub async fn motors_task<R: RobotBehavior>(data: &SharedRobotData<R>, motors: R::Motors) -> ! {
    let status_sender = data.motor_control.sender();
    // Watch for config changes instead of just using .get() to enable detecting when no one is
    // setting new configs, and turn off motors
    let mut config_watch = data.config.receiver().unwrap();

    let robot = &data.robot_definition;
    let config = FrequentServerToRobot::new(data.name);
    let pid = config.pid;

    let pid_controllers = [0; 3].map(|_| {
        let mut pid_controller = Pid::new(0.0, robot.pwm_top as f32);
        pid_controller
            .p(pid[0], robot.pwm_top as f32)
            .i(pid[1], robot.pwm_top as f32)
            .d(pid[2], robot.pwm_top as f32);
        pid_controller
    });

    let mut motors_data = MotorsData {
        config,

        motors,
        pid_controllers,

        motor_speeds: [0.0; 3],
        set_points: Default::default(),
        pwm: Default::default(),
    };

    let mut last_command = R::Instant::default();

    let mut utilization_monitor: UtilizationMonitor<50, R::Instant> =
        UtilizationMonitor::new(0.0, 0.0);
    utilization_monitor.start();

    let mut cv_over_time = motors_data.config.cv_location;
    let mut cv_over_time_time = R::Instant::default();
    let mut last_motor_speeds = [0, 1, 2].map(|i| data.sig_motor_speeds[i].load(Ordering::Relaxed));

    loop {
        if let Some(config) = config_watch.try_changed() {
            last_command = R::Instant::default();
            if config.cv_location != cv_over_time {
                cv_over_time = config.cv_location;
                cv_over_time_time = R::Instant::default();
            }
            motors_data.config = config;
            for m in 0..3 {
                motors_data.pid_controllers[m]
                    .p(motors_data.config.pid[0], robot.pwm_top as f32)
                    .i(motors_data.config.pid[1], robot.pwm_top as f32)
                    .d(motors_data.config.pid[2], robot.pwm_top as f32);
            }
        }
        if last_command.elapsed() > Duration::from_millis(300) {
            // we might have disconnected, set all motors to stop
            motors_data.config = FrequentServerToRobot::new(data.name);
            motors_data.config.pwm_override = [[Some(0); 2]; 3];
        }
        let new_speeds = [0, 1, 2].map(|i| data.sig_motor_speeds[i].load(Ordering::Relaxed));
        if new_speeds != last_motor_speeds {
            last_motor_speeds = new_speeds;
            for (i, speed) in motors_data.motor_speeds.iter_mut().enumerate() {
                *speed = new_speeds[motors_data.config.encoder_config[i].0]
                    * if motors_data.config.encoder_config[i].1 {
                        -1.0
                    } else {
                        1.0
                    };
            }
        }

        let is_enabled = cv_over_time.is_some()
            && motors_data.config.follow_target_path
            && motors_data.config.target_path.len() > 0;
        if !is_enabled {
            cv_over_time_time = R::Instant::default();
        }
        let stuck = cv_over_time_time.elapsed() > Duration::from_secs(3) && is_enabled;
        // if cv_over_time_time.elapsed() > Duration::from_secs(2) {
        //     cv_over_time_time = R::Instant::default();
        // }
        // data.set_extra_bool_indicator(1, stuck);
        // data.set_extra_i32_indicator(
        //     1,
        //     if !stuck {
        //         0
        //     } else {
        //         cv_over_time_time.elapsed().as_secs() as i32
        //     },
        // );
        motors_data
            .do_motors(
                &data.robot_definition.drive_system,
                &data.sensors.try_get(),
                if !stuck {
                    0
                } else {
                    cv_over_time_time.elapsed().as_secs()
                },
                3.0, // todo make this into an option
                2.0,
                0.05,
                0.0,
            )
            .await;

        status_sender.send(MotorControlStatus {
            pwm: motors_data.pwm,
            measured_speeds: motors_data.motor_speeds,
            speed_set_points: motors_data.set_points,
        });
        data.utilization[Task::Motors as usize]
            .store(utilization_monitor.utilization(), Ordering::Relaxed);

        utilization_monitor.stop();
        R::Instant::sleep(Duration::from_millis(30)).await;
        utilization_monitor.start();
    }
}

fn adjust_ang_vel(curr_ang: f32, desired_ang: f32, p: f32, tol: f32, offset: f32) -> f32 {
    // Calculate the difference between desired angle and current angle
    let mut angle_diff = desired_ang - curr_ang;

    // account for angles that cross discontinuity
    if angle_diff > core::f32::consts::PI {
        angle_diff -= 2.0 * core::f32::consts::PI;
    } else if angle_diff < -core::f32::consts::PI {
        angle_diff += 2.0 * core::f32::consts::PI;
    }

    // clamp if within tol rads
    if angle_diff.abs() < tol {
        0.0
    } else {
        angle_diff * p + offset * angle_diff.signum()
    }
}

impl<M: RobotMotorsBehavior> MotorsData<3, M> {
    pub async fn do_motors(
        &mut self,
        drive_system: &DriveSystem<3>,
        sensors: &Option<SensorData>,
        stuck_time: u64,
        snapping_multiplier: f32,
        angle_p: f32,
        angle_tol: f32, // rad
        angle_snapping_offset: f32,
    ) {
        if self.config.follow_target_path {
            if let Some(sensors) = sensors {
                let mut target_velocity = (Vector2::new(0.0, 0.0), 0.0);
                // maintain heading 0
                if let Ok(angle) = sensors.angle {
                    target_velocity.1 =
                        adjust_ang_vel(angle, 0.0, angle_p, angle_tol, angle_snapping_offset);
                    // let angle = Rotation2::new(angle).angle();
                    // if angle.abs() < 20.0_f32.to_radians() {
                    // now that we've made sure we're facing the right way, try to follow the path
                    if let Some(vel) = pure_pursuit(
                        sensors,
                        &self.config.target_path,
                        if stuck_time > 2 {
                            self.config.lookahead_dist * 0.1
                        } else {
                            self.config.lookahead_dist
                        },
                        self.config.robot_speed,
                        self.config.snapping_dist,
                        snapping_multiplier,
                        self.config.cv_location,
                    ) {
                        target_velocity.0 = vel;
                        if stuck_time % 6 > 3 {
                            // let phase = stuck_time % 4;
                            // let speed = 2.5;
                            // let angle = (180.0 as f32).to_radians();
                            // let x_speed = angle.cos() * speed;
                            // let y_speed = angle.sin() * speed;
                            // target_velocity.0 = Vector2::new(x_speed, y_speed);
                            target_velocity.0 = -target_velocity.0;
                            target_velocity.1 = -target_velocity.1;
                        }
                    }
                    // }
                }
                // calculate wheel velocities
                self.config.target_velocity =
                    VelocityControl::LinVelAngVel(target_velocity.0, target_velocity.1);
            }
        }

        self.set_points = [0.0; 3];
        self.pwm = [[0; 2]; 3];
        if let Some((mut lin, ang)) = match self.config.target_velocity {
            VelocityControl::None | VelocityControl::AssistedDriving(_) => None,
            VelocityControl::Stop => Some((Vector2::new(0.0, 0.0), 0.0)),
            VelocityControl::LinVelAngVel(lin, ang) => Some((lin, ang)),
            VelocityControl::LinVelFixedAng(lin, set_ang) => sensors
                .as_ref()
                .and_then(|s| s.angle.clone().ok())
                .map(|cur_ang| {
                    (
                        lin,
                        adjust_ang_vel(cur_ang, set_ang, angle_p, angle_tol, angle_snapping_offset),
                    )
                }),
            VelocityControl::LinVelFaceForward(lin) => sensors
                .as_ref()
                .and_then(|s| s.angle.clone().ok())
                .map(|cur_ang| {
                    (
                        lin,
                        if lin.magnitude() < 0.01 {
                            0.0
                        } else {
                            adjust_ang_vel(
                                cur_ang,
                                f32::atan2(lin.y, lin.x),
                                angle_p,
                                angle_tol,
                                angle_snapping_offset,
                            )
                        },
                    )
                }),
        } {
            if let Some(SensorData {
                angle: Ok(angle), ..
            }) = &sensors
            {
                lin = Vector2::new(
                    lin.x * (-angle).cos() - lin.y * (-angle).sin(),
                    lin.x * (-angle).sin() + lin.y * (-angle).cos(),
                );
            }
            // let lin_x = lin.x *
            self.set_points = drive_system.get_motor_speed_omni(lin, ang);
        }
        #[allow(clippy::needless_range_loop)]
        for m in 0..3 {
            if let Some(motor_override) = self.config.motors_override[m] {
                self.set_points[m] = motor_override;
            }
            // calculate pid
            self.pid_controllers[m].setpoint(self.set_points[m]);
            let output = if self.set_points[m] == 0.0 {
                self.pid_controllers[m].reset_integral_term();
                0.0
            } else {
                self.pid_controllers[m]
                    .next_control_output(self.motor_speeds[m])
                    .output
            };

            // set value to PWM on motors
            if output > 0.0 {
                self.pwm[m] = [output.abs().round() as u16, 0];
            } else {
                self.pwm[m] = [0, output.abs().round() as u16];
            }
            for p in 0..2 {
                if let Some(pwm_override) = self.config.pwm_override[m][p] {
                    self.pwm[m][p] = pwm_override;
                }
                self.motors
                    .set_pwm(self.config.motor_config[m][p], self.pwm[m][p])
                    .await;
            }
        }
    }
}