I'm working on an autopilot program for an ATMega328. In one of my header files I'm defining the control function and asking it to constrain a number (so the plane doesn't try banking unsafely ). Constrain is a built in function, and so it should work just about anywhere, right? I keep getting "PID.h:23:error: ' constrain' was not declared in this scope"
Here's the code in question. Help?
#define PITCHNAV 0
#define ROLLNAV 1
#define PITCHOUT 2
#define ROLLOUT 3
#define YAWOUT 4
#define THROTOUT 5
#define TURNBANK 30
#define CLIMBRATE 2.032
#define MAXPITCHUP 30
#define MAXPITCHDOWN -15
#define MAXBANK 60
const int KP[] = { 15, 1, 0.1, 0.1, 5, -0.5, 3000, 15};
const int KI[] = {0.03, 1, 0.01, 0.001, 1, -0.004, 600, 0};
const int KD[] = { -10, 1, 0.002, 0.00005, 0.05, -0.03, 0, 1};
float integ[] = {0, 0, 0, 0, 0, 0, 0, 0};
float preverror[] = {0, 0, 0, 0, 0, 0, 0, 0};
float PID(int mode, float target, float actual, float dt){
if (mode == PITCHOUT){
target = constrain(target, MAXPITCHDOWN, MAXPITCHUP);
}
if (mode == ROLLOUT){
target = constrain(target, -MAXBANK, MAXBANK);
}
float error = target - actual;
integ[mode] = integ[mode] + error * dt;
float deriv = (error - preverror[mode]) / dt;
preverror[mode] = error;
return ((KP[mode] * error) + (KI[mode] * integ[mode]) + (KD[mode] * deriv));
}