Skip to content

Commit

Permalink
1.LED频闪改用信号量+守护线程来实现,不会阻塞主调线程 2.BMS逻辑增加一条:电压骤降将会被判断为电机堵转
Browse files Browse the repository at this point in the history
  • Loading branch information
g122622 committed May 7, 2024
1 parent d6c876d commit 58f654d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 17 deletions.
24 changes: 19 additions & 5 deletions main/BMS/BMSTick.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2024-04-28 22:52:22
* Author: Guoyi
* -----
* Last Modified: 2024-04-28 23:10:33
* Last Modified: 2024-05-08 00:13:36
* Modified By: Guoyi
* -----
* Copyright (c) 2024 Guoyi Inc.
Expand All @@ -20,13 +20,27 @@

#define BATTERY_VOLTAGE_LOW_THR (3200)
#define BATTERY_VOLTAGE_EXTREMELY_LOW_THR (3000)
#define BATTERY_VOLTAGE_DROP_THR (150)

static void fatalStop()
{
flightState = 0;
stopAllMotors(); // 多余的一步。在这里是为了万无一失,确保关停所有电机。
flashStatusLED(10, 500);
}

void BMSTick()
{
batteryVoltagePrev = batteryVoltage;
batteryVoltage = BMS_ADC_ReadBatteryVoltage();
if (batteryVoltage != -1 && batteryVoltage < BATTERY_VOLTAGE_EXTREMELY_LOW_THR){
flightState = 0;
stopAllMotors(); // 多余的一步。在这里是为了万无一失,确保关停所有电机。
flashStatusLED(10, 500);

// 电压墙策略
if (batteryVoltage != -1 && batteryVoltage < BATTERY_VOLTAGE_EXTREMELY_LOW_THR)
{
fatalStop();
}
if (batteryVoltagePrev > 0 && batteryVoltagePrev - batteryVoltage > BATTERY_VOLTAGE_DROP_THR)
{
fatalStop();
}
}
35 changes: 27 additions & 8 deletions main/LED/StatusLED.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2024-04-28 15:07:32
* Author: Guoyi
* -----
* Last Modified: 2024-04-28 23:00:13
* Last Modified: 2024-05-08 00:04:25
* Modified By: Guoyi
* -----
* Copyright (c) 2024 Guoyi Inc.
Expand All @@ -20,11 +20,34 @@
#include "driver/gpio.h"

static gpio_num_t gpio_led_num = GPIO_NUM_23; // 连接LED的GPIO
static int s_count = 0;
static int s_duration = 0;

static void flashStatusLEDTask(void *argument)
{
while (1)
{
xSemaphoreTake(semapHandle, portMAX_DELAY);
for (int i = 0; i < *(int *)argument; i++)
{
enableStatusLED();
vTaskDelay(*(int *)(argument + 1) / portTICK_PERIOD_MS);
disableStatusLED();
vTaskDelay(*(int *)(argument + 1) / portTICK_PERIOD_MS);
}
}
}
static TaskHandle_t flashStatusLEDTaskHandle;
static SemaphoreHandle_t semapHandle;

void StatusLED_Init()
{
// 设置控制LED的GPIO为输出模式
gpio_set_direction(gpio_led_num, GPIO_MODE_OUTPUT);
xTaskCreatePinnedToCore(flashStatusLEDTask, "flashStatusLEDTask",
4096, NULL, 3, flashStatusLEDTaskHandle, 0);
// 创建信号量,用于拉起LED频闪任务
semapHandle = xSemaphoreCreateBinary();
}

void enableStatusLED()
Expand All @@ -39,13 +62,9 @@ void disableStatusLED()

void flashStatusLED(int count, int duration)
{
for (int i = 0; i < count; i++)
{
enableStatusLED();
vTaskDelay(duration / portTICK_PERIOD_MS);
disableStatusLED();
vTaskDelay(duration / portTICK_PERIOD_MS);
}
s_count = count;
s_duration = duration;
xSemaphoreGive(semapHandle); // 拉起信号量
}

#endif
3 changes: 2 additions & 1 deletion main/globalStates/BMSState.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2024-04-27 12:59:00
* Author: Guoyi
* -----
* Last Modified: 2024-04-27 13:20:17
* Last Modified: 2024-05-08 00:11:32
* Modified By: Guoyi
* -----
* Copyright (c) 2024 Guoyi Inc.
Expand All @@ -16,5 +16,6 @@
#define BMS_STATE_H

int __attribute__((weak)) batteryVoltage = -1;
int __attribute__((weak)) batteryVoltagePrev = -1;

#endif
2 changes: 1 addition & 1 deletion main/mpu6050/algorithm/slidingFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2024-04-29 21:10:08
* Author: Guoyi
* -----
* Last Modified: 2024-05-02 19:40:40
* Last Modified: 2024-05-07 12:33:32
* Modified By: Guoyi
* -----
* Copyright (c) 2024 Guoyi Inc.
Expand Down
4 changes: 2 additions & 2 deletions main/mpu6050/mpu6050.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2024-03-07 19:55:56
* Author: Guoyi
* -----
* Last Modified: 2024-04-29 23:48:41
* Last Modified: 2024-05-07 13:56:23
* Modified By: Guoyi
* -----
* Copyright (c) 2024 Guoyi Inc.
Expand Down Expand Up @@ -58,7 +58,7 @@ void MPU6050_Init()
uint8_t flag = ESP_OK;
flag = MPU6050_register_write_byte(PWR_MGMT_1, 0x00); // 解除休眠状态
flag = MPU6050_register_write_byte(SMPLRT_DIV, 0x00); // 陀螺仪采样率。1KHz
flag = MPU6050_register_write_byte(CONFIG, 0x06); // 启用低通滤波。等级为0x05
flag = MPU6050_register_write_byte(CONFIG, 0x05); // 启用低通滤波。等级为0x05
flag = MPU6050_register_write_byte(GYRO_CONFIG, 0x0); // 陀螺仪自检及测量范围。不自检,250deg/s
flag = MPU6050_register_write_byte(ACCEL_CONFIG, 0x01); // 加速计自检、测量范围及高通滤波频率。不自检,2G,5Hz
if (flag != ESP_OK)
Expand Down

0 comments on commit 58f654d

Please sign in to comment.