Skip to content

Commit 3514a3e

Browse files
mahony fusion verified working
1 parent f15dbe9 commit 3514a3e

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

main/biodyn_constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define BIODYN_HARDWARE_VERSION "v0"
1212
#define BIODYN_MODEL_NUMBER "0001"
1313
#define BIODYN_SERIAL_NUMBER "000T"
14-
#define BIODYN_FIRMWARE_VERSION "0.0.6"
14+
#define BIODYN_FIRMWARE_VERSION "0.0.7"
1515
#define BIODYN_SYSTEM_ID "Alpha"
1616

1717
// LOG TAGS

main/system/data_fast.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,24 @@ typedef struct
2727

2828
static 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

5461
static 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

Comments
 (0)