PID complete code

This page contains the complete code of PID.

In AxisConfiguration.cpp, your PID code should be as follows:

Copy
#include "RT_Project_01.h"
#include "AxisConfiguration.h"
using namespace std;

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");
}