Skip to content

Commit

Permalink
use gyro to determine desired looptime
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Oct 29, 2023
1 parent e59b676 commit 375c505
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 60 deletions.
22 changes: 11 additions & 11 deletions src/core/looptime.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "core/looptime.h"

#include "core/project.h"
#include "driver/spi_gyro.h"
#include "driver/time.h"
#include "flight/control.h"

Expand All @@ -9,9 +10,12 @@ uint8_t looptime_warning = 0;
static uint32_t last_loop_cycles;

void looptime_init() {
// attempt 8k looptime for f405 or 4k looptime for f411
state.looptime = LOOPTIME * 1e-6;
state.looptime_autodetect = LOOPTIME;
float target = gyro_update_period();
while (target < LOOPTIME_MAX)
target *= 2.0f;
state.looptime = target * 1e-6f;
state.looptime_us = target;
state.looptime_autodetect = target;

looptime_reset();
}
Expand All @@ -37,15 +41,11 @@ static void looptime_auto_detect() {

if (loop_counter == 200) {
loop_avg /= 200;

if (loop_avg < 130.f) {
state.looptime_autodetect = LOOPTIME_8K;
} else if (loop_avg < 255.f) {
state.looptime_autodetect = LOOPTIME_4K;
} else {
state.looptime_autodetect = LOOPTIME_2K;
if (loop_avg > (state.looptime_autodetect + 5.0f)) {
state.looptime_autodetect *= 2.0f;
} else if (loop_avg < (state.looptime_autodetect * 0.5f)) {
state.looptime_autodetect *= 0.5f;
}

loop_counter++;
}

Expand Down
6 changes: 0 additions & 6 deletions src/core/looptime.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

#include <stdint.h>

typedef enum {
LOOPTIME_2K = 500,
LOOPTIME_4K = 250,
LOOPTIME_8K = 125,
} looptime_t;

void looptime_init();
void looptime_reset();
void looptime_update();
5 changes: 1 addition & 4 deletions src/core/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "core/debug.h"
#include "core/failloop.h"
#include "core/flash.h"
#include "core/looptime.h"
#include "core/profile.h"
#include "core/project.h"
#include "core/scheduler.h"
Expand Down Expand Up @@ -66,11 +65,9 @@ memory_section_init() {
}

__attribute__((__used__)) int main() {
scheduler_init();

// init timer so we can use delays etc
time_init();
looptime_init();
scheduler_init();

// load settings from flash
flash_load();
Expand Down
27 changes: 14 additions & 13 deletions src/core/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,35 +95,36 @@ static inline void task_run(task_t *task) {
}
}

static uint8_t scheduler_task_mask() {
uint8_t task_mask = TASK_MASK_DEFAULT;
if (flags.in_air || flags.arm_state) {
task_mask |= TASK_MASK_IN_AIR;
} else {
task_mask |= TASK_MASK_ON_GROUND;
}
return task_mask;
}

void task_reset_runtime() {
if (active_task == NULL) {
return;
if (active_task != NULL) {
active_task->flags |= TASK_FLAG_SKIP_STATS;
}
active_task->flags |= TASK_FLAG_SKIP_STATS;
looptime_reset();
}

void scheduler_init() {
looptime_init();

for (uint32_t i = 0; i < TASK_MAX; i++) {
task_queue_push(&tasks[i]);
}
}

void scheduler_run() {
looptime_reset();
looptime_init();

while (1) {
const volatile uint32_t cycles = time_cycles();

uint8_t task_mask = TASK_MASK_DEFAULT;
if (flags.in_air || flags.arm_state) {
task_mask |= TASK_MASK_IN_AIR;
} else {
task_mask |= TASK_MASK_ON_GROUND;
}

const uint8_t task_mask = scheduler_task_mask();
for (uint32_t i = 0; i < task_queue_size; i++) {
task_t *task = task_queue[i];
if (task_should_run(cycles, task_mask, task)) {
Expand Down
12 changes: 4 additions & 8 deletions src/core/tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
#include "project.h"
#include "rx/rx.h"

void task_main() {
// all flight calculations and motors
control();

// attitude calculations for level mode
imu_calc();

void util_task() {
// battery low logic
vbat_calc();

Expand All @@ -48,8 +42,10 @@ void task_main() {

FAST_RAM task_t tasks[TASK_MAX] = {
[TASK_GYRO] = CREATE_TASK("GYRO", TASK_MASK_ALWAYS, TASK_PRIORITY_REALTIME, sixaxis_read),
[TASK_MAIN] = CREATE_TASK("MAIN", TASK_MASK_ALWAYS, TASK_PRIORITY_REALTIME, task_main),
[TASK_IMU] = CREATE_TASK("IMU", TASK_MASK_ALWAYS, TASK_PRIORITY_REALTIME, imu_calc),
[TASK_PID] = CREATE_TASK("PID", TASK_MASK_ALWAYS, TASK_PRIORITY_REALTIME, control),
[TASK_RX] = CREATE_TASK("RX", TASK_MASK_ALWAYS, TASK_PRIORITY_HIGH, rx_update),
[TASK_UTIL] = CREATE_TASK("UTIL", TASK_MASK_ALWAYS, TASK_PRIORITY_HIGH, util_task),
#ifdef ENABLE_BLACKBOX
[TASK_BLACKBOX] = CREATE_TASK("BLACKBOX", TASK_MASK_ALWAYS, TASK_PRIORITY_MEDIUM, blackbox_update),
#endif
Expand Down
4 changes: 3 additions & 1 deletion src/core/tasks.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

typedef enum {
TASK_GYRO,
TASK_MAIN,
TASK_IMU,
TASK_PID,
TASK_RX,
TASK_UTIL,
#ifdef ENABLE_BLACKBOX
TASK_BLACKBOX,
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/driver/at32/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define PWM_CLOCK_FREQ_HZ 288000000
#define SPI_CLOCK_FREQ_HZ (SYS_CLOCK_FREQ_HZ / 2)

#define LOOPTIME LOOPTIME_8K
#define LOOPTIME_MAX 125

#define UID_BASE 0x1FFFF7E8
#endif
Expand Down
22 changes: 22 additions & 0 deletions src/driver/spi_gyro.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ uint8_t gyro_spi_init() {
return gyro_type;
}

float gyro_update_period() {
switch (gyro_type) {
case GYRO_TYPE_MPU6000:
case GYRO_TYPE_MPU6500:
case GYRO_TYPE_ICM20601:
case GYRO_TYPE_ICM20602:
case GYRO_TYPE_ICM20608:
case GYRO_TYPE_ICM20689:
return 125.0f;

case GYRO_TYPE_ICM42605:
case GYRO_TYPE_ICM42688P:
return 125.0f;

case GYRO_TYPE_BMI270:
return 312.5f;

default:
return 250.0f;
}
}

gyro_data_t gyro_spi_read() {
gyro_data_t data;

Expand Down
1 change: 1 addition & 0 deletions src/driver/spi_gyro.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ typedef struct {
extern gyro_types_t gyro_type;

uint8_t gyro_spi_init();
float gyro_update_period();
gyro_data_t gyro_spi_read();
void gyro_spi_calibrate();
8 changes: 4 additions & 4 deletions src/driver/stm32/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@
#define PWM_CLOCK_FREQ_HZ 108000000
#define SPI_CLOCK_FREQ_HZ (SYS_CLOCK_FREQ_HZ / 2)

#define LOOPTIME LOOPTIME_4K
#define LOOPTIME_MAX 250
#endif

#ifdef STM32F405
#define SYS_CLOCK_FREQ_HZ 168000000
#define PWM_CLOCK_FREQ_HZ 84000000
#define SPI_CLOCK_FREQ_HZ (SYS_CLOCK_FREQ_HZ / 4)

#define LOOPTIME LOOPTIME_8K
#define LOOPTIME_MAX 125
#endif

#ifdef STM32F7
#define SYS_CLOCK_FREQ_HZ 216000000
#define PWM_CLOCK_FREQ_HZ 216000000
#define SPI_CLOCK_FREQ_HZ (SYS_CLOCK_FREQ_HZ / 4)

#define LOOPTIME LOOPTIME_8K
#define LOOPTIME_MAX 125

#define WITHIN_DTCM_RAM(p) (((uint32_t)p & 0xffff0000) == 0x20000000)
#define WITHIN_DMA_RAM(p) (false)
Expand All @@ -80,7 +80,7 @@
#define PWM_CLOCK_FREQ_HZ (SYS_CLOCK_FREQ_HZ / 2)
#define SPI_CLOCK_FREQ_HZ (SYS_CLOCK_FREQ_HZ / 4)

#define LOOPTIME LOOPTIME_8K
#define LOOPTIME_MAX 125

#define WITHIN_DTCM_RAM(p) (((uint32_t)p & 0xfffe0000) == 0x20000000)
#define WITHIN_DMA_RAM(p) (((uint32_t)p & 0xfffe0000) == 0x30000000)
Expand Down
23 changes: 12 additions & 11 deletions src/flight/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <stdint.h>

#include "core/failloop.h"
#include "core/looptime.h"
#include "core/project.h"
#include "core/scheduler.h"
#include "rx/rx.h"
#include "util/vector.h"

Expand Down Expand Up @@ -43,14 +43,15 @@ extern control_flags_t flags;
typedef struct {
failloop_t failloop;

looptime_t looptime_autodetect;
float looptime; // looptime in seconds
float timefactor; // timefactor for pid calc
uint32_t looptime_us; // looptime in us
uint32_t loop_counter; // number of loops ran
float uptime; // running sum of looptimes
float armtime; // running sum of looptimes (while armed)
uint32_t cpu_load; // micros we have had left last loop
float looptime; // looptime in seconds
float looptime_us; // looptime in us
float looptime_autodetect; // desired looptime in us
float timefactor; // timefactor for pid calc
uint32_t loop_counter; // number of loops ran

float uptime; // running sum of looptimes
float armtime; // running sum of looptimes (while armed)
uint32_t cpu_load; // micros we have had left last loop

uint32_t failsafe_time_ms; // time the last failsafe occured in ms

Expand Down Expand Up @@ -109,10 +110,10 @@ typedef struct {

#define STATE_MEMBERS \
MEMBER(failloop, uint8_t) \
MEMBER(looptime_autodetect, uint16_t) \
MEMBER(looptime, float) \
MEMBER(looptime_us, float) \
MEMBER(looptime_autodetect, float) \
MEMBER(timefactor, float) \
MEMBER(looptime_us, uint32_t) \
MEMBER(loop_counter, uint32_t) \
MEMBER(uptime, float) \
MEMBER(armtime, float) \
Expand Down
2 changes: 1 addition & 1 deletion src/osd/render.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ static uint8_t print_osd_system_status(osd_element_t *el) {
printing.flags.arm_state = 0;
return print_stage;
}
if (((looptime_warning != last_sys_status.loop_warning) || printing.loop_warning) && (state.looptime_autodetect != LOOPTIME_8K)) { // mute warnings till we are on the edge of 4k->2k
if (((looptime_warning != last_sys_status.loop_warning) || printing.loop_warning) && (state.looptime_autodetect < 250.0f)) { // mute warnings till we are on the edge of 4k->2k
last_sys_status.loop_warning = looptime_warning;
printing.loop_warning = 1;
print_stage = print_status(el, TEMP, LOOP);
Expand Down

0 comments on commit 375c505

Please sign in to comment.