Jelajahi Sumber

validation order changed; first chekc if the state is EMPTY or the batteryLimittReceived is 0 then state is IDLE else check further

namrota ghosh 6 bulan lalu
induk
melakukan
7ece20b088
1 mengubah file dengan 20 tambahan dan 18 penghapusan
  1. 20 18
      src/cc_cv_charging.c

+ 20 - 18
src/cc_cv_charging.c

@@ -14,7 +14,7 @@ static bool battery_discharge= false;
 
 // Functions for Charging and Discharging State
 void CC_CV_UpdateChargingState(uint8_t slot_id) {
-    printf("Update charging state\n");
+    //printf("Update charging state\n");
     static bool cv_charging_started = false;
     BatteryInfo *battery= &battery_data[slot_id];
     
@@ -31,28 +31,29 @@ void CC_CV_UpdateChargingState(uint8_t slot_id) {
     uint8_t *previous_count= &battery->previous_cycle_number;
     battery->battery_charging_state= charging_state;
 
-    //Validate if the battery state is empty or battery limit received is false then:
+    //Validate if the battery state is empty or battery limit received is false then state should be IDLE:
     if(battery_data[slot_id].battery_state == STATE_EMPTY || battery_data[slot_id].batteryLimitReceived==0){
       battery->battery_charging_state= STATE_IDLE;
       cv_charging_started= false;
       return;
     }
-
-    //Added condition to support the edge case when the voltage drops unexpectedly to 0 during the cycle making the relays to clear
-    if(batt_voltage== 0){
-        battery_data[slot_id].counter+=1;
-        if(battery_data[slot_id].counter > 3){
-            printf("Persistent zero voltage. Setting to IDLE.\n");
-            battery->battery_charging_state = STATE_IDLE;
-            cv_charging_started = false;
-        }
-        return;
-    }
-
-    // Once the battery limits are received then start with the charging
+    
+    // Once the battery limits are received then enter the Charging and Discharging cycle:
     if(battery_data[slot_id].batteryLimitReceived == 1){
         printf("Battery Limit %d received for Slot ID %d\n", battery_data[slot_id].batteryLimitReceived, slot_id);
 
+        //Added condition to support the edge case when the voltage drops unexpectedly to 0 during the cycle making the relays to clear
+        if(batt_voltage== 0){
+            battery_data[slot_id].counter+=1;
+            if(battery_data[slot_id].counter > 3){
+                printf("Persistent zero voltage. Setting to IDLE.\n");
+                battery->battery_charging_state = STATE_IDLE;
+                __BKPT(0); //remove in production
+                cv_charging_started = false;
+            }
+            return;
+        }
+
         //Handling WWDT Reset, if the Cycle count received from Pi is greater than the current cycle:count then set the cycle_count to the previous count as received from Pi
         if (*previous_count > *cycle_count && *previous_count < MAX_CYCLES){
             printf("WWDT Reset detected. Setting cycle count to previous count: %d\n", *previous_count);
@@ -74,7 +75,7 @@ void CC_CV_UpdateChargingState(uint8_t slot_id) {
         }  
 
         // 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) {
+        else if (batt_voltage >= batt_max_voltage - BATTERY_THRESHOLD && batt_voltage <= batt_max_voltage) {
             battery->battery_charging_state = STATE_CV_CHARGING;
             cv_charging_started = true;
             printf("CV CHARGING\n");
@@ -108,7 +109,7 @@ void CC_CV_UpdateChargingState(uint8_t slot_id) {
         //6. Check if the battery is overcharged
         else if (batt_voltage > batt_max_voltage) {
             battery->battery_charging_state = STATE_ERROR;
-            printf("ERROR\n");
+            printf("ERROR: Battery Voltage exceeded to %d\n", batt_voltage);
         }
 
         //Handling MCU reset:
@@ -157,7 +158,7 @@ void CC_CV_ControlCharging(uint8_t slot_id) {
     
     //setting charge and discharge current:
     int16_t charge_current= ((batt_capacitance * charge_fraction)/100);
-    printf("Charge Current set to %d\n", charge_current);
+    //printf("Charge Current set to %d\n", charge_current);
     
     switch (battery->battery_charging_state) {
     // PRE CHARGE STATE: Battery Voltage is lesser than 3000 mVolts
@@ -236,6 +237,7 @@ void CC_CV_ControlCharging(uint8_t slot_id) {
       Battery_disableDischarging(slot_id);
       printf("ERROR: Slot %d.\n", slot_id);
       chargeCurrent_initialized= false;
+      battery->batteryLimitReceived= false;
       break;
   
     case STATE_IDLE: