cc_cv_charging.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #include "cc_cv_charging.h"
  2. #include "src/config.h"
  3. #include "src/battery_data/battery.h"
  4. #include <stdio.h>
  5. #include "ti/driverlib/dl_gpio.h"
  6. #include "ti_msp_dl_config.h"
  7. #include "src/controller/controller.h"
  8. static ChargingState charging_state= STATE_IDLE;
  9. static uint16_t cycle_count = 0;
  10. // declaring static global variables
  11. static uint16_t batt_voltage;
  12. static int16_t batt_current;
  13. static uint16_t batt_min_voltage;
  14. static uint16_t batt_max_voltage;
  15. static uint16_t batt_cutoff_current;
  16. static uint16_t batt_capacitance;
  17. static int16_t batt_charge_discharge;
  18. // Functions for Charging and Discharging State
  19. void CC_CV_UpdateChargingState(uint8_t slot_id) {
  20. // Flag for CV charging, the charging mode flips back to CC mode
  21. static bool cv_charging_started = false;
  22. batt_voltage = battery_data[slot_id].battery_measurement.voltage;
  23. batt_current = battery_data[slot_id].battery_measurement.current;
  24. batt_min_voltage = battery_data[slot_id].min_voltage;
  25. batt_max_voltage = battery_data[slot_id].max_voltage;
  26. batt_cutoff_current = battery_data[slot_id].cut_off_current;
  27. batt_capacitance = battery_data[slot_id].capacitance;
  28. batt_charge_discharge= battery_data[slot_id].charge_discharge;
  29. //Check if the slot is empty or if the battery limits are not set
  30. if (battery_data[slot_id].battery_state == STATE_EMPTY || (batt_min_voltage == 0 && batt_max_voltage == 0 &&
  31. batt_cutoff_current== 0 && batt_capacitance== 0)) {
  32. charging_state = STATE_IDLE;
  33. return;
  34. }
  35. //Check if the battery is overcharged
  36. if (batt_voltage > batt_max_voltage) {
  37. charging_state = STATE_ERROR;
  38. return;
  39. }
  40. //1. Pre Charge: if the battery is deeply discharged
  41. if ((batt_voltage < batt_min_voltage)) {
  42. charging_state = STATE_PRE_CHARGE;
  43. cv_charging_started = false;
  44. }
  45. // 2. Fast Charging Condition: CC Charging: cv_charging_started condition
  46. // added to avoid toggling back to CC after CV state is reached
  47. else if (!cv_charging_started &&
  48. (batt_voltage >= batt_min_voltage) &&
  49. (batt_voltage < batt_max_voltage - BATTERY_THRESHOLD)) {
  50. charging_state = STATE_CC_CHARGING;
  51. }
  52. // 3. CV Charging condition: Changing the cv_charging_state to True, once the
  53. // CV mode is reached
  54. else if (batt_voltage >= batt_max_voltage - BATTERY_THRESHOLD) {
  55. charging_state = STATE_CV_CHARGING;
  56. cv_charging_started = true;
  57. }
  58. // 4. Cut-off check inside CV
  59. else if ((charging_state == STATE_CV_CHARGING) &&
  60. (batt_current <= batt_cutoff_current + BATTERY_THRESHOLD)) {
  61. if (cycle_count < MAX_CYCLES) {
  62. charging_state = STATE_DISCHARGING;
  63. cycle_count++;
  64. } else {
  65. charging_state = STATE_FINAL_DISCHARGE;
  66. }
  67. }
  68. // 5. State Discharging condition
  69. else if (charging_state == STATE_DISCHARGING &&
  70. batt_voltage <= batt_min_voltage + BATTERY_THRESHOLD) {
  71. DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
  72. charging_state = STATE_CC_CHARGING;
  73. cv_charging_started = false;
  74. }
  75. // 6. Hold previous state if none of the condition matches (Previously,
  76. // CV_STATE was jumping directly to STATE_ERROR in else statement, to handle
  77. // the condition better, else condition is being changed).
  78. else {
  79. printf("[CC-CV] Current state: Voltage= %d mV, Current= %d mA\n",
  80. batt_voltage, batt_current);
  81. }
  82. }
  83. /*
  84. Function for Battery Charging and Discharging:
  85. ** Pre Charge: Once the battery is in discharged state, pre-charging
  86. begins. Starts charging safely with a typically low current C/10
  87. ** Constant Current: Continues until the battery voltage has reached the "full"
  88. or floating voltage level
  89. ** Constant Voltage: CV voltage starts once the maximum
  90. voltage threshold is obtained.
  91. */
  92. void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
  93. // Calling function to get all the conditional states
  94. CC_CV_UpdateChargingState(slot_id);
  95. batt_voltage = battery_data[slot_id].battery_measurement.voltage;
  96. batt_current= battery_data[slot_id].battery_measurement.current;
  97. batt_min_voltage= battery_data[slot_id].min_voltage;
  98. batt_max_voltage= battery_data[slot_id].max_voltage;
  99. batt_cutoff_current = battery_data[slot_id].cut_off_current;
  100. batt_capacitance= battery_data[slot_id].capacitance;
  101. batt_charge_discharge= battery_data[slot_id].charge_discharge;
  102. // DAC channel value:
  103. // dac_channel_value is for CV mode where the charge current is calibrated till battery current is less than (cutoff current+threshold)
  104. // Adding a variable to check the stepness in the voltage drop in CV charging
  105. // mode:
  106. static uint16_t last_voltage = 0;
  107. //flag for DAC: so that the dac value is initialized only once and then can be decremented rather than resetting to the initial value
  108. static bool dac_initialized= false;
  109. switch (charging_state) {
  110. // PRE CHARGE STATE: Battery Voltage is lesser than 3000 mVolts
  111. case STATE_PRE_CHARGE:
  112. DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB6_PIN);
  113. controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
  114. if (true) {
  115. printf("PRE CHARGING: Slot %d at %d mA.\n", slot_id, charge_current);
  116. }
  117. break;
  118. // CC CHARGING STATE: Keeps on checking the condition until battery voltage
  119. // reaches to (MAXIMUM_VOLTAGE-BATTERY_THRESHOLD)= 4150 mVolts
  120. case STATE_CC_CHARGING:
  121. controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
  122. printf("CC CHARGING: Slot %d, Current: %d mA, Voltage: %d mV.\n", slot_id, batt_current, batt_voltage);
  123. break;
  124. // CV CHARGING STATE: Keeps on checking the condition until battery current
  125. // decreases till it exceeds (CUTOFF_CURRENT_MA + BATTERY_THRESHOLD)= 290 mAs
  126. case STATE_CV_CHARGING:
  127. if (batt_current >= batt_cutoff_current + BATTERY_THRESHOLD) {
  128. // Detect steep voltage drop:
  129. if (last_voltage != 0 && (last_voltage - batt_voltage) > 100) {
  130. printf("!!! CV CHARGING: Voltage dropped too fast: %d -> %d mV.\n", last_voltage, batt_voltage);
  131. }
  132. }else {
  133. charging_state = STATE_FINAL_DISCHARGE;
  134. dac_initialized= false;
  135. }
  136. last_voltage = batt_voltage;
  137. controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
  138. break;
  139. case STATE_DISCHARGING:
  140. //DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB6_PIN);
  141. DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
  142. controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
  143. printf("DISCHARGING: Slot %d at %d mA.\n", slot_id, batt_voltage);
  144. break;
  145. case STATE_FINAL_DISCHARGE:
  146. //Once the cycle gets done, the battery state transitions to "STATE_MEASUREMENT_DONE"
  147. battery_data[slot_id].battery_state= STATE_MEASUREMENT_DONE;
  148. DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB6_PIN);
  149. DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
  150. controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, 0);
  151. charging_state = STATE_IDLE;
  152. break;
  153. case STATE_ERROR:
  154. DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB6_PIN);
  155. DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
  156. printf("ERROR: Slot %d.\n", slot_id);
  157. break;
  158. case STATE_IDLE:
  159. DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB6_PIN);
  160. DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
  161. charging_state = STATE_PRE_CHARGE;
  162. break;
  163. default:
  164. break;
  165. }
  166. }
  167. // End of the file