33
44#include <stdbool.h>
55#include <string.h>
6+ #include <math.h>
67
78#include <pbdrv/clock.h>
89#include <pbdrv/imu.h>
@@ -133,34 +134,52 @@ bool pbio_imu_is_stationary(void) {
133134 return pbdrv_imu_is_stationary (imu_dev );
134135}
135136
137+ // Measured rotation of the Z axis in the user frame for exactly one rotation
138+ // of the hub. This will be used to adjust the heading value, which is slightly
139+ // different for each hub.
140+ static float heading_degrees_per_rotation = 360.0f ;
141+
136142/**
137- * Sets the thresholds that define when the hub is stationary. When the
138- * measurements are steadily below these levels, the orientation module
139- * automatically recalibrates.
140- *
141- * If a value is negative, it is ignored.
143+ * Sets the IMU settings. This includes the thresholds that define when the hub
144+ * is stationary. When the measurements are steadily below these levels, the
145+ * orientation module automatically recalibrates. Also includes the hub-specific
146+ * correction value to get a more accurate heading value.
147+ *
148+ * If a value is nan, it is ignored.
142149 *
143- * @param [in] angular_velocity Angular velocity threshold in deg/s.
144- * @param [in] acceleration Acceleration threshold in mm/s^2
150+ * @param [in] angular_velocity Angular velocity threshold in deg/s.
151+ * @param [in] acceleration Acceleration threshold in mm/s^2
152+ * @param [in] heading_correction Measured degrees per full rotation of the hub.
153+ * @returns ::PBIO_ERROR_INVALID_ARG if the heading correction is out of range,
154+ * otherwise ::PBIO_SUCCESS.
145155 */
146- void pbio_imu_set_stationary_thresholds (float angular_velocity , float acceleration ) {
147- if (angular_velocity >= 0 ) {
156+ pbio_error_t pbio_imu_set_settings (float angular_velocity , float acceleration , float heading_correction ) {
157+ if (! isnan ( angular_velocity ) ) {
148158 imu_config -> gyro_stationary_threshold = pbio_int_math_bind (angular_velocity / imu_config -> gyro_scale , 1 , INT16_MAX );
149159 }
150- if (acceleration >= 0 ) {
160+ if (! isnan ( acceleration ) ) {
151161 imu_config -> accel_stationary_threshold = pbio_int_math_bind (acceleration / imu_config -> accel_scale , 1 , INT16_MAX );
152162 }
163+ if (!isnan (heading_correction )) {
164+ if (heading_correction < 350 || heading_correction > 370 ) {
165+ return PBIO_ERROR_INVALID_ARG ;
166+ }
167+ heading_degrees_per_rotation = heading_correction ;
168+ }
169+ return PBIO_SUCCESS ;
153170}
154171
155172/**
156173 * Gets the thresholds that define when the hub is stationary.
157174 *
158- * @param [out] angular_velocity Angular velocity threshold in deg/s.
159- * @param [out] acceleration Acceleration threshold in mm/s^2
175+ * @param [out] angular_velocity Angular velocity threshold in deg/s.
176+ * @param [out] acceleration Acceleration threshold in mm/s^2
177+ * @param [out] heading_correction Measured degrees per full rotation of the hub.
160178 */
161- void pbio_imu_get_stationary_thresholds (float * angular_velocity , float * acceleration ) {
179+ void pbio_imu_get_settings (float * angular_velocity , float * acceleration , float * heading_correction ) {
162180 * angular_velocity = imu_config -> gyro_stationary_threshold * imu_config -> gyro_scale ;
163181 * acceleration = imu_config -> accel_stationary_threshold * imu_config -> accel_scale ;
182+ * heading_correction = heading_degrees_per_rotation ;
164183}
165184
166185/**
@@ -216,7 +235,8 @@ pbio_geometry_side_t pbio_imu_get_up_side(void) {
216235static float heading_offset = 0 ;
217236
218237/**
219- * Reads the estimated IMU heading in degrees, accounting for user offset.
238+ * Reads the estimated IMU heading in degrees, accounting for user offset and
239+ * user-specified heading correction scaling constant.
220240 *
221241 * Heading is defined as clockwise positive.
222242 *
@@ -228,7 +248,7 @@ float pbio_imu_get_heading(void) {
228248
229249 pbio_geometry_vector_map (& pbio_orientation_base_orientation , & single_axis_rotation , & heading_mapped );
230250
231- return - heading_mapped .z - heading_offset ;
251+ return - heading_mapped .z * 360.0f / heading_degrees_per_rotation - heading_offset ;
232252}
233253
234254/**
0 commit comments