Force Simulink Embedded Coder to do a simple cast when converting float to uint16?
Ältere Kommentare anzeigen
I've got a problem with Embedded Coder trying to be too helpful and it's breaking my code.
I want to simply cast a floating point to a uint16 (using the convert block).
Embedded Coder generates this monstruosity:
rtb_DataTypeConversion[0] = (uint16_T)(int32_T)fmod((int32_T)floor(fmax(0.0, fmin(65535.0, PWMGen_TestCode_B.Add_b))), 65536.0);
Which for some reason, when I run on my target (Aurix TC299), just returns 0 all the time (it doesn't like fmod...) I've also tried every option and blocks around, adding saturation, floor etc, it still wants to fmod which breaks my code.
I've hand modified my code to do:
rtb_DataTypeConversion[0] = (uint16_T)(PWMGen_TestCode_B.Add_b);
Which is a simple cast to uint16, and it works! It's as simple as that.
How can I force Simulink/Embedded coder to only generate a simple cast? I don't want to have to hand modify my code everytime, this kind of defeats the point, plus it takes ages...
Thanks for your help.
edit: Solution thanks to Les Backman

2 Kommentare
Les Beckham
am 26 Mär. 2020
Maybe the answer to this question will help with your issue:
Quentin Coret
am 26 Mär. 2020
Antworten (1)
Birdman
am 26 Mär. 2020
Why don't you use a MATLAB Function block and write this line inside it? For example:
y=uint16(x);
This line should go inside MATLAB Function instead using Data Type Conversion block.
5 Kommentare
Quentin Coret
am 26 Mär. 2020
Birdman
am 26 Mär. 2020
Can you share your model? It will help me to understand the situation better.
Quentin Coret
am 26 Mär. 2020
Bearbeitet: Quentin Coret
am 26 Mär. 2020
Birdman
am 26 Mär. 2020
One question:
-Is the ton/toff ratio smaller than 1? If it so, for example, if it is 0.6, then it might be rounded to 0, which is what you are seeing. Can you also share the mask for Data Type Conversion block?
Quentin Coret
am 26 Mär. 2020
Kategorien
Mehr zu Simulink Functions finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


