Browse Source

Bug where the state was transitioning utomatically from IDLE to PRE_CHARGE has been fixed in cc_cv file

namrota ghosh 7 months ago
parent
commit
3d8b897232
5 changed files with 77 additions and 65 deletions
  1. 4 2
      main.c
  2. 0 1
      src/battery_data/battery.h
  3. 67 60
      src/cc_cv_charging.c
  4. 5 1
      src/pi/i2c_pi_target.c
  5. 1 1
      src/pi/i2c_pi_target.h

+ 4 - 2
main.c

@@ -133,14 +133,16 @@ int main(void)
             Battery_StateCondition(slot_id);
             //Reading Charging state condition
             CC_CV_ControlCharging(slot_id, 50);
+            printf("Battery Charging State: %u\n", battery_data[slot_id].battery_charging_state);
+            printf("Battery Limit Received: %u\n", battery_data[slot_id].batteryLimitReceived);
             printf("STATUS ***Reading Battery Measurement for Slot ID %u:: Battery State: %u, Voltage: %u, Current: %u, Temperature: %u, Slot state: %u***\n", slot_id, battery_data[slot_id].battery_state, battery_data[slot_id].battery_measurement.voltage,
             battery_data[slot_id].battery_measurement.current, battery_data[slot_id].battery_measurement.temperature, battery_data[slot_id].battery_measurement.slot_state);
             //If target received battery limits from Pi then start charging:       
             if(battery_data[slot_id].batteryLimitReceived){
                 printf("Battery Limits: Slot: %d, Max Voltage:%u, Min Voltage:%u, "
-                "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u\n", slot_id, battery_data[slot_id].max_voltage,
+                "Cutoff Current: %u, Capacitance:%u, Charge Fraction:%u, Cycle Number: %u\n", slot_id, battery_data[slot_id].max_voltage,
                 battery_data[slot_id].min_voltage, battery_data[slot_id].cut_off_current,
-                battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction);
+                battery_data[slot_id].capacitance, battery_data[slot_id].charge_fraction, battery_data[slot_id].cycle_number);
                 
             }
      

+ 0 - 1
src/battery_data/battery.h

@@ -77,7 +77,6 @@ typedef struct{
     uint16_t capacitance;
     uint8_t charge_fraction;
     uint8_t cycle_number;
-    uint8_t previous_cycle_number;
     BatteryChargingState battery_charging_state;
     bool batteryLimitReceived;
 } BatteryInfo;

+ 67 - 60
src/cc_cv_charging.c

@@ -22,8 +22,8 @@ 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
-static uint8_t previous_cycle_count;
+//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;
 
 // Functions for Charging and Discharging State
 
@@ -43,77 +43,84 @@ 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_count= 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 == false)) {
+    if ((battery_data[slot_id].battery_state == STATE_EMPTY) || (battery_data[slot_id].batteryLimitReceived == 0)) {
       charging_state = STATE_IDLE;
       return;
     }
-    
-    //Check if the battery is overcharged
-    if (batt_voltage > batt_max_voltage) {
-      charging_state = STATE_ERROR;
-      return;
-    }
-  
-    //1. Pre Charge: if the battery is deeply discharged
-    if ((batt_voltage < batt_min_voltage)) {
-      charging_state = STATE_PRE_CHARGE;
-      cv_charging_started = false;
-    }
-    
-    // 2. Fast Charging Condition: CC Charging: cv_charging_started condition
-    // added to avoid toggling back to CC after CV state is reached
-  
-    else if (!cv_charging_started &&
-             (batt_voltage >= batt_min_voltage) &&
-             (batt_voltage < batt_max_voltage - BATTERY_THRESHOLD)) {
-      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;
-    }
-  
-    // 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;
-        cycle_count++;
-      } else {
-        charging_state = STATE_FINAL_DISCHARGE;
-      }
+
+    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;
     }
+
+    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;
+            cv_charging_started = false;
+        }
+        // 2. Fast Charging Condition: CC Charging: cv_charging_started condition added to avoid toggling back to CC after CV state is reached
   
-    // 5. State Discharging condition
+        else if (!cv_charging_started &&
+                (batt_voltage >= batt_min_voltage) &&
+                (batt_voltage < batt_max_voltage - BATTERY_THRESHOLD)) {
+        charging_state = STATE_CC_CHARGING;
+        }
+        // 3. CV Charging condition: Changing the cv_charging_state to True, once the CV mode is reached
   
-    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;
-    }
+        else if (batt_voltage >= batt_max_voltage - BATTERY_THRESHOLD) {
+        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;
+        }
+        }
   
-    // 6. 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 {
-      printf("[CC-CV] Current state: Voltage= %d mV, Current= %d mA\n",
-             batt_voltage, batt_current);
+        // 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;
+        }
+
+        //6. Check if the battery is overcharged
+        else if (batt_voltage > batt_max_voltage) {
+        charging_state = STATE_ERROR;
+        }
+    
+        // 7. 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 {
+        printf("[CC-CV] Current state: Voltage= %d mV, Current= %d mA\n",
+                batt_voltage, batt_current);
+        }
     }
-  }
+}
+    
+    
+    
 
   /*
 Function for Battery Charging and Discharging:
@@ -216,7 +223,7 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
     case STATE_IDLE:
       DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB6_PIN);
       DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
-      charging_state = STATE_PRE_CHARGE;
+      //charging_state = STATE_PRE_CHARGE;
       break;
 
     default:

+ 5 - 1
src/pi/i2c_pi_target.c

@@ -65,7 +65,11 @@ void pi_i2c_mcu(){
     //bitmasked GET command:
     else if((receivedCommand & 0x0F)== 0x02){
         //I2Ctools command: i2cget -y 1 0x20 0xn2 i 8
-        //Get Battery Measurement data for slot_id: Voltage, Current, Temperture
+        //Get Battery Measurement data for slot_id: Voltage, Current, Temperture, Cycle Number and Cycle State
+        // on two of the testings have received:
+        //0x00 0xff 0xff 0xff 0xff 0xff 0xff 0xff
+        //0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
+
         uint8_t requestedSlot = (receivedCommand & 0xF0)>>4;
         printf("Requested slot:%d\n", requestedSlot);
         BatteryData battery_measure;

+ 1 - 1
src/pi/i2c_pi_target.h

@@ -24,7 +24,7 @@ typedef struct __attribute__((packed)){
     uint8_t cut_off_current;
     uint16_t capacitance;
     uint8_t charge_fraction;
-    //uint8_t previous_cycle_number;
+    uint8_t previous_cycle_number;
 } BatteryLimitMsg;