PID tuning
To tune your PID, you need to write a series of PID values into your axis.
To set PID values, define them using the McPidSettings structure, and then use one of the following functions to apply the values to your axis.
- SetAxisTorquePid: configures the PID settings used by an axis in the torque mode.
- SetAxisVelocityPid: configures the PID settings used by an axis in the velocity mode.
The ways to use SetAxisTorquePid and SetAxisVelocityPid are similar. The following code shows how to define PID values and apply them using SetAxisVelocityPid. To let you adjust the PID values flexibly, we define them in AxisConfiguration.h
. After the PID values are applied, we use SetAxisParameter to set feedback delay.
Code
In AxisConfiguration.cpp
, add the following code:
Copy
VOID UpdatePID(int Index)
{
RtPrintf("PID tuning.\n");
KsCommandStatus status = { 0 };
KsError nRet = errNoError;
McPidSettings myPid =
{
KP, //Proportional gain.
KI, //Integral gain.
KI_LIMIT_PERCENT, //The maximum integral error in percent of the maximum output.
KD, //Derivative gain.
KV, //Velocity feedforward gain.
KAA, //Acceleration feedforward gain.
KAD, //Deceleration feedforward gain.
KJ, //Jerk feedforward gain.
REDUCED_GAIN_DELAY, //Determine when the axis starts using RgFactor after finishing the move. Unit: second.
REDUCED_GAIN_FACTOR, //KP, KI, and KD multiplied by this factor after ReducedGainsDelay.
KI_STOPPED_ONLY, /*Use KI gain only after the move is done, in case you want to avoid KI during the move
to avoid overshooting but have it after to remove the steady-state error.
By default, it's TRUE.*/
KD_USE_INTERNAL_ENCODER, //Use the internal position encoder for KD. By default, it's FALSE.
MINIMUM_OUTPUT, //The minimum output of the PID. The minimum torque in % or minimum velocity.
MAXIMUM_OUTPUT //The maximum output of the PID. The maximum torque in % or maximum velocity.
};
//Apply the PID settings to an axis.
nRet = SetAxisVelocityPid(Index, myPid);
if (nRet != errNoError)
RtPrintf("SetAxisVelocityPid failed: %x\n", nRet);
//Set FeedBackDelay to 3 cycles.
status = SetAxisParameter(Index, mcFeedbackDelay, FEEDBACK_DELAY, mcImmediately);
if (status.Error)
RtPrintf("Failed to set mcFeedbackDelay: %d\n", status.ErrorId);
RtPrintf("\n");
}