瀏覽代碼

Logic for cycle number has been added if the MCU is reset by WWDT; logic is untested

namrota ghosh 7 月之前
父節點
當前提交
3f8d6e07fb
共有 1 個文件被更改,包括 25 次插入21 次删除
  1. 25 21
      src/cc_cv_charging.c

+ 25 - 21
src/cc_cv_charging.c

@@ -22,9 +22,7 @@ static uint16_t batt_max_voltage;
 static uint16_t batt_cutoff_current;
 static uint16_t batt_capacitance;
 static int16_t batt_charge_discharge;
-//previous cycle count variable keeps track of the WWDT reset during the charging else by default it is 0, currently initialized to 0
-static uint8_t previous_cycle_counter= 0;
-
+static uint8_t previous_cycle_number;
 // Functions for Charging and Discharging State
 
 void CC_CV_UpdateChargingState(uint8_t slot_id) {
@@ -43,13 +41,13 @@ void CC_CV_UpdateChargingState(uint8_t slot_id) {
     batt_cutoff_current = battery_data[slot_id].cut_off_current;
     batt_capacitance = battery_data[slot_id].capacitance;
     batt_charge_discharge= battery_data[slot_id].charge_discharge;
-    
+    previous_cycle_number= battery_data[slot_id].previous_cycle_number;
 
     //For logging state transitions
-    /*if(charging_state != previous_state){
+    if(charging_state != previous_state){
         printf("State transition: %d -> %d\n", previous_state, charging_state);
         previous_state= charging_state;
-    }*/
+    }
 
     //Check if the slot is empty or if the battery limits are not set
     if ((battery_data[slot_id].battery_state == STATE_EMPTY) || (battery_data[slot_id].batteryLimitReceived == 0)) {
@@ -57,12 +55,15 @@ void CC_CV_UpdateChargingState(uint8_t slot_id) {
       return;
     }
 
+    //Checking the condition where all the battery measurements are NULL
     if((battery_data[slot_id].battery_measurement.voltage == 0) && (battery_data[slot_id].battery_measurement.current == 0) && (battery_data[slot_id].battery_measurement.temperature == 0)){
         charging_state= STATE_IDLE;
         return;
     }
 
+    // Once the battery limits are received then start with the charging
     if(battery_data[slot_id].batteryLimitReceived == 1){
+
         //1. Pre Charge: if the battery is deeply discharged
         if ((batt_voltage < batt_min_voltage)) {
             charging_state = STATE_PRE_CHARGE;
@@ -73,43 +74,46 @@ void CC_CV_UpdateChargingState(uint8_t slot_id) {
         else if (!cv_charging_started &&
                 (batt_voltage >= batt_min_voltage) &&
                 (batt_voltage < batt_max_voltage - BATTERY_THRESHOLD)) {
-        charging_state = STATE_CC_CHARGING;
+            charging_state = STATE_CC_CHARGING;
         }
         // 3. CV Charging condition: Changing the cv_charging_state to True, once the CV mode is reached
   
         else if (batt_voltage >= batt_max_voltage - BATTERY_THRESHOLD) {
-        charging_state = STATE_CV_CHARGING;
-        cv_charging_started = true;
+            charging_state = STATE_CV_CHARGING;
+            cv_charging_started = true;
         }
     
         // 4. Cut-off check inside CV
     
         else if ((charging_state == STATE_CV_CHARGING) &&
                 (batt_current <= batt_cutoff_current + BATTERY_THRESHOLD)) {
-        if (cycle_count < MAX_CYCLES) {
-            charging_state = STATE_DISCHARGING;
-            previous_cycle_counter= cycle_count;
-            cycle_count++;
-        } else {
-            charging_state = STATE_FINAL_DISCHARGE;
-        }
+            if (cycle_count < MAX_CYCLES) {
+                charging_state = STATE_DISCHARGING;
+                cycle_count++;
+            } else if(previous_cycle_number < cycle_count){
+                cycle_count= previous_cycle_number;
+            }else {
+                charging_state = STATE_FINAL_DISCHARGE;
+            }
+            
+            
         }
   
         // 5. State Discharging condition
     
         else if (charging_state == STATE_DISCHARGING &&
                 batt_voltage <= batt_min_voltage + BATTERY_THRESHOLD) {
-        DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
-        charging_state = STATE_CC_CHARGING;
-        cv_charging_started = false;
+            DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
+            charging_state = STATE_CC_CHARGING;
+            cv_charging_started = false;
         }
 
         //6. Check if the battery is overcharged
         else if (batt_voltage > batt_max_voltage) {
-        charging_state = STATE_ERROR;
+            charging_state = STATE_ERROR;
         }
     
-        // 7. Hold previous state if none of the condition matches (Previously,
+        // 8. Hold previous state if none of the condition matches (Previously,
         // CV_STATE was jumping directly to STATE_ERROR in else statement, to handle
         // the condition better, else condition is being changed).
         else {