akhilrb

ObjectScoping

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