Skip to content

Commit

Permalink
add example for pidController
Browse files Browse the repository at this point in the history
  • Loading branch information
camilo committed Feb 15, 2024
1 parent 01b9286 commit de107f4
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions examples/simple_pid_controller/simple_pid_controller.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <qlibs.h>

continuousTF<1> processTransferFunction= {
{ 0.0f, 1.5f, },
{ 2.0f, 1.0f, },
};

constexpr real_t dt = 0.05f; /*Time step*/
real_t yt = 0.0f; /*Process output*/
real_t ut = 0.0f; /*Controller output*/
real_t wt = 0.0f; /*Set-Point*/
continuousSystem process( processTransferFunction, dt );
pidController controller;
int inputPin = A0; // select the input pin for the potentiometer that will drive the setPoint

void setup() {
Serial.begin(9600);
controller.setup( 5.0f, 10.0f ,0.01f ,dt );
controller.setSaturation( 0.0f, 100.0f );
}

void loop() {
/*Set-Point is driven from a potentiometer connected to analog input*/
wt = map( analogRead( inputPin ), 0, 1023, 0.0f, 100.0f );
ut = controller.control( wt, yt );
yt = process.excite( ut );

Serial.print( ut );
Serial.print( "," );
Serial.print( wt );
Serial.print( "," );
Serial.println( yt );
delay( dt*1000 );
}

0 comments on commit de107f4

Please sign in to comment.