Hello,
For context, I am using the STM32 Hardware Support Add-On and Simulink in order to deploy a model on a STM32 MCU.
After some signal processing, the model extracts 2 values (double), and I want these values to be sent through USART to another device.
This is the model I've come up with :
My goal is to generate a uint8 vector containing :
My first idea for this seemingly trivial task was to use sprintf :
function data = fcn(valA, valB)
msg = sprintf('A = %06.2f | B = %06.2f\r\n', valA, valB);
I got this error message :
Error:'data' is inferred as a variable-size matrix, but its size is specified as inherited or fixed. Verify 'data' is defined in terms of non-tunable parameters, or select the 'Variable Size' check box and specify the upper bounds in the Size box.
However, since I'm running code generation tools, I cannot use variable-size signals, so ticking the "Variable Size" box is not an option. I have tried many things, but could not solve this issue. Here is what I ended up with :
function data = USART(valA, valB)
valB = round(valB * 100) / 100;
valA = round(valA * 100) / 100;
message = sprintf('A = %06.2f | B = %06.2f', valA, valB);
elseif numel(message) < 50
message = [message, repmat(' ', 1, 50-numel(message))];
My reasoning is that :
- valA or valB have a maximum character count (both before/after the decimal point) ;
- The sprintf formatting ensures that if the values are smaller than the maximum character count, the missing characters are padded with zeros ;
- The truncating/padding instructions ensures that signal width is constant (redundant).
This functions runs great in MATLAB, and generates a fixed length uint8 vector no matter what values valA and valB were given, positive, negative, small, big...
However, in Simulink, same error remains.
The only case where I can get any output from data is when I use hard coded values :
message = sprintf('A = %06.2f | B = %06.2f\r\n', valA, valB);
This compiles and gets deployed on the target device, but hard-coded values have no practical use.
How can this be solved ? How can I get my desired output ?
Thanks in advance,
Nicolas