Browse Source

battery data: handles and stores ADC values, handle DAC values from the Controller in struct format

namrota ghosh 8 months ago
parent
commit
abd44672d6
2 changed files with 141 additions and 0 deletions
  1. 78 0
      src/battery_data/battery.c
  2. 63 0
      src/battery_data/battery.h

+ 78 - 0
src/battery_data/battery.c

@@ -0,0 +1,78 @@
+#include "battery.h"
+#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
+// Correct temp_threshold yet to be analyzed
+#define TEMP_THRESHOLD  (460)
+//#define VOLTAGE_THRESHOLD ()
+
+uint16_t charge_change_threshold;
+
+/*Initialize battery array and default parameters*/
+void Battery_Init(){
+    for(uint8_t i=0; i< NUM_SLOTS; i++){
+    batteries[i].slot_id= i;
+    batteries[i].state= STATE_EMPTY;
+    batteries[i].voltage= 0;
+    batteries[i].current= 0;
+    batteries[i].temperature= 0;
+    batteries[i].min_voltage= 0;
+    batteries[i].max_voltage= 0;
+    batteries[i].cut_off_current= 0;
+    batteries[i].capacitance= 0;
+    batteries[i].charge_fraction= 0;
+    }
+}
+
+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(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;
+  }
+}
+
+
+

+ 63 - 0
src/battery_data/battery.h

@@ -0,0 +1,63 @@
+#ifndef BATTERY_H
+#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)
+//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_WAITING_FOR_LIMITS= 0x03,
+    STATE_MEASUREMENT_IN_PROGRESS= 0x04,
+    STATE_MEASUREMENT_DONE= 0x04,
+    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
+
+typedef struct{
+    uint8_t slot_id;
+    BatteryState state;
+    uint16_t voltage;
+    int16_t current;
+    uint16_t temperature;
+    uint16_t min_voltage;
+    uint16_t max_voltage;
+    uint8_t cut_off_current;
+    uint16_t capacitance;
+    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_Discharge_SafetyCheck(uint8_t slot_id);
+void Battery_ReadState(uint8_t slot_id);
+#endif