Skip to content

Commit

Permalink
支持蓝牙获取BMS信息
Browse files Browse the repository at this point in the history
  • Loading branch information
g122622 committed Apr 27, 2024
1 parent 5af75b3 commit c07ac3d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 17 deletions.
26 changes: 14 additions & 12 deletions main/BMS/ADC/batteryVoltage.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2024-04-24 13:47:31
* Author: Guoyi
* -----
* Last Modified: 2024-04-24 17:54:26
* Last Modified: 2024-04-27 13:02:40
* Modified By:
* -----
* Copyright (c) 2024 Guoyi Inc.
Expand Down Expand Up @@ -32,9 +32,9 @@ const static char *BMS_ADC_LOG_TAG = "BMS_ADC";

static int adc_raw[2][10];
static int voltage[2][10];
adc_oneshot_unit_handle_t adc1_handle;
adc_cali_handle_t adc1_cali_handle = NULL;
bool do_calibration1_chan0 = false;
static adc_oneshot_unit_handle_t adc1_handle;
static adc_cali_handle_t adc1_cali_handle = NULL;
static bool do_calibration1_chan0 = false;

static bool battery_adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t atten, adc_cali_handle_t *out_handle);
static void battery_adc_calibration_deinit(adc_cali_handle_t handle);
Expand All @@ -55,19 +55,21 @@ void BMS_ADC_Init(void)
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, BATTERY_ADC1_CHANNEL, &config));

//-------------ADC1 Calibration Init---------------//
do_calibration1_chan0 = battery_adc_calibration_init(ADC_UNIT_1, BATTERY_ADC1_CHANNEL, BATTERY_ADC_ATTEN, &adc1_cali_handle);
do_calibration1_chan0 = battery_adc_calibration_init(ADC_UNIT_1, BATTERY_ADC1_CHANNEL, BATTERY_ADC_ATTEN, &adc1_cali_handle);
}

// 如果读取数据时还未校准,那么返回-1
int BMS_ADC_ReadBatteryVoltage()
{
if (do_calibration1_chan0)
{
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, BATTERY_ADC1_CHANNEL, &adc_raw[0][0]));
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[0][0], &voltage[0][0]));
return voltage[0][0];
} else {
// 如果读取数据时还未校准,那么返回-1
adc_oneshot_read(adc1_handle, BATTERY_ADC1_CHANNEL, &adc_raw[0][0]);
adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[0][0], &voltage[0][0]);
return voltage[0][0] * 2; // 两个10k电阻分压
}
else
{
// 如果读取数据时还未校准,那么返回-1
return -1;
}
return -1;
Expand All @@ -76,7 +78,7 @@ int BMS_ADC_ReadBatteryVoltage()
void BMS_ADC_Deinit()
{
// Tear Down
ESP_ERROR_CHECK(adc_oneshot_del_unit(adc1_handle));
adc_oneshot_del_unit(adc1_handle);
battery_adc_calibration_deinit(adc1_cali_handle);
}

Expand Down Expand Up @@ -125,5 +127,5 @@ battery_adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t
static void battery_adc_calibration_deinit(adc_cali_handle_t handle)
{
ESP_LOGI(BMS_ADC_LOG_TAG, "deregister %s calibration scheme", "Line Fitting");
ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle));
adc_cali_delete_scheme_line_fitting(handle);
}
6 changes: 6 additions & 0 deletions main/bluetooth/gatt_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
.flags = BLE_GATT_CHR_F_READ,
.val_handle = &gatt_remoteInfo_chr_basic_motion_val_handle,
},
{
.uuid = &gatt_remoteInfo_chr_battery_voltage_uuid.u,
.access_cb = gatt_remoteInfo_svc_access,
.flags = BLE_GATT_CHR_F_READ,
.val_handle = &gatt_remoteInfo_chr_battery_voltage_val_handle,
},
{
0, /* No more characteristics in this service. */
}},
Expand Down
11 changes: 10 additions & 1 deletion main/bluetooth/services/remoteInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2024-03-11 22:55:56
* Author: Guoyi
* -----
* Last Modified: 2024-04-10 23:08:29
* Last Modified: 2024-04-27 13:19:04
* Modified By: Guoyi
* -----
* Copyright (c) 2024 Guoyi Inc.
Expand Down Expand Up @@ -36,6 +36,7 @@
#include "services/ans/ble_svc_ans.h"

#include "../../globalStates/motionState.h"
#include "../../globalStates/BMSState.h"
#include "../../utils/F1D.h"

// ===========================远程信息服务 0x1022================================== //
Expand All @@ -48,6 +49,9 @@ static const ble_uuid16_t gatt_remoteInfo_svc_uuid = BLE_UUID16_INIT(0x1022);
static uint32_t gatt_remoteInfo_chr_basic_motion_val[6];
static uint16_t gatt_remoteInfo_chr_basic_motion_val_handle;
static const ble_uuid16_t gatt_remoteInfo_chr_basic_motion_uuid = BLE_UUID16_INIT(0x1023);
// 2.电池电压
static uint16_t gatt_remoteInfo_chr_battery_voltage_val_handle;
static const ble_uuid16_t gatt_remoteInfo_chr_battery_voltage_uuid = BLE_UUID16_INIT(0x1024);

void calcBasicMotionVal()
{
Expand Down Expand Up @@ -83,6 +87,11 @@ static int gatt_remoteInfo_svc_access(uint16_t conn_handle, uint16_t attr_handle
gatt_remoteInfo_chr_basic_motion_val,
sizeof(gatt_remoteInfo_chr_basic_motion_val));
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
} else if(attr_handle == gatt_remoteInfo_chr_battery_voltage_val_handle){
rc = os_mbuf_append(ctxt->om,
&batteryVoltage,
sizeof(batteryVoltage));
return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
}
goto unknown;

Expand Down
20 changes: 20 additions & 0 deletions main/globalStates/BMSState.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* File: \BMSState.h
* Project: globalStates
* Created Date: 2024-04-27 12:59:00
* Author: Guoyi
* -----
* Last Modified: 2024-04-27 13:20:17
* Modified By: Guoyi
* -----
* Copyright (c) 2024 Guoyi Inc.
*
* ------------------------------------
*/

#ifndef BMS_STATE_H
#define BMS_STATE_H

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

#endif
7 changes: 3 additions & 4 deletions main/tasks/BMSTaskLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2024-04-24 17:28:06
* Author: Guoyi
* -----
* Last Modified: 2024-04-24 17:31:36
* Last Modified: 2024-04-27 13:00:59
* Modified By:
* -----
* Copyright (c) 2024 Guoyi Inc.
Expand All @@ -19,15 +19,14 @@
#include "freertos/task.h"

#include "../BMS/ADC/batteryVoltage.h"
#include "../globalStates/BMSState.h"

void BMSTaskLoop(void *argument)
{
BMS_ADC_Init();
while (1)
{
BMS_ADC_ReadBatteryVoltage();
int volt = BMS_ADC_ReadBatteryVoltage();
printf("volt=%d", volt);
batteryVoltage = BMS_ADC_ReadBatteryVoltage();
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
Expand Down

0 comments on commit c07ac3d

Please sign in to comment.