Pass bus signals to a function as an array (Stateflow)

3 次查看(过去 30 天)
Hello!
I'm working within stateflow and defining a transition check:
[checkInRange(User_CMDs.r,0,1)==1]
Check In range is a simple function as follows and returns true if all inputs are true (In Range):
function result = checkInRange(input,request,tolerance)
result = abs(request - input) <= tolerance;
result = all(result(:));
end
Ive been able to pass arrays as input to the function, however i am trying to define a transition and for a state within simulink. The User_CMD.r is a bus with 4 singals called X1, X2, X3 and X4. I need all of these to be in range for state transition. Is there a way of doing this without doing it as below?
[checkInRange([User_CMDs.r.X1,User_CMDs.r.X1,User_CMDs.r.X1,User_CMDs.r.X1], 0,1)==1]

回答(1 个)

Purvaja
Purvaja 2025-4-2
编辑:Purvaja 2025-4-2
I understand that you want to pass array as input to the function in Stateflow diagram as transition condition to next state. To simulate given diagram in your question, I combined three different signals generated by “Signal Generator” block in Simulink using a “Bus Selector” and passed it to the “Stateflow” block as inputs.
To access a particular signal in combined signals, we must mention its index in the array instead of dot operator, since a MATLAB function accepts scalar or array values. The state also needs a condition back to initial state when the signal at that time instant does not meet the tolerance. This allows us to check only a single value of signal at a particular time instant and not entire signal.
Refer to the below diagrams for results achieved using the user-defined tolerance function i.e. “checkInRange” function as given in the question:
  • Simulink Model consisting of dummy signals, showing input and output of “Stateflow” block:
  • Stateflow diagram to determine tolerance for combined signal:
  • Stateflow diagram to determine tolerance for a particular signal, where “User_CMDs(2)”, denotes 2nd signal of input and it is closed inside “[]” to translate it into array:
For more details, kindly refer to the following MathWorks documentation:
  1. Stateflow: https://www.mathworks.com/help/stateflow/getting-started.html
  2. Bus Selector: https://www.mathworks.com/help/simulink/slref/busselector.html
To access the documentation for the MATLAB release you are using, please execute the following command in the MATLAB command window:
web(fullfile(docroot, 'stateflow/getting-started.html'))
web(fullfile(docroot, 'simulink/slref/busselector.html'))
Let me know in case you expected a different answer and try to provide your model so that I have correct measurements and block parameters, that you intend to work with.

类别

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

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by