From d66ce4b75b1904419f62f666fb231201312a4a11 Mon Sep 17 00:00:00 2001 From: Fabien Poussin Date: Thu, 22 Mar 2018 16:30:35 +0100 Subject: Adding PID library --- os/various/pid.h | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 os/various/pid.h (limited to 'os/various/pid.h') diff --git a/os/various/pid.h b/os/various/pid.h new file mode 100644 index 0000000..d98df3e --- /dev/null +++ b/os/various/pid.h @@ -0,0 +1,78 @@ +#ifndef PID_h +#define PID_h + +#include "chtypes.h" + +//Constants used in some of the functions below +#define PID_AUTOMATIC 1 +#define PID_MANUAL 0 +#define PID_DIRECT 0 +#define PID_REVERSE 1 +#define PID_P_ON_M 0 +#define PID_P_ON_E 1 + + +typedef struct { + + float dispKp; // * we'll hold on to the tuning parameters in user-entered + float dispKi; // format for display purposes + float dispKd; // + + float kp; // * (P)roportional Tuning Parameter + float ki; // * (I)ntegral Tuning Parameter + float kd; // * (D)erivative Tuning Parameter + + int controllerDirection; + int pOn; + + float *myInput; // * Pointers to the Input, Output, and Setpoint variables + float *myOutput; // This creates a hard link between the variables and the + float *mySetpoint; // PID, freeing the user from having to constantly tell us + // what these values are. with pointers we'll just know. + unsigned long lastTime; + float outputSum; + float lastInput; + + unsigned long SampleTime; + float outMin; + float outMax; + + bool inAuto; + bool pOnE; + +} pid_t; + + +//commonly used functions ************************************************************************** +void pid_create(pid_t* p, float* Input, float* Output, float* Setpoint, // * constructor. links the PID to the Input, Output, and + float Kp, float Ki, float Kd, int POn, int ControllerDirection); // Setpoint. Initial tuning parameters are also set here. + // (overload for specifying proportional mode) + +void pid_setmode(pid_t* p, int mode); // * sets PID to either Manual (0) or Auto (non-0) + +bool pid_compute(pid_t* p); // * performs the PID calculation. it should be + // called every time loop() cycles. ON/OFF and + // calculation frequency can be set using SetMode + // SetSampleTime respectively + +void pid_setOutputLimits(pid_t* p, float Min, float Max); // * clamps the output to a specific range. 0-255 by default, but + // it's likely the user will want to change this depending on + // the application + + + +//available but not commonly used functions ******************************************************** +void pid_setTunings(pid_t* p, float Kp, float Ki, float Kd, int POn); // * While most users will set the tunings once in the + // constructor, this function gives the user the option + // of changing tunings during runtime for Adaptive control + +void pid_setControllerDirection(pid_t* p, int Direction); // * Sets the Direction, or "Action" of the controller. DIRECT + // means the output will increase when error is positive. REVERSE + // means the opposite. it's very unlikely that this will be needed + // once it is set in the constructor. +void pid_setSampleTime(pid_t* p, int NewSampleTime); // * sets the frequency, in Milliseconds, with which + // the PID calculation is performed. default is 100 + +void pid_initialize(pid_t* p); + +#endif -- cgit v1.2.3 From 12552897f32efc7448810c8ccd70d3c22dadccf7 Mon Sep 17 00:00:00 2001 From: Fabien Poussin Date: Thu, 22 Mar 2018 16:58:48 +0100 Subject: Cleaning PID lib. --- os/various/pid.h | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'os/various/pid.h') diff --git a/os/various/pid.h b/os/various/pid.h index d98df3e..d20b35d 100644 --- a/os/various/pid.h +++ b/os/various/pid.h @@ -14,26 +14,26 @@ typedef struct { + float kp; // * (P)roportional Tuning Parameter + float ki; // * (I)ntegral Tuning Parameter + float kd; // * (D)erivative Tuning Parameter + float dispKp; // * we'll hold on to the tuning parameters in user-entered float dispKi; // format for display purposes float dispKd; // - - float kp; // * (P)roportional Tuning Parameter - float ki; // * (I)ntegral Tuning Parameter - float kd; // * (D)erivative Tuning Parameter - int controllerDirection; + int direction; int pOn; - float *myInput; // * Pointers to the Input, Output, and Setpoint variables - float *myOutput; // This creates a hard link between the variables and the - float *mySetpoint; // PID, freeing the user from having to constantly tell us - // what these values are. with pointers we'll just know. + float *input; // * Pointers to the Input, Output, and Setpoint variables + float *output; // This creates a hard link between the variables and the + float *setPoint; // PID, freeing the user from having to constantly tell us + // what these values are. with pointers we'll just know. unsigned long lastTime; float outputSum; float lastInput; - unsigned long SampleTime; + unsigned long sampleTime; float outMin; float outMax; @@ -45,15 +45,15 @@ typedef struct { //commonly used functions ************************************************************************** void pid_create(pid_t* p, float* Input, float* Output, float* Setpoint, // * constructor. links the PID to the Input, Output, and - float Kp, float Ki, float Kd, int POn, int ControllerDirection); // Setpoint. Initial tuning parameters are also set here. - // (overload for specifying proportional mode) + float Kp, float Ki, float Kd, int POn, int Direction); // Setpoint. Initial tuning parameters are also set here. + // (overload for specifying proportional mode) void pid_setmode(pid_t* p, int mode); // * sets PID to either Manual (0) or Auto (non-0) bool pid_compute(pid_t* p); // * performs the PID calculation. it should be // called every time loop() cycles. ON/OFF and // calculation frequency can be set using SetMode - // SetSampleTime respectively + // SetsampleTime respectively void pid_setOutputLimits(pid_t* p, float Min, float Max); // * clamps the output to a specific range. 0-255 by default, but // it's likely the user will want to change this depending on @@ -66,10 +66,10 @@ void pid_setTunings(pid_t* p, float Kp, float Ki, float Kd, int POn); // * Whil // constructor, this function gives the user the option // of changing tunings during runtime for Adaptive control -void pid_setControllerDirection(pid_t* p, int Direction); // * Sets the Direction, or "Action" of the controller. DIRECT - // means the output will increase when error is positive. REVERSE - // means the opposite. it's very unlikely that this will be needed - // once it is set in the constructor. +void pid_setDirection(pid_t* p, int Direction); // * Sets the Direction, or "Action" of the controller. DIRECT + // means the output will increase when error is positive. REVERSE + // means the opposite. it's very unlikely that this will be needed + // once it is set in the constructor. void pid_setSampleTime(pid_t* p, int NewSampleTime); // * sets the frequency, in Milliseconds, with which // the PID calculation is performed. default is 100 -- cgit v1.2.3 From 9e5aba8bd9da9fbba617f6abc95a4e1a2a841882 Mon Sep 17 00:00:00 2001 From: Fabien Poussin Date: Thu, 22 Mar 2018 18:16:20 +0100 Subject: Adding basic PID demo. --- os/various/pid.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'os/various/pid.h') diff --git a/os/various/pid.h b/os/various/pid.h index d20b35d..74d116d 100644 --- a/os/various/pid.h +++ b/os/various/pid.h @@ -8,8 +8,8 @@ #define PID_MANUAL 0 #define PID_DIRECT 0 #define PID_REVERSE 1 -#define PID_P_ON_M 0 -#define PID_P_ON_E 1 +#define PID_ON_M 0 +#define PID_ON_E 1 typedef struct { @@ -48,7 +48,7 @@ void pid_create(pid_t* p, float* Input, float* Output, float* Setpoint, // * co float Kp, float Ki, float Kd, int POn, int Direction); // Setpoint. Initial tuning parameters are also set here. // (overload for specifying proportional mode) -void pid_setmode(pid_t* p, int mode); // * sets PID to either Manual (0) or Auto (non-0) +void pid_setMode(pid_t* p, int mode); // * sets PID to either Manual (0) or Auto (non-0) bool pid_compute(pid_t* p); // * performs the PID calculation. it should be // called every time loop() cycles. ON/OFF and -- cgit v1.2.3 From 71fe8e7ced20bbcea503e0ef22e155c09f63a1f7 Mon Sep 17 00:00:00 2001 From: Fabien Poussin Date: Sun, 25 Mar 2018 04:21:11 +0200 Subject: Renaming PID struct typedef to avoid conflicts --- os/various/pid.h | 54 +++++++++++++++++++++++++++--------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) (limited to 'os/various/pid.h') diff --git a/os/various/pid.h b/os/various/pid.h index 74d116d..49ccd6f 100644 --- a/os/various/pid.h +++ b/os/various/pid.h @@ -13,11 +13,11 @@ typedef struct { - + float kp; // * (P)roportional Tuning Parameter float ki; // * (I)ntegral Tuning Parameter float kd; // * (D)erivative Tuning Parameter - + float dispKp; // * we'll hold on to the tuning parameters in user-entered float dispKi; // format for display purposes float dispKd; // @@ -36,43 +36,43 @@ typedef struct { unsigned long sampleTime; float outMin; float outMax; - + bool inAuto; bool pOnE; -} pid_t; +} pidc_t; //commonly used functions ************************************************************************** -void pid_create(pid_t* p, float* Input, float* Output, float* Setpoint, // * constructor. links the PID to the Input, Output, and - float Kp, float Ki, float Kd, int POn, int Direction); // Setpoint. Initial tuning parameters are also set here. - // (overload for specifying proportional mode) +void pid_create(pidc_t* p, float* Input, float* Output, float* Setpoint, // * constructor. links the PID to the Input, Output, and + float Kp, float Ki, float Kd, int POn, int Direction); // Setpoint. Initial tuning parameters are also set here. + // (overload for specifying proportional mode) -void pid_setMode(pid_t* p, int mode); // * sets PID to either Manual (0) or Auto (non-0) +void pid_setMode(pidc_t* p, int mode); // * sets PID to either Manual (0) or Auto (non-0) -bool pid_compute(pid_t* p); // * performs the PID calculation. it should be - // called every time loop() cycles. ON/OFF and - // calculation frequency can be set using SetMode - // SetsampleTime respectively +bool pid_compute(pidc_t* p); // * performs the PID calculation. it should be + // called every time loop() cycles. ON/OFF and + // calculation frequency can be set using SetMode + // SetsampleTime respectively -void pid_setOutputLimits(pid_t* p, float Min, float Max); // * clamps the output to a specific range. 0-255 by default, but - // it's likely the user will want to change this depending on - // the application +void pid_setOutputLimits(pidc_t* p, float Min, float Max); // * clamps the output to a specific range. 0-255 by default, but + // it's likely the user will want to change this depending on + // the application //available but not commonly used functions ******************************************************** -void pid_setTunings(pid_t* p, float Kp, float Ki, float Kd, int POn); // * While most users will set the tunings once in the - // constructor, this function gives the user the option - // of changing tunings during runtime for Adaptive control - -void pid_setDirection(pid_t* p, int Direction); // * Sets the Direction, or "Action" of the controller. DIRECT - // means the output will increase when error is positive. REVERSE - // means the opposite. it's very unlikely that this will be needed - // once it is set in the constructor. -void pid_setSampleTime(pid_t* p, int NewSampleTime); // * sets the frequency, in Milliseconds, with which - // the PID calculation is performed. default is 100 - -void pid_initialize(pid_t* p); +void pid_setTunings(pidc_t* p, float Kp, float Ki, float Kd, int POn); // * While most users will set the tunings once in the + // constructor, this function gives the user the option + // of changing tunings during runtime for Adaptive control + +void pid_setDirection(pidc_t* p, int Direction); // * Sets the Direction, or "Action" of the controller. DIRECT + // means the output will increase when error is positive. REVERSE + // means the opposite. it's very unlikely that this will be needed + // once it is set in the constructor. +void pid_setSampleTime(pidc_t* p, int NewSampleTime); // * sets the frequency, in Milliseconds, with which + // the PID calculation is performed. default is 100 + +void pid_initialize(pidc_t* p); #endif -- cgit v1.2.3