daily pastebin goal
8%
SHARE
TWEET

Solar power heat dumper

a guest Jun 21st, 2011 1,507 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <PID_v1.h>
  2.  
  3. //Basic energy monitoring sketch - by Trystan Lea
  4. //Licenced under GNU General Public Licence more details here
  5. // openenergymonitor.org
  6. // modified by Tim Robinson to include PID library
  7.  
  8. //Sketch measures current only.
  9. //and then calculates useful values like
  10. //apparent power and Irms.
  11. //Apparent power is calculated from set Vrms.
  12. double Vrms=240.0;
  13.  
  14. //Setup variables
  15. int numberOfSamples = 1000;
  16.  
  17.  
  18. //Set current input pin
  19. int In_pv = 1;
  20. int In_used = 0;
  21. int analogOutPin = 9;
  22. double time;
  23. // values for samples = 2000
  24. //double Kp = 0.08;
  25. //double Ki = 0.3;
  26. //double Kd = 0.000;
  27.  
  28. // values for samples = 100
  29. double Kp = 0.03;
  30. double Ki = 0.3;
  31. double Kd = 0.0005;
  32.  
  33. //Current calibration coeficient
  34. double ICAL_used = 0.15007;
  35. double ICAL_pv = 0.1529;
  36. //Sample variables
  37. int lastSampleI_pv,sampleI_pv,lastSampleI_used,sampleI_used ;
  38. int heater_power;
  39.  
  40. //Filter variables
  41. double lastFilteredI_pv, filteredI_pv;
  42. double lastFilteredI_used, filteredI_used;
  43. //Power calculation variables
  44. double sqI_pv,sumI_pv;
  45. double sqI_used,sumI_used;
  46. //Useful value variables
  47. double apparentPower_pv, Irms_pv;
  48. double apparentPower_used, Irms_used;
  49. double export_pwr;
  50.  
  51. //Define Variables we'll be connecting to
  52. double Setpoint, Input, Output;
  53.  
  54. //Specify the links and initial tuning parameters
  55. PID myPID(&Input, &Output, &Setpoint,Kp,Ki,Kd,DIRECT);
  56.        
  57. void setup()
  58. {
  59.    Serial.begin(115200);
  60.    heater_power = 0;
  61.    myPID.SetMode(AUTOMATIC);
  62.    Setpoint = 0;
  63. }
  64.  
  65. void loop()
  66. {
  67.  
  68. for (int n=0; n<numberOfSamples; n++)
  69. {
  70.  
  71.    //Used for offset removal
  72.    lastSampleI_pv=sampleI_pv;
  73.    lastSampleI_used=sampleI_used;
  74.    //Read in current samples.  
  75.    sampleI_pv = analogRead(In_pv);
  76.    sampleI_used = analogRead(In_used);
  77.    //Used for offset removal
  78.    lastFilteredI_pv = filteredI_pv;
  79.   lastFilteredI_used = filteredI_used;
  80.  
  81.    //Digital high pass filters to remove 2.5V DC offset.
  82.    filteredI_pv = 0.996*(lastFilteredI_pv+sampleI_pv-lastSampleI_pv);
  83.    filteredI_used = 0.996*(lastFilteredI_used+sampleI_used-lastSampleI_used);
  84.    
  85.    // debug to remove filter for use with a pot
  86. // filteredI_pv=sampleI_pv;
  87.    
  88.    //Root-mean-square method current
  89.    //1) square current values
  90.    sqI_pv = filteredI_pv * filteredI_pv;
  91.    sqI_used = filteredI_used * filteredI_used;
  92.    //2) sum
  93.    sumI_pv += sqI_pv;
  94.    sumI_used += sqI_used;
  95. }
  96.  
  97. //Calculation of the root of the mean of the current squared (rms)
  98. //Calibration coeficients applied.
  99. Irms_pv = ICAL_pv*sqrt(sumI_pv / numberOfSamples);
  100. Irms_used = ICAL_used*sqrt(sumI_used / numberOfSamples);
  101. //Calculation power values
  102.  
  103. apparentPower_pv = Vrms * Irms_pv;
  104. apparentPower_used = Vrms * Irms_used;
  105. Input = apparentPower_used;
  106. Setpoint = apparentPower_pv;
  107.  
  108. myPID.Compute();
  109. heater_power = Output;    
  110.  
  111.    if (heater_power > 255) heater_power=255;
  112.    if (heater_power < 0) heater_power=0;
  113.    
  114. analogWrite(analogOutPin, 255-heater_power);
  115. time=millis();
  116. //Output to serial
  117. Serial.print("timestamp: ");
  118. Serial.print(time/1000,3);
  119. Serial.print("  PV: ");
  120. Serial.print(Setpoint,0);
  121. Serial.print("  Using: ");
  122. Serial.print(apparentPower_used,0);
  123. Serial.print("  Delta : ");
  124. Serial.print(apparentPower_used-Setpoint,0);
  125. Serial.print("  Heater power: ");
  126. Serial.println(heater_power,DEC);
  127.  
  128.  
  129.  
  130.  
  131.  
  132. //Reset current accumulator
  133. sumI_pv = 0;
  134. sumI_used = 0;
  135. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top