Main Content

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 example.

Required Hardware

  • Android device, configured using androidhwsetup

  • USB cable to connect the device to host computer

  • Headphones with microphone

Task 1 - Set Up Android 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 androidhwsetup 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 androidAudioUsageExample Simulink model.

2. In your Simulink model, on the Modeling tab, select Model Settings to open the Configuration Parameters dialog box.

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

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

7. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

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 androidAudioFileReadExample Simulink model.

2. In the model, note that the Audio File Read block reads a mono audio file with 44100 Hz sampling frequency. Double-click 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 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. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

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 androidSensorAndUDPUsageExample Simulink 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 the Gyroscope block and observe the Sample time parameter is set to 0.1. Double-click the Light Sensor block and observe the same. This ensures that both run at the same rate.

5. In the model, double-click 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. On the Hardware tab of the Simulink model, in the Mode section, select Run on board and then click Monitor & Tune.

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 androidUDPReceiveUsageExample Simulink model.

2. In the model, double-click 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, on the Simulation tab, ensure that the Simulation mode on the toolbar is set to Normal.

5. In the model, on the Simulation tab, click Run.

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.