From the series: Raspberry Pi Support from MATLAB
Meeshawn Marathe, MathWorks
MATLAB® Support Package for Raspberry Pi™ enables you to interactively control Raspberry Pi™ from your computer. Learn how to develop and prototype MATLAB® algorithms on Raspberry Pi™ using the support package. You'll also learn how to deploy MATLAB algorithms as standalone applications using MATLAB Coder™. The LED blink and file operation examples covered in the video guide you through the step-by-step workflow.
What is going on, everybody. My name is Meeshawn, and welcome to this video on how to deploy MATLAB algorithms to Raspberry Pi. For a while, we have been deploying algorithms from Simulink to Raspberry Pi. In this video, I will show you how to deploy your MATLAB code to the Raspberry Pi hardware. I will help you get started with a simple LED blink example. I will show you how to write MATLAB code to deploy it on Raspberry Pi. Later, I will show a more complex edge detection example.
Let's quickly take a look at the things you will require-- a Raspberry Pi board powered by a micro USB cable, and ethernet cable, a USB webcam, which is a V4L2-supported webcam, and of course, MATLAB. You can use any Raspberry Pi board supported by your MATLAB release. However, this example requires you to have MATLAB release 2018 and higher. For this video demonstration, I have used a Raspberry Pi 3 Model B board. The webcam will be used for the edge detection example discussed towards the end of the video. You will also need MATLAB Coder product, along with MATLAB for code generation.
All right, so now that we are aware of the prerequisites, let's get started in MATLAB. In order to work with Raspberry Pi in MATLAB, you would first need to install the MATLAB Support Package for Raspberry Pi. The Support Package enables you to communicate with the Raspberry Pi hardware remotely from a computer running MATLAB. It includes a MATLAB command line interface for accessing the hardware's peripherals and communication interfaces. To know more about the Support Package, refer to this documentation with the help of this command.
Let's begin by creating a raspi object to connect to the hardware from MATLAB. If this is the first time that you are connecting with the Raspberry Pi hardware, then you might be prompted to enter the IP address, username, and password, as well. Using this interface, you can collect data from sensors connected to the Raspberry Pi hardware through SBI and I2C buses, process them in MATLAB and actual devices attached to a Raspberry Pi hardware.
For example, you can set the on-board green LED on or off using the method writeLED. In short, the Support Package lets you interactively control the Raspberry Pi hardware from MATLAB. The Raspberry object also provides methods to execute Linux commands on the Raspberry Pi hardware.
All right, so now let's create a script using this command to blink the LED. In addition to this, let us perform some file operations as well on the Raspberry Pi hardware. For this purpose, let's create an empty text file. W means write permission here.
Next, let's write a for loop to blink the LED for, let's say, seven times. Let's implement a period of one second using the pause function. The pause function is Codegen compatible for Linux boards. At the end of the cycle, we will write the count of the blink operations to the file we just created. End of the loop. Now, after writing the data, let's close the file and save the script. Let's call it blinkNWrite since we are doing both LED blink and file write operations.
All right, so now let's take a look at a typical work flow to deploy algorithms to Raspberry Pi hardware. The first step in the workflow is to create a MATLAB function. We just created a MATLAB script a moment ago. And now we will be adding the keyword function at the top, followed by the name of the function.
In order to deploy an algorithm to the Raspberry Pi hardware, it is required that we implement the script as a MATLAB function. Currently, as of MATLAB 2019b, you can deploy MATLAB functions without any input/output arguments. All right, so our next step in the workflow is to execute this MATLAB function. Clicking on red button or pressing F5 will run the function in MATLAB and control the LED on-board Raspberry Pi based on the logic implemented in the function.
Right, so now we can see a file, sample.txt, has been created in the current directory. And as you can see here, the LED has started to blink. And it just completed doing so 7 times. Now, let's open this file. Blinking on-board LED for count is equal to one up to seven. Perfect. So our MATLAB function just toggled the LED and created a file in the current working directory of MATLAB. Let's delete this file for now.
So this is the connected I/O workflow, wherein MATLAB interacts and controls the peripherals of the Raspberry Pi hardware, which in this case is the on-board LED of the Raspberry Pi hardware. Next, we look at deploying this MATLAB function to the hardware. In this workflow, MATLAB coder will convert the MATLAB function into an equivalent C C++ code, followed by a build, load, and execute on the hardware. This executable will run independent of MATLAB, otherwise known as standalone execution.
We begin by adding the directive on pragma %#codegen after the function signature to indicate that we intend to deploy the MATLAB function on the hardware. Adding this directive instructs the MATLAB Code Analyzer to help you diagnose and fix violations that would result in possible errors during deployment. The Analyzer provides an indicator at the top right of the editor window. Green glow indicates that no issues were detected.
And as you can see here, green is displayed at the top for our function. And hence, we can proceed to the next step. Please note that not all function calls are supported for code generation. And hence, our next objective is to check for code generation support.
To fix the unsupported function call errors, you can either declare the function as coder.extrinsic, or replace the function with an equivalent function call that is supported for code generation. For more information on coder.extrinsic, search in the documentation by typing doc coder.extrinsic.
For a list of supported function calls, search in the documentation by typing doc language support code generation. For a list of Raspberry Pi peripheral functions submitted for deployment, search the documentation for doc functions supported for deployment. Luckily in our case, we do not have any unsupported function calls.
Next, we'll talk about two important functions which will help us deploy the MATLAB function as a standalone on the hardware, which is our final objective. We begin by creating a hardware configuration object using the function target hardware. For example, to create a configuration object for the Raspberry Pi hardware enter target object equals target hardware Raspberry Pi.
The object created helps one to configure various options for deployment, such as the hardware device address, username, and password. The default username and password are Pi and Raspberry, respectively. The other options include setting the Build Directory location on the hardware, build action preferences for deployment, such as a build, load, and run, or only build, and RunOnBoot option to automatically execute the function after the hardware is switched on. All these can be configured prior to deploying the MATLAB function to the hardware.
Now we are ready to deploy our MATLAB function to the Raspberry Pi hardware. And we will do so with the help of the function deploy. This function deploys the MATLAB function as a standalone executable on the hardware. It also generates a deployment report which contains the information about the deployment status.
In case of any errors generated during deployment, you can use the report to debug the function and verify that the function is suitable for deployment. Additionally, we must also take care that the MATLAB function to be deployed must not have any input or output arguments as of MATLAB 2019b release. Deploy, target object, blinkNWrite.
This function requires two arguments, namely the hardware configuration object and the name of the MATLAB function to be deployed, which is blinkNWrite in our case. Executing this command will deploy the MATLAB function on the Raspberry Pi hardware. Code generation is successful, and the code generation report is generated. As you can see here, the LED is blinking.
The file sample.txt will be returned in a Linux directory, as specified in the Build Directory of the target hardware configuration object. Note that the file sample.txt is now generated on Raspberry Pi, unlike the connected I/O workflow, where it got generated in the MATLAB Working Directory. We can now copy the file from the hardware into MATLAB by executing these commands-- r is equal to raspi, getfiler, Build Directory location.
As you can see here, the file pops up in the Working Directory. Let's open this file. Blinking on-board LED for count equal to one up to seven.
All righty. So far, we have developed a MATLAB function, executed it initially in MATLAB using the connected I/O workflow, and then successfully deployed it to the Raspberry Pi hardware. The code generated out of the MATLAB function ran on the hardware and manipulated the LED independent of MATLAB.
Now let's recap and understand the two workflows discussed so far, connected I/O and deployment. Connected I/O lets you interact with the hardware and its peripherals from MATLAB. You can collect data from sensors, process it in MATLAB, and control the actuators accordingly. Whereas in deployment, code is generated from the MATLAB function, loaded and executed on the hardware for a standalone execution.
The connected I/O workflow is better for debugging, takes less time to compile, and hence, is useful for rapid prototyping. However, due to its tethered mode of operation, a physical connection is always needed. This is to facilitate the on-demand request response being shared between MATLAB and Raspberry Pi in the connected I/O workflow. Hence, there is a turnaround time for every step of calculation.
On the other hand, deployment matches production workflows where the code has to run independent of MATLAB and the host computer used to design the project. The execution is in real time, as the deployed code doesn't have to wait for MATLAB for every step of calculation. But it does involve compilation and deploy, which is not very ideal for debugging and rapid prototyping.
All right, so that was a summary of the two workflows discussed so far. In the next video, we'll take a look at a more dedicated application, which is the edge prediction algorithm. For more interesting videos, please visit www.MathWorks.com/videos. Thank you.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .Select web site
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.