Simulink S-Function - Determine Output Size based on Input Data Type

14 次查看(过去 30 天)
I'm trying to achieve the following behavior: I have a Simulink block that must perform encoding of multiple Bus Objects.
The block has been coded in C++ using the S-Function notation (not the S-function builder). I would expect the output to be dynamically sized, i.e., according to the Bus Object that is fed into the input port, the Output Size is set. However, I am not able to do this since, when running the ssInitializeSizes function, the Data Type of the input port appears unknown. In particular, a DataIdx of -1, corresponding to the 'auto' type, is returned.
Note that this behavior for the Data Type is then solved later on during the simulation. Indeed, if I determine the dataIdx and Name during the simulation, the data and Bus object name is properly identified (and this allows me to customize the encoding logic using properly-developed C++ templates).
Is there any way to determine the Data Type before the data sizes have been set?
I understand that I could also work using variable output sizes; however, this forces me to have a fixed sample time for this block.

回答(1 个)

UDAYA PEDDIRAJU
UDAYA PEDDIRAJU 2024-8-30
编辑:UDAYA PEDDIRAJU 2024-9-20
Hi Gianfranco,
As per my understanding you're facing the following issues:
  1. You're using an S-Function to perform encoding on multiple Bus Objects.
  2. The output size should be dynamic based on the input Bus Object.
  3. You're facing issues with determining the data type during "ssInitializeSizes".
Possible Solution:
Utilize "ssGetDataType":
  • Instead of relying solely on "ssGetDataType", use "ssGetDataType" in conjunction with "ssGetInputPortWidth" to determine the data type and size.
  • This should provide you with a more accurate representation of the input data type.
Conditional Logic:
  • Implement conditional logic within your S-Function to handle different input data types and adjust the output size accordingly.
  • You can use "ssGetDataType" and "ssGetInputPortWidth" to determine the data type and size, and then use "ssSetOutputPortWidth" to set the output size.
#include "simstruc.h"
static void mdlOutputs(SimStruct *S, int_T tid) {
// ... your output code here
// Determine input data type and size
DTypeId dtype = ssGetDataType(S, 0);
int_T width = ssGetInputPortWidth(S, 0);
// Set output size based on data type and width
if (dtype == SS_DOUBLE) {
// Set output size for double data type
ssSetOutputPortWidth(S, 0, width);
} else if (dtype == SS_SINGLE) {
// Set output size for single data type
// ...
} else {
// Handle other data types
}
}
  2 个评论
Gianfranco Di Domenico
This does not solve the problem (and, to be honest, also seems a bit AI-generated...).
I clearly specified that while ssGetDataType works in mdlOutputs, at that time the sizes have already been propagated my mdlInitializeSizes, since that is the first function called by the Simulink loop.
UDAYA PEDDIRAJU
UDAYA PEDDIRAJU 2024-9-20
Hey, I have'nt used the AI, I am working S functions from long, MathWorks documentation helps whenever I got into any issue, I read available docs and suggested you that thinking it resolves. There's a misunderstanding.
I came across the following also, did you try using the mdlSetInputPortDataType and mdlSetOutputPortDataType callback methods to handle data type propagation. This allows you to dynamically adjust the output size based on the resolved input data type during simulation setup, rather than relying on a fixed sample time or encountering unknown data types during ssInitializeSizes.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Simulink Coder 的更多信息

产品


版本

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by