mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.6 PID_RT
This commit is contained in:
parent
9a24a34bca
commit
3d42b11e80
@ -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
|
||||
|
@ -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
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
- **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 calculation.
|
||||
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
|
||||
|
@ -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
|
@ -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 --
|
||||
|
@ -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": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=PID_RT
|
||||
version=0.1.5
|
||||
version=0.1.6
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino PID library
|
||||
|
@ -103,4 +103,5 @@ unittest(test_compute)
|
||||
unittest_main()
|
||||
|
||||
|
||||
// --------
|
||||
// -- END OF FILE --
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user