# Help Converting Control Block to C Code

#### lindula

Joined Sep 23, 2016
49

#### lindula

Joined Sep 23, 2016
49

#### bogosort

Joined Sep 24, 2011
465
The transfer function can be written simply as $\frac{Y(s)}{X(s)} = \frac{K_1}{s+ K_1 K_2} = \frac{2}{s + 6}$ Therefore, $Y(s) = \left(\frac{2}{s + 6}\right) X(s)$ and so $y(t) = 2e^{-6t} x(t)$ As this is a function of continuous time, what you do next depends on your specific application. Presumably the input x is a sampled signal, and so you could simply evaluate y at the given sample points.

If you need a more general solution in the discrete domain, you can work from the z-transform of the transfer function. Alternatively, you can express y as a differential equation in the time domain, and then derive an appropriate difference equation that can be implemented in C.

#### lindula

Joined Sep 23, 2016
49
The transfer function can be written simply as $\frac{Y(s)}{X(s)} = \frac{K_1}{s+ K_1 K_2} = \frac{2}{s + 6}$ Therefore, $Y(s) = \left(\frac{2}{s + 6}\right) X(s)$ and so $y(t) = 2e^{-6t} x(t)$ As this is a function of continuous time, what you do next depends on your specific application. Presumably the input x is a sampled signal, and so you could simply evaluate y at the given sample points.

If you need a more general solution in the discrete domain, you can work from the z-transform of the transfer function. Alternatively, you can express y as a differential equation in the time domain, and then derive an appropriate difference equation that can be implemented in C.
Hello, thank you for responding to my message. I've updated my Simulink model: I added the Discrete integrator blocks

Here is my math: Is my Difference Equation correct? I want to implement this in C code.

Thank you,
Joe

#### lindula

Joined Sep 23, 2016
49
Hello, thank you for responding to my message. I've updated my Simulink model:
View attachment 210927
I added the Discrete integrator blocks

Here is my math:
View attachment 210928
Is my Difference Equation correct? I want to implement this in C code.

Thank you,
Joe Plots zoomed out: The top plot is the outputs of my C function that implements the Difference Equation from my math above. The input to the function I call Motor Drive where a positive step would drive the motor clockwise and a negative step drives the motor in the counter-clockwise direction. The bottom plots of the S-Domain and Z-Domain plots.

Here I zoom in a little. My model shows a very fast rise time and I believe this is because of the type of Solver I have set in Simulink it is set to Variable-step. If I change the Solver to Fixed-Step my model's plot looks better but the Continuous time goes crazy. I probably should have a separate Simulink project with just the Z Domain and my function. Now I will include the views of how to add the C code into matlab:
My C Code that tries to implement a control that would take in a motor drive command and it's final position.  Matlab Function: #### lindula

Joined Sep 23, 2016
49
View attachment 211321
Plots zoomed out:
View attachment 211322

The top plot is the outputs of my C function that implements the Difference Equation from my math above. The input to the function I call Motor Drive where a positive step would drive the motor clockwise and a negative step drives the motor in the counter-clockwise direction. The bottom plots of the S-Domain and Z-Domain plots.

Here I zoom in a little. My model shows a very fast rise time and I believe this is because of the type of Solver I have set in Simulink it is set to Variable-step. If I change the Solver to Fixed-Step my model's plot looks better but the Continuous time goes crazy. I probably should have a separate Simulink project with just the Z Domain and my function.
View attachment 211323
Now I will include the views of how to add the C code into matlab:
My C Code that tries to implement a control that would take in a motor drive command and it's final position.

View attachment 211327

View attachment 211330
Matlab Function:
View attachment 211329
In Simulink go to Model Settings > Simulation: 