Skip to content
Snippets Groups Projects
Commit c809cfad authored by Benedikt Burger's avatar Benedikt Burger
Browse files

Add the duty cycle as a readable parameter via Serial.

parent b1f59c78
No related branches found
No related tags found
No related merge requests found
# CHANGELOG
......@@ -3,6 +3,29 @@
Sketches for Arduinos & Co.
You can use this repository as the arduino sketchbook in the IDE.
For that reason, the sketches (`ino` files) have the be in a folder of the same name.
## Good practices
* Add a `CHANGELOG.md` file for each arduino sketch.
* Whenever you do a change, change the version string in that file according to [semantic versioning](https://semver.org/): Major.Minor.Patch.
* If a sketch allows serial communication, you should be able to read the currently installed sketch.
Several programs use `v\n` to request the version.
The version should contain the Filename at compilation, the version, and the compilation date (for cross referencing a not mentioned change).
For that, you can use the following lines:
```
// Top of the file
const String version = "1.3.0"; // Version of this script. Filename is added before, date of compilation afterwards.
// wherever serial communication is handled
Serial.print(__FILE__); // file name at compilation time
Serial.print(" ");
Serial.print(version); // the version string
Serial.print(" ");
Serial.print(__DATE__); // compilation date
Serial.println();
```
# List of sketches with their functions
......@@ -52,7 +75,7 @@ Schaltet die Shutter des Quanta-Rays.
## TemperatureController
Kontrolliert die Temperatur mittels Ventilsteuerung und kann einerseits Messwerte (wie *ReadSensors*) ausgeben, andererseits können Parameter (Solltemperatur & Co.) eingestellt werden.
Kontrolliert die Temperatur (oder andere Größe) mittels Schaltung von binären Steuerelementen (Ventil auf/zu) und kann einerseits Messwerte (wie *ReadSensors*) ausgeben, andererseits können Parameter (Solltemperatur & Co.) eingestellt werden.
Es ist eine deutlich verbesserte Variante des *LegacyTemperatureController*.
......
# CHANGELOG
## 1.3.0
### Added
* Add the duty cycle (in per mille) as a global variable (clamped to 0 to 1000) readable via debug.
......@@ -9,7 +9,7 @@
*/
//** CONSTANTS PARAMETERS change for your application **//
const String version = "1.2.3"; // Version of this script. Filename is added before, date of compilation afterwards.
const String version = "1.3.0"; // Version of this script. Filename is added before, date of compilation afterwards.
const int cyclePeriod = 1; //program cycle period in s
const int controlPin = 11; //Pin by which the control unit is controlled (11 or 12)
......@@ -69,13 +69,14 @@ RunningAverage average5(averaging); //generate running average variable
unsigned long nextCycle = 3000; //set starting values for an initial delay of some seconds to prevent racing conditions on start
unsigned long nextOff = 0; // When to disable the control for the next time
//******************************PID-CONTROL*****************************************
// Werte werden aus EEPROM gelesen
float setpoint; // setpoint for the temperature = 36°C
float PIDKp; //Wert für die PID Regelung (Proportional-Teil)
float PIDKi; //Wert für die PID Regelung (Integral-Teil)
float integral; //Wert für die PID Integralteil
float duty_cycle; //Current duty cycle in per mille
int controller = 1; // controller mode: 0 = off, 1 = on
......@@ -214,6 +215,8 @@ void loop(){
Serial.print(PIDKi);
Serial.print(", setpoint: ");
Serial.print(setpoint);
Serial.print(", duty cycle: ");
Serial.print(duty_cycle);
Serial.println();
break;
case 'v': // Send Version
......@@ -248,7 +251,7 @@ void loop(){
}
}
// Control temperature, if desired
// Start new pulse, calculating the duty cycle and pulse length
if ( millis() >= nextCycle ){
if (controller > 0){
calculatePID();
......@@ -256,6 +259,7 @@ void loop(){
nextCycle += cyclePeriod * 1000;
}
// stop the pulse, if pulse length elapsed
if ( millis() >= nextOff && nextOff > 0){
enable_control(false);
}
......@@ -305,8 +309,10 @@ void sendSensorDataAverage(){
void calculatePID(){
// Calculate the PID signal
// Calculate the PID signal and duty cycle, and start a new pulse
float error = setpoint - average0.getAverage();
// calculate integral part, clamp it between 0 and 1000
integral += PIDKi * error;
if (integral > 1000){
integral = 1000;
......@@ -314,25 +320,28 @@ void calculatePID(){
else if (integral < 0){
integral = 0;
}
float pid = PIDKp * error + integral;
if (pid < 0){
// caclulate the duty cycle, clamp it between 0 and 1000
duty_cycle = PIDKp * error + integral;
if (duty_cycle < 0){
nextOff = 0;
duty_cycle = 0;
enable_control( false );
}
else {
nextOff = millis() + cyclePeriod * pid;
// start a new pulse and set the pulse length
nextOff = millis() + cyclePeriod * duty_cycle;
enable_control( true );
}
if (duty_cycle > 1000){
duty_cycle = 1000;
}
}
void enable_control(const boolean enabled){
//Enable or disable the control (relay, valve...)
if ( enabled ){
digitalWrite(controlPin, HIGH);
digitalWrite(indicatorPin, HIGH);
}
else {
digitalWrite(controlPin, LOW);
digitalWrite(indicatorPin, LOW);
if (digitalRead(controlPin) != enabled){
digitalWrite(controlPin, enabled);
digitalWrite(indicatorPin, enabled);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment