deploy matlab algorithms on raspberry pi | raspberry pi support from matlab, part 2 video -凯发k8网页登录
from the series:
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.
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 mathworks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- (español)
- (english)
- (english)
欧洲
- (english)
- (english)
- (deutsch)
- (español)
- (english)
- (français)
- (english)
- (italiano)
- (english)
- (english)
- (english)
- (deutsch)
- (english)
- (english)
- switzerland
- (english)
亚太
- (english)
- (english)
- (english)
- 中国
- (日本語)
- (한국어)