Skip to content

Commit

Permalink
pbio/drivebase: Use gyro for speed state.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
laurensvalk committed Apr 20, 2023
1 parent a34d7cb commit 26b8e62
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/pbio/include/pbio/imu.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ float pbio_imu_get_heading(void);

void pbio_imu_set_heading(float desired_heading);

void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t ctl_steps_per_degree);
void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t *heading_rate, int32_t ctl_steps_per_degree);

#else // PBIO_CONFIG_IMU

Expand Down Expand Up @@ -79,7 +79,7 @@ static inline float pbio_imu_get_heading(void) {
static inline void pbio_imu_set_heading(float desired_heading) {
}

static inline void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t ctl_steps_per_degree) {
static inline void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t *heading_rate, int32_t ctl_steps_per_degree) {
}

#endif // PBIO_CONFIG_IMU
Expand Down
2 changes: 1 addition & 1 deletion lib/pbio/src/drivebase.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ static pbio_error_t pbio_drivebase_get_state_control(pbio_drivebase_t *db, pbio_

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

return PBIO_SUCCESS;
Expand Down
14 changes: 12 additions & 2 deletions lib/pbio/src/imu.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ static float heading_offset = 0;
/**
* Reads the estimated IMU heading in degrees, accounting for user offset.
*
* Heading is defined as clockwise positive.
*
* @return Heading angle in the base frame.
*/
float pbio_imu_get_heading(void) {
Expand Down Expand Up @@ -242,10 +244,13 @@ void pbio_imu_set_heading(float desired_heading) {
* drivebase, which measures heading as the half the difference of the two
* motor positions in millidegrees.
*
* @param [out] heading The output angle object.
* Heading is defined as clockwise positive.
*
* @param [out] heading The heading angle in control units.
* @param [out] heading_rate The heading rate in control units.
* @param [in] ctl_steps_per_degree The number of control steps per heading degree.
*/
void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t ctl_steps_per_degree) {
void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t *heading_rate, int32_t ctl_steps_per_degree) {

// Heading in degrees of the robot.
float heading_degrees = pbio_imu_get_heading();
Expand All @@ -256,6 +261,11 @@ void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t ctl_steps_per_de
// The truncated part represents everything else.
float truncated = heading_degrees - heading->rotations * (360000 / ctl_steps_per_degree);
heading->millidegrees = truncated * ctl_steps_per_degree;

// The heading rate can be obtained by a simple scale because it always fits.
pbio_geometry_xyz_t angular_rate;
pbio_imu_get_angular_velocity(&angular_rate);
*heading_rate = (int32_t)(-angular_rate.z * ctl_steps_per_degree);
}

#endif // PBIO_CONFIG_IMU

0 comments on commit 26b8e62

Please sign in to comment.