matlabsimulinkfunction-callstateflow

How can I generate an output pulse in Simulink every time a function-call trigger occurs?


I have an existing Simulink model from a customer that uses function-call semantics as event triggers on Stateflow charts, amongst other things. For our current purposes, I need to be able to generate a simple output pulse when a function-call trigger occurs, such that:

Stateflow diagram example

And although this does generate a pulse, the transition from High to Low state only occurs when the E event occurs, rather than 1ms after when the initial E event occurred. That is, it is obvious that the chart is going to sleep in between the E function-call events. The E events are around 200ms apart, and each pulse of 'y' is also the same width - when I want it to only be 1ms wide when y=1. Is there a way to change the chart settings so that it stays awake until the transition from High back to Low has occurred? Or could I acheive the same behaviour using a Function-call subsytem instead of a Stateflow chart? Either is fine for me.

(The overall model uses a Variable solver, as the block generating the function-call pulses requires it.)

Edit: Top level model would be something like this, although the block generating the function call is more complex and doesn't just generate the function call at a regular sample time interval.

enter image description here


Solution

  • It looks like you're using the event as an enabled trigger for your chart, which means the chart is only being evaluated when the trigger is enabled. This means the chart output is only updated once per trigger, giving your issue.

    Instead, one approach would be to convert the event source into a Boolean signal which is the full rate of your model, i.e. kHz to capture a 1ms sample, then use a detected increase in this signal as a "normal" (not an enabled trigger) variable for your stateflow.

    Here is a minimal model

    model

    In this case, I'm deliberately driving an enabled subsystem from the event, shown below, which creates the Boolean signal. Note that you need to set the "Output when disabled" property of the out port to "reset" so that it flips back to 0 between event triggers. Otherwise you have the same problem.

    event to Boolean

    The "Detect Increase" block is in the standard Simulink library, and converts our Boolean (which lasts the frequency of the event) to a single sample on the rising edge. You may not need this depending on what your E signal looks like, in my case it was required because E comes from a signal-builder pulse.

    Now the chart has a simple Boolean input which is true for one sample on the event trigger. We can implement a chart very similar to the one you had:

    stateflow

    Obviously you can change the after duration to suit your needs.