akhilrb

ObjectScope

Dec 20th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.17 KB | None | 0 0
  1. /*
  2. measure and average voltage, current and power.
  3. control max output power by max output current.
  4. */
  5.  
  6. #define MOSPIN 5
  7. #define CURRPIN A0
  8. #define BATTVOLTPIN A1
  9. #define SUPPLYVOLTPIN A2
  10. #define CURRSENS analogRead(CURRPIN)
  11. #define BATTVOLT analogRead(BATTVOLTPIN)
  12. #define SUPPLYVOLT analogRead(SUPPLYVOLTPIN)
  13. #define MOSDRIVE(x) analogWrite(MOSPIN,x)
  14.  
  15. int current, battVolts, supplyVolts, battCurrent;
  16. float outPower;
  17. String inputString = "";
  18. boolean stringComplete;
  19.  
  20. // Timer class to keep track of different timeouts
  21. class Timer
  22. {
  23.     uint64_t timer;
  24. public:
  25.     // timer in ms, toggle mode
  26.     Timer(int _timer)
  27.     {
  28.         timer = _timer;
  29.         setPoint = millis();
  30.         state = false;
  31.     }
  32.  
  33.     ~Timer() {}
  34.  
  35.     void update()
  36.     {
  37.         if(!timer)
  38.             return;
  39.         if(millis() - ticks > timer && !state)
  40.             state = true;
  41.  
  42.         /* toggle mode
  43.         if(millis() - ticks > timer && mode)
  44.             state = !state;
  45.         */
  46.     }
  47.     void timeout()
  48.     {
  49.         if(!timer)
  50.             return;
  51.         if(state)
  52.         {
  53.             state = false;
  54.             return true;
  55.         }
  56.         else
  57.             return false;
  58.     }
  59.  
  60.     void setTimer(int _timer)
  61.     {
  62.         timer = _timer;
  63.     }
  64. private:
  65.     uint64_t setPoint;
  66.     bool state;
  67. };
  68.  
  69. Timer measureTimer(1000);
  70. Timer shutdown(0);
  71.  
  72. // Class to calculate Rolling Average
  73. class RollAvg
  74. {
  75. public:
  76.     RollAvg(int n)
  77.     {
  78.         int totalReadings = n;
  79.     }
  80.  
  81.     void push(float value) const
  82.     {
  83.         for (int i = totalReadings-1; i > 0; --i)
  84.             readings[i] = readings[i-1];
  85.         readings[0] = value;
  86.     }
  87.  
  88.     float getAvg()
  89.     {
  90.         for (int i = 0; i < totalReadings; --i)
  91.             sum += readings[i];
  92.         return float(sum/totalReadings);
  93.     }
  94.  
  95.     void reset()
  96.     {
  97.         for (int i = 0; i < totalReadings; ++i)
  98.             readings[i] = 0;
  99.     }
  100. private:
  101.     int totalReadings;
  102.     int readings[totalReadings];
  103. };
  104.  
  105. RollAvg avgPowerOut(100);
  106. RollAvg avgCurrent(100);
  107.  
  108. void setup()
  109. {
  110.     Serial.begin(115200);
  111.     pinMode(MOSPIN, OUTPUT);
  112.     analogWrite(MOSPIN, 255);
  113.     inputString.reserve(50);
  114. }
  115.  
  116. void loop()
  117. {
  118.     randomSeed(CURRPIN);
  119.     if(stringComplete)
  120.         manCli();
  121. }
  122.  
  123. // will run after every loop
  124. void serialEvent()
  125. {
  126.     while (Serial.available())
  127.     {
  128.         char inChar = (char)Serial.read();
  129.         inputString += inChar;
  130.         if (inChar == '\n')
  131.         stringComplete = true;
  132.     }
  133. }
  134.  
  135. void measure()
  136. {
  137.     current =  CURRSENS;
  138.     battVolts = float(1609.7*BATTVOLT*3.3)/1023 ;
  139.     supplyVolts = float(1609.7*SUPPLYVOLT*3.3)/1023;
  140.  
  141.     outPower = (supplyVolts*current)/1000;
  142.     battCurrent = (supplyVolts*current)/battVolts;
  143.  
  144.     avgCurrent.push(current);
  145.     avgPowerOut.push(outPower);
  146.  
  147. }
  148.  
  149. void manCli()
  150. {
  151.     if(inputString.equals("stats"))
  152.         printDetails();
  153.     else if(inputString.equals("help") || inputString.equals("?"))
  154.     {
  155.         Serial.println(F("shutdown <s>    :   disrupts power after specified seconds"));
  156.         Serial.println(F("startup <s>     :   supplies power after specified seconds after shutdown"));
  157.         Serial.println(F("limitPower <mW> :   limits power to specified mWatts in CV mode"));
  158.         Serial.println(F("setTime <hh:mm> :   set local time"));
  159.         Serial.println(F("stats           :   show power metrics and statistics"));
  160.         Serial.println(F("config          :   show configuration"));
  161.         Serial.println(F("help ; ?        :   display this message"));
  162.     }
  163.     else if((inputString.substring(0,7)).equals("shutdown"))
  164.         Timer shutdown(inputString.parseInt());
  165. }
  166.  
  167. void printDetails()
  168. {
  169.     Serial.print(F("Output Voltage: "));
  170.     Serial.print(supplyVolts);
  171.     Serial.print(F("mV\nOutput Current: "));
  172.     Serial.print(current);
  173.     Serial.print(F("mA\nPower Draw: "));
  174.     Serial.print(outPower);
  175.     Serial.print(F("mW\nBattery Voltage: "));
  176.     Serial.print(battVolts);
  177.     Serial.print(F("mV\nBattery Load: "));
  178.     Serial.print(battCurrent + random(0,50));
  179.     Serial.print(F("mA\nAverage Power Supply: "));
  180.     Serial.print(avgPowerOut.getAvg())
  181. }
Add Comment
Please, Sign In to add comment