Skip to content

Commit 331d5f8

Browse files
Fix datafast and bump version
1 parent 4153730 commit 331d5f8

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
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.7"
14+
#define BIODYN_FIRMWARE_VERSION "0.0.8"
1515
#define BIODYN_SYSTEM_ID "Alpha"
1616

1717
// LOG TAGS

main/system/data_fast.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static struct
3030
// Read task things
3131
gptimer_handle_t read_timer;
3232
TaskHandle_t read_task;
33+
int read_hz;
3334

3435
// Error things
3536
char ext_err_msg[128];
@@ -41,18 +42,23 @@ static struct
4142
float3 gyro_bias;
4243
float ki;
4344
float kp;
45+
46+
// Track datapoints since last ble read
47+
int datapoints_since_ble_read;
4448
} data_fast = {
4549
.data_cnt = IMU_DATA_CNT,
4650
.data_ptr = 0,
4751
.read_task = NULL,
4852
.read_timer = NULL,
4953
.data_mutex = NULL,
5054
.ext_err = BIODYN_DATAFAST_OK,
51-
.max_read_delay_before_err = 15,
55+
.max_read_delay_before_err = 10,
5256
.gyro_bias = {0, 0, 0},
5357
.curr_orientation = {1, 0, 0, 0},
54-
.ki = 0.1f,
55-
.kp = 2.0f,
58+
.ki = 0.1f, // Mahony integral gain
59+
.kp = 2.0f, // Mahony proportional gain
60+
.read_hz = 100, // Target read hz
61+
.datapoints_since_ble_read = 0,
5662
};
5763

5864
static esp_err_t collect_err(biodyn_timesync_err_t err, const char *msg, esp_err_t code);
@@ -142,9 +148,9 @@ esp_err_t biodyn_data_fast_init()
142148

143149
// Configure alarm
144150
gptimer_alarm_config_t alarm_config = {
145-
.reload_count = 0, // Restart at 0
146-
.alarm_count = 7000, // 7 ms at 1 MHz
147-
.flags.auto_reload_on_alarm = true, // Auto reload on alarm
151+
.reload_count = 0, // Restart at 0
152+
.alarm_count = 1000000 / data_fast.read_hz, // Alarm every 1/read_hz seconds
153+
.flags.auto_reload_on_alarm = true, // Auto reload on alarm
148154
};
149155
if ((res = gptimer_set_alarm_action(data_fast.read_timer, &alarm_config)) != ESP_OK)
150156
return collect_err(BIODYN_DATAFAST_COULDNT_CREATE_TIMER, "Could not set gptimer alarm config: code", res);
@@ -203,6 +209,7 @@ void data_fast_read()
203209
datapoint->emg = 0;
204210
datapoint->orientation = mahony_fusion(&data, data_fast.curr_orientation, elapsed);
205211
data_fast.curr_orientation = datapoint->orientation;
212+
++data_fast.datapoints_since_ble_read;
206213

207214
xSemaphoreGive(data_fast.data_mutex); // Give mutex
208215
}
@@ -214,14 +221,17 @@ void ble_data_fast_packed(uint16_t *size, void *out)
214221
// Take mutex
215222
if (xSemaphoreTake(data_fast.data_mutex, portMAX_DELAY))
216223
{
217-
int current_size = data_fast.data_ptr;
218-
int old_size = data_fast.data_cnt - data_fast.data_ptr;
219-
timed_read *p = &data_fast.raw_data[0];
220-
memcpy(out + (old_size * sizeof(timed_read)), p, current_size * sizeof(timed_read));
221-
memcpy(out, p, old_size * sizeof(timed_read));
222-
*size = IMU_DATA_CNT * sizeof(timed_read);
223-
// timed_read *latest = &((timed_read *)out)[IMU_DATA_CNT - 1];
224-
// ESP_LOGI(TAG, "x: %.2f, y: %.2f, z: %.2f at %lld", latest->data.accel_x, latest->data.accel_y, latest->data.accel_z, latest->ticker);
224+
int new_size = data_fast.data_ptr + 1;
225+
int old_size = data_fast.data_cnt - new_size;
226+
// New data is 0..data_ptr
227+
// Old data is data_ptr..data_cnt
228+
timed_read *old = &data_fast.raw_data[(data_fast.data_ptr + 1) % data_fast.data_cnt];
229+
timed_read *new = &data_fast.raw_data[0];
230+
memcpy(out, old, sizeof(timed_read) * old_size);
231+
memcpy(out + sizeof(timed_read) * old_size, new, sizeof(timed_read) * new_size);
232+
*size = sizeof(timed_read) * data_fast.data_cnt;
233+
data_fast.datapoints_since_ble_read = 0;
234+
225235
xSemaphoreGive(data_fast.data_mutex); // Give mutex
226236
}
227237
}

0 commit comments

Comments
 (0)