3

I'm trying to create a simple PID simulator in C. It is a simple cruise control system and I have chosen to model my system as a low pas filter.

I'm having difficulty writing my mathematical model in C.

I found a very similar question and a somewhat useful answer in:

Convert first order transfer function to c code however I did not understand the answer completely.

I would like to create a function that accepts an input (later the output of my PID but for now I'd like to test it with a step) and my sampling time and provides me with an output from 0 to steady state.

For simplicity sake Id like to program the following transfer function G(s) = 1/(s+10) with a sampling rate of 0.01.

After applying tustin transformation I end up with: (0.004762z + 0.004762)/(z-0.9048)

How do I go from here to writing it in C?

From the above link the answer provided was:

double firstOrderLag(double input, double coeff, double dT, double *state){
// Function to implement the discretization of a continuous time first
// order lag sys = coeff/(s+coeff) using the Tustin (Bilinear) transformation.

    double num = (1/(1+2/coeff/dT)); // numerator
    double den = (1-2/coeff/dT)*num; // denominator
    double temp;
    double output;

    temp = input - den*(*state);
    output = num*(temp + (*state));
    *state = temp;

    return output;
}

So in my particular case num will be 0.004762 and den = -0.9048 I'm curious as to how this was acheived from the z domain transfer function.

I'm also not clear as to what the pointer variable state is and thus struggle to understand the rest of the code.

Any help clarifying this will be much appreciated as I feel that if I have a clear understanding of this function I will be able to implement it in a for loop to simulate open loop response, feedback response and eventually integrate my PID conotroller.

Community
  • 1
  • 1
David
  • 31
  • 1
  • 2
  • Read about Z-transforms: by filter's transfer function you can build recursive formula that will help you to compute signal on exit of filter having signal on its input. – Eddy_Em May 06 '13 at 12:43
  • Thanks Eddy, I have been doing that but most of the material I find falls short on actual implementation. Is there any specific book/article/website you can suggest I read? thanks! – David May 07 '13 at 05:40
  • Read for example [this](http://courses.media.mit.edu/2009fall/mas160/Recitation10_09.pdf), [wiki](http://en.wikipedia.org/wiki/Z-transform#Linear_constant-coefficient_difference_equation) and also google: z-transform difference equation. – Eddy_Em May 07 '13 at 05:49
  • I think something finally clicked, it is the difference equations that are implementable in code. So if I have the transfer function in z domain and inverse z transform it I will be left with the unit pulse response. My output then becomes a sum of this function and the input. Edit: Cheers for the help! I'll get on implementing and see how I go. – David May 07 '13 at 11:05
  • Actually in the z transfer function you have all coefficients that you need for both your previous inputs and outputs. There is no need for any inverse z transform. – Martin Dec 25 '18 at 19:53

0 Answers0