@@ -27,19 +27,24 @@ typedef struct
2727
2828static struct
2929{
30+ // Rolling data storage
3031 timed_read raw_data [IMU_DATA_CNT ];
3132 SemaphoreHandle_t data_mutex ;
3233 int data_cnt ;
3334 int data_ptr ;
3435
3536 TaskHandle_t read_task ;
3637
38+ // Error things
3739 char ext_err_msg [128 ];
3840 biodyn_df_err_t ext_err ;
3941 double max_read_delay_before_err ;
4042
43+ // Mahony fusion things
4144 float3 gyro_bias ;
4245 quaternion curr_orientation ;
46+ float ki ;
47+ float kp ;
4348} data_fast = {
4449 .data_cnt = IMU_DATA_CNT ,
4550 .data_ptr = 0 ,
@@ -49,6 +54,8 @@ static struct
4954 .max_read_delay_before_err = 15 ,
5055 .gyro_bias = {0 , 0 , 0 },
5156 .curr_orientation = {1 , 0 , 0 , 0 },
57+ .ki = 0.0f ,
58+ .kp = 2.0f ,
5259};
5360
5461static esp_err_t collect_err (biodyn_timesync_err_t err , const char * msg , esp_err_t code );
@@ -145,6 +152,7 @@ void data_fast_read()
145152 datapoint -> data = data ;
146153 datapoint -> emg = 0 ;
147154 datapoint -> orientation = mahony_fusion (& data , data_fast .curr_orientation , elapsed );
155+ data_fast .curr_orientation = datapoint -> orientation ;
148156
149157 xSemaphoreGive (data_fast .data_mutex ); // Give mutex
150158 }
@@ -188,20 +196,20 @@ static quaternion mahony_fusion(const imu_motion_data *data, quaternion q, float
188196 float3 error = cross_f3 (& accel , & gravity );
189197
190198 // Keep internal bias
191- float ki = 0 ;
199+ float ki = data_fast . ki ;
192200 data_fast .gyro_bias .x += ki * error .x * dt_s ;
193201 data_fast .gyro_bias .y += ki * error .y * dt_s ;
194202 data_fast .gyro_bias .z += ki * error .z * dt_s ;
195203
196204 // Adjust gyro with error
197- float kp = 2 ;
205+ float kp = data_fast . kp ;
198206 float3 gyro_corr = {
199207 gyro .x + kp * error .x + data_fast .gyro_bias .x ,
200208 gyro .y + kp * error .y + data_fast .gyro_bias .y ,
201209 gyro .z + kp * error .z + data_fast .gyro_bias .z };
202210
203211 // Don't actually correct if accel is too much (>1g)
204- if (len_f3 (& accel ) > 1.2f )
212+ if (len_f3 (& data -> accel ) > 11.f )
205213 gyro_corr = gyro ;
206214
207215 // Update orientation quaternion, integrating current gyro
0 commit comments