Skip to content

Commit 900ae17

Browse files
committed
pbio/imu: Fix internal rounding error from imu to motor control units.
The drivebase controller operates in terms of millidegrees of the wheels. When the gyro is used for heading, it is scaled to these wheel units. Depending on the robot dimensions, this scaling could be off by one or two degrees per rotation due to a rounding error. Fixes pybricks/support#1886
1 parent e2b6a8e commit 900ae17

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

lib/pbio/src/imu.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,11 +288,12 @@ void pbio_imu_get_heading_scaled(pbio_angle_t *heading, int32_t *heading_rate, i
288288
float heading_degrees = pbio_imu_get_heading();
289289

290290
// Number of whole rotations in control units (in terms of wheels, not robot).
291-
heading->rotations = heading_degrees / (360000 / ctl_steps_per_degree);
291+
heading->rotations = (int32_t)(heading_degrees / (360000.0f / ctl_steps_per_degree));
292292

293-
// The truncated part represents everything else.
294-
float truncated = heading_degrees - heading->rotations * (360000 / ctl_steps_per_degree);
295-
heading->millidegrees = truncated * ctl_steps_per_degree;
293+
// The truncated part represents everything else. NB: The scaling factor
294+
// is a float here to ensure we don't lose precision while scaling.
295+
float truncated = heading_degrees - heading->rotations * (360000.0f / ctl_steps_per_degree);
296+
heading->millidegrees = (int32_t)(truncated * ctl_steps_per_degree);
296297

297298
// The heading rate can be obtained by a simple scale because it always fits.
298299
pbio_geometry_xyz_t angular_rate;

0 commit comments

Comments
 (0)