Documentation

Working with Apple iOS Block Library

This example shows how to use blocks from the Simulink® Support Package for Apple iOS Devices to create a Simulink model and run it on an iPhone, iPod or an iPad.

Introduction

Simulink Support Package for Apple iOS Devices enables you to create and run Simulink models on the iPhone, iPod and iPad. The support package includes a library of Simulink blocks for using device sensors and network interfaces.

In this example, you will learn how to create and run Simulink models that use Audio blocks, sensor blocks like the Gyroscope, and communication blocks like the UDP Send and Receive.

Prerequisites

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

Required Hardware

  • iPhone, iPod or iPad

  • USB cable to connect the device to your host computer

  • Headphones with microphone

Task 1 - Setting up the device

In this task, you will ensure that you are able to communicate with your device.

1. Connect your Apple iOS device to your host computer.

2. Turn on the device and ensure that the device is provisioned. If not, run targetupdater to provision the device.

3. Connect the headphone with microphone to the audio jack on the device.

Task 2 - Audio

In this task, you will learn to use Audio Capture and Audio Playback.

1. Open the Audio Usage Example 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. In the model, double-click on the Audio Capture block. Note that the Audio sampling frequency is set to 44100 Hz and the Frame size is set to 4410. With these settings, With these settings, the Audio Capture block will process 4410 samples of audio data for every 0.1 second(4410/44100(Hz)).

5. In the model, double-click on the Audio Playback block. Note that the Audio sampling frequency is set to 44100 Hz to match the setting in Audio Capture block.

6. In the model, click the Deploy to Hardware button on the toolbar.

7. As the model is running on the device, you will hear audio from the speakers/headphones connected to the device.

8. Other Things to Try: You can change the Audio Sampling frequency to other values and deploy the model to your device.

Task 3 - Play audio file

In this task, you will learn to use Audio File Read.

1. Open the Audio File Read Example model.

2. In the model, note that the Audio File Read block reads a mono audio file with 44100 Hz sampling frequency. Double-click on the Audio File Read block. Note that Frame size is set to 4410. With these settings, the Audio File Read block will process 4410 samples of audio data for every 0.1 second(4410/44100(Hz)).

3. In the model, double-click on the Audio Playback block. Note that the Audio sampling frequency is set to 44100 Hz to match the sampling frequency of the audio file.

4. In the model, note that Matrix Concatenate is used to construct stereo audio data from the mono audio data.

5. In the model, click the Deploy to Hardware button on the toolbar.

6. As the model is running on the device, you will hear the music.

7. Other Things to Try: Select another music file to play.

Task 4 - Sensor blocks with UDP

In this task, you will learn to use sensor blocks and communication blocks.

1. Open the Sensor and UDP Usage Example 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. In the model, double click on the Gyroscope block and observe the Sample time parameter is set to 0.1. Double click on the Location Sensor block and observe the same. This ensures that both run at the same rate.

5. In the model, double click on the UDP Send blocks and observe that the Remote IP address is set to broadcast. Also, observe that the two blocks send to two different ports: 25000 and 25001.

6. In the model, click the Deploy to Hardware button on the toolbar.

7. As the model is running on the device, the Gyroscope data and Location sensor data are sent over UDP.

Task 5 - UDP Receive block

In this task, you will learn to use the UDP Receive block.

1. Open the UDP Receive Usage Example model.

2. In the model, double click on the UDP Receive blocks and observe that the Data type and Data size are set to match the input to the UDP Send blocks in the Sensor and UDP Usage Example model. Also observe that the two blocks send to two different ports: 25000 and 25001.

3. During simulation, UDP Receive blocks can be used to receive data on your MATLAB host from your Apple iOS device. Similarly, you can use UDP Send blocks to send data from your MATLAB host to your Apple iOS device.

4. In the model, make sure that the Simulation mode on the toolbar is set to Normal.

5. In the model, click the Run button on the toolbar.

6. As the model simulates, your MATLAB host receives data sent by your Apple iOS device. Observe the scope and display update.

Other Things to Try

Replace Gyroscope with Accelerometer and repeat the same exercise.

Change the Sample times of the source blocks: Gyroscope and Location sensor to different values, and observe the different rates in model.

Summary

This example showed how to use various blocks in the Simulink Support Package for Apple iOS Devices.

Was this topic helpful?