Electronic – Implementation of D (differentiation) in PID

cpid controller

I am implementing a PID controller for motor speed control. I am done with implementing the PI control and it works perfectly fine for me. In the specification I have been told to implement a filtering technique by using the following equation for the D part:

enter image description here

Now what I understand so far, is that s represents "dx/dt" generally which corresponds to the rate of change of error, but here I can relate it with the rate of change of feedback. Td/N is for limiting the overall gain output (hope I got this right).
Now to represent this in terms of C code, I tried the following way:

        s = (CurrentFeedback()-Old_Feedback)*100/(MaxFeedback()); //to calculate the % change in feedback
        s = s*1000/sampleTime;      //1000 is multiplied because sampleTime is in milliseconds
        D = (Td*s)/(1+(s*Td/N));
        D = D*KP;   //Kp is multiplied as per the standard pid equation.

        Old_Feedback = CurrentFeedback();
        PID = P+I-D;

Well the results by adding D are not what I have predicted.
I just want to know if I implemented the D portion equation correctly? Am I making any mistakes in my understanding of the basic maths of differentiation?

NOTE: I am not in liberty to change the recalculate the kp,ti,td as it comes directly from the VFD.

Best Answer

I have 3 points to share :

1-I will tell you why people mess with D term interms of sign , PID= P + I + D but , D = kd* (error -olderror) , and in position control if you are approaching the set point , always olderror is < error so D term will be negative , and thus it decreases the output and prevent the overshoot.

2-Regarding time , donot divide or use time . since you are using embedded microcontroller use just make a time constant and cancel it from all equations : for example use a timer interrupt to generate 1ms time constant and call your function inside .

3-Why you are using PID for velocity control , 90% of the time PI is enough in speed control , since D here is more like accelaration control .

Hope that helps