Model a Fitness Tracker
This example shows how to create an interface between a Stateflow® chart and a MATLAB® app created in App Designer. For other examples that show how to connect a Stateflow chart to a MATLAB app, seeModel a Power Window ControllerandSimulate a Media Player.For a version of this example that uses standalone charts in MATLAB, seeModel a Fitness App by Using Standalone Charts.
In this example, a MATLAB app models a fitness tracker. During simulation, you can adjust the settings for the tracker and select an activity (Sleep,Rest,Walk, orExercise). When you chooseExercise, you can also set the intensity of your workout.
The Stateflow chartApp Interface
provides a bidirectional connection between the MATLAB app and the control and plant systems in the Simulink® model. When you interact with the widgets in the app, the chart communicates your selections to the other charts in the model. Conversely, the chart uses the output of the fitness tracker to update the numeric and text fields in the app. For example, when you click theRestbutton on the app, theApp Interface
chart sets the value of the outputactivity
to the enumerated valueActivity.Rest
.TheHuman Simulator
chart responds by producing vital sign values that model a person at rest. TheFitness Tracker
chart analyses these values and sets the output signalstatus
toActivity.Rest
.TheApp Interface
chart monitors this signal and updates the contents of theStatusfield in the app toRest
.
To run the example, open the Simulink model and clickRun.The chartApp Interface
opens the app and initializes theHuman Simulator
andFitness Tracker
charts. While the example is running, one second of simulation represents one minute of exercise time. To stop the simulation, click停止or close the app.
Connect Chart to MATLAB App
The chartApp Interface
is configured to communicate with the MATLAB appsf_fitness_app
.
The app uses a property called
chart
to interface with the chartApp Interface
.应用callbacks use this property to read the chart inputs and write to the chart outputs. For example, when you change the value of one of the fields in theSettingspane, a callback updates the value of the corresponding field of the output structurethreshold
.Similarly, when you select a new activity or change the intensity of your workout in theHuman Simulatorpane, a callback sets the value of the chart outputsactivity
andintensity
.最后,当您关闭应用程序时,UIFigureCloseRequest
callback sets the value of the chart outputstop
totrue
.In the chart, the entry actions in the
InterfaceWithApp
state run the appsf_fitness_app
and store the returned value as the local data objectapp
.The chart uses this local data object when it calls the helper functionsupdateStatus
,updateClock
,updateText
,updateSteps
, andupdateHeartRate
.In the app, these helper functions change the contents of the activity status, clock, and step counter fields, and create the animation effects in the heartbeat and footstep displays. For example, when the chart receives anotification
message, the substateMainDisplay
calls the helper functionupdateText
.This function replaces the contents of the clock display with a customized notification. After five seconds, the substate calls the helper functionupdateClock
to restore the clock display.
For more information on how to create a bidirectional connection between your MATLAB app and a Stateflow chart, seeModel a Power Window ControllerandSimulate a Media Player.
The functions used to interact with the app are not supported for code generation, so theInterfaceWithApp
state first calls thecoder.extrinsic
function to declare them as extrinsic MATLAB code. For more information, seeCall Extrinsic MATLAB Functions in Stateflow Charts.
Simulate Vital Signs Based on Activity
TheHuman Simulator
chart models the vital signs of a human engaged in the activity you select in the app. The chart uses the output structurevitals
to relay these vital signs to the fitness tracker. The fields of the structure represent your heart rate, speed, and the number of steps that you have taken. When you select a new activity or adjust the intensity of your workout, the chart calls the functiontransition
to ensure that these vital signs change gradually over time. To detect changes in activity or exercise intensity, the chart calls thehasChanged
operator. For more information, seeDetect Changes in Data and Expression Values.
Determine Fitness Tracker Output
The chartFitness Tracker
models the core logic of the fitness tracker. The chart consists of four subcharts that correspond to the possible activities. The chart registers your activity status based on the heart rate and speed produced by theHuman Simulator
chart and transitions between these subcharts. To filter out signal noise, the chart uses theduration
operator to implement simple debouncing logic. For instance, when you are at rest, you can make some quick and sudden movements that do not correspond to exercise. The chart determines that you are walking or exercising only if your motion lasts longer than two minutes (or two seconds of simulation time). The chart monitors the active child state and passes this information to theApp Interface
chart through the output datastatus
.For more information, seeMonitor State Activity Through Active State Data.
The chart uses other temporal logic operators to track the amount of time you spend in each activity and determine when to send notifications to the app:
The exit actions in each subchart call the
elapsed
operator to determine how long the subchart was active. The chart communicates this value, along with other information such as your heart rate and your total number of steps, to theApp Interface
chart through the output structuredisplay
.The chart uses the
after
operator to determine when you sleep or walk for longer than five minutes, rest or exercise for longer than the threshold you specify in the app, or exercise at a high intensity (taking more than 4 steps a second) for longer than 15 minutes. In each of these cases, the chart sends aNotification
message. TheApp Interface
chart receives this message and causes a notification to appear in the main display of the app. Depending on the type of notification, the notification button changes color.
See Also
停止Simulation(Simulink)|after|duration|elapsed|hasChanged|coder.extrinsic
(MATLAB Coder)