Formatted and added bmi task

This commit is contained in:
2026-03-19 19:27:06 +01:00
parent f5f47c26fd
commit 00e354c2b3
7 changed files with 284 additions and 151 deletions

View File

@@ -13,7 +13,8 @@
#define MOTOR_PWM_MAX_DUTY ((1 << 10) - 1)
#define MOTOR_MIN_VALUE 60
typedef struct {
typedef struct
{
int fwd_pin;
int bak_pin;
ledc_channel_t fwd_channel;
@@ -39,13 +40,17 @@ static uint32_t percentile_to_duty(percentile_t percentile)
{
int clamped = percentile;
const uint32_t min_duty = (MOTOR_PWM_MAX_DUTY * MOTOR_MIN_VALUE) / 100;
if (clamped > 100) {
if (clamped > 100)
{
clamped = 100;
} else if (clamped < -100) {
}
else if (clamped < -100)
{
clamped = -100;
}
if (clamped == 0) {
if (clamped == 0)
{
return 0;
}
@@ -58,7 +63,7 @@ static void set_channel_duty(ledc_channel_t channel, uint32_t duty)
ESP_ERROR_CHECK(ledc_update_duty(MOTOR_PWM_MODE, channel));
}
void init_motors(void)
esp_err_t init_motors(void)
{
const ledc_timer_config_t timer_config = {
.speed_mode = MOTOR_PWM_MODE,
@@ -70,7 +75,8 @@ void init_motors(void)
ESP_ERROR_CHECK(ledc_timer_config(&timer_config));
for (size_t i = 0; i < (sizeof(s_motor_configs) / sizeof(s_motor_configs[0])); ++i) {
for (size_t i = 0; i < (sizeof(s_motor_configs) / sizeof(s_motor_configs[0])); ++i)
{
const motor_config_t *motor = &s_motor_configs[i];
const ledc_channel_config_t fwd_channel_config = {
.gpio_num = motor->fwd_pin,
@@ -96,24 +102,31 @@ void init_motors(void)
}
set_motors(0, 0);
return ESP_OK;
}
void set_motor(motor_t motor, percentile_t percentile)
{
if (motor < MOTOR1 || motor > MOTOR2) {
if (motor < MOTOR1 || motor > MOTOR2)
{
return;
}
const motor_config_t *config = &s_motor_configs[motor];
const uint32_t duty = percentile_to_duty(percentile);
if (percentile > 0) {
if (percentile > 0)
{
set_channel_duty(config->bak_channel, 0);
set_channel_duty(config->fwd_channel, duty);
} else if (percentile < 0) {
}
else if (percentile < 0)
{
set_channel_duty(config->fwd_channel, 0);
set_channel_duty(config->bak_channel, duty);
} else {
}
else
{
set_channel_duty(config->fwd_channel, 0);
set_channel_duty(config->bak_channel, 0);
}