Hi,
I'm programming a fligh controller with Arduino Pro Mini 16Mhz
I'm having troubles instancing the PID library for yaw pitch roll.
Declaration
//PID
float outputRollStable;
float outputPitchStable;
float outputYawStable;
float targetYaw
float targetPitch;
float targetRoll;
PID yawReg(&ypr[0], &outputYawStable, &targetYaw, YAW_P_VAL, YAW_I_VAL, YAW_D_VAL, DIRECT);
PID pitchReg(&ypr[1], &outputPitchStable, &targetPitch, PITCH_P_VAL, PITCH_I_VAL, PITCH_D_VAL, REVERSE);
PID rollReg(&ypr[2], &outputRollStable, &targetRoll, ROLL_P_VAL, ROLL_I_VAL, ROLL_D_VAL, REVERSE);
Init function en setup()
bool initPID()
{
yawReg.SetMode(AUTOMATIC);
pitchReg.SetMode(AUTOMATIC);
rollReg.SetMode(AUTOMATIC);
yawReg.SetOutputLimits(-YAW_PID_LIMIT, YAW_PID_LIMIT);
pitchReg.SetOutputLimits(-PITCH_PID_LIMIT, PITCH_PID_LIMIT);
rollReg.SetOutputLimits(-ROLL_PID_LIMIT, ROLL_PID_LIMIT);
//yawReg.SetSampleTime(10);
//pitchReg.SetSampleTime(10);
//rollReg.SetSampleTime(10);
return true;
}
Compute function
void computePID()
{
//RX
channel[THRO] = receiverInputChannel3;
channel[YAW] = receiverInputChannel4;
channel[PITCH] = receiverInputChannel2;
channel[ROLL] = receiverInputChannel1;
targetYaw = map((float)channel[YAW], RC_MIN, RC_MAX, -YAW_PID_INFLUENCE, YAW_PID_INFLUENCE);
targetPitch = map((float)channel[PITCH], RC_MIN, RC_MAX, -PITCH_PID_INFLUENCE, PITCH_PID_INFLUENCE);
targetRoll = map((float)channel[ROLL], RC_MIN, RC_MAX, -ROLL_PID_INFLUENCE, ROLL_PID_INFLUENCE);
yawReg.Compute();
pitchReg.Compute();
rollReg.Compute();
}
The only PID that computes is rollReg.
That is because in the declaration is the last one. If I move the pitchReg after rollReg, then only roll will be computed.
//PID
float outputRollStable;
float outputPitchStable;
float outputYawStable;
float targetYaw
float targetPitch;
float targetRoll;
PID yawReg(&ypr[0], &outputYawStable, &targetYaw, YAW_P_VAL, YAW_I_VAL, YAW_D_VAL, DIRECT);
PID rollReg(&ypr[2], &outputRollStable, &targetRoll, ROLL_P_VAL, ROLL_I_VAL, ROLL_D_VAL, REVERSE);
PID pitchReg(&ypr[1], &outputPitchStable, &targetPitch, PITCH_P_VAL, PITCH_I_VAL, PITCH_D_VAL, REVERSE);
(now pitch works)
The PID library is Arduino standard, just modified doubles for float (attached). The values of radio are ok.
Any idea? The functions aren't static, so I dont understand the behaviour. Maybe pointers?
Thanks you.
PID.cpp (6.4 KB)
PID.h (3.47 KB)