Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

197 Zeilen
4.2 KiB

  1. //#include <proc/p32mx440f256h.h>
  2. #include "BatteryMonitor.h"
  3. #include "BoardCfg.h"
  4. #include "timer.h"
  5. #include "ina219.h"
  6. #include "WiFiCtrl.h"
  7. #include "I2C.h"
  8. float mBatteryVoltage;
  9. int mBatteryCurrent;
  10. int mBatterySOC;
  11. float mVoltageMeanSum;
  12. int mVoltageMeanCount;
  13. unsigned int mCurrentMeanSum;
  14. int mCurrentMeanCount;
  15. bool mCurrentModuleOK;
  16. void InitBatteryMonitor()
  17. {
  18. mBatteryVoltage = 0;
  19. mBatteryCurrent = 0;
  20. mBatterySOC = 0;
  21. mVoltageMeanCount = 0;
  22. mCurrentMeanCount = 0;
  23. mCurrentModuleOK = true;
  24. TimerStart(BATTERY_MONITOR_TIMER,100);
  25. //experimental stuff!
  26. mVoltageMeanSum = 0.0;
  27. mVoltageMeanCount = 0;
  28. // if(ina219Init() == RET_ERROR)
  29. // {
  30. // mCurrentModuleOK = false;
  31. // }
  32. //ina219SetCalibration_16V_500mA();
  33. // ina219SetCalibration_16V_200mA();
  34. }
  35. void BatteryMonitorTick()
  36. {
  37. static int NetworkSendCounter; //Every second (10 counts) we want to send the battery data.
  38. if(IsTimerExpired(BATTERY_MONITOR_TIMER))
  39. {
  40. unsigned int adc;
  41. double conv, raw;
  42. AD1CHSbits.CH0SA = 1; //AN1
  43. AD1CON1bits.SAMP = 0;
  44. while(AD1CON1bits.DONE == 0);
  45. adc = ADC1BUF0;
  46. AD1CON1bits.SAMP = 1;
  47. // adc &= 0xFFFE;
  48. conv = (float)adc / 1023;
  49. conv *= 3.36;
  50. raw = conv;
  51. conv *= 11;
  52. //avoid rollovers in case the LORA network gets disconnected.
  53. //This could go for a long time but 5000 samples is too much anyways.
  54. if(mVoltageMeanCount >= 5000)
  55. {
  56. mVoltageMeanCount = 0;
  57. mVoltageMeanSum = conv;
  58. }
  59. else
  60. {
  61. mVoltageMeanCount++;
  62. mVoltageMeanSum += conv;
  63. }
  64. mBatteryVoltage = conv;
  65. TimerStart(BATTERY_MONITOR_TIMER,100);
  66. if(mCurrentModuleOK == true)
  67. {
  68. // mBatteryCurrent = ina219GetCurrent_mA();
  69. // if(I2CWasLastTransactionOK() == 0 )
  70. // {
  71. // mCurrentModuleOK = false;
  72. // }
  73. }
  74. else
  75. {
  76. unsigned int Ref = 0;
  77. AD1CHSbits.CH0SA = 0; //AN0
  78. AD1CON1bits.SAMP = 0;
  79. while(AD1CON1bits.DONE == 0);
  80. Ref = ADC1BUF0;
  81. AD1CON1bits.SAMP = 1;
  82. AD1CHSbits.CH0SA = 2; //AN2
  83. AD1CON1bits.SAMP = 0;
  84. while(AD1CON1bits.DONE == 0);
  85. adc = ADC1BUF0;
  86. AD1CON1bits.SAMP = 1;
  87. // adc &= 0xFFFE;
  88. adc -= Ref;
  89. conv = (double)adc * 1.0;
  90. conv /= 1023;
  91. conv *= 3.3; //Volts
  92. conv /= 0.05; //Amps (50mV/A)
  93. raw = conv;
  94. //avoid rollovers in case the LORA network gets disconnected.
  95. //This could go for a long time but 5000 samples is too much anyways.
  96. if(mCurrentMeanCount >= 500)
  97. {
  98. mCurrentMeanCount = 1;
  99. mCurrentMeanSum = adc;
  100. }
  101. else
  102. {
  103. mCurrentMeanCount++;
  104. mCurrentMeanSum += adc;
  105. }
  106. mBatteryCurrent = adc;
  107. //mBatteryCurrent = mCurrentMeanSum / mCurrentMeanCount;
  108. }
  109. }
  110. }
  111. float GetBatteryVoltage(int Reset)
  112. {
  113. if(Reset == 1)
  114. {
  115. mBatteryVoltage = (mVoltageMeanSum/mVoltageMeanCount);
  116. mVoltageMeanSum = 0.0;
  117. mVoltageMeanCount = 0;
  118. }
  119. return mBatteryVoltage;
  120. }
  121. int GetSolarPanelCurrent()
  122. {
  123. //mBatteryCurrent = (mCurrentMeanSum/mCurrentMeanCount);
  124. //mVoltageMeanCount = 0;
  125. mCurrentMeanSum = 0.0;
  126. return mBatteryCurrent;
  127. }
  128. float GetConvertedSolarPanelCurrent()
  129. {
  130. float Cur = (mBatteryCurrent - 2) * (3.3/1023);
  131. return Cur;
  132. }
  133. int GetBatterySOC()
  134. {
  135. return mBatterySOC;
  136. }
  137. int SendNetworkBatteryData()
  138. {
  139. // int VoltageMilliVolts = (int)(mBatteryVoltage * 1000);
  140. // int BattCurrent = mBatteryCurrent;
  141. //
  142. // char BatData[10];
  143. // BatData[0] = (char)(VoltageMilliVolts & 0x000000FF); //Battery Voltage 1
  144. // VoltageMilliVolts >>= 8;
  145. // BatData[1] = (char)(VoltageMilliVolts & 0x000000FF); //Battery Voltage 2
  146. // BatData[2] = (char)(BattCurrent & 0x000000FF); //Solar panel Current 1
  147. // BattCurrent >>= 8;
  148. // BatData[3] = (char)(BattCurrent & 0x000000FF); //Solar panel Current 2
  149. //
  150. // SendNetworkData(BatData,4);
  151. printf("Battery voltage: %f\n",mBatteryVoltage);
  152. }
  153. bool GetCurrentModuleOK()
  154. {
  155. return mCurrentModuleOK;
  156. }