I need to write a simulink s-function that is triggered by an input signal - simulink

I wish to write data to a file when an integrator saturates. I would like to route the integrator's saturation output port to one of the s-function's inputs. I realize that I can continuously poll the signal, but I'd like to explore triggering on the signal. In other words, for the s-function to behave similarly to a software interrupt.

Madison,
If you like to call s-function based on trigger, you might explore using a relational operator to check the condition. When the condition is True, function would be triggered.
Trying to embed a screenshot below:
Screenshot

F. Madison,
If you like to trigger it on signal change from 0 to 1, you can make use of a condition made up of relational operator and Unit delay:
Another Screenshot

Related

How to store a specific time that a signal value is changed in another parameter in Matlab simulink?

I have two Matlab function blocks that one is producing zero signal for the other. I want to store the exact time that signal changes to one in another parameter in order to use it elsewhere.
How can I do it?
This type of functinality is achieved using a Triggered Subsystem, as per the image below,
The output of the trigger block will take on value of the input (which in this case is the simulation time) every time the trigger signal rises. Look at the parameters of the Trigger block inside the subsystem for other options such as falling edge, or both edge, triggering.

Simulink: get step time in a triggered subsystem

I have a question about my triggered subsystem in Simulink:
So the Susbsystem is executed every 0.01 s.
My question is: how can I get this variable (0.01) inside my subsystem?
NB: I don't need the Simulation step time.
I've already thought to two solutions:
1) use a Digital Clock with a Memory block
2) use a Weighted Sample Time block
but, both of them gave me an initial value of 0.
Do you have an elegant solution for my task
Thank you very much.
Have a nice day
The easiest solution is to have a constant block feeding into the subsystem. Define a MATLAB Workspace variable dt=0.01; and have the constant set to dt, and the sample rate of your Pulse Generator set to dt.
Alternatively you can use the Probe block to get the Sample Rate of the signal coming out of the Pulse Generator, and feed that as an input to the Subsystem.
Another way is to use get_param() command in your workspace.
In your case, it should be:
get_param('<path-to-block>/Pulse Generator','Period')
This should output:
>> ans =
0.01
I had this same problem myself. I received the following feedback from MATHWORKS when I reported it as a bug:
If triggered subsystems are executed on edge detection on the triggering event then it will require at least two model execution time steps in order to satisfy the triggering condition. As a result the triggered subsystem will not execute at 0 causing the results you are seeing. This is expected behavior.
If you require the triggered subsystem to execute on the first timestep you can change the "Trigger type" option to "function call." This option will execute the subsystem or model when the control signal receives a function-call event from a Statflow chart, Function-Call Generator block, or an S-function block." In this case you will just need to confirm that your function call block executes prior to the triggered subsystem block. This can be confirmed using the "Blocks - Execution Order" Debug tool under "Information Overlays."

Restarting or resetting input signal independently of simulation time and many times

I have designed a signal by using the Signal Builder block in Simulink. During the run of my simulation, the signal builder is to restart depending on the satisfaction of a condition. The condition can be satisfied many times, so, the signal should start from the beginning each time. If I want to put it in another way: You can take step input instead of the designed signal. Step input is to be reset conditionally. How can I make it?
I haven't seen such a functionality built-in Simulink (maybe the newer versions have it?) but here is a workaround:
You can simulate passed time with an integrator that has a Constant block set to 1 as input. The point is that the integrator block has a reset port which you can connect to your condition. So, when your condition becomes true, the integrator restarts a time variable from 0 (which is set in the initial conditions). Beforehand, you need to have your signal saved in something like a Table Lookup block, which outputs the signal as a function of time. Then you connect the integrator output to the Table Lookup block.
Have you tried putting your Signal Builder block in an enabled subsystem? You probably need to set the states when enabling to reset for it to work. Have a look at Create an Enabled Subsystem in the documentation for more details.

Dynamic wiring in linked block

The problem:
In my simulink model, I have a bus creator that will send all the signals to a block. The block purpose is to make a selector and a switch dynamically based on an index. Example : I have 3 objects, each with a position and a velocity signal, and I want to get the position of the 3 objects, so 3 signals (this means the bus selector will have 3 output and the multi switch will have 4 (one for the index)). The block is working correctly, with a recursive function script called in the callback, it checks the bus for all possible signals and count the number of objects, and modifies the bus selector and the switch accordingly. The problem is that this block is linked to a library, so I have to disable the link in order for the callback script to work.
The questions:
What should i change in order to avoid disabling the link?
Is there another way of making dynamic wiring and don't use a callback script?
I suggest a using vector concatenation block instead of bus creator plus a matlab function block to choose. Here is the model
and here is the code in matlab function:
In this way the block in the library does not need to change each time. Also if you have to keep your buses you can use a bus to vector block to convert them to vector.
The idea of using a MATLAB Function block to do the selecting is a good one, but I would try and stay with a Bus input to preserve the advantages of working with busses, rather than convert to vector and back. So instead of having your code as a callback to the bus selector block, my suggestion would be to implement it in a MATLAB Function block with a bus input and the signal input used to do the selection in the bus, and define the output still as a bus object.

control simulink from M-file

I am trying to control a simulink from a M-file.
What I want to do in the M-file is give the simulink model some input, run the simulink model, change one input value at 0.6 seconds, continue running the simulink model with the new input.
I already know that by using set_param, I can start, pause and continue the simulink, but the problem is I don't know how to pause the simulink model at a certain time(0.6s), is it possible to get the current time from simulink model and read it in the M-file?
Another way I already know is using sim to run simulink model from 0 to 0.6s, and use SimState to save the information at 0.6s, then load these information to resume the simulation. I am trying to change the input before the simulation resumed, but it seems that the model will load the input values from the information it saved, it won't take the new input value.
I stuck in this problem for a very long time, could someone help me with this please?
Thank you very much.
You can get the current time of a running simulation with:
get_param('simulink_model_name', 'SimulationTime');
So for instance by checking this value from your M-file during simulation by using
timer(...)
you can detect when the simulation is at 0.6 seconds.
I used a combination of simulink and m-script to achieve a similar goal.
In your model, add one 'assert' block. Double click it, and uncheck 'Stop Simulation when assertion fails'. In the 'Simulation Callback when assertion fails' field, add three commands:
set_param(bdroot,'SimulationCommand','pause');
run('myscript.m'); %insert the script name
set_param(bdroot,'SimulationCommand','continue');
Now connect the inport of this block to a 'not equal to' relational operator. Connect the first inport of the relational operator to a clock (pls set the decimation for analog clock or the sample time [usually -1 for inherited] for the digital clock).
The second inport is connected to constant block with a value of 0.6
On simulating the model, the simulation will pause at 0.6 sec, execute the m-file to change the input parameter (considering that it's tunable) and then continue with the simulation.
The assertion block is called when its input signal becomes 0. At 0.6 sec, the output of the relational operator will be 0.
Let me know if it worked.
This is not currently possible from an M-file. If you want to dynamically change the input at a given time externally, it will require an S-Function. Even this solution is difficult and wrought with flakey-ness since the Mathworks does not want to support this functionality in that it defeats one of the features of another toolbox they sell. In time, I believe they will grant this privledge, but it does not exist today. Also, why not use a dynamic input block to change the input value, like a map, signal builder, etc. ?