00001 #ifndef PID_H 00002 #define PID_H 00003 00004 /* 00005 pid.h 00006 00007 Decls for PID control law data structure and class 00008 00009 Modification history: 00010 00011 6-Jul-2001 FMP removed decls for the useless functions pidGetCycleTime(), 00012 pidGetGains(). 00013 7-Nov-2000 WPS added lastSetPoint_was_set flag. 00014 13-Jul-2000 WPS added some hacks surrounded by THROWAWAY_CUMULATIVE_ERROR_ON_SIGN_CHANGES and SMOOTH_D_FACTOR 00015 13-Mar-2000 WPS added unused attribute to pid_h to avoid 'defined but not used' compiler warning. 00016 24-Feb-2000 FMP added deadband 00017 18-Aug-1998 FMP took out reverse gains, added bias 00018 5-Jun-1998 FMP added reverse gains 00019 14-Apr-1998 FMP added backlash 00020 18-Dec-1997 FMP took out C++ interface 00021 19-Nov-1997 FMP added ff2 00022 16-Oct-1997 FMP changed vf, af to ff0, ff1 00023 15-Aug-1997 FMP added maxError 00024 01-Aug-1997 FMP added lastOutput to PID_STRUCT; added pidReset() 00025 24-Apr-1997 FMP added pidIniLoad(); removed inifile 00026 17-Apr-1997 FMP split into C/C++ sections 00027 */ 00028 00029 /* ident tag */ 00030 #ifndef __GNUC__ 00031 #ifndef __attribute__ 00032 #define __attribute__(x) 00033 #endif 00034 #endif 00035 00036 /* Uncomment these lines if you like these mathematically impure effects. */ 00037 /* #define SMOOTH_D_FACTOR */ 00038 /* #define THROWAWAY_CUMULATIVE_ERROR_ON_SIGN_CHANGES */ 00039 00040 static char __attribute__((unused)) pid_h[] = "$Id: pid.h,v 1.6 2001/07/06 21:50:05 proctor Exp $"; 00041 00042 typedef struct 00043 { 00044 double p; /* proportional gain, volts/unit */ 00045 double i; /* integral gain, volts/unit */ 00046 double d; /* derivative gain, volts/unit per sec */ 00047 double ff0; /* 0th order feedforward, volts/unit */ 00048 double ff1; /* 1st order feedforward, volts/unit/sec */ 00049 double ff2; /* 2nd order feedforward, volts/unit/sec^2 */ 00050 double backlash; /* backlash */ 00051 double bias; /* bias for gravity comp, for example */ 00052 double maxError; /* ceiling for cumError */ 00053 double deadband; /* delta below which error is deemed to be 0 */ 00054 double cycleTime; /* copy of arg to setCycleTime() */ 00055 double error; /* error this cycle */ 00056 double lastError; /* last error computed */ 00057 double lastSetpoint; /* last setpoint received */ 00058 double lastOutput; /* last value output */ 00059 double lastSetpointDot; /* delta setpoint / delta t */ 00060 double cumError; /* cumulative error, all cycles */ 00061 int configured; /* configure flags */ 00062 #ifdef SMOOTH_D_FACTOR 00063 #define MAX_ERROR_WINDOW 4 00064 double oldErrors[MAX_ERROR_WINDOW]; /* This is a ring buffer that stores the 00065 last MAX_ERROR_WINDOW errors. */ 00066 double errorWindowFactors[MAX_ERROR_WINDOW];/* Weights to use in smoothing 00067 error factors. */ 00068 int errorIndex; /* Index into the oldErrors ring buffer. */ 00069 #endif 00070 int lastSetpoint_was_set; /* true when lastSetpoint is valid. */ 00071 00072 } PID_STRUCT; 00073 00074 extern int pidInit(PID_STRUCT * pid); 00075 extern int pidReset(PID_STRUCT * pid); 00076 extern int pidSetCycleTime(PID_STRUCT * pid, double seconds); 00077 extern int pidSetGains(PID_STRUCT * pid, PID_STRUCT parameters); 00078 extern double pidRunCycle(PID_STRUCT * pid, double sample, double setpoint); 00079 extern int pidIniLoad(PID_STRUCT * pid, const char * filename); 00080 00081 #endif /* PID_H */