From 3d42b11e804c3dae68737e626a7b587601c7f918 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Sun, 12 Feb 2023 13:01:13 +0100 Subject: [PATCH] 0.1.6 PID_RT --- .../PID_RT/.github/workflows/arduino-lint.yml | 2 +- .../.github/workflows/arduino_test_runner.yml | 2 +- .../PID_RT/.github/workflows/jsoncheck.yml | 2 +- libraries/PID_RT/CHANGELOG.md | 8 +- libraries/PID_RT/LICENSE | 2 +- libraries/PID_RT/PID_RT.cpp | 2 +- libraries/PID_RT/PID_RT.h | 6 +- libraries/PID_RT/README.md | 55 ++++++++--- .../PID_simulated_heater/.arduino-ci.yml | 27 ++++++ .../PID_simulated_heater.ino | 93 +++++++++++++++++++ libraries/PID_RT/library.json | 2 +- libraries/PID_RT/library.properties | 2 +- libraries/PID_RT/test/unit_test_001.cpp | 3 +- 13 files changed, 179 insertions(+), 27 deletions(-) create mode 100644 libraries/PID_RT/examples/PID_simulated_heater/.arduino-ci.yml create mode 100644 libraries/PID_RT/examples/PID_simulated_heater/PID_simulated_heater.ino diff --git a/libraries/PID_RT/.github/workflows/arduino-lint.yml b/libraries/PID_RT/.github/workflows/arduino-lint.yml index b2ca058c..8a26f14a 100644 --- a/libraries/PID_RT/.github/workflows/arduino-lint.yml +++ b/libraries/PID_RT/.github/workflows/arduino-lint.yml @@ -6,7 +6,7 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: arduino/arduino-lint-action@v1 with: library-manager: update diff --git a/libraries/PID_RT/.github/workflows/arduino_test_runner.yml b/libraries/PID_RT/.github/workflows/arduino_test_runner.yml index 096b975b..fadfa904 100644 --- a/libraries/PID_RT/.github/workflows/arduino_test_runner.yml +++ b/libraries/PID_RT/.github/workflows/arduino_test_runner.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: ruby/setup-ruby@v1 with: ruby-version: 2.6 diff --git a/libraries/PID_RT/.github/workflows/jsoncheck.yml b/libraries/PID_RT/.github/workflows/jsoncheck.yml index 04603d08..37a11298 100644 --- a/libraries/PID_RT/.github/workflows/jsoncheck.yml +++ b/libraries/PID_RT/.github/workflows/jsoncheck.yml @@ -10,7 +10,7 @@ jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: json-syntax-check uses: limitusus/json-syntax-check@v1 with: diff --git a/libraries/PID_RT/CHANGELOG.md b/libraries/PID_RT/CHANGELOG.md index 1f79522a..3c425c01 100644 --- a/libraries/PID_RT/CHANGELOG.md +++ b/libraries/PID_RT/CHANGELOG.md @@ -6,12 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.1.6] - 2023-02-12 +- add example **PID_simulated_heater.ino** Kudos to drf5n +- update readme.md +- update GitHub actions +- update license 2023 + + ## [0.1.5] - 2022-10-23 - add RP2040 to build-CI - add changelog.md - minor edits readme.md - ## [0.1.4] - 2021-12-23 - update library.json, license, - minor edits diff --git a/libraries/PID_RT/LICENSE b/libraries/PID_RT/LICENSE index c3d6b3da..42604f34 100644 --- a/libraries/PID_RT/LICENSE +++ b/libraries/PID_RT/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2020-2022 Rob Tillaart +Copyright (c) 2020-2023 Rob Tillaart Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/libraries/PID_RT/PID_RT.cpp b/libraries/PID_RT/PID_RT.cpp index e221e0ef..3666981c 100644 --- a/libraries/PID_RT/PID_RT.cpp +++ b/libraries/PID_RT/PID_RT.cpp @@ -1,7 +1,7 @@ // // FILE: PID_RT.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.1.5 +// VERSION: 0.1.6 // PURPOSE: PID library for Arduino // URL: https://github.com/RobTillaart/PID diff --git a/libraries/PID_RT/PID_RT.h b/libraries/PID_RT/PID_RT.h index 6e406755..e8e2b591 100644 --- a/libraries/PID_RT/PID_RT.h +++ b/libraries/PID_RT/PID_RT.h @@ -2,7 +2,7 @@ // // FILE: PID_RT.h // AUTHOR: Rob Tillaart -// VERSION: 0.1.5 +// VERSION: 0.1.6 // PURPOSE: PID library for Arduino // URL: https://github.com/RobTillaart/PID_RT @@ -10,7 +10,7 @@ #include "Arduino.h" -#define PID_LIB_VERSION (F("0.1.5")) +#define PID_LIB_VERSION (F("0.1.6")) class PID_RT @@ -64,7 +64,7 @@ public: // set Proportional on Input or on Error - void setPropOnInput() { _POI = true; }; // default + void setPropOnInput() { _POI = true; }; // default void setPropOnError() { _POI = false; }; bool isPropOnInput() { return _POI == true; }; bool isPropOnError() { return _POI == false; }; diff --git a/libraries/PID_RT/README.md b/libraries/PID_RT/README.md index 8b1b8ee1..23fa3ac3 100644 --- a/libraries/PID_RT/README.md +++ b/libraries/PID_RT/README.md @@ -15,9 +15,29 @@ Arduino library for PID controller. The PID_RT class allows the user to instantiate a PID controller. +This library allows one to +- adjust the K parameters runtime. +- stop / start computing runtime. + +(to be elaborated) + + +#### Some PID background + +- https://en.wikipedia.org/wiki/PID_controller +- https://www.ni.com/nl-nl/innovations/white-papers/06/pid-theory-explained.html +- https://www.youtube.com/watch?v=wkfEZmsQqiA + +E-book +- https://www.elektor.nl/pid-based-practical-digital-control-with-raspberry-pi-and-arduino-uno-e-book + ## Interface +```cpp +#include "PID_RT.h" +``` + ### Constructor - **PID_RT()** minimal constructor. @@ -26,14 +46,14 @@ The PID_RT class allows the user to instantiate a PID controller. ### Core -- **void reset()** resets internals to startup. -- **void setPoint(float sp)** sets setPoint, that needs to be reached. -- **float getSetPoint()** read back setPoint. -- **bool compute(float input)** does one iteration of the PID controller. -Returns **true** after calculation. +- **void reset()** resets internals to startup (Kp == Ki == Kd == 0). +- **void setPoint(float sp)** sets the setPoint, that needs to be reached. +- **float getSetPoint()** read back the setPoint. +- **bool compute(float input)** does one iteration of the PID controller. +Returns **true** after a calculation is done. Returns **false** if not computed, either due to stop flag or not yet time to do the calculation. - **float getOutput()** get the last calculated output value. -- **bool setK(float Kp, float Ki, float Kd)** Set the initial P I D parameters as a group. +- **bool setK(float Kp, float Ki, float Kd)** Set the initial **P I D** parameters as a group. Overwrites the values set in the constructor. @@ -86,25 +106,30 @@ almost sure that no iterations are missed. ## Operations -See examples. +See examples and +- https://wokwi.com/projects/356437164264235009 (thanks to drf5n) ## Future -#### must +#### Must - update / improve documentation - more testing -#### should -- add examples to test more -- improve unit test -- move all code to .cpp +#### Should -#### could - -- add reference to PID book / website? - investigate if it works as PI or P controller too. - PI as derived or base class? +- add examples to test more +- improve unit test + +#### Could + +- add reference to PID book / website? +- move all code to .cpp + + +#### Wont diff --git a/libraries/PID_RT/examples/PID_simulated_heater/.arduino-ci.yml b/libraries/PID_RT/examples/PID_simulated_heater/.arduino-ci.yml new file mode 100644 index 00000000..25980972 --- /dev/null +++ b/libraries/PID_RT/examples/PID_simulated_heater/.arduino-ci.yml @@ -0,0 +1,27 @@ +platforms: + rpipico: + board: rp2040:rp2040:rpipico + package: rp2040:rp2040 + gcc: + features: + defines: + - ARDUINO_ARCH_RP2040 + warnings: + flags: + +packages: + rp2040:rp2040: + url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json + +compile: + # Choosing to run compilation tests on 2 different Arduino platforms + platforms: + - uno + # - due + # - zero + # - leonardo + - m4 + - esp32 + # - esp8266 + # - mega2560 + - rpipico diff --git a/libraries/PID_RT/examples/PID_simulated_heater/PID_simulated_heater.ino b/libraries/PID_RT/examples/PID_simulated_heater/PID_simulated_heater.ino new file mode 100644 index 00000000..b57a3fcb --- /dev/null +++ b/libraries/PID_RT/examples/PID_simulated_heater/PID_simulated_heater.ino @@ -0,0 +1,93 @@ +// +// FILE: PID_simulated_heater.ino +// AUTHOR: drf5n (based on basic example) +// PURPOSE: demo +// +// This simulates a 20W heater block driven by the PID +// Wokwi https://wokwi.com/projects/356437164264235009 +// +// https://github.com/RobTillaart/PID_RT/issues/5 + + +#include "PID_RT.h" + +PID_RT PID; + +const int PWM_PIN = 3; // UNO PWM pin + +int op = 0; +float input = 0; + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + PID.setPoint(125); + PID.setOutputRange(0, 255); // PWM range + PID.setInterval(50); + PID.setK(2, 5, 1); + PID.start(); + + op = analogRead(A0); +} + + +void loop() +{ + float heaterWatts = ((int)op)*20.0/255; // 20W heater + float blockTemp = simPlant(heaterWatts); // simulate heating + input = blockTemp; + // input = analogRead(A0); + if (PID.compute(input)) + { + op = PID.getOutput(); + analogWrite(PWM_PIN, op); + + // Serial.print(PID.getInput()); + // Serial.print('\t'); + // Serial.println(op); + } + report(); +} + + +void report(void){ + static uint32_t last = 0; + const int interval = 1000; + if (millis() - last > interval){ + last += interval; +// Serial.print(millis()/1000.0); + Serial.print(PID.getSetPoint()); + Serial.print(' '); + Serial.print(input); + Serial.print(' '); + Serial.println(op); + } +} + +float simPlant(float Q){ // heat input in W (or J/s) + // simulate a 1x1x2cm aluminum block with a heater and passive ambient cooling + // next line "C is not used", fails in arduino-build-ci for ESP32 + // float C = 237; // W/mK thermal conduction coefficient for Al + float h = 5; // W/m2K thermal convection coefficient for Al passive + float Cps = 0.89; // J/g°C + float area = 1e-4; // m2 area for convection + float mass = 10; // g + float Tamb = 25; // °C + + static float T = Tamb; // °C + static uint32_t last = 0; // last call + uint32_t interval = 100; // milliseconds + + if(millis() - last >= interval){ + last += interval; + T = T + Q * interval / 1000 / mass / Cps - (T - Tamb) * area * h; + } + return T; +} + + +// -- END OF FILE -- + diff --git a/libraries/PID_RT/library.json b/libraries/PID_RT/library.json index a6b1dc4e..098e9fe7 100644 --- a/libraries/PID_RT/library.json +++ b/libraries/PID_RT/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/PID_RT" }, - "version": "0.1.5", + "version": "0.1.6", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/libraries/PID_RT/library.properties b/libraries/PID_RT/library.properties index 78e05f11..56ad36ae 100644 --- a/libraries/PID_RT/library.properties +++ b/libraries/PID_RT/library.properties @@ -1,5 +1,5 @@ name=PID_RT -version=0.1.5 +version=0.1.6 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino PID library diff --git a/libraries/PID_RT/test/unit_test_001.cpp b/libraries/PID_RT/test/unit_test_001.cpp index a815b75b..2bd0bf1f 100644 --- a/libraries/PID_RT/test/unit_test_001.cpp +++ b/libraries/PID_RT/test/unit_test_001.cpp @@ -103,4 +103,5 @@ unittest(test_compute) unittest_main() -// -------- +// -- END OF FILE -- +