File Exchange

image thumbnail

Run on Hardware

Examples showing how to run MATLAB code on Raspberry Pi Hardware.

98 Downloads

Updated 11 Sep 2018

View License

This package includes examples from image processing and audio signal processing domains that demonstrate the workflow of generating C code from a MATLAB algorithm and running it on hardware as a stand-alone application using MATLAB Coder. A MATLAB function, called runOnHardware, is provided for deploying a MATLAB algorithm for prototyping on a Raspberry Pi hardware board.

The Sobel Edge detection example includes the system objects for capturing images from a USB webcam and for displaying images on the Raspberry Pi desktop.

Here is a video showing you how you can try the edge detection example included in this entry:
https://www.youtube.com/watch?v=A22kMroW-W0

The Pitch shifting example demonstrates how you can deploy an audio processing application to a Raspberry Pi using MATLAB code generation capabilities.

This example shows how to generate code from a simple audio processing function in MATLAB for standalone deployment to a Raspberry Pi for real-time prototyping purposes. System Objects used for capture and playback of audio data from the Raspberry Pi are included in the example.

Here is a video showing you how you can try the audio processing example included in this entry:
https://www.youtube.com/watch?v=cwY45Ur0Teo

Comments and Ratings (21)

dgmcik

@Ashwini Patil: targetHardware support starts from R2018a release of MATLAB

hi,i am using R2017b version and i get the error for creating configuration object for hardware
boaed=targetHardware(r,'myApplication')
i received error
undefined function or variable targetHardware
can i help me solve this problem

Henry Murr

Henry Murr

The function automatically saves as a log file on the raspberry pi when it needs to be an elf file. Can anyone help me fix this problem?

problem of download

Jaeseon Lee

download is hanging!!

Murat Belge

@Afagh: The error message says 'unit8' not 'uint8'. You might have a typo somewhere.

Hello, I am using R2017b version and I get the following error:
runOnHardware(r,'myApplication')
??? Undefined function or variable 'unit8'.

Error in ==> myApplication Line: 11 Column: 13
Code generation failed: View Error Report
Error using codegen

Error in runOnHardware (line 52)
codegen('-config ',cfg,fcnName,'-report');

which does not make any sense because I use the uint8 right there in the command prompt and it works perfectly. Would anyone have an idea why this is happening? Thanks in advance for your help.

Jyot Buch

Murat Belge

For R2018a users: use "raspberrypi" object when calling runOnHardware function. That is:

>> r = raspberrypi;
>> runOnHardware(r,'myFunction')

Using "raspi" object results in runOnHardware throwing an error.

Murat Belge

@michele benetti: This example requires Embedded Coder license in addition to MATLAB Coder for R2018a and earlier. This dependency was stated in the overview section. I'll update the description accordingly. The error message you are seeing is due to the Embedded Coder requirement.

hi, I have installed the package but when I run
runOnHardware(r,'myApplication')
I receive as error:
Trial>> runOnHardware(r,'myApplication')
Error using coder.hardware

There are no hardware support packages installed that support Processor-in-the-Loop (PIL)
simulation.
The following support packages provide support for PIL simulation with MATLAB coder:

Embedded Coder Support Package for BeagleBone Black Hardware
Embedded Coder Support Package for Xilinx Zynq-7000 Platform
Embedded Coder Support Package for Altera SoC Platform
Embedded Coder Support Package for ARM Cortex-A Processors
Embedded Coder Support Package for ARM Cortex-M Processors
Embedded Coder Support Package for STMicroelectronics Discovery Boards

Error in runOnHardware (line 41)
hw = coder.hardware('Raspberry Pi');

Krishna

Hi, I am not sure why I am receiving this error.
##########################################
Error using coder.ceval (line 48)
The coder.ceval function is not supported in MATLAB.

Error in AudioCapture/open (line 57)
coder.ceval('MW_AudioOpen',...

Error in pitchShiftDeployment (line 17)
open(capture,4410,2)
##########################################

Why not turn on the camera

function bet2()
%#codegen
% Copyright 2017 The MathWorks, Inc.
persistent w
persistent d
if isempty(w)
w = matlab.raspi.webcam(0,[640,480]);
d = matlab.raspi.SDLVideoDisplay;
end

for k = 1:1000

img = snapshot(w);

Rmin=25;
Rmax=35;

[centersDark, radiiDark] = imfindcircles(img, [Rmin Rmax], ...
'ObjectPolarity','dark','sensitivity',0.93);

view([centersDark, radiiDark])


for i=1:length(radiiDark)
hold on
view([centersDark, radiiDark])
hold off
RGB = insertShape(img,'circle', [centersDark(i,1),centersDark(i,2),radiiDark(i)] ,'LineWidth',5);

displayImage(d,RGB);

end
end

release(w);
release(d);

Murat Belge

@Ola Olsson: The output of fprintf goes to stdout. In the runOnHardware function, I direct the output to a *.log file. For example, if your M-file is called myApplication.m and you compile and generate code out of it, the output of the application myApplication.elf goes to myApplication.log file. And by default the *.log file is generated in the same directory where you launch the application. So check the *.log file for output.

There is a way to send the output to a file of your choice. This is illustrated in raspberrypi_motion_sensor_camera.slx. There is a MATLAB function block in this Simulink demo where we save an image to disk. Take a look at that for details.

Ola Olsson

Really great example. Thanks for providing this! I am about to try to develop a little computer vision tool, and this is a very good start. Now, in that I also want to save some numbers to a text file on disk on the Raspberry Pi, using fprintf. However, it seems that either it does not do anything with it, or it puts the file in place where I am unable to find it. The code generation report is not complaining (as it was when I tried to fprintf an array instead of scalars), so there is no guidance there.

Ram

Add your comments on the example and the proposed workflow in the comment section and let us know if you would like to have a more detailed discussion.

Ram

Ram

Updates

2.0.3

Fixed a minor issue where URL encoded spaces are inserted into the directory names when installing manually instead of using the add-on explorer.

2.0.2

Minor updates.

2.0.1

Updated for new releases.

2.0.0.0

Updated dependency - Embedded Coder

2.0.0.0

Added a new video

2.0.0.0

New audio signal processing example added.

1.0.0.0

Added the video link.

1.0.0.0

Updated title and thumbnail image.

MATLAB Release Compatibility
Created with R2018a
Compatible with R2017a to any release
Platform Compatibility
Windows macOS Linux
Communities
More Files in the  MATLAB Maker Community

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor

Pitch_Shift_Example

Sobel_Edge_Detection