Skip to content

Commit 26b8e62

Browse files
committed
pbio/drivebase: Use gyro for speed state.
While we have to use the motor speed estimate to get stable motor motion, we can still use the measured gyro rate for things like stall detection. This improves accuracy of stall detection when the robot is lifted in the air. This is not a typical operation that happens in the field, but people often do this to test that the gyro is "working", so we should make sure to produce intuitive behavior. Partially fixes pybricks/support#1032
1 parent a34d7cb commit 26b8e62

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

lib/pbio/include/pbio/imu.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ float pbio_imu_get_heading(void);
4444

4545
void pbio_imu_set_heading(float desired_heading);
4646

47-
void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t ctl_steps_per_degree);
47+
void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t *heading_rate, int32_t ctl_steps_per_degree);
4848

4949
#else // PBIO_CONFIG_IMU
5050

@@ -79,7 +79,7 @@ static inline float pbio_imu_get_heading(void) {
7979
static inline void pbio_imu_set_heading(float desired_heading) {
8080
}
8181

82-
static inline void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t ctl_steps_per_degree) {
82+
static inline void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t *heading_rate, int32_t ctl_steps_per_degree) {
8383
}
8484

8585
#endif // PBIO_CONFIG_IMU

lib/pbio/src/drivebase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ static pbio_error_t pbio_drivebase_get_state_control(pbio_drivebase_t *db, pbio_
147147

148148
// Optionally use gyro to override the heading source for more accuracy.
149149
if (db->use_gyro) {
150-
pbio_imu_get_heading_scaled(&state_heading->position, db->control_heading.settings.ctl_steps_per_app_step);
150+
pbio_imu_get_heading_scaled(&state_heading->position, &state_heading->speed, db->control_heading.settings.ctl_steps_per_app_step);
151151
}
152152

153153
return PBIO_SUCCESS;

lib/pbio/src/imu.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ static float heading_offset = 0;
212212
/**
213213
* Reads the estimated IMU heading in degrees, accounting for user offset.
214214
*
215+
* Heading is defined as clockwise positive.
216+
*
215217
* @return Heading angle in the base frame.
216218
*/
217219
float pbio_imu_get_heading(void) {
@@ -242,10 +244,13 @@ void pbio_imu_set_heading(float desired_heading) {
242244
* drivebase, which measures heading as the half the difference of the two
243245
* motor positions in millidegrees.
244246
*
245-
* @param [out] heading The output angle object.
247+
* Heading is defined as clockwise positive.
248+
*
249+
* @param [out] heading The heading angle in control units.
250+
* @param [out] heading_rate The heading rate in control units.
246251
* @param [in] ctl_steps_per_degree The number of control steps per heading degree.
247252
*/
248-
void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t ctl_steps_per_degree) {
253+
void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t *heading_rate, int32_t ctl_steps_per_degree) {
249254

250255
// Heading in degrees of the robot.
251256
float heading_degrees = pbio_imu_get_heading();
@@ -256,6 +261,11 @@ void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t ctl_steps_per_de
256261
// The truncated part represents everything else.
257262
float truncated = heading_degrees - heading->rotations * (360000 / ctl_steps_per_degree);
258263
heading->millidegrees = truncated * ctl_steps_per_degree;
264+
265+
// The heading rate can be obtained by a simple scale because it always fits.
266+
pbio_geometry_xyz_t angular_rate;
267+
pbio_imu_get_angular_velocity(&angular_rate);
268+
*heading_rate = (int32_t)(-angular_rate.z * ctl_steps_per_degree);
259269
}
260270

261271
#endif // PBIO_CONFIG_IMU

0 commit comments

Comments
 (0)