瀏覽代碼

Handling different Battery States: new logic to wait for battery limits from Pi

namrota ghosh 8 月之前
父節點
當前提交
d9d660c9bb
共有 3 個文件被更改,包括 75 次插入16 次删除
  1. 44 8
      src/battery.c
  2. 28 8
      src/battery.h
  3. 3 0
      src/cc_cv_charging.c

+ 44 - 8
src/battery.c

@@ -2,6 +2,7 @@
 #include "ti/driverlib/dl_i2c.h"
 #include "ti_msp_dl_config.h"
 
+
 Battery batteries[NUM_SLOTS];
 
 // Permissible charge temperature for LiIon battery is 0.0 degree Celsius to 45.0 degree Celsius
@@ -23,19 +24,54 @@ void Battery_Init(){
     batteries[i].max_voltage= 0;
     batteries[i].cut_off_current= 0;
     batteries[i].capacitance= 0;
-
+    batteries[i].charge_fraction= 0;
     }
 }
 
-void Battery_SafetyCheck(uint8_t slot){
-    if(slot>= NUM_SLOTS){
-        return;
-    }
+void Battery_Discharge_SafetyCheck(uint8_t slot_id){
+    //As of now declared a varaible with 0, in future it will call a function to read the output volatge of the boost converter
+    uint16_t boost_vout_mv= 0;
+    DischargeSafetyCondition condition;
+    Battery *battery= &batteries[slot_id];
 
-    if (batteries[slot].temperature > TEMP_THRESHOLD){
-        batteries[slot].state= STATE_OVERCHARGING;
-        //How to exit? Disable the DAC channel?
+    if(battery->temperature > TEMPERATURE_MAX_C){
+        battery->condition= STATE_OVERTEMPERATURE;
+        
+    }
+    //if battery
+    else if(boost_vout_mv >= BOOST_HOV_THRESHOLD_MV-HYSTERISIS){
+        battery->condition= STATE_HOV;
+    }
+    else if(boost_vout_mv <= BOOST_SOV_THRESHOLD_MV-HYSTERISIS){
+        battery->condition= STATE_SOV;
     }
+    else{
+        battery->condition= STATE_OK;
+    }
+}
+
+void Battery_ReadState(uint8_t slot_id) {
+
+  uint16_t voltage_mv = batteries[slot_id].voltage;
+  uint16_t min_voltage = batteries[slot_id].min_voltage;
+  uint16_t max_voltage = batteries[slot_id].max_voltage;
+
+  /*
+  Added another state: STATE_WAITING_FOR_LIMITS (0x03) so that Pi is aware that battery limits needs to be sent to the MCU 
+  */
+  if(min_voltage == 0 || max_voltage == 0){
+    batteries[slot_id].state= STATE_WAITING_FOR_LIMITS;
+    return;
+  }
+  if (voltage_mv < BATTERY_THRESHOLD) {
+    batteries[slot_id].state = STATE_EMPTY;
+  } else if (voltage_mv >= BATTERY_THRESHOLD && voltage_mv < min_voltage) { 
+    batteries[slot_id].state = STATE_BATTERY_DETECTED;
+  } else if (voltage_mv >= min_voltage && voltage_mv < max_voltage) {
+    batteries[slot_id].state = STATE_MEASUREMENT_IN_PROGRESS;
+  } else {
+    batteries[slot_id].state = STATE_OVERHEATING;
+  }
 }
 
 

+ 28 - 8
src/battery.h

@@ -2,21 +2,37 @@
 #define BATTERY_H
 
 #include <stdint.h>
+#include <stdbool.h>
 //define macro to be used by multiple files in the program witout the variable being overwritten
 //for testing
 #define NUM_SLOTS (1)
-#define BATTERY_THRESHOLD (100)
-
+//Battery Tolerance
+#define BATTERY_THRESHOLD (50)
+// for i2c communication to know battery health
+#define BOOST_SOV_THRESHOLD_MV (4900)
+#define BOOST_HOV_THRESHOLD_MV (5400)
+#define TEMPERATURE_MAX_C (60)
+// Discharge hysterisis
+#define HYSTERISIS (50)
 
 //Battery states
 typedef enum{
     STATE_EMPTY= 0x01,
     STATE_BATTERY_DETECTED= 0x02,
-    STATE_MEASUREMENT_IN_PROGRESS= 0x03,
+    STATE_WAITING_FOR_LIMITS= 0x03,
+    STATE_MEASUREMENT_IN_PROGRESS= 0x04,
     STATE_MEASUREMENT_DONE= 0x04,
-    STATE_OVERCHARGING= 0x05
+    STATE_OVERHEATING= 0x05,
+
 } BatteryState;
 
+//Battery Discharge Safety Check
+typedef enum{
+    STATE_OK= 0x00,
+    STATE_SOV= 0x01,
+    STATE_HOV= 0x02,
+    STATE_OVERTEMPERATURE= 0x03,
+}DischargeSafetyCondition;
 
 
 //Battery Structure
@@ -29,15 +45,19 @@ typedef struct{
     uint16_t temperature;
     uint16_t min_voltage;
     uint16_t max_voltage;
-    uint16_t cut_off_current;
+    uint8_t cut_off_current;
     uint16_t capacitance;
-    uint16_t charge_fraction;
+    uint8_t charge_fraction;
+    int16_t charge_discharge_current;
+    DischargeSafetyCondition condition;
+    bool pwm_hov_state;
+    bool batteryLimitReceived;
 } Battery;
 
 //global battery array declaration: extending visiblity of the variable to multiple source files
 extern Battery batteries[NUM_SLOTS];
 
 void Battery_Init();
-void Battery_SafetyCheck(uint8_t slot);
-
+void Battery_Discharge_SafetyCheck(uint8_t slot_id);
+void Battery_ReadState(uint8_t slot_id);
 #endif

+ 3 - 0
src/cc_cv_charging.c

@@ -2,6 +2,7 @@
 References:
 https://www.monolithicpower.com/learning/resources/how-to-select-lithium-ion-battery-charge-management-ic
 https://www.monolithicpower.com/learning/resources/battery-charger-fundamentals
+https://www.ossila.com/pages/what-is-battery-c-rate
 
 */
 
@@ -219,6 +220,8 @@ void CC_CV_ControlCharging(uint8_t slot_id) {
     break;
 
   case STATE_FINAL_DISCHARGE:
+    //Once the cycle gets done, the battery state transitions to "STATE_MEASUREMENT_DONE"
+    batteries[slot_id].state= STATE_MEASUREMENT_DONE;
     DL_GPIO_clearPins(GPIO_Battery_Charging_PORT,
                       GPIO_Battery_Charging_PIN_PB4_PIN);
     DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT,