|
@@ -14,7 +14,7 @@ static bool battery_discharge= false;
|
|
|
|
|
|
|
|
// 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) {
|
|
|
-
|
|
|
|
|
|
|
+ printf("Update charging state\n");
|
|
|
static bool cv_charging_started = false;
|
|
static bool cv_charging_started = false;
|
|
|
BatteryInfo *battery= &battery_data[slot_id];
|
|
BatteryInfo *battery= &battery_data[slot_id];
|
|
|
|
|
|
|
@@ -38,73 +38,71 @@ void CC_CV_UpdateChargingState(uint8_t slot_id) {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //Check if the slot is empty or if the battery limits are not set
|
|
|
|
|
- if ((battery_data[slot_id].battery_state == STATE_EMPTY) || (battery->batteryLimitReceived == 0)) {
|
|
|
|
|
- battery->battery_charging_state= STATE_IDLE;
|
|
|
|
|
- cv_charging_started= false;
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- //Checking the condition where all the battery measurements are NULL
|
|
|
|
|
- if((batt_voltage== 0) && (batt_current == 0)){
|
|
|
|
|
|
|
+ //Checking the condition where all the battery measurements are NULL: STATE
|
|
|
|
|
+ if((batt_voltage== 0) || (batt_current == 0)){
|
|
|
battery->battery_charging_state= STATE_IDLE;
|
|
battery->battery_charging_state= STATE_IDLE;
|
|
|
cv_charging_started= false;
|
|
cv_charging_started= false;
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // Once the battery limits are received then start with the charging
|
|
|
|
|
- if(battery->batteryLimitReceived == 1){
|
|
|
|
|
- printf("Battery Limit Received:%d\n", battery->batteryLimitReceived);
|
|
|
|
|
|
|
+ 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;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ // Once the battery limits are received then start with the charging
|
|
|
|
|
+ if(battery_data[slot_id].batteryLimitReceived == 1){
|
|
|
|
|
+ printf("Battery Limit %d received for Slot ID %d\n", battery_data[slot_id].batteryLimitReceived, slot_id);
|
|
|
//1. Pre Charge: if the battery is deeply discharged
|
|
//1. Pre Charge: if the battery is deeply discharged
|
|
|
if ((batt_voltage < batt_min_voltage)) {
|
|
if ((batt_voltage < batt_min_voltage)) {
|
|
|
- charging_state = STATE_PRE_CHARGE;
|
|
|
|
|
|
|
+ battery->battery_charging_state = STATE_PRE_CHARGE;
|
|
|
printf("PRE CHARGING\n");
|
|
printf("PRE CHARGING\n");
|
|
|
cv_charging_started = false;
|
|
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
|
|
// 2. Fast Charging Condition: CC Charging: cv_charging_started condition added to avoid toggling back to CC after CV state is reached
|
|
|
else if ((!battery_discharge) && (!cv_charging_started) && (batt_voltage >= batt_min_voltage) && (batt_voltage < batt_max_voltage - BATTERY_THRESHOLD)){
|
|
else if ((!battery_discharge) && (!cv_charging_started) && (batt_voltage >= batt_min_voltage) && (batt_voltage < batt_max_voltage - BATTERY_THRESHOLD)){
|
|
|
- charging_state = STATE_CC_CHARGING;
|
|
|
|
|
|
|
+ battery->battery_charging_state = STATE_CC_CHARGING;
|
|
|
printf("CC CHARGING\n");
|
|
printf("CC CHARGING\n");
|
|
|
cv_charging_started = false;
|
|
cv_charging_started = false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 3. CV Charging condition: Changing the cv_charging_state to True, once the CV mode is reached
|
|
// 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) {
|
|
|
- charging_state = STATE_CV_CHARGING;
|
|
|
|
|
|
|
+ battery->battery_charging_state = STATE_CV_CHARGING;
|
|
|
cv_charging_started = true;
|
|
cv_charging_started = true;
|
|
|
printf("CV CHARGING\n");
|
|
printf("CV CHARGING\n");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//4. When in CV state
|
|
//4. When in CV state
|
|
|
- else if(charging_state== STATE_CV_CHARGING && battery_discharge== true){
|
|
|
|
|
|
|
+ else if(battery->battery_charging_state== STATE_CV_CHARGING && battery_discharge== true){
|
|
|
if(*cycle_count < MAX_CYCLES){
|
|
if(*cycle_count < MAX_CYCLES){
|
|
|
- charging_state= STATE_DISCHARGING;
|
|
|
|
|
|
|
+ battery->battery_charging_state= STATE_DISCHARGING;
|
|
|
cv_charging_started= false;
|
|
cv_charging_started= false;
|
|
|
printf("Transition to DISCHARGING, new cycle: %d\n", battery->cycle_number);
|
|
printf("Transition to DISCHARGING, new cycle: %d\n", battery->cycle_number);
|
|
|
}else {
|
|
}else {
|
|
|
- charging_state = STATE_FINAL_DISCHARGE;
|
|
|
|
|
|
|
+ battery->battery_charging_state = STATE_FINAL_DISCHARGE;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 5. State Discharging condition
|
|
// 5. State Discharging condition
|
|
|
- else if (charging_state == STATE_DISCHARGING){
|
|
|
|
|
|
|
+ else if (battery->battery_charging_state == STATE_DISCHARGING){
|
|
|
if(batt_voltage <= batt_min_voltage + BATTERY_THRESHOLD){
|
|
if(batt_voltage <= batt_min_voltage + BATTERY_THRESHOLD){
|
|
|
- DL_GPIO_clearPins(GPIO_Battery_Discharging_PIN_L0_PORT, GPIO_Battery_Discharging_PIN_L0_PIN);
|
|
|
|
|
- charging_state = STATE_CC_CHARGING;
|
|
|
|
|
|
|
+ Battery_disableDischarging(slot_id);
|
|
|
|
|
+ battery->battery_charging_state = STATE_CC_CHARGING;
|
|
|
(*cycle_count)++;
|
|
(*cycle_count)++;
|
|
|
cv_charging_started = false;
|
|
cv_charging_started = false;
|
|
|
battery_discharge= false;
|
|
battery_discharge= false;
|
|
|
printf("DISCHARGE -> CC CHARGING\n");
|
|
printf("DISCHARGE -> CC CHARGING\n");
|
|
|
}else{
|
|
}else{
|
|
|
- charging_state= STATE_DISCHARGING;
|
|
|
|
|
|
|
+ battery->battery_charging_state= STATE_DISCHARGING;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//6. Check if the battery is overcharged
|
|
//6. Check if the battery is overcharged
|
|
|
else if (batt_voltage > batt_max_voltage) {
|
|
else if (batt_voltage > batt_max_voltage) {
|
|
|
- charging_state = STATE_ERROR;
|
|
|
|
|
|
|
+ battery->battery_charging_state = STATE_ERROR;
|
|
|
printf("ERROR\n");
|
|
printf("ERROR\n");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -114,7 +112,7 @@ void CC_CV_UpdateChargingState(uint8_t slot_id) {
|
|
|
// the condition better, else condition is being changed).
|
|
// the condition better, else condition is being changed).
|
|
|
else {
|
|
else {
|
|
|
printf("[CC-CV] Current state: Voltage= %d mV, Current= %d mA\n, Current Cycle= %c\n",
|
|
printf("[CC-CV] Current state: Voltage= %d mV, Current= %d mA\n, Current Cycle= %c\n",
|
|
|
- batt_voltage, batt_current, charging_state);
|
|
|
|
|
|
|
+ batt_voltage, batt_current, battery->battery_charging_state);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -155,10 +153,11 @@ void CC_CV_ControlCharging(uint8_t slot_id) {
|
|
|
//setting charge and discharge current:
|
|
//setting charge and discharge current:
|
|
|
int16_t charge_current= ((batt_capacitance * charge_fraction)/100);
|
|
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 (charging_state) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ switch (battery->battery_charging_state) {
|
|
|
// PRE CHARGE STATE: Battery Voltage is lesser than 3000 mVolts
|
|
// PRE CHARGE STATE: Battery Voltage is lesser than 3000 mVolts
|
|
|
case STATE_PRE_CHARGE:
|
|
case STATE_PRE_CHARGE:
|
|
|
- DL_GPIO_setPins(GPIO_Battery_Charging_PIN_0_PORT, GPIO_Battery_Charging_PIN_0_PIN);
|
|
|
|
|
|
|
+ Battery_enableCharging(slot_id);
|
|
|
controller_SetCurrent(slot_id, charge_current);
|
|
controller_SetCurrent(slot_id, charge_current);
|
|
|
printf("PRE CHARGING: Slot %d at %d mA.\n", slot_id, charge_current);
|
|
printf("PRE CHARGING: Slot %d at %d mA.\n", slot_id, charge_current);
|
|
|
chargeCurrent_initialized= false;
|
|
chargeCurrent_initialized= false;
|
|
@@ -167,7 +166,7 @@ void CC_CV_ControlCharging(uint8_t slot_id) {
|
|
|
// CC CHARGING STATE: Keeps on checking the condition until battery voltage
|
|
// CC CHARGING STATE: Keeps on checking the condition until battery voltage
|
|
|
// reaches to (MAXIMUM_VOLTAGE(4200)-BATTERY_THRESHOLD(20))= 4180 mV
|
|
// reaches to (MAXIMUM_VOLTAGE(4200)-BATTERY_THRESHOLD(20))= 4180 mV
|
|
|
case STATE_CC_CHARGING:
|
|
case STATE_CC_CHARGING:
|
|
|
- DL_GPIO_setPins(GPIO_Battery_Charging_PIN_0_PORT, GPIO_Battery_Charging_PIN_0_PIN);
|
|
|
|
|
|
|
+ Battery_enableCharging(slot_id);
|
|
|
controller_SetCurrent(slot_id, charge_current);
|
|
controller_SetCurrent(slot_id, charge_current);
|
|
|
printf("CC CHARGING: Slot %d, Current: %d mA, Voltage: %d mV.\n", slot_id, batt_current, batt_voltage);
|
|
printf("CC CHARGING: Slot %d, Current: %d mA, Voltage: %d mV.\n", slot_id, batt_current, batt_voltage);
|
|
|
chargeCurrent_initialized= false;
|
|
chargeCurrent_initialized= false;
|
|
@@ -176,7 +175,7 @@ void CC_CV_ControlCharging(uint8_t slot_id) {
|
|
|
// CV CHARGING STATE: Keeps on checking the condition until battery current
|
|
// CV CHARGING STATE: Keeps on checking the condition until battery current
|
|
|
// decreases till it lowers to (CUTOFF_CURRENT_MA + BATTERY_THRESHOLD)
|
|
// decreases till it lowers to (CUTOFF_CURRENT_MA + BATTERY_THRESHOLD)
|
|
|
case STATE_CV_CHARGING:
|
|
case STATE_CV_CHARGING:
|
|
|
- DL_GPIO_setPins(GPIO_Battery_Charging_PIN_0_PORT, GPIO_Battery_Charging_PIN_0_PIN);
|
|
|
|
|
|
|
+ Battery_enableCharging(slot_id);
|
|
|
if(!chargeCurrent_initialized){
|
|
if(!chargeCurrent_initialized){
|
|
|
cvChargingPhaseCurrent= charge_current;
|
|
cvChargingPhaseCurrent= charge_current;
|
|
|
chargeCurrent_initialized= true;
|
|
chargeCurrent_initialized= true;
|
|
@@ -185,7 +184,7 @@ void CC_CV_ControlCharging(uint8_t slot_id) {
|
|
|
if (batt_current >= batt_cutoff_current + BATTERY_THRESHOLD) {
|
|
if (batt_current >= batt_cutoff_current + BATTERY_THRESHOLD) {
|
|
|
|
|
|
|
|
if(cvChargingPhaseCurrent >= batt_cutoff_current){
|
|
if(cvChargingPhaseCurrent >= batt_cutoff_current){
|
|
|
- printf("DAC CV Current: %d\n", cvChargingPhaseCurrent);
|
|
|
|
|
|
|
+ printf("DAC CV Current: %d, Battery Voltage: %d, Battery Current: %d\n", cvChargingPhaseCurrent, batt_voltage, batt_current);
|
|
|
cvChargingPhaseCurrent-= 5;
|
|
cvChargingPhaseCurrent-= 5;
|
|
|
controller_SetCurrent(slot_id, cvChargingPhaseCurrent);
|
|
controller_SetCurrent(slot_id, cvChargingPhaseCurrent);
|
|
|
}else{
|
|
}else{
|
|
@@ -196,18 +195,18 @@ void CC_CV_ControlCharging(uint8_t slot_id) {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
last_voltage = batt_voltage;
|
|
last_voltage = batt_voltage;
|
|
|
- printf("CV CHARGING: Slot %d, Current: %d mA, Voltage: %d mV.\n", slot_id, batt_current, batt_voltage);
|
|
|
|
|
|
|
+ //printf("CV CHARGING: Slot %d, Current: %d mA, Voltage: %d mV.\n", slot_id, batt_current, batt_voltage);
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case STATE_DISCHARGING:
|
|
case STATE_DISCHARGING:
|
|
|
- DL_GPIO_setPins(GPIO_Battery_Charging_PIN_0_PORT, GPIO_Battery_Charging_PIN_0_PIN);
|
|
|
|
|
- DL_GPIO_setPins(GPIO_Battery_Discharging_PIN_L0_PORT, GPIO_Battery_Discharging_PIN_L0_PIN);
|
|
|
|
|
|
|
+ Battery_enableCharging(slot_id);
|
|
|
|
|
+ Battery_enableDischarging(slot_id);
|
|
|
controller_SetCurrent(slot_id, (-1 * charge_current));
|
|
controller_SetCurrent(slot_id, (-1 * charge_current));
|
|
|
printf("DISCHARGING: Slot %d at %d mA.\n", slot_id, batt_current);
|
|
printf("DISCHARGING: Slot %d at %d mA.\n", slot_id, batt_current);
|
|
|
chargeCurrent_initialized= false;
|
|
chargeCurrent_initialized= false;
|
|
|
//Safety Check
|
|
//Safety Check
|
|
|
if(batt_voltage< batt_min_voltage){
|
|
if(batt_voltage< batt_min_voltage){
|
|
|
- charging_state= STATE_ERROR;
|
|
|
|
|
|
|
+ battery->battery_charging_state= STATE_ERROR;
|
|
|
printf("ERROR: Battery voltage below minimum voltage during discharge\n");
|
|
printf("ERROR: Battery voltage below minimum voltage during discharge\n");
|
|
|
}
|
|
}
|
|
|
break;
|
|
break;
|
|
@@ -217,26 +216,26 @@ void CC_CV_ControlCharging(uint8_t slot_id) {
|
|
|
if((batt_voltage >= HEALTHY_BATTERY_VALUE) && (batt_voltage <= batt_max_voltage - BATTERY_THRESHOLD)){
|
|
if((batt_voltage >= HEALTHY_BATTERY_VALUE) && (batt_voltage <= batt_max_voltage - BATTERY_THRESHOLD)){
|
|
|
//Once the cycle gets done, the battery state transitions to "STATE_MEASUREMENT_DONE"
|
|
//Once the cycle gets done, the battery state transitions to "STATE_MEASUREMENT_DONE"
|
|
|
battery_data[slot_id].battery_state= STATE_MEASUREMENT_DONE;
|
|
battery_data[slot_id].battery_state= STATE_MEASUREMENT_DONE;
|
|
|
- DL_GPIO_clearPins(GPIO_Battery_Charging_PIN_0_PORT, GPIO_Battery_Charging_PIN_0_PIN);
|
|
|
|
|
- DL_GPIO_clearPins(GPIO_Battery_Discharging_PIN_L0_PORT, GPIO_Battery_Discharging_PIN_L0_PIN);
|
|
|
|
|
|
|
+ Battery_disableCharging(slot_id);
|
|
|
|
|
+ Battery_disableDischarging(slot_id);
|
|
|
printf("Final Discharge with charge current:%d\n", charge_current);
|
|
printf("Final Discharge with charge current:%d\n", charge_current);
|
|
|
controller_SetCurrent(slot_id, 0);
|
|
controller_SetCurrent(slot_id, 0);
|
|
|
}else{
|
|
}else{
|
|
|
- charging_state= STATE_CC_CHARGING;
|
|
|
|
|
|
|
+ battery->battery_charging_state= STATE_CC_CHARGING;
|
|
|
}
|
|
}
|
|
|
chargeCurrent_initialized= false;
|
|
chargeCurrent_initialized= false;
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case STATE_ERROR:
|
|
case STATE_ERROR:
|
|
|
- DL_GPIO_clearPins(GPIO_Battery_Charging_PIN_0_PORT, GPIO_Battery_Charging_PIN_0_PIN);
|
|
|
|
|
- DL_GPIO_clearPins(GPIO_Battery_Discharging_PIN_L0_PORT, GPIO_Battery_Discharging_PIN_L0_PIN);
|
|
|
|
|
|
|
+ Battery_disableCharging(slot_id);
|
|
|
|
|
+ Battery_disableDischarging(slot_id);
|
|
|
printf("ERROR: Slot %d.\n", slot_id);
|
|
printf("ERROR: Slot %d.\n", slot_id);
|
|
|
chargeCurrent_initialized= false;
|
|
chargeCurrent_initialized= false;
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
case STATE_IDLE:
|
|
case STATE_IDLE:
|
|
|
- DL_GPIO_clearPins(GPIO_Battery_Charging_PIN_0_PORT, GPIO_Battery_Charging_PIN_0_PIN);
|
|
|
|
|
- DL_GPIO_clearPins(GPIO_Battery_Discharging_PIN_L0_PORT, GPIO_Battery_Discharging_PIN_L0_PIN);
|
|
|
|
|
|
|
+ Battery_disableCharging(slot_id);
|
|
|
|
|
+ Battery_disableDischarging(slot_id);
|
|
|
break;
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
default:
|