Main Content

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 Interfaceprovides 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 Interfacechart sets the value of the outputactivityto the enumerated valueActivity.Rest.TheHuman Simulatorchart responds by producing vital sign values that model a person at rest. TheFitness Trackerchart analyses these values and sets the output signalstatustoActivity.Rest.TheApp Interfacechart 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 Interfaceopens the app and initializes theHuman SimulatorandFitness Trackercharts. 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 Interfaceis configured to communicate with the MATLAB appsf_fitness_app

  • The app uses a property calledchartto 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 outputsactivityandintensity.最后,当您关闭应用程序时,UIFigureCloseRequestcallback sets the value of the chart outputstoptotrue

  • In the chart, the entry actions in theInterfaceWithAppstate run the appsf_fitness_appand 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 anotificationmessage, the substateMainDisplaycalls the helper functionupdateText.This function replaces the contents of the clock display with a customized notification. After five seconds, the substate calls the helper functionupdateClockto 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 theInterfaceWithAppstate first calls thecoder.extrinsicfunction to declare them as extrinsic MATLAB code. For more information, seeCall Extrinsic MATLAB Functions in Stateflow Charts

Simulate Vital Signs Based on Activity

TheHuman Simulatorchart models the vital signs of a human engaged in the activity you select in the app. The chart uses the output structurevitalsto 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 functiontransitionto ensure that these vital signs change gradually over time. To detect changes in activity or exercise intensity, the chart calls thehasChangedoperator. For more information, seeDetect Changes in Data and Expression Values

Determine Fitness Tracker Output

The chartFitness Trackermodels 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 Simulatorchart and transitions between these subcharts. To filter out signal noise, the chart uses thedurationoperator 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 Interfacechart 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 theelapsedoperator 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 Interfacechart through the output structuredisplay

  • The chart uses theafteroperator 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 aNotificationmessage. TheApp Interfacechart 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

(Simulink)|||||(MATLAB Coder)

Related Topics