How can I create a custom CAN Receive block with device parameters?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi all,
I'm new to Vehicle Network Toolbox and I want to create a CAN Receive simulink block which includes device paramters.
I just found this link and followed the steps to create my custom block and it was very helpfull but I couldn't figure out to how can I add a can device paramters to this custom CAN block. I know there is already Can Receive block includes matwork supported can devices but I'm trying to unpack same can message comes from different can devices and I just want to be able to change the can message source device that changes based on device parameters.
How can I add device paramters to custom CAN receive block(s-function)?
Thanks!
0 件のコメント
回答 (1 件)
Aditya
2024 年 1 月 24 日
Hi Cell,
I understand that you want to add device parameters to custom CAN. Following steps might help you.
To add device parameters to a custom CAN receive block implemented as an S-function in Simulink, you'll need to modify the S-function code to include parameters for the CAN device configuration. This typically involves the following steps:
1. Define the parameters: Add parameters to your S-function that represent the device configuration (e.g., device channel, baud rate, etc.).
2. Use the parameters in the setup phase: In the `mdlInitializeSizes` function, set up the S-function parameters using `ssSetNumSFcnParams`. These parameters will be used to configure the CAN device during the initialization phase.
3. Retrieve the parameter values: In the `mdlStart` function, which is called when the simulation starts, retrieve the parameter values using `ssGetSFcnParam`. Use these values to initialize and configure the CAN device.
4. Create a mask for the S-function block: To make it user-friendly, create a mask for the S-function block that exposes the device parameters as block parameters. This allows users to enter the device configuration directly in the block mask.
Here's a simplified example of how you might modify the S-function code to include a device channel parameter:
function [sys,x0,str,ts,simStateCompliance] = sfuntmpl(t,x,u,flag,deviceChannel)
switch flag
% Initialization
case 0
[sys,x0,str,ts,simStateCompliance] = mdlInitializeSizes(deviceChannel);
% Rest of the cases (mdlOutputs, mdlUpdate, etc.)
% ...
end
end
% Function to initialize sizes
function [sys,x0,str,ts,simStateCompliance] = mdlInitializeSizes(deviceChannel)
sizes = simsizes;
% ... (other size initializations)
sys = simsizes(sizes);
x0 = []; % Initial state
str = [];
ts = [0, 0]; % Sample time: [0, 0] for inherited sample time
simStateCompliance = 'UnknownSimState';
% Define S-function parameters
block.NumDialogPrms = 1; % Number of S-function parameters
block.DialogPrmsTunable = {'Nontunable'}; % Parameter tunability
% ... (other initialization code)
end
% Function called at the start of the simulation
function mdlStart(s)
deviceChannel = ssGetSFcnParam(s, 0); % Get the device channel parameter
% Initialize and configure the CAN device using the deviceChannel
% ...
end
Once you have modified the S-function code, you can create a mask for the block that allows the user to input the device parameters:
1. Right-click on the S-function block in your Simulink model and select "Create Mask."
2. In the Mask Editor, add the parameters you want to expose (e.g., device channel, baud rate).
3. Link each parameter in the Mask Editor to the corresponding S-function parameter by setting the "Variable name" to match the index of the `ssGetSFcnParam` call in the S-function code (e.g., `deviceChannel` for the first parameter).
Now, when you use the custom CAN receive block in your model, you'll be able to set the device parameters directly from the block mask, and the block will use these parameters to configure the CAN device during simulation.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!