Advertisement
ecobayod

Untitled

Apr 18th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.70 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <stdint.h>
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>
  5. #include <LiquidCrystal.h>
  6. #include "Linduino.h"
  7. #include "LT_SPI.h"
  8. #include "UserInterface.h"
  9. #include "LTC68042.h"
  10. #include "Average.h"
  11.  
  12.  
  13. #define TOTAL_IC 1  // Number of ICs in the isoSPI network LTC6804-2 ICs must be addressed in ascending order starting at 0.
  14.  
  15. /***** Pack and sensor characteristics *****/
  16. const float MAX_CURRENT = 5000000000.;       // Maximum battery current(amps) before relay opens
  17. const float MAX_TEMP = 50.;                  // Maximum pack temperature (deg C) before relay opens
  18. const float MIN_CELL_V = 2.20;               // Minimum allowable cell voltage. Depends on battery chemistry.
  19. const float MAX_CELL_V = 3.60;               // Maximum allowable cell voltage. Depends on battery chemistry.
  20. const float CELL_BALANCE_THRESHOLD_V = 3.3;  // Cell balancing occurrs when voltage is above this value
  21.  
  22. /******** Arduino pin definitions ********/
  23. int chargeRelayPin = 8;     // Relay output for overcharge conditions
  24. int dischargeRelayPin = 9;  // Relay output for undercharge conditions
  25. int currentPin = A2;        // LEM Input should be Vcc/2 + I * 1.667 / LEM_RANGE
  26. int tempPin = A3;
  27.  
  28. /********  Variables for tracking cell voltages and states ***************/
  29. int overCharge_state = LOW;   // Over charge state. HIGH = relay on, LOW = relay off
  30. int underCharge_state = LOW;  // Under charge state. HIGH = relay on, LOW = relay off
  31. int overTemp_state = LOW;     // Over temperature state. HIGH = relay on, LOW = relay off
  32. int overCurrent_state = LOW;  // Over current state. HIGH = relay on, LOW = relay off
  33. int chargeRelay_state;
  34. int dischargeRelay_state;
  35. int cellMax_i;    // Temporary variable for holding index of cell with max voltage
  36. int cellMin_i;    // Temporary variable for holding index of cell with min voltage
  37. float cellMin_V;  // Temporary variable for holding  min measured cell voltage
  38. float cellMax_V;  // Temporary variable for holding  max measured cell voltage
  39. float minV1;
  40. float maxV1;
  41.  
  42.  
  43. /********   Current and temperature variables ***********************/
  44. const uint16_t imax = 100;  // Size of arrays for averaging read measurements
  45. Average<float> lemHistory(imax);
  46. Average<float> lemBiasHistory(imax);
  47. float lem = 0;
  48. float lemBias = 0;
  49. float lemZeroCal = 0;
  50. float current = 0;
  51.  
  52. #define MAX_TEMP_SENSOR 2
  53. OneWire owTemp(tempPin);
  54. DallasTemperature tempSensors(&owTemp);
  55. int tempSensorCount = 0;
  56. float temp[MAX_TEMP_SENSOR];
  57.  
  58. LiquidCrystal lcd(6, 7, 9, 10, 11, 12);
  59.  
  60. int error = 0;
  61. unsigned long tstart;
  62.  
  63.  
  64.  
  65. /******************************************************
  66.   Global Battery Variables received from 6804 commands
  67.   These variables store the results from the LTC6804
  68.   register reads and the array lengths must be based
  69.   on the number of ICs on the stack
  70.  ******************************************************/
  71.  
  72.  
  73. uint16_t cell_codes[TOTAL_IC][12];
  74. /*!<
  75.   The cell codes will be stored in the cell_codes[][12] array in the following format:
  76.  
  77.   |  cell_codes[0][0]| cell_codes[0][1] |  cell_codes[0][2]|    .....     |  cell_codes[0][11]|  cell_codes[1][0] | cell_codes[1][1]|  .....   |
  78.   |------------------|------------------|------------------|--------------|-------------------|-------------------|-----------------|----------|
  79.   |IC1 Cell 1        |IC1 Cell 2        |IC1 Cell 3        |    .....     |  IC1 Cell 12      |IC2 Cell 1         |IC2 Cell 2       | .....    |
  80. ****/
  81.  
  82. uint16_t aux_codes[TOTAL_IC][6];
  83. /*!<
  84.   The GPIO codes will be stored in the aux_codes[][6] array in the following format:
  85.  
  86.   |  aux_codes[0][0]| aux_codes[0][1] |  aux_codes[0][2]|  aux_codes[0][3]|  aux_codes[0][4]|  aux_codes[0][5]| aux_codes[1][0] |aux_codes[1][1]|  .....    |
  87.   |-----------------|-----------------|-----------------|-----------------|-----------------|-----------------|-----------------|---------------|-----------|
  88.   |IC1 GPIO1        |IC1 GPIO2        |IC1 GPIO3        |IC1 GPIO4        |IC1 GPIO5        |IC1 Vref2        |IC2 GPIO1        |IC2 GPIO2      |  .....    |
  89. */
  90.  
  91. uint8_t tx_cfg[TOTAL_IC][6];
  92. /*!<
  93.   The tx_cfg[][6] stores the LTC6804 configuration data that is going to be written
  94.   to the LTC6804 ICs on the daisy chain. The LTC6804 configuration data that will be
  95.   written should be stored in blocks of 6 bytes. The array should have the following format:
  96.  
  97.   |  tx_cfg[0][0]| tx_cfg[0][1] |  tx_cfg[0][2]|  tx_cfg[0][3]|  tx_cfg[0][4]|  tx_cfg[0][5]| tx_cfg[1][0] |  tx_cfg[1][1]|  tx_cfg[1][2]|  .....    |
  98.   |--------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|-----------|
  99.   |IC1 CFGR0     |IC1 CFGR1     |IC1 CFGR2     |IC1 CFGR3     |IC1 CFGR4     |IC1 CFGR5     |IC2 CFGR0     |IC2 CFGR1     | IC2 CFGR2    |  .....    |
  100.  
  101. */
  102.  
  103. uint8_t rx_cfg[TOTAL_IC][8];
  104. /*!<
  105.   the rx_cfg[][8] array stores the data that is read back from a LTC6804-1 daisy chain.
  106.   The configuration data for each IC  is stored in blocks of 8 bytes. Below is an table illustrating the array organization:
  107.  
  108.   |rx_config[0][0]|rx_config[0][1]|rx_config[0][2]|rx_config[0][3]|rx_config[0][4]|rx_config[0][5]|rx_config[0][6]  |rx_config[0][7] |rx_config[1][0]|rx_config[1][1]|  .....    |
  109.   |---------------|---------------|---------------|---------------|---------------|---------------|-----------------|----------------|---------------|---------------|-----------|
  110.   |IC1 CFGR0      |IC1 CFGR1      |IC1 CFGR2      |IC1 CFGR3      |IC1 CFGR4      |IC1 CFGR5      |IC1 PEC High     |IC1 PEC Low     |IC2 CFGR0      |IC2 CFGR1      |  .....    |
  111. */
  112.  
  113.  
  114.  
  115.  
  116. /*!**********************************************************************
  117.   \brief  Inititializes hardware and variables
  118.  ***********************************************************************/
  119. void setup() {
  120.   pinMode(chargeRelayPin, OUTPUT);
  121.   pinMode(dischargeRelayPin, OUTPUT);
  122.   pinMode(currentPin, INPUT);
  123.   digitalWrite(dischargeRelayPin, LOW);  // turn off relays during setup
  124.   digitalWrite(chargeRelayPin, LOW);     // turn off relays during setup
  125.  
  126.   overCharge_state = HIGH;
  127.   underCharge_state = HIGH;
  128.  
  129.  
  130.  
  131.   Serial.begin(9600);
  132.   LTC6804_initialize();  //Initialize LTC6804 hardware
  133.   init_cfg();            //initialize the 6804 configuration array to be written
  134.   delay(1000);
  135.   lemZeroCal = zeroCurrentCalibrate();  // Calibrates LEM sensor at zero current
  136.   overCurrent_state = HIGH;
  137.   tstart = millis();
  138.  
  139.  
  140.   tempSensors.begin();
  141.   tempSensorCount = tempSensors.getDeviceCount();
  142.   Serial.print("Devive count: ");
  143.   Serial.println(tempSensorCount);
  144.  
  145.   lcd.begin(20, 4);
  146. }
  147.  
  148.  
  149.  
  150. /*!*********************************************************************
  151.   \brief main loop
  152. ***********************************************************************/
  153. void loop() {
  154.  
  155.  
  156.   //  while (overCurrent_state == LOW) {
  157.   //    Serial.println("RESET TO CONTINUE");
  158.   //    delay(10000);
  159.   //  }
  160.  
  161.   // read current:
  162.   //overCurrent_state = HIGH;
  163.   current = readCurrent();
  164.  
  165.  
  166.   // read temperatures:
  167.   overTemp_state = HIGH;
  168.   readTemp();
  169.   for (int i = 0; i < MAX_TEMP_SENSOR && i < tempSensorCount; i++) {
  170.     if (temp[i] > MAX_TEMP) {
  171.       overTemp_state = LOW;
  172.       Serial.println("OVER TEMPERATURE STATE DETECTED.");
  173.     }
  174.   }
  175.  
  176.   // read cells:
  177.   wakeup_idle();
  178.   LTC6804_adcv();  // do cell AD conversion and fill cell registers
  179.   delay(10);
  180.   wakeup_idle();
  181.   error = LTC6804_rdcv(0, TOTAL_IC, cell_codes);  // read cell voltages from registers
  182.  
  183.   if (error == -1) {
  184.     Serial.println("A PEC error was detected in the received data");
  185.   }
  186.  
  187.   // test for over charge/undercharge states:
  188.   minV1 = MIN_CELL_V;
  189.   maxV1 = MAX_CELL_V;
  190.  
  191.   if (overCharge_state == LOW) {  // add hysteresis
  192.     maxV1 = maxV1 - .2;
  193.   }
  194.  
  195.   if (underCharge_state == LOW) {  // add hysteresis
  196.     minV1 = minV1 + .2;
  197.   }
  198.  
  199.   // get maximum and minimum cells:
  200.   cellMax_i = -1;
  201.   cellMin_i = -1;
  202.   cellMin_V = 100.;
  203.   cellMax_V = 0.;
  204.   for (int i = 0; i < 12; i++) {
  205.     float V = cell_codes[0][i] * 0.0001;
  206.     if (V < cellMin_V) {
  207.       cellMin_V = V;
  208.       cellMin_i = i;
  209.     }
  210.     if (V > cellMax_V) {
  211.       cellMax_V = V;
  212.       cellMax_i = i;
  213.     }
  214.   }
  215.  
  216.   underCharge_state = HIGH;
  217.   overCharge_state = HIGH;
  218.   overCurrent_state = HIGH;
  219.  
  220.   if (cellMin_V <= minV1) {
  221.     underCharge_state = LOW;
  222.     // Serial.println("V <= MIN_CELL_V");
  223.   }
  224.   if (cellMax_V >= maxV1) {
  225.     overCharge_state = LOW;
  226.     //Serial.println("V >= MAX_CELL_V");
  227.   }
  228.   if (abs(current) > MAX_CURRENT) {
  229.     overCurrent_state = LOW;
  230.   }
  231.   // set relay states:
  232.  
  233.   chargeRelay_state = overCurrent_state && underCharge_state && overCharge_state && overTemp_state;
  234.   dischargeRelay_state = overCurrent_state && overCharge_state && underCharge_state && overTemp_state;
  235.   digitalWrite(chargeRelayPin, chargeRelay_state);
  236.   digitalWrite(dischargeRelayPin, dischargeRelay_state);
  237.  
  238.   // print to serial outputs:
  239.   print_cells();
  240.  
  241.   //while (chargeRelay_state == LOW || dischargeRelay_state== LOW ) {
  242.   //  Serial.println("RESET TO CONTINUE");
  243.   //  delay(10000);
  244.   //}
  245.  
  246.   if (abs(current) > MAX_CURRENT) {
  247.     Serial.println("OVER CURRENT STATE DETECTED.");
  248.   }
  249.  
  250.   if (underCharge_state == LOW) {
  251.     Serial.println("UNDER VOLTAGE STATE DETECTED.");
  252.   }
  253.  
  254.   if (overCharge_state == LOW) {
  255.     Serial.println("OVER VOLTAGE STATE DETECTED.");
  256.   }
  257.  
  258.   //  if (abs(current) > MAX_CURRENT) {
  259.   //    overCurrent_state = LOW;
  260.   //    digitalWrite(chargeRelayPin,  overCurrent_state  );
  261.   //    digitalWrite(dischargeRelayPin,  overCurrent_state );
  262.   //    Serial.println("OVER CURRENT STATE DETECTED. PRESS RESET TO CONTINUE");
  263.   //    delay(10000);
  264.   //  } else {
  265.   //    overCurrent_state = HIGH;
  266.   //    //    chargeRelay_state = overCharge_state &&  overTemp_state ;
  267.   //    //    dischargeRelay_state =  underCharge_state &&  overTemp_state;
  268.   //    chargeRelay_state = underCharge_state && overCharge_state &&  overTemp_state ;
  269.   //    dischargeRelay_state =  overCharge_state && underCharge_state &&  overTemp_state;
  270.   //    digitalWrite(chargeRelayPin, chargeRelay_state  );
  271.   //    digitalWrite(dischargeRelayPin, dischargeRelay_state);
  272.   //  }
  273.  
  274.   //
  275.   //  if (underCharge_state == LOW ) {
  276.   //    Serial.println("UNDER VOLTAGE STATE DETECTED.");
  277.   //    digitalWrite(dischargeRelayPin, dischargeRelay_state);
  278.   //  }
  279.   //
  280.   //  if (overCharge_state == LOW ) {
  281.   //    Serial.println("OVER VOLTAGE STATE DETECTED.");
  282.   //    digitalWrite(chargeRelayPin, chargeRelay_state  );
  283.   //  }
  284.  
  285.  
  286.   // take advantage of open relay to recalibrate LEM zero current setting:
  287.   if (underCharge_state == LOW or overCharge_state == LOW) {
  288.     lemZeroCal = zeroCurrentCalibrate();
  289.   }
  290.  
  291.  
  292.   //  cell balancing:
  293.   //  Turn on switch Sx for highest cell x if voltage is above threshold
  294.   //  Note: DCP is set to 0 in initialize() This turns off discharge when cell voltages are read.
  295.   // set values in tx_cfg
  296.  
  297.  
  298.   //cellMax_i = 5;
  299.   if (cellMax_V >= CELL_BALANCE_THRESHOLD_V) {
  300.     balance_cfg(0, cellMax_i);
  301.     //Serial.print("Balance ");
  302.     //Serial.println(cellMax_i);
  303.   } else {
  304.     balance_cfg(0, -1);
  305.   }
  306.  
  307.   // write tx_cfg to LTC6804. This sets the LTC6804 DCCx registers which control the S pins for balancing:
  308.   LTC6804_wrcfg(TOTAL_IC, tx_cfg);
  309.  
  310.   delay(5000);
  311. }
  312.  
  313.  
  314.  
  315. /*!***********************************
  316.   \brief Initializes the configuration array
  317.  **************************************/
  318. void init_cfg() {
  319.   for (int i = 0; i < TOTAL_IC; i++) {
  320.     tx_cfg[i][0] = 0xFE;
  321.     //tx_cfg[i][1] = 0x04 ;
  322.     tx_cfg[i][1] = 0x4E1;  // 2.0V
  323.     //tx_cfg[i][2] = 0xE1 ;
  324.     tx_cfg[i][2] = 0x8CA;  // 3.6V
  325.     tx_cfg[i][3] = 0x00;
  326.     tx_cfg[i][4] = 0x00;  // discharge switches  0->off  1-> on.  S0 = 0x01, S1 = 0x02, S2 = 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
  327.     // tx_cfg[i][5] = 0x00 ;
  328.     tx_cfg[i][5] = 0x20;  // sets the software timer to 1 minute
  329.   }
  330. }
  331.  
  332.  
  333.  
  334. /*!***********************************
  335.   \brief sets  the configuration array for cell balancing
  336.   uses CFGR4 and lowest 4 bits of CGFR5
  337.  **************************************/
  338. void balance_cfg(int ic, int cell) {
  339.   tx_cfg[ic][4] = 0x00;                  // clears S1-8
  340.   tx_cfg[ic][5] = tx_cfg[ic][5] & 0xF0;  // clears S9-12 and sets software timer to 1 min
  341.   //Serial.println(tx_cfg[ic][5] & 0xF0,BIN);
  342.   if (cell >= 0 and cell <= 7) {
  343.     tx_cfg[ic][4] = tx_cfg[ic][4] | 1 << cell;
  344.   }
  345.   if (cell > 7) {
  346.     tx_cfg[ic][5] = tx_cfg[ic][5] | (1 << (cell - 8));
  347.   }
  348. }
  349.  
  350.  
  351. /*!************************************************************
  352.   \brief Prints Cell Voltage Codes to the serial port
  353.  *************************************************************/
  354. void print_cells() {
  355.   unsigned long elasped = millis() - tstart;
  356.   float moduleV;
  357.   serialPrint(elasped);  //ELAPSED TIME:
  358.  
  359.   //INDIVIDUAL CELL VOLTAGES:
  360.   for (int current_ic = 0; current_ic < TOTAL_IC; current_ic++) {
  361.     moduleV = 0.;
  362.     for (int i = 0; i < 12; i++) {
  363.       moduleV = moduleV + cell_codes[current_ic][i] * 0.0001;
  364.       serialPrint(cell_codes[current_ic][i] * 0.0001);
  365.     }
  366.   }
  367.   serialPrint(moduleV);  // TOTAL MODULE VOLTAGE:
  368.   serialPrint(current);  //MODULE CURRENT:
  369.  
  370.  
  371.   //TEMPERATURES:
  372.   for (int i = 0; i < MAX_TEMP_SENSOR && i < tempSensorCount; i++) {
  373.     serialPrint(temp[i]);
  374.   }
  375.  
  376.   //RELAY STATES:
  377.   serialPrint(chargeRelay_state);
  378.   serialPrint(dischargeRelay_state);
  379.  
  380.   serialPrint("\r\n");
  381.  
  382.  
  383.   lcd.clear();
  384.   lcd.setCursor(0, 0);
  385.   lcd.print("Voltage: ");
  386.   lcd.print(moduleV, 2);
  387.   lcd.print("V");
  388.  
  389.   lcd.setCursor(0, 1);
  390.   lcd.print("Current: ");
  391.   lcd.print(current, 2);
  392.   lcd.print("A");
  393.  
  394.   lcd.setCursor(0, 2);
  395.   lcd.print("Temp: ");
  396.   lcd.print(temp[0], 2);
  397.   lcd.print("C ");
  398.   lcd.print(temp[1], 2);
  399.   lcd.print("C");
  400.  
  401.   lcd.setCursor(0, 3);
  402.   if (abs(current) > MAX_CURRENT) {
  403.     lcd.print("OVER CURRENT");
  404.   } else if (overCharge_state == LOW) {
  405.     lcd.print("OVER VOLTAGE");
  406.   } else if (underCharge_state == LOW) {
  407.     lcd.print("UNDER VOLTAGE");
  408.   } else if (overTemp_state == LOW) {
  409.     lcd.print("OVER TEMPERATURE");
  410.   } else {
  411.     lcd.print("Everything normal");
  412.   }
  413. }
  414.  
  415.  
  416. /*!****************************************************************************
  417.   \brief print function overloads:
  418.  *****************************************************************************/
  419. void serialPrint(String val) {
  420.   Serial.print(val);
  421.   Serial.print("\t");
  422. }
  423.  
  424. void serialPrint(unsigned long val) {
  425.   Serial.print(val);
  426.   Serial.print("\t");
  427. }
  428.  
  429.  
  430. void serialPrint(double val) {
  431.   Serial.print(val, 4);
  432.   Serial.print("\t");
  433. }
  434.  
  435. void serialPrint(int val) {
  436.   Serial.print(val);
  437.   Serial.print("\t");
  438. }
  439.  
  440.  
  441. /*!****************************************************************************
  442.   \brief Prints GPIO Voltage Codes and Vref2 Voltage Code onto the serial port
  443.  *****************************************************************************/
  444. void print_aux() {
  445.  
  446.   for (int current_ic = 0; current_ic < TOTAL_IC; current_ic++) {
  447.     Serial.print(" IC ");
  448.     Serial.print(current_ic + 1, DEC);
  449.     for (int i = 0; i < 5; i++) {
  450.       Serial.print(" GPIO-");
  451.       Serial.print(i + 1, DEC);
  452.       Serial.print(":");
  453.       Serial.print(aux_codes[current_ic][i] * 0.0001, 4);
  454.       Serial.print(",");
  455.     }
  456.     Serial.print(" Vref2");
  457.     Serial.print(":");
  458.     Serial.print(aux_codes[current_ic][5] * 0.0001, 4);
  459.     Serial.println();
  460.   }
  461.   Serial.println();
  462. }
  463. /*!******************************************************************************
  464.   \brief Prints the Configuration data that is going to be written to the LTC6804
  465.   to the serial port.
  466.  ********************************************************************************/
  467. void print_config() {
  468.   int cfg_pec;
  469.  
  470.   Serial.println("Written Configuration: ");
  471.   for (int current_ic = 0; current_ic < TOTAL_IC; current_ic++) {
  472.     Serial.print(" IC ");
  473.     Serial.print(current_ic + 1, DEC);
  474.     Serial.print(": ");
  475.     Serial.print("0x");
  476.     serial_print_hex(tx_cfg[current_ic][0]);
  477.     Serial.print(", 0x");
  478.     serial_print_hex(tx_cfg[current_ic][1]);
  479.     Serial.print(", 0x");
  480.     serial_print_hex(tx_cfg[current_ic][2]);
  481.     Serial.print(", 0x");
  482.     serial_print_hex(tx_cfg[current_ic][3]);
  483.     Serial.print(", 0x");
  484.     serial_print_hex(tx_cfg[current_ic][4]);
  485.     Serial.print(", 0x");
  486.     serial_print_hex(tx_cfg[current_ic][5]);
  487.     Serial.print(", Calculated PEC: 0x");
  488.     cfg_pec = pec15_calc(6, &tx_cfg[current_ic][0]);
  489.     serial_print_hex((uint8_t)(cfg_pec >> 8));
  490.     Serial.print(", 0x");
  491.     serial_print_hex((uint8_t)(cfg_pec));
  492.     Serial.println();
  493.   }
  494.   Serial.println();
  495. }
  496.  
  497. /*!*****************************************************************
  498.   \brief Prints the Configuration data that was read back from the
  499.   LTC6804 to the serial port.
  500.  *******************************************************************/
  501. void print_rxconfig() {
  502.   Serial.println("Received Configuration ");
  503.   for (int current_ic = 0; current_ic < TOTAL_IC; current_ic++) {
  504.     Serial.print(" IC ");
  505.     Serial.print(current_ic + 1, DEC);
  506.     Serial.print(": 0x");
  507.     serial_print_hex(rx_cfg[current_ic][0]);
  508.     Serial.print(", 0x");
  509.     serial_print_hex(rx_cfg[current_ic][1]);
  510.     Serial.print(", 0x");
  511.     serial_print_hex(rx_cfg[current_ic][2]);
  512.     Serial.print(", 0x");
  513.     serial_print_hex(rx_cfg[current_ic][3]);
  514.     Serial.print(", 0x");
  515.     serial_print_hex(rx_cfg[current_ic][4]);
  516.     Serial.print(", 0x");
  517.     serial_print_hex(rx_cfg[current_ic][5]);
  518.     Serial.print(", Received PEC: 0x");
  519.     serial_print_hex(rx_cfg[current_ic][6]);
  520.     Serial.print(", 0x");
  521.     serial_print_hex(rx_cfg[current_ic][7]);
  522.     Serial.println();
  523.   }
  524.   Serial.println();
  525. }
  526.  
  527. void serial_print_hex(uint8_t data) {
  528.   if (data < 16) {
  529.     Serial.print("0");
  530.     Serial.print((byte)data, HEX);
  531.   } else
  532.     Serial.print((byte)data, HEX);
  533. }
  534.  
  535.  
  536. /*!***********************************
  537.   \brief Reads current input from LEM sensor
  538.  **************************************/
  539. float readCurrent() {
  540.   for (int i = 0; i < imax; i++) {
  541.     lem = lemHistory.rolling(analogRead(currentPin));
  542.   }
  543.  
  544.   current = 5.0 / 1024 * (lemBias - lem) / 0.185;
  545.   return current;
  546. }
  547.  
  548. /*!***********************************
  549.   \brief Reads  LEM sensor value when current is zero. Used to calibrates to zero output for zero current
  550.  **************************************/
  551. float zeroCurrentCalibrate() {
  552.   // get initial readings for current and set the zero calibration
  553.   int iSample = 500;
  554.   for (int i = 0; i < iSample; i++) {
  555.     lemBiasHistory.push(analogRead(currentPin));
  556.   }
  557.  
  558.   lemBias = lemBiasHistory.mean();
  559.   return lemBias;
  560. }
  561.  
  562. void readTemp() {
  563.   tempSensors.requestTemperatures();
  564.  
  565.   for (int i = 0; i < MAX_TEMP_SENSOR && i < tempSensorCount; ++i) {
  566.     temp[i] = tempSensors.getTempCByIndex(i);
  567.   }
  568. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement