This example shows a simple model of a fitness watcher implemented by using the
duration operator in Stateflow®. This example also demonstrates a workflow for building a Simulink® model that can interact with a user interface that was designed using the MATLAB® App Designer.
The fitness watcher in this model has four parts. Human_Simulator is a subsystem modeling your activity, and outputs signals like your heart rate and speed. Fitness Watcher is a Stateflow chart modeling the core logic, such as how to determine your status and when to send out different useful notifications. UI Controller is a Stateflow chart that talks to the graphical user interface (UI). The last part is an app designed with MATLAB App Designer, which is responsible for UI implementation and callback definition.
The Fitness Watcher chart controls the output of the fitness watch. This chart receives inputs from the subsystem Human_Simulator, where the heart rate and speed are calculated. It also receives inputs from the UI Setting panel for parameters such as how soon to remind the person for some exercise. Then the chart determines the activity of the person. As the inputs from Human_Simulator actively change, the detected activity also changes. This transitional process filters out signal noises by using the
duration operator. For instance, while you are resting you may make some quick and sudden movements, but it does not necessarily mean that you are exercising. The fitness watch thinks you are walking or exercising only after the movement lasts for some period of time. The
duration operator is ideal for situations like this when debouncing is required. The chart Fitness Watcher also tracks the duration of each activity. Under different conditions, the chart sends Stateflow messages to the UI Controller chart, allowing the UI to display customized notifications.
The output data and messages from the Fitness Watcher chart are processed by the UI Controller chart. The chart talks to the UI by invoking callback functions defined in the App Designer created app. Since these callback functions are defined within the app, they are not immediately known to the Stateflow chart. The
Init state contains scripts enabling Stateflow to talk to the app. By declaring the app name
coder.extrinsic, the Stateflow model can open the app and store the app handle using a local data app. By declaring the callback functions as
coder.extrinsic, you can call them anywhere in the same chart. All callback functions from the app take the local data
app as the first argument.
The MATLAB App Designer can be used to quickly lay out the visual components of a user interface through its Design View. It can also be used to program the app behavior via the Code View. In this example, the model created by Simulink® and Stateflow can co-simulate with the app created by the MATLAB App Designer. During simulation, you can interact with the UI, changing the Simulink signals which drive the inputs to the Stateflow charts. For example, when you press the Rest button on the UI, the constant block named Activity changes its value to the enum
Activity.Rest. Then the Human Simulator starts to produce corresponding signals modeling a resting person. On the other hand, Stateflow can invoke callback functions defined in the app, which changes the UI display. For example, if you remain in the
Rest state for a given amount of time, the Fitness Watcher chart sends out a Stateflow message to the UI Controller chart, which invokes the callback
updateText. A notification then shows up in the main display and replaces the clock for five seconds.