Re: [diy-pid-control] Digest for diy-pid-control@googlegroups.com - 7 updates in 1 topic

37 views
Skip to first unread message

Lukas Podschlapp

unread,
Jul 11, 2023, 4:04:46 AM7/11/23
to diy-pid...@googlegroups.com
Thanks very much for all the replies!

I have tried Steve's recommendation and it seems to get rid of the initial issue with the shifting of window at unexpected times. Great!
However I am facing another issue that I didn't have before.

For the record, pump and flow through heater are switched off, so the output should be 100 at all times, however I get outputs of 45.02 at random times and looking at the actual relay status, the window does not seem to adjust properly either.
I am starting to lose my mind here :D

My current code:


```cpp
#include <FlowSensor.h>
#include <LcdBarGraph.h>
#include <LiquidCrystal.h>

//Internal NTC Stuff
const long int RT0 = 50000;  // Ω
const int B = 3970;          //  K
const int VCC = 5;           //Supply  voltage
const long int R = 12000;    //R=10KΩ

//Variables
float RT, VR, ln, TX, T0, VRT;

//Thermocouple Stuff
#include <SPI.h>
#include "Adafruit_MAX31855.h"

// Example creating a thermocouple instance with software SPI on any three
// digital IO pins.
#define MAXDO_1 31
#define MAXCS_1 30
#define MAXCLK_1 29


// initialize the Thermocouple
Adafruit_MAX31855 thermocouple_1(MAXCLK_1, MAXCS_1, MAXDO_1);

#define MAXDO_2 26
#define MAXCS_2 27
#define MAXCLK_2 28

// initialize the Thermocouple
Adafruit_MAX31855 thermocouple_2(MAXCLK_2, MAXCS_2, MAXDO_2);

//PID Stuff
#include <PID_v1.h>
#define RELAY_PIN 19
#define RELAY_ON HIGH
#define RELAY_OFF LOW

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

//Specify the links and initial tuning parameters
double Kp = 30, Ki = 10, Kd = 100;
PID myPID(&Input, &Output, &Setpoint, Kp, Ki, Kd, DIRECT);

//WindowSize DON'T CHANGE FOR NOW!!!
int WindowSize = 100;
unsigned long windowStartTime;

//LCD
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
byte LCD_Columns = 20;
byte LCD_Rows = 4;

//Flow Sensor
FlowSensor Sensor(YFS201, 18);
unsigned long timebefore = 0;  // same type as millis(), is not being used again in this sketch


const int Pump = 10;

int variables[2][2] = { { 0, 1 }, { 2, 200 } };  // I want to link to the array in the array that I setup in the Menu file

void count() {
  Sensor.count();
}

void setup() {
  Serial.begin(2000000);
  //PID Stuff
  //windowStartTime = millis();
  windowStartTime = 0;
  //initialize the variables we're linked to
  Setpoint = 70;  //Temperature is the Setpoint
  //tell the PID to range between 0 and the full window size
  myPID.SetOutputLimits(0, WindowSize);
  myPID.SetSampleTime(WindowSize / 10);  // make sure we measure and sample at least 10 times per WindowSize.
  //turn the PID on
  myPID.SetMode(AUTOMATIC);

  //Thermocouple
  thermocouple_1.begin();
  thermocouple_2.begin();

  //NTC
  T0 = 25 + 273.15;

  //Flowmeter
  Sensor.begin(count);

  //Flow Through Heater
  pinMode(RELAY_PIN, OUTPUT);

  //Pump
  pinMode(Pump, OUTPUT);

  //LCD Begin and Test
  lcd.begin(20, 4);
  lcd.print("Test");
  delay(1000);
  lcd.clear();
}

void loop() {
  // Pump and show progress on LCD until the desired volume has been reached
  LcdBarGraph lbg0(&lcd, 19, 1, 2);
  for (variables[0][0]; variables[0][0] < variables[1][1];) {
    //for (millis(); millis() < 10000;) {
    lcd.setCursor(7, 1);
    lcd.print("PROGRESS");
    lbg0.drawValue(variables[0][0], variables[1][1]);
    //delay(10);
    digitalWrite(Pump, HIGH);
    Sensor.read();
    variables[0][0] = Sensor.getVolume() * 1000;
    Serial.print("Volume: ");
    Serial.print(variables[0][0]);
    Serial.print("ml --- ");
    //Temperature Stuff
    VRT = analogRead(A15);         //Acquisition analog value of VRT
    VRT = (5.00 / 1023.00) * VRT;  //Conversion to voltage
    VR = VCC - VRT;
    RT = VRT / (VR / R);  //Resistance of RT
    ln = log(RT / RT0);
    TX = (1 / ((ln / B) + (1 / T0)));  //Temperature from thermistor
    TX = TX - 273.15;                  //Conversion to Celsius
    Serial.print("NTC Temperature: ");
    Serial.print(TX);
    Serial.print(" degC --- ");
    //Thermocouple
    //Serial.print("Internal Temp 2 = ");
    //Serial.print(thermocouple_2.readInternal());
    double d = thermocouple_2.readCelsius();
    Serial.print("FTH Control Temp. = ");
    Serial.print(d);
    Serial.print(" degC");
    //Serial.print(" --- Internal Temp 1 = ");
    //Serial.print(thermocouple_1.readInternal());

    Input = TX;
    myPID.Compute();

    /************************************************
   * turn the output pin on/off based on pid output
   ************************************************/
    if (windowStartTime == 0) windowStartTime = millis();
    if (millis() - windowStartTime > WindowSize) {  //time to shift the Relay Window
      windowStartTime += WindowSize;
      Serial.println("");
      Serial.print("Relay Window shifted");
      //Serial.print(" --- VALUES: Input: ");
      //Serial.print(Input);
      Serial.print(" --- Setpoint: ");
      Serial.print(Setpoint);
      Serial.print(" --- Output: ");
      Serial.print(Output);
    }

    if (Output > millis() - windowStartTime) {
      digitalWrite(RELAY_PIN, RELAY_ON);
      Serial.print(" --- Relay ON ");
      Serial.print(" --- Time: ");
      Serial.print(millis());
      Serial.println("ms");

    } else {
      digitalWrite(RELAY_PIN, RELAY_OFF);
      Serial.print(" --- Relay OFF");
      Serial.print(" --- Time: ");
      Serial.print(millis());
      Serial.println("ms");
    }
  }
  digitalWrite(RELAY_PIN, RELAY_OFF);
  digitalWrite(Pump, RELAY_OFF);
  lcd.clear();
  lcd.setCursor(8, 1);
  lcd.print("DONE");
  while (1) {}
}

```

And parts of what I get (everything behind "Time: XXX" in each line has been added by me in the email)

09:55:29.980 -> Relay Window shifted --- Setpoint: 70.00 --- Output: 45.02 --- Relay ON  --- Time: 9742ms
09:55:29.980 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9747ms
09:55:30.012 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9752ms
09:55:30.012 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9758ms
09:55:30.012 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9763ms
09:55:30.012 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9769ms
09:55:30.012 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9775ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9780ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9786ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay OFF --- Time: 9791ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay OFF --- Time: 9796ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9802ms
09:55:30.045 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9807ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9812ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9819ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9824ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9830ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay OFF --- Time: 9835ms
09:55:30.079 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC

09:55:29.391 -> Relay Window shifted --- Setpoint: 70.00 --- Output: 100.00 --- Relay ON  --- Time: 9143ms
09:55:29.391 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9148ms
09:55:29.391 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9153ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9159ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9164ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9169ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9176ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9181ms
09:55:29.423 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay OFF --- Time: 9186ms <--- OFF even though Output = 100
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9192ms
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9197ms
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9203ms
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9208ms
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9213ms
09:55:29.456 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9220ms
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9225ms
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9230ms
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay OFF --- Time: 9236ms <--- OFF even though Output = 100
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.56 degC --- FTH Control Temp. = 24.25 degC
09:55:29.489 -> Relay Window shifted --- Setpoint: 70.00 --- Output: 100.00 --- Relay ON  --- Time: 9242ms
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9248ms
09:55:29.489 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9253ms
09:55:29.522 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9259ms
09:55:29.554 -> Volume: 0ml --- NTC Temperature: 25.42 degC --- FTH Control Temp. = 24.25 degC --- Relay ON  --- Time: 9308ms

On Sun, 9 Jul 2023 at 14:24, <diy-pid...@googlegroups.com> wrote:
Pieter S <pieterswa...@gmail.com>: Jul 08 04:24PM +0200

What happens if Windowsize is increased fro 500ms to say 5000ms (5s)?
 
Speed of relays switching?
 
On Sat, 08 Jul 2023, 13:51 Lukas Podschlapp, <lukas.po...@gmail.com>
wrote:
 
Lukas Podschlapp <lukas.po...@gmail.com>: Jul 08 05:50PM +0200

I changed it from 100ms to 500ms. It decreases the amount of wrong windows
in the beginning to only two and then keeps going normally. I would like to
keep it at 100ms though.
 
17:48:41.919 -> Volume: 0ml --- NTC Temperature: 26.11 degC --- FTH
Control Temp. = 27.00 degC
 
17:48:41.919 -> Relay Window shifted --- Setpoint: 70.00 --- Output:
0.00 --- Relay OFF --- Time: 1038ms
 
17:48:41.919 -> Volume: 0ml --- NTC Temperature: 26.11 degC --- FTH
Control Temp. = 27.00 degC
 
17:48:41.919 -> Relay Window shifted --- Setpoint: 70.00 --- Output:
0.00 --- Relay OFF --- Time: 1044ms
 
17:48:41.952 -> Volume: 0ml --- NTC Temperature: 26.25 degC --- FTH
Control Temp. = 27.00 degC --- Relay OFF --- Time: 1050ms
 
17:48:41.952 -> Volume: 0ml --- NTC Temperature: 26.11 degC --- FTH
Control Temp. = 27.00 degC --- Relay OFF --- Time: 1055ms
 
17:48:41.952 -> Volume: 0ml --- NTC Temperature: 26.25 degC --- FTH
Control Temp. = 27.00 degC --- Relay OFF --- Time: 1060ms
 
 
 
Steve Edmonds <st...@edmondsfamily.co.nz>: Jul 09 06:21AM +1200

In the first part, relay is off although output is 100 for about one window.
Serial.print WindowSize and windowStartTime, I suspect  initially you
loop through if millis() - windowStartTime > WindowSize){}
 
Steve
 
On 8/07/23 23:51, Lukas Podschlapp wrote:
Pieter S <pieterswa...@gmail.com>: Jul 08 08:30PM +0200

What is pid.compute() cycle time? 1000ms comes to mind?
 
Try matching the pid cycle time with window?
 
Steve Edmonds <st...@edmondsfamily.co.nz>: Jul 09 06:47AM +1200

Looking at serial output it seems to take 14-15ms to loop.
 
On 9/07/23 06:30, Pieter S wrote:
Steve Edmonds <st...@edmondsfamily.co.nz>: Jul 09 07:11AM +1200

I have found adding a window start time initialisation first time
through the loop helped. I have abstracted the relay control to an
interrupt cycle so it runs independent of the loop cycle but think the
equivalent in your code would be;
in setup()
windowStartTime = 0;
 
In loop()
before
 
  if (millis() - windowStartTime > WindowSize) {  //time to shift the
Relay Window
      windowStartTime += WindowSize;
      Serial.println("");
.............
 
Add
if (windowStartTime ==0 ) windowStartTime = millis();
 
I.e.
if (windowStartTime ==0 ) windowStartTime = millis();
  if (millis() - windowStartTime > WindowSize) {  //time to shift the
Relay Window
      windowStartTime += WindowSize;
      Serial.println("");
 
On 8/07/23 23:51, Lukas Podschlapp wrote:
Pieter S <pieterswa...@gmail.com>: Jul 09 08:29AM +0200

Yes!, that old bug... variables not probably init.
 
I see the default sampling is indeed 200ms
 
https://playground.arduino.cc/Code/PIDLibrarySetSampleTime/
 
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to diy-pid-contr...@googlegroups.com.

Steve Edmonds

unread,
Jul 12, 2023, 5:34:13 AM7/12/23
to diy-pid...@googlegroups.com
Hi Lukas, where did you get your Kp = 30, Ki = 10, Kd = 100 from.
If your Input ranges from say 10-100C and your output is from 0-100, Kp = 30 seems quite high.

Does the NTC sensor sense the water temperature or the heater body temperature
--
You received this message because you are subscribed to the Google Groups "DIY PID Control" group.
To unsubscribe from this group and stop receiving emails from it, send an email to diy-pid-contr...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/diy-pid-control/CABKR0RJGtj2xM%2B7Lp_DSWDz3hhehDYFj9BNnTzDQ-6mwGHBqXQ%40mail.gmail.com.

Reply all
Reply to author
Forward
0 new messages