ChatGPT wrote a PID Controller
Posted: 01 Apr 2023, 09:50
Using Easy ESP rules type programming for ESP8266 outlined on this web site: https://espeasy.readthedocs.io/en/latest/ , write rules to implement a PID temperature controller.
Sure, here's an example of implementing a PID temperature controller using Easy ESP rules type programming for ESP8266:
1. First, we need to define the variables for the PID controller:
Here, we're defining 5 variables for the PID controller, with initial values of 0.
2. Next, we need to read the current temperature:
This code sets up an event to read the temperature using the ADC input of the ESP8266, and stores the temperature value in variable 6.
3. We also need to set the desired temperature setpoint:
Here, we're setting the desired temperature setpoint to 25°C, but you can adjust this value as needed.
4. Now, we can implement the PID controller using the following rules:
Here, we're defining the PID parameters (Kp, Ki, and Kd) in variables 9, 10, and 11 respectively. Then, we set up an event to run the PID controller every second using a timer.
Within the event, we calculate the error between
Is this original code or does anyone recognise it from somewhere on the web?
Sure, here's an example of implementing a PID temperature controller using Easy ESP rules type programming for ESP8266:
1. First, we need to define the variables for the PID controller:
Code: Select all
On System#Boot do
TaskValueSet 1,1,0
TaskValueSet 1,2,0
TaskValueSet 1,3,0
TaskValueSet 1,4,0
TaskValueSet 1,5,0
EndOn
2. Next, we need to read the current temperature:
Code: Select all
On System#Boot do
TaskValueSet 1,6,0
TaskValueSet 1,7,0
EndOn
On ADC#Temperature do
TaskValueSet 1,6,[ADC#Temperature]
EndOn
3. We also need to set the desired temperature setpoint:
Code: Select all
On System#Boot do
TaskValueSet 1,8,25
EndOn
4. Now, we can implement the PID controller using the following rules:
Code: Select all
On System#Boot do
TaskValueSet 1,9,0.5
TaskValueSet 1,10,0.05
TaskValueSet 1,11,0.1
EndOn
On Rules#Timer=1 do
TaskValueSet 1,12,[TaskValue(1,1)] // Error
TaskValueSet 1,13,[TaskValue(1,2)] // Integral
TaskValueSet 1,14,[TaskValue(1,3)] // Derivative
TaskValueSet 1,15,[TaskValue(1,4)] // Output
TaskValueSet 1,16,[TaskValue(1,6)] // Temperature
TaskValueSet 1,17,[TaskValue(1,8)] // Setpoint
// Calculate error
TaskValueSet 1,12,[TaskValue(1,17)]-[TaskValue(1,16)]
// Calculate integral
TaskValueSet 1,13,[TaskValue(1,13)]+[TaskValue(1,12)]
// Calculate derivative
TaskValueSet 1,14,[TaskValue(1,12)]-[TaskValue(1,15)]
// Calculate output
TaskValueSet 1,15,[TaskValue(1,9)]*[TaskValue(1,12)]+[TaskValue(1,10)]*[TaskValue(1,13)]+[TaskValue(1,11)]*[TaskValue(1,14)]
// Output to pin (use PWM pin for best results)
PWM,12,[TaskValue(1,15)],500
// Update variables
TaskValueSet 1,2,[TaskValue(1,13)]
TaskValueSet 1,3,[TaskValue(1,14)]
TaskValueSet 1,4,[TaskValue(1,15)]
EndOn
Within the event, we calculate the error between
Is this original code or does anyone recognise it from somewhere on the web?