|
@@ -8,7 +8,7 @@
|
|
|
|
|
|
|
|
static ChargingState charging_state= STATE_IDLE;
|
|
static ChargingState charging_state= STATE_IDLE;
|
|
|
static uint16_t cycle_count = 0;
|
|
static uint16_t cycle_count = 0;
|
|
|
-// declaring static global variables
|
|
|
|
|
|
|
+// declaring static global variables to retain their values between multiple function calls till the end of the program
|
|
|
static uint16_t batt_voltage;
|
|
static uint16_t batt_voltage;
|
|
|
static int16_t batt_current;
|
|
static int16_t batt_current;
|
|
|
static uint16_t batt_min_voltage;
|
|
static uint16_t batt_min_voltage;
|
|
@@ -19,8 +19,13 @@ static int16_t batt_charge_discharge;
|
|
|
// Functions for Charging and Discharging State
|
|
// Functions for Charging and Discharging State
|
|
|
|
|
|
|
|
void CC_CV_UpdateChargingState(uint8_t slot_id) {
|
|
void CC_CV_UpdateChargingState(uint8_t slot_id) {
|
|
|
|
|
+ //static variable to keep a log on the transition during charging and dischrging phases
|
|
|
|
|
+ static ChargingState previous_state= STATE_IDLE;
|
|
|
|
|
+
|
|
|
// Flag for CV charging, the charging mode flips back to CC mode
|
|
// Flag for CV charging, the charging mode flips back to CC mode
|
|
|
static bool cv_charging_started = false;
|
|
static bool cv_charging_started = false;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
batt_voltage = battery_data[slot_id].battery_measurement.voltage;
|
|
batt_voltage = battery_data[slot_id].battery_measurement.voltage;
|
|
|
batt_current = battery_data[slot_id].battery_measurement.current;
|
|
batt_current = battery_data[slot_id].battery_measurement.current;
|
|
|
batt_min_voltage = battery_data[slot_id].min_voltage;
|
|
batt_min_voltage = battery_data[slot_id].min_voltage;
|
|
@@ -28,6 +33,14 @@ void CC_CV_UpdateChargingState(uint8_t slot_id) {
|
|
|
batt_cutoff_current = battery_data[slot_id].cut_off_current;
|
|
batt_cutoff_current = battery_data[slot_id].cut_off_current;
|
|
|
batt_capacitance = battery_data[slot_id].capacitance;
|
|
batt_capacitance = battery_data[slot_id].capacitance;
|
|
|
batt_charge_discharge= battery_data[slot_id].charge_discharge;
|
|
batt_charge_discharge= battery_data[slot_id].charge_discharge;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //Log for transitions
|
|
|
|
|
+ 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
|
|
//Check if the slot is empty or if the battery limits are not set
|
|
|
if (battery_data[slot_id].battery_state == STATE_EMPTY || (batt_min_voltage == 0 && batt_max_voltage == 0 &&
|
|
if (battery_data[slot_id].battery_state == STATE_EMPTY || (batt_min_voltage == 0 && batt_max_voltage == 0 &&
|
|
|
batt_cutoff_current== 0 && batt_capacitance== 0)) {
|
|
batt_cutoff_current== 0 && batt_capacitance== 0)) {
|
|
@@ -148,20 +161,28 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
|
|
|
// Detect steep voltage drop:
|
|
// Detect steep voltage drop:
|
|
|
if (last_voltage != 0 && (last_voltage - batt_voltage) > 100) {
|
|
if (last_voltage != 0 && (last_voltage - batt_voltage) > 100) {
|
|
|
printf("!!! CV CHARGING: Voltage dropped too fast: %d -> %d mV.\n", last_voltage, batt_voltage);
|
|
printf("!!! CV CHARGING: Voltage dropped too fast: %d -> %d mV.\n", last_voltage, batt_voltage);
|
|
|
|
|
+ charging_state= STATE_ERROR;
|
|
|
|
|
+ break;
|
|
|
}
|
|
}
|
|
|
|
|
+ controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
|
|
|
}else {
|
|
}else {
|
|
|
charging_state = STATE_FINAL_DISCHARGE;
|
|
charging_state = STATE_FINAL_DISCHARGE;
|
|
|
dac_initialized= false;
|
|
dac_initialized= false;
|
|
|
}
|
|
}
|
|
|
last_voltage = batt_voltage;
|
|
last_voltage = batt_voltage;
|
|
|
- controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
|
|
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case STATE_DISCHARGING:
|
|
case STATE_DISCHARGING:
|
|
|
//DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB6_PIN);
|
|
//DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB6_PIN);
|
|
|
DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
|
|
DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
|
|
|
controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
|
|
controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
|
|
|
- printf("DISCHARGING: Slot %d at %d mA.\n", slot_id, batt_voltage);
|
|
|
|
|
|
|
+ printf("DISCHARGING: Slot %d at %d mA.\n", slot_id, batt_current);
|
|
|
|
|
+
|
|
|
|
|
+ //Safety Check
|
|
|
|
|
+ if(batt_voltage<batt_min_voltage){
|
|
|
|
|
+ charging_state= STATE_ERROR;
|
|
|
|
|
+ printf("ERROR: Battery voltage below minimum voltage during discharge\n");
|
|
|
|
|
+ }
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case STATE_FINAL_DISCHARGE:
|
|
case STATE_FINAL_DISCHARGE:
|