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.