How to get the value from a inport of a customed block with s-function and tlc concept

8 ビュー (過去 30 日間)
Chuyen
Chuyen 2025 年 1 月 7 日
コメント済み: Chuyen 2025 年 1 月 14 日
Hello all,
Currently I have a Simulink customed block (named as Teq block) for my own project which was built in s-function for simulation and tlc for code generation. Because of some reasons on algorithm, I have to find a way to get the value of a Simulink.Parameter(another block) via my Teq_sf.tlc file. However, my tlc file can only get the name of Simulink.Parameter instead of its value as my expectation.
I tried to define a mdlRTW function in sf.c file and call this input to tlc file via RTW, as followings:
static void mdlRTW(SimStruct *S) % in sf.c file
{
real64 *u1 = (real64 *) ssGetInputPortSignal(S, 0);
if (!ssWriteRTWParamSettings( S, 1, SSWRITE_VALUE_QSTR, "Input_Value", *u1))
{
ssSetErrorStatus(S,"Error writing parameter data to .rtw file");
return;
}
}
and %assign input_value = SFcnParamSettings.Input_Value (in tlc)
However, this way did not work because system can not understand SFcnParamSettings. Currently, this is the big blocker for my progress.
Do you know any valid solutions which I can get a value of Simulink.Parameter into my Teq_sf.tlc file but not the name?
Thank you in advance!
  2 件のコメント
prabhat kumar sharma
prabhat kumar sharma 2025 年 1 月 7 日
The error you're encountering is related to the data type mismatch in Simulink. Your MATLAB Function block is outputting regular matrices, but the Simulink model expects a bus signal. Here's how you can resolve this issue:
You can try using the below steps:
1. Understand the Bus Structure:
- First, you need to understand the structure of the bus signal that your controller expects. This can usually be found in the Simulink model's workspace or in the relevant block parameters.
2. Create a Bus Object:
- You need to create a bus object that matches the expected structure. You can do this programmatically or using the Simulink Bus Editor.
3. Modify the MATLAB Function Block:
- Update your MATLAB function to output a bus signal instead of regular matrices. Here's an example of how you might do it:
function TrajectoryBus = extractWaypoints(TrajectoryInfo)
% Extract X and Y waypoints from the planned trajectory
optimalTrajIndex = TrajectoryInfo.OptimalTrajectoryIndex;
trajectory = TrajectoryInfo.GlobalTrajectory(optimalTrajIndex).Trajectory;
% Create a bus signal
TrajectoryBus = struct('X_ref', trajectory(:, 1), 'Y_ref', trajectory(:, 2));
end
4. Define the Bus in the Model:
- In the Simulink model, define the bus object to match the structure created in the MATLAB function. You can use the following MATLAB code to define a bus:
elems(1) = Simulink.BusElement;
elems(1).Name = 'X_ref';
elems(1).Dimensions = [N 1]; % Replace N with the appropriate dimension
elems(2) = Simulink.BusElement;
elems(2).Name = 'Y_ref';
elems(2).Dimensions = [N 1]; % Replace N with the appropriate dimension
TrajectoryBusType = Simulink.Bus;
TrajectoryBusType.Elements = elems;
% Register the bus object in the base workspace
assignin('base', 'TrajectoryBusType', TrajectoryBusType);
5. Configure the MATLAB Function Block:
- Set the output of the MATLAB function block to be of type `Bus: TrajectoryBusType`.
6. Verify and Test:
- Ensure that the bus object is correctly defined and linked in your Simulink model. Test the model to verify that the data is being passed correctly.
I hope it will help you!
Chuyen
Chuyen 2025 年 1 月 14 日
Dear Sharma,
Thank you so much for approaching me with the very detail answer.
Actually, I have not used Matlab Function block inside my customed block and what I applied for my block is S-function and TLC code. The expectation here is that I can get the value of another block which is considered as an input of my customed block. In S-function file, this expectation was sastified because its policy. However, when I come the same with TLC file which serves for Code generation normally, it returned me the name of this parameter instead of the value.
So, I thought about two potential solution:
  1. First solution which I will add some more command in TLC file then I can take the value of input block.
  2. Create a intermediate variable in S-function and take it via RTW to TLC file.
Unfortunately, I could not know any commands/functions availables for my request.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeSimulink Coder についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by