添加PCB NTC,Battery NTC 驱动 温度值的计算
authorSnickerSun <snickersun@163.com>
Fri, 29 Mar 2024 09:54:05 +0000 (17:54 +0800)
committerSnickerSun <snickersun@163.com>
Fri, 29 Mar 2024 09:54:05 +0000 (17:54 +0800)
code/sdk/application/battery/service/battery_driver.cpp
code/sdk/application/battery/service/battery_service.cpp

index 40ec001..5fc6329 100644 (file)
@@ -77,11 +77,158 @@ typedef enum {
 #define adc_trans_sytick_to_voltage_without_auto_scan(x) ((float)(((float)(x) * 18) / 40960))
 #define adc_trans_sytick_to_voltage(x) ((float)(((float)(x) * 17) / 40960))
 #define adc_trans_sytick_to_voltage_diff(x) ((float)(((float)((x) - 2048) * 17) / 20480))
-#define adc_trans_systick_to_battery_ntc(x) ((float)(((float)(x) * 18) / 40960))
-#define adc_trans_systick_to_pcb_ntc(x) ((float)(((float)(x) * 17) / 40960))
-#define adc_trans_systick_to_battery_voltage(x) ((float)((float)(x)  / 749.75838433852f))
+
+
+float pcb_ntc_map[][2]={
+{27.2186,0},
+{26.076,1},
+{24.9877,2},
+{23.9509,3},
+{22.9629,4},
+{22.0211,5},
+{21.123,6},
+{20.2666,7},
+{19.4495,8},
+{18.6698,9},
+{17.9255,10},
+{17.2139,11},
+{16.5344,12},
+{15.8856,13},
+{15.2658,14},
+{14.6735,15},
+{14.1075,16},
+{13.5664,17},
+{13.0489,18},
+{12.554,19},
+{12.0805,20},
+{11.6281,21},
+{11.1947,22},
+{10.7795,23},
+{10.3815,24},
+{10,25},
+{9.6342,26},
+{9.2835,27},
+{8.947,28},
+{8.6242,29},
+{8.3145,30},
+{8.0181,31},
+{7.7337,32},
+{7.4609,33},
+{7.1991,34},
+{6.9479,35},
+{6.7067,36},
+{6.4751,37},
+{6.2526,38},
+{6.039,39},
+{5.8336,40},
+{5.6357,41},
+{5.4454,42},
+{5.2623,43},
+{5.0863,44},
+{4.9169,45},
+{4.7539,46},
+{4.5971,47},
+{4.4461,48},
+{4.3008,49},
+{4.1609,50},
+{4.0262,51},
+{3.8964,52},
+{3.7714,53},
+{3.651,54},
+{3.535,55},
+{3.4231,56},
+{3.3152,57},
+{3.2113,58},
+{3.111,59},
+{3.0143,60},
+{2.9224,61},
+{2.8337,62},
+{2.7482,63},
+{2.6657,64},
+{2.5861,65},
+{2.5093,66},
+{2.4351,67},
+{2.3635,68},
+{2.2943,69},
+{2.2275,70}
+};
+float battery_ntc_map[][2]={
+    {27.8,0},
+    {26.6,1},
+    {25.46,2},
+    {24.38,3},
+    {23.35,4},
+    {22.37,5},
+    {21.44,6},
+    {20.55,7},
+    {19.7,8},
+    {18.9,9},
+    {18.13,10},
+    {17.39,11},
+    {16.69,12},
+    {16.02,13},
+    {15.39,14},
+    {14.78,15},
+    {14.2,16},
+    {13.64,17},
+    {13.11,18},
+    {12.61,19},
+    {12.12,20},
+    {11.66,21},
+    {11.22,22},
+    {10.79,23},
+    {10.39,24},
+    {10,25},
+    {9.63,26},
+    {9.27,27},
+    {8.93,28},
+    {8.61,29},
+    {8.3,30},
+    {8,31},
+    {7.71,32},
+    {7.43,33},
+    {7.17,34},
+    {6.92,35},
+    {6.67,36},
+    {6.44,37},
+    {6.22,38},
+    {6,39},
+    {5.8,40},
+    {5.6,41},
+    {5.41,42},
+    {5.23,43},
+    {5.05,44},
+    {4.88,45},
+    {4.72,46},
+    {4.57,47},
+    {4.42,48},
+    {4.27,49},
+    {4.13,50},
+    {4,51},
+    {3.87,52},
+    {3.75,53},
+    {3.63,54},
+    {3.51,55},
+    {3.4,56},
+    {3.3,57},
+    {3.19,58},
+    {3.09,59},
+    {3,60},
+    {2.91,61},
+    {2.82,62},
+    {2.73,63},
+    {2.65,64},
+    {2.57,65},
+    {2.49,66},
+    {2.42,67},
+    {2.35,68},
+    {2.28,69},
+    {2.21,70}
+};
+
+
 #define batteryservice_VOLTAGE_MAX (4.2f)
-#define batteryservice_VOLTAGE_MIN (3.2f)
+#define batteryservice_VOLTAGE_MIN (3.3f)
 
 
 
@@ -96,7 +243,6 @@ static uint32_t g_adc_sample_stick = 0;
 
 
 
-
 static battery_info_t current_battery_info={0};
 
 battery_info_t *get_current_battery_info(void)
@@ -104,6 +250,13 @@ battery_info_t *get_current_battery_info(void)
     return &current_battery_info;
 }
 
+void dump_battery_info(void)
+{
+    printf("battery_voltage = %.02f\n",current_battery_info.battery_voltage);
+    printf("battery_ntc_temp = %.02f\n",current_battery_info.battery_ntc_temp);
+    printf("pcb_ntc_temp = %.02f\n",current_battery_info.pcb_ntc_temp);
+}
+
 static uint32_t adc_get_sample_times(void)
 {
     return g_adc_sample_times;
@@ -131,6 +284,80 @@ static float adc_stick_transfer_voltage(uint16_t stick, adc_channel_t channel)
     return adc_trans_sytick_to_voltage_without_auto_scan(stick);
 }
 
+/**
+ * 分压关系: 2M+1M两个电阻串联,分压 VBat , adc测量的电压为VBat的 1/3 
+ * 测量算得大致系数为  mV/stick = 1000/749.75838433852f 
+*/
+static float adc_trans_systick_to_battery_voltage(int sticks)
+{
+// #define adc_trans_systick_to_battery_voltage(x) ((float)((float)(x)  / 749.75838433852f))
+    float ret = ((float)((float)(sticks)  / 749.75838433852f));
+    return ret;
+}
+
+/**
+ * 分压关系 温敏电阻与10K电阻串联,共同分压1800mv
+ * 根据测量,计算 mV/stick = 0.45283089
+ * 反算 温敏电阻阻值 = (0.45283089 * sticks * 10)/(1800 - 0.45283089 * sticks )
+ * 查表反推温度.
+ * 另外,不考虑电阻值温度map,直接换算数学公式可为 ((float)(((float)(sticks) * (-0.02681f)) + 79.14f));
+ * 需要额外验证
+*/
+static float  adc_trans_systick_to_pcb_ntc(int sticks)
+{
+// #define adc_trans_systick_to_pcb_ntc(x) ((float)(((float)(x) * (-0.02681f)) + 79.14f))
+     float ret = ((float)(((float)(sticks) * (-0.02681f)) + 79.14f));
+
+     float r_value = (0.45283089f * sticks * 10.0f)/(1800.0f - 0.45283089f * sticks );
+    int map_len = sizeof(pcb_ntc_map)/sizeof(pcb_ntc_map[0]);
+    float temp_value = 0.0f;
+    for (int i =0;i<map_len -1;i++)
+    {
+        //阻值在前后两个之间
+        if (r_value <= pcb_ntc_map[i][0] && r_value > pcb_ntc_map[i+1][0] )
+        {
+            //阻值 前大后小
+            float r_div = ( pcb_ntc_map[i][0] - r_value)/(pcb_ntc_map[i][0] - pcb_ntc_map[i+1][0]);
+            //温度 前小后大
+            temp_value =  pcb_ntc_map[i][1] + (pcb_ntc_map[i+1][1] - pcb_ntc_map[i][1]) * r_div ;
+            break;
+        }
+    }
+    // osal_printk("pcb ntc temp value1 %.4f value2 %.4f\r\n",ret,temp_value);
+    return temp_value;
+}
+/**
+ * 分压关系 温敏电阻与10K电阻串联,共同分压1800mv
+ * 根据测量,计算 mV/stick = 0.45283089
+ * 反算 温敏电阻阻值 = (0.45283089 * sticks * 10)/(1800 - 0.45283089 * sticks )
+ * 查表反推温度.
+ * 
+*/
+static float adc_trans_systick_to_battery_ntc(int sticks)
+{
+// #define float adc_trans_systick_to_battery_ntc(x) ((float)(((float)(x) * (-0.02681f)) + 79.14f))    //-0.02681 x + 79.14
+    float ret = ((float)(((float)(sticks) * (-0.02681f)) + 79.14f));
+
+       float r_value = (0.45283089f * sticks * 10.0f)/(1800.0f - 0.45283089f * sticks );
+    int map_len = sizeof(battery_ntc_map)/sizeof(battery_ntc_map[0]);
+    float temp_value = 0.0f;
+    for (int i =0;i<map_len -1;i++)
+    {
+        //阻值在前后两个之间
+        if (r_value <= battery_ntc_map[i][0] && r_value > battery_ntc_map[i+1][0] )
+        {
+            //阻值 前大后小
+            float r_div = ( battery_ntc_map[i][0] - r_value)/(battery_ntc_map[i][0] - battery_ntc_map[i+1][0]);
+            //温度 前小后大
+            temp_value =  battery_ntc_map[i][1] + (battery_ntc_map[i+1][1] - battery_ntc_map[i][1]) * r_div ;
+            break;
+        }
+    }
+    // osal_printk("battery ntc temp value1 %.4f value2 %.4f\r\n",ret,temp_value);
+    return temp_value;
+}
+
+
 static bool adc_get_sample_result(float *voltage, uint8_t channel)
 {
     uint32_t sample_number = 0;
@@ -185,7 +412,7 @@ static bool adc_get_sample_result(float *voltage, uint8_t channel)
     *voltage = adc_trans_systick_to_battery_ntc(stick);
     else if (channel == 5)
     *voltage = adc_trans_systick_to_pcb_ntc(stick);
-    // osal_printk("channel = %d , tick = %d , voltage = %.04f.\r\n", channel,stick,*voltage);
+    // osal_printk("channel = %d , stick = %d , voltage = %.04f.\r\n", channel,stick,*voltage);
 
     UNUSED(discard_number);
     return true;
index 7afeecb..42c3ffc 100644 (file)
@@ -358,6 +358,7 @@ void uapi_battery_create_queue(void)
     }
 
 }
+#define BATTERY_TASK_SIZE 0x1C00
 errcode_t uapi_battery_init(void)
 {
 
@@ -367,8 +368,8 @@ errcode_t uapi_battery_init(void)
     PRINT("uapi_battery_init !\r\n");
     taskcfg.create_queue_handle = uapi_battery_create_queue;
     taskcfg.attr.name="battery_service";
-    taskcfg.attr.priority = TASK_PRIORITY_APP;
-    taskcfg.attr.stack_size = APP_BOARD_TASK_SIZE;
+    taskcfg.attr.priority = osPriorityBelowNormal7;
+    taskcfg.attr.stack_size = BATTERY_TASK_SIZE;
     taskcfg.func = battery_check_task;
     taskcfg.use_mem = USE_EXTEND_MEM;