Documentation

Working with Android™ Devices Block Library

This example shows how to use blocks from the Simulink Support Package for Android Devices to create a Simulink® model and run it on an Android device.

Introduction

Simulink Support Package for Android Devices enables you to create and run Simulink models on Android devices. 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, GUI blocks like the Slider, sensor blocks like the Gyroscope, and communication blocks like the UDP Send and Receive.

Prerequisites

We recommend completing Getting Started with Android Devices and Communicating with Android Devices Block Library examples.

Required Hardware

  • Android device, configured using targetupdater

  • USB cable to connect the device to 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 Android device to your host computer.

2. Turn on the device and ensure USB debugging is enabled. If not, run targetupdater to setup 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, Audio Playback and Slider.

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, 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, double-click on the Slider block. Note that the Minimum, Initial value and Maximum are set to 0, 1 and 2.

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

8. As the model is running on the device, you will hear audio from the speakers/headphones connected to the device. When you slide the slider, you will hear the audio volume change.

9. 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 Light 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 data 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 Light 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 parameters 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 data 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 Android device. Similarly, you can use UDP Send blocks to send data from your MATLAB host to your Android 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 Android 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 Light 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 Android Devices.

Was this topic helpful?