Skip to content

Commit

Permalink
WIP: IMU Semaphore
Browse files Browse the repository at this point in the history
  • Loading branch information
lukash committed Aug 4, 2024
1 parent cca24e5 commit c674e56
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
(define erpm (ext-dbg 7))
(define current (ext-dbg 8))
(define atr_filtered_current (ext-dbg 9))
(define imu_updates_missed (ext-dbg 10))
(sleep 0.1)
)))
56 changes: 51 additions & 5 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ typedef struct {
lib_thread main_thread;
lib_thread led_thread;

lib_semaphore imu_semaphore;
volatile bool should_terminate;
uint32_t imu_updates_missed;

RefloatConfig float_conf;

// Firmware version, passed in from Lisp
Expand Down Expand Up @@ -140,6 +144,7 @@ typedef struct {
float noseangling_interpolated, inputtilt_interpolated;
float turntilt_target, turntilt_interpolated;
float current_time;
systime_t last_ticks;
float disengage_timer, nag_timer;
float idle_voltage;
float fault_angle_pitch_timer, fault_angle_roll_timer, fault_switch_timer,
Expand Down Expand Up @@ -1066,14 +1071,31 @@ static void imu_ref_callback(float *acc, float *gyro, float *mag, float dt) {

data *d = (data *) ARG;
balance_filter_update(&d->balance_filter, gyro, acc, dt);

if (d->imu_semaphore) {
VESC_IF->sem_signal(d->imu_semaphore);
}
}

static void refloat_thd(void *arg) {
data *d = (data *) arg;

configure(d);

while (!VESC_IF->should_terminate()) {
while (!d->should_terminate) {
if (d->imu_semaphore) {
if (!VESC_IF->sem_wait_to(d->imu_semaphore, SYSTEM_TICK_RATE_HZ / 10)) {
d->imu_updates_missed += 10000;
continue;
}

systime_t ticks = VESC_IF->system_time_ticks();
if (ticks - d->last_ticks < 3) {
d->imu_updates_missed++;
continue;
}
d->last_ticks = ticks;
}

beeper_update(d);

charging_timeout(&d->charging, &d->state);
Expand Down Expand Up @@ -1510,7 +1532,9 @@ static void refloat_thd(void *arg) {
break;
}

VESC_IF->sleep_us(d->loop_time_us);
if (!d->imu_semaphore) {
VESC_IF->sleep_us(d->loop_time_us);
}
}
}

Expand Down Expand Up @@ -1624,6 +1648,8 @@ static float app_get_debug(int index) {
return d->motor.current;
case (9):
return d->motor.atr_filtered_current;
case (10):
return d->imu_updates_missed;
default:
return 0;
}
Expand Down Expand Up @@ -2379,7 +2405,7 @@ static void send_realtime_data2(data *d) {

buffer_append_float32_auto(buffer, d->footpad_sensor.adc1, &ind);
buffer_append_float32_auto(buffer, d->footpad_sensor.adc2, &ind);
buffer_append_float32_auto(buffer, d->throttle_val, &ind);
buffer_append_uint32(buffer, d->imu_updates_missed, &ind);

if (d->state.state == STATE_RUNNING) {
// Setpoints
Expand Down Expand Up @@ -2678,6 +2704,15 @@ static void stop(void *arg) {
VESC_IF->imu_set_read_callback(NULL);
VESC_IF->set_app_data_handler(NULL);
VESC_IF->conf_custom_clear_configs();

d->should_terminate = true;

if (d->imu_semaphore) {
// Most of the time this reset will happen while the thread is waiting on semaphore
// but there is a small race happening which is taken care of by the semaphore timeout.
VESC_IF->sem_reset(d->imu_semaphore);
}

if (d->led_thread) {
VESC_IF->request_terminate(d->led_thread);
}
Expand All @@ -2686,6 +2721,10 @@ static void stop(void *arg) {
}
log_msg("Terminating.");
leds_destroy(&d->leds);

if (d->imu_semaphore) {
VESC_IF->free(d->imu_semaphore);
}
VESC_IF->free(d);
}

Expand All @@ -2702,6 +2741,12 @@ INIT_FUN(lib_info *info) {

info->stop_fun = stop;
info->arg = d;
if (VESC_IF->sem_create) {
log_msg("IMU Semaphore mode active.");
d->imu_semaphore = VESC_IF->sem_create();
} else {
log_msg("Loop Hertz mode active.");
}

VESC_IF->conf_custom_add_config(get_cfg, set_cfg, get_cfg_xml);

Expand All @@ -2711,7 +2756,6 @@ INIT_FUN(lib_info *info) {
}

balance_filter_init(&d->balance_filter);
VESC_IF->imu_set_read_callback(imu_ref_callback);

footpad_sensor_update(&d->footpad_sensor, &d->float_conf);

Expand All @@ -2733,6 +2777,8 @@ INIT_FUN(lib_info *info) {
}
}

VESC_IF->imu_set_read_callback(imu_ref_callback);

VESC_IF->set_app_data_handler(on_command_received);
VESC_IF->lbm_add_extension("ext-dbg", ext_dbg);
VESC_IF->lbm_add_extension("ext-set-fw-version", ext_set_fw_version);
Expand Down
3 changes: 2 additions & 1 deletion ui.qml.in
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,8 @@ Item {

state.adc1Voltage = dv.getFloat32(ind); ind += 4;
state.adc2Voltage = dv.getFloat32(ind); ind += 4;
debugRemoteInput.value = Math.round(dv.getFloat32(ind) * 100); ind += 4;
//debugRemoteInput.value = Math.round(dv.getFloat32(ind) * 100); ind += 4;
debugRemoteInput.value = dv.getUint32(ind); ind += 4;

if (hasRuntime) {
pitch.setpointValue = dv.getFloat32(ind); ind += 4;
Expand Down

0 comments on commit c674e56

Please sign in to comment.