ChatGPT wrote a PID Controller

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

ChatGPT wrote a PID Controller

#1 Post by wiredcharlie » 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:

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
Here, we're defining 5 variables for the PID controller, with initial values of 0.

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
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:

Code: Select all

On System#Boot do
  TaskValueSet 1,8,25
EndOn
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:

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
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?

TD-er
Core team member
Posts: 9153
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: ChatGPT wrote a PID Controller

#2 Post by TD-er » 01 Apr 2023, 10:03

ChatGTP doesn't understand how variables and dummy tasks are to be used.
So this code will not work, for the simple fact we don't have 6 taskvalues.

Also [TaskValue(1,4)] is invalid ESPEasy syntax.

It may have used some parts of the (wiki) documentation as there are uses of [ADC#Temperature] instead of %eventvalue1%.

But it is really nice to see ChatGPT does kinda understand rules writing for ESPEasy.

I've also seen someone let ChatGPT generate config for HomeAssistant based on a normal formulated question.

User avatar
ThomasB
Normal user
Posts: 1210
Joined: 17 Jun 2018, 20:41
Location: USA

Re: ChatGPT wrote a PID Controller

#3 Post by ThomasB » 04 Apr 2023, 18:57

Honestly, I was initially impressed that ChatGP could create rules that looked legit, even if they did not work. But then I saw the April 1 date.

- Thomas

TD-er
Core team member
Posts: 9153
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: ChatGPT wrote a PID Controller

#4 Post by TD-er » 04 Apr 2023, 20:57

Well I have seen others (not on April 1st) asking ChatGPT 3 to generate ESPEasy rules and it actually did write them.
Like you said, looking legit at first glance, but invalid syntax.

wiredcharlie
Normal user
Posts: 57
Joined: 28 Sep 2020, 13:58

Re: ChatGPT wrote a PID Controller

#5 Post by wiredcharlie » 03 Jun 2023, 12:01

Oh - didn't realise I posted on April 1st!

I've been playing with ChatGPT quite a bit, and would characterise it as similar to a smart journalist that didn't go to any maths, science or engineering lessons and understands the world from reading stuff written by other journalists that didn't go to any maths, science or engineering lessons.

bidrohini
Normal user
Posts: 110
Joined: 03 Nov 2022, 16:24

Re: ChatGPT wrote a PID Controller

#6 Post by bidrohini » 29 Jun 2023, 13:17

Did anyone test this code?

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests