Documentation

Sensor Application

This example shows you how to use Simulink® Support Package for Apple iOS Devices to design an algorithm and augment the same with a custom GUI.

Introduction

The raw output of the motion sensors are very noisy changing rapidly even when the device is resting on a stable surface. In this example, you will implement a simple low pass filter that smooths the output of Gyroscope and the filtered output is used to control a custom GUI.

The following sections will describe how the low pass filter is specified, run on the device, how the outputs can be analyzed and finally add a custom GUI that uses the output.

Prerequisites

We recommend completing Getting Started with Apple iOS Devices and Communicating with Apple iOS Devices examples.

Required Products

  • DSP System Toolbox™

Required Hardware

  • iPhone, iPod or iPad

  • USB cable to connect the device to your host computer

Task 1 - Filter design

1. Open the Filtering Sensor Data model.

2. In your Simulink model, click Simulation > Model Configuration Parameters to open Configuration Parameters dialog.

3. Select the Hardware Implementation pane and review the parameters on the page that opens.

4. Click Device options and ensure Device match your device setting.

5. In the model, check that the Simulation mode on the toolbar is set to External and the Stop time is set to 40.

6. In the model, click the Run button on the toolbar to run the model on your Apple iOS device.

7. Once the model finishes running, double click To plot the data... block in the model to plot the data.

Task 2 - Build a base app

1. Open the Sensor application model.

2. This model uses the same filter to smooth out the Gyroscope data and send the filtered data is sent to the app using ToApp block.

3. Double click ToApp block and observe the function name specified: setAngles. This function is called with a vector of three single precision values as input.

4. In your Simulink model, click Simulation > Model Configuration Parameters to open Configuration Parameters dialog.

5. Select the Hardware Implementation pane and review the parameters on the page that opens.

6. Click the Deploy to Hardware button on the toolbar. The model will now be deployed to the iOS device.

Task 3 - Update the generated app

1. Open the Xcode project for the application under ios_sensorapplication_ert_rtw/ios_sensorapplication.

2. In the Xcode IDE project window, open ios_sensorapplication > ios_sensorapplication > InfoViewController.mm.

3. You can now add a custom function setAngles as specified in the ToApp block in the model. For example, you can add the following lines to InfoViewController.mm to print the values in the debugger window:

-(void)setAngles:(float*)inAngles
{
   NSLog(@"X: %g, Y: %g, Z: %g", inAngles[0], inAngles[1], inAngles[2]);
}

4. In Xcode IDE, select Xcode > Product > Run to run the app on your device.

5. Move the device and notice the values printing in Xcode debugger window.

Task 4 - Add a rotating membrane

1. You can use the InfoViewController's setAngles method to rotate a figure based on the input values. For this you can use OpenGL to create an object and rotate an object.

2. For your convenience, there is an updated InfoViewController.mm provided with the support package. Along with the InfoViewController.mm, there are accompanying files: InfoViewController.h, Main_iPad.storyboard, Main_iPhone.storyboard. Copy these files to project directory. You can run the following commands from MATLAB command prompt:

copyfile(fullfile(fileparts(which('ios_sensorapplication')),'InfoViewController.h'), 'ios_sensorapplication_ert_rtw/ios_sensorapplication','f')
copyfile(fullfile(fileparts(which('ios_sensorapplication')),'InfoViewController.mm'), 'ios_sensorapplication_ert_rtw/ios_sensorapplication','f')
copyfile(fullfile(fileparts(which('ios_sensorapplication')),'Main_iPad.storyboard'), 'ios_sensorapplication_ert_rtw/ios_sensorapplication','f')
copyfile(fullfile(fileparts(which('ios_sensorapplication')),'Main_iPhone.storyboard'), 'ios_sensorapplication_ert_rtw/ios_sensorapplication','f')

3. Since this example is using OpenGL, update the Xcode project to add the following frameworks: GLKit.framework and OpenGLES.framework. In Xcode project window, select ios_sensorapplication. Select Build Phases. Under "Link Binary with Libraries", press "+" to add the GLKit.framework and OpenGLES.framework.

4. In Xcode IDE, select Xcode > Product > Run to run the app on your device.

5. Move the device and watch the L-shaped membrane rotating.

Other Things to Try

Replace Gyroscope with Accelerometer and repeat the same exercise. Also try rotating the membrane about all three axis.

Summary

This example showed you how to use Simulink Support Package for Apple iOS Devices to design an algorithm and augment the same with a custom GUI.

Was this topic helpful?