battery.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "battery.h"
  2. #include "ti/driverlib/dl_i2c.h"
  3. #include "ti/driverlib/dl_timerg.h"
  4. #include "ti/driverlib/dl_comp.h"
  5. #include "ti_msp_dl_config.h"
  6. #include "src/config.h"
  7. BatterySlot slot;
  8. static void set_dac(uint16_t value) {
  9. slot.dac_value = value;
  10. DL_COMP_setDACCode0(COMP_0_INST, value);
  11. }
  12. static void set_pwm(uint16_t value) {
  13. slot.pwm_value = value;
  14. DL_TimerG_setCaptureCompareValue(PWM_INST, value, DL_TIMER_CC_1_INDEX);
  15. if (value > 0 && !DL_TimerG_isRunning(PWM_INST)) {
  16. DL_TimerG_startCounter(PWM_INST);
  17. }
  18. if (value == 0 && DL_TimerG_isRunning(PWM_INST)) {
  19. DL_TimerG_stopCounter(PWM_INST);
  20. }
  21. }
  22. void slot_disable() {
  23. if (slot.dac_value != 0) {
  24. set_dac(0);
  25. }
  26. if (slot.pwm_value != 0) {
  27. set_pwm(0);
  28. }
  29. }
  30. /*Initialize battery array and default parameters*/
  31. void slot_init() {
  32. slot.measurement.state = SLOT_STATE_OK;
  33. // convinience trick:
  34. // with that we can set *battery_slots[i].state = SLOT_STATE_* or SLOT_ERR_*
  35. // like e.g. *battery_slots[i].state = SLOT_ERR_OVERTEMPERATURE
  36. slot.state = &slot.measurement.state;
  37. slot.measurement.voltage = 0;
  38. slot.measurement.current = 0;
  39. slot.measurement.temperature = 0;
  40. slot.set_current = 0;
  41. set_pwm(0);
  42. set_dac(0);
  43. }
  44. void slot_read_state() {
  45. ;
  46. }
  47. void slot_adjust_current() {
  48. if (slot.set_current > 0) {
  49. // positive current -> charge (with DAC)
  50. if (slot.pwm_value != 0) {
  51. // seems like we switched from a charging before
  52. // -> disable DAC before getting active
  53. set_pwm(0);
  54. }
  55. if (slot.set_current + BATTERY_CURRENT_THRESHOLD < slot.measurement.current) {
  56. // we are outside of the tolerance band
  57. // exceeded to the upper limit
  58. // -> update dac value, decrease the voltage
  59. if (slot.dac_value-1 >= 0) {
  60. set_dac(--slot.dac_value);
  61. }
  62. else {
  63. // we want to give more current, but we can't ?!
  64. *slot.state = SLOT_WARN_LOWER_DAC_NOT_POSSIBLE;
  65. }
  66. } else if (slot.set_current - BATTERY_CURRENT_THRESHOLD > slot.measurement.current) {
  67. // we are outside of the tolerance band
  68. // exceeded to the upplowerer limit
  69. // -> update dac value, increase the voltage
  70. if (slot.dac_value+1 <= MAX_DAC_VALUE) {
  71. set_dac(++slot.dac_value);
  72. }
  73. else {
  74. // we want to give more current, but we can't ?!
  75. *slot.state = SLOT_WARN_HIGHER_DAC_NOT_POSSIBLE;
  76. }
  77. }
  78. // no else statement here: we are ok, since we are in the tolerance measure
  79. } else if (slot.set_current < 0) {
  80. // negative current -> discharge (with PWM)
  81. if (slot.dac_value != 0) {
  82. // seems like we switched from a charging before
  83. // -> disable DAC before getting active
  84. set_dac(0);
  85. }
  86. if (slot.set_current + BATTERY_CURRENT_THRESHOLD < slot.measurement.current) {
  87. // we are outside of the tolerance band
  88. // exceeded to the upper limit
  89. // -> update pwm value, decrease the voltage
  90. if (slot.pwm_value+1 <= MAX_PWM_CYCLE) {
  91. // pwm is inverse to the DAC since dragging more current means more negative
  92. set_pwm(++slot.pwm_value);
  93. }
  94. else {
  95. // we want to give more current, but we can't ?!
  96. *slot.state = SLOT_WARN_HIGHER_PWM_NOT_POSSIBLE;
  97. }
  98. } else if (slot.set_current - BATTERY_CURRENT_THRESHOLD > slot.measurement.current) {
  99. // we are outside of the tolerance band
  100. // exceeded to the upplowerer limit
  101. // -> update pwm value, increase the voltage
  102. if (slot.pwm_value-1 >= 0) {
  103. set_pwm(--slot.pwm_value);
  104. }
  105. else {
  106. // we want to give more current, but we can't ?!
  107. *slot.state = SLOT_WARN_LOWER_PWM_NOT_POSSIBLE;
  108. }
  109. }
  110. } else {
  111. // we have 0 -> stop charging and discharging
  112. slot_disable();
  113. }
  114. }