-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopta_pid.ino
45 lines (40 loc) · 1.25 KB
/
opta_pid.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <PortentaEthernet.h>
#include "PID_v2.h"
arduino::EthernetClass eth(&m_netInterface);
double Kp = 2, Ki = 5, Kd = 1;
PID_v2 myPID(Kp, Ki, Kd, PID::Reverse);
unsigned long windowStartTime;
void setup() {
IPAddress ip(192, 168, 1, 40);
IPAddress dns(192, 168, 1, 254);
IPAddress gateway(192, 168, 1, 254);
IPAddress subnet(255, 255, 255, 0);
eth.begin(ip, dns, gateway, subnet);
delay(5000);
windowStartTime = millis();
myPID.SetOutputLimits(0, 5000);
myPID.Start(PLCOut.out_temperature, 0, 100);
}
void loop()
{
const double input = PLCOut.out_temperature;
const double Kp = PLCOut.out_kp;
const double Ki = PLCOut.out_ki;
const double Kd = PLCOut.out_kd;
const int WindowSize = PLCOut.out_windowsize;
const double setpoint = PLCOut.out_setpoint;
myPID.SetOutputLimits(0, WindowSize);
myPID.Setpoint(setpoint);
myPID.SetTunings(Kp, Ki, Kd);
const double output = myPID.Run(input);
PLCIn.in_output = output;
while (millis() - windowStartTime > WindowSize) {
windowStartTime += WindowSize;
}
unsigned long window = millis() - windowStartTime;
PLCIn.in_window = window;
if (output < window)
PLCIn.in_heater = true;
else
PLCIn.in_heater = false;
}