# Help Converting Control Block to C Code

#### lindula

Joined Sep 23, 2016
78
Hello, I have the a Control Block that I need to implement in C code.

Here is my math:
Y(s) = K1 E(s) (1/s)
E(s) = X(x) - K2 Y(s)

Now put E(s) in Y(s)

Y(s) = K1 (1/s)(X(s) - K2Y(s))

Y(s) = K1(1/s)X(s) - K1K2X(s)Y(s)

y(t) = K1 x(t) y(t-1) - K1K2 x(t) y(t)

I know my math is not right but can anyone help me please? If I can get y(t) I should be able to get in C code.

Thanks,
Joe

#### Attachments

• 14.7 KB Views: 1

#### lindula

Joined Sep 23, 2016
78

If I take the Inverse Laplace I get an exponential in the y(t). Is the a why I can avoid using an an exponential?

I needed to add in a Saturation Block in the feedback path.

There mus be a simple why one could convert the block to a C program.

#### bogosort

Joined Sep 24, 2011
674
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
78
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
78
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:

#### Attachments

• 42.8 KB Views: 0
• 9.5 KB Views: 0
• 23 KB Views: 0
• 9.5 KB Views: 0

#### lindula

Joined Sep 23, 2016
78
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: