d00m1n0

PIDmaster

Aug 16th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. #include <PID_AutoTune_v0.h>
  2. #include <ESP8266WiFi.h>
  3. #include <math.h>
  4. #include <PID_v1.h>
  5. #include <PID_AutoTune_v0.h>
  6.  
  7. //Define analog
  8. #define THERMISTORPIN A0
  9. //Define PWM
  10. #define PWMPIN D1
  11. //Define portsselector of MUX
  12. #define selectorA D8
  13. #define selectorB D7
  14. #define selectorC D6
  15. // resistance at 25 degrees C
  16. #define THERMISTORNOMINAL 47000
  17. // temp. for nominal resistance (almost always 25 C)
  18. #define TEMPERATURENOMINAL 25
  19. // how many samples to take and average, more takes longer
  20. // but is more 'smooth'
  21. #define NUMSAMPLES 5
  22. // The beta coefficient of the thermistor (usually 3000-4000)
  23. #define BCOEFFICIENT 4450
  24. // the value of the 'other' resistor
  25. #define SERIESRESISTOR 47000
  26. // The port to listen for incoming TCP connections
  27. #define LISTEN_PORT 80
  28. // Create an instance of the server on Port 80
  29. WiFiServer server(LISTEN_PORT);
  30. // global Variables
  31. const char* ssid = "GothamCity";
  32. const char* password = "1888054937563387";
  33. unsigned long ulReqcount;
  34. unsigned long ulReconncount;
  35. int samples[NUMSAMPLES];
  36. double Setpoint, temperatur, Output;
  37. boolean tuning = false;
  38. double kp = 2, ki = 0.5, kd = 2;
  39. double aTuneStep = 50, aTuneNoise = 1, aTuneStartValue = 100;
  40. unsigned int aTuneLookBack = 20;
  41. byte ATuneModeRemember = 2;
  42.  
  43. //Specify the links and initial tuning parameters
  44. PID myPID(&temperatur, &Output, &Setpoint, kp, ki, kd, DIRECT);
  45. PID_ATune aTune(&temperatur, &Output);
  46.  
  47. void setup(void)
  48. {
  49. // setup globals
  50. ulReqcount = 0;
  51. ulReconncount = 0;
  52.  
  53. digitalWrite(selectorA, 0);
  54. digitalWrite(selectorB, 0);
  55. digitalWrite(selectorC, 0);
  56.  
  57. //Set up the PID
  58. myPID.SetMode(AUTOMATIC);
  59.  
  60. if (tuning)
  61. {
  62. tuning = false;
  63. changeAutoTune();
  64. tuning = true;
  65. }
  66.  
  67. // start serial
  68. Serial.begin(9600);
  69. delay(1);
  70.  
  71. // inital connect
  72. WiFi.mode(WIFI_STA);
  73. WiFiStart();
  74. server.begin();
  75. Serial.println("HTTP server started");
  76. }
  77.  
  78. void WiFiStart()
  79. {
  80. ulReconncount++;
  81.  
  82. // Connect to WiFi network
  83. Serial.println();
  84. Serial.println();
  85. Serial.print("Connecting to ");
  86. Serial.println(ssid);
  87.  
  88. WiFi.begin(ssid, password);
  89.  
  90. while (WiFi.status() != WL_CONNECTED)
  91. {
  92. delay(1000);
  93. Serial.print(".");
  94. }
  95. Serial.println("");
  96. Serial.println("WiFi connected");
  97.  
  98. // Start the server
  99. server.begin();
  100. Serial.println("Server started");
  101.  
  102. // Print the IP address
  103. Serial.println(WiFi.localIP());
  104. }
  105.  
  106. void changeAutoTune()
  107. {
  108. if (!tuning)
  109. {
  110. //Set the output to the desired starting frequency.
  111. Output = aTuneStartValue;
  112. aTune.SetNoiseBand(aTuneNoise);
  113. aTune.SetOutputStep(aTuneStep);
  114. aTune.SetLookbackSec((int)aTuneLookBack);
  115. AutoTuneHelper(true);
  116. tuning = true;
  117. }
  118. else
  119. { //cancel autotune
  120. aTune.Cancel();
  121. tuning = false;
  122. AutoTuneHelper(false);
  123. }
  124. }
  125.  
  126. void AutoTuneHelper(boolean start)
  127. {
  128. if (start)
  129. ATuneModeRemember = myPID.GetMode();
  130. else
  131. myPID.SetMode(ATuneModeRemember);
  132. }
  133.  
  134. float average()
  135. {
  136. uint8_t i;
  137. float average = 0;
  138. // take N samples in a row, with a slight delay
  139. for (i = 0; i < NUMSAMPLES; i++)
  140. {
  141. samples[i] = analogRead(THERMISTORPIN);
  142. delay(1000);
  143. }
  144. // average all the samples out
  145. for (i = 0; i < NUMSAMPLES; i++)
  146. {
  147. average += samples[i];
  148. }
  149. average /= NUMSAMPLES;
  150. return average;
  151. }
  152.  
  153. float resistance(float resist)
  154. {
  155. // convert the value to resistance
  156. resist = 1023 / resist - 1;
  157. resist = SERIESRESISTOR / resist;
  158. return resist;
  159. }
  160.  
  161. float steinhart(float durch)
  162. {
  163. float steinhart = 0;
  164. steinhart = durch / THERMISTORNOMINAL; // (R/Ro)
  165. steinhart = log(steinhart); // ln(R/Ro)
  166. steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
  167. steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  168. steinhart = 1.0 / steinhart; // Invert
  169. steinhart -= 273.15; // convert to C
  170. return steinhart;
  171. }
  172.  
  173. void loop()
  174. {
  175. float durchschnitt = average();
  176. float watt = resistance(durchschnitt);
  177. temperatur = steinhart(watt);
  178.  
  179. if (tuning)
  180. {
  181. byte val = (aTune.Runtime());
  182. if (val != 0)
  183. {
  184. tuning = false;
  185. }
  186. if (!tuning)
  187. { //we're done, set the tuning parameters
  188. kp = aTune.GetKp();
  189. ki = aTune.GetKi();
  190. kd = aTune.GetKd();
  191. myPID.SetTunings(kp, ki, kd);
  192. AutoTuneHelper(false);
  193. }
  194. }
  195. else myPID.Compute();
  196.  
  197. Serial.print("Durchschnittlicher Wert an A0: ");
  198. Serial.println(durchschnitt);
  199. Serial.print("Thermistor Widerstand: ");
  200. Serial.println(watt);
  201. Serial.print("Temperatur: ");
  202. Serial.print(temperatur);
  203. Serial.println(" *C");
  204. //Serial.print("t0.txt=");
  205. //Serial.write(temperatur);
  206. Serial.print("Autotune= ");
  207. Serial.println(tuning);
  208. Serial.print("PID Output: ");
  209. Serial.println(Output);
  210. delay(1000);
  211. // check if WLAN is connected
  212. if (WiFi.status() != WL_CONNECTED)
  213. {
  214. WiFiStart();
  215. }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment