Main Content

Send Inputs to MATLAB Function from Command Line of Raspberry Pi

This example shows you how to deploy a MATLAB® function that accepts inputs from command line of Raspberry Pi™ using MATLAB Support Package for Raspberry Pi Hardware. In this example, a MATLAB function for adding two numbers is used for deployment.

When you generate code for adding numbers the Raspberry Pi support package builds the executable on the hardware. You can then provide inputs to the executable as command line arguments. The executable adds the numbers and displays the output on the Raspberry Pi Linux® terminal.

Note : This example is applicable to the installed MATLAB Support Package for Raspberry Pi Hardware.

Required Hardware

  • Raspberry Pi hardware

  • USB cable

  • Ethernet cable

  • A monitor connected to the Raspberry Pi hardware and a HDMI cable (optional)

Prerequisite

Configure the Raspberry Pi network using the Hardware Setup screen.

Step 1: Connect the Raspberry Pi Hardware

Tip: Before you start this example, we recommend you to complete the Getting Started with MATLAB Support Package for Raspberry Pi Hardware example.

Connect the micro end of the USB cable to the Raspberry Pi and the regular end of the USB cable to the computer. Wait until the PWR LED on the hardware starts blinking.

In the MATLAB Command Window, create a connection to the Raspberry Pi hardware.

r = raspi;

Step 2: Open the Sum Function

In this example, the simpleSum function implements the logic for adding two numbers.

For more information on the function, enter this command in the MATLAB Command Window.

type simpleSum
function simpleSum(a,b)
%#codegen
assert(all(size(a) ==1));
assert(isa(a,'double'));
assert(all(size(b) ==1));
assert(isa(b,'double'));
c = a + b;
fprintf('%f + %f = %f\n',a,b,c);
end

You can modify the function by using the edit command.

edit simpleSum

Step 3: Generate C Code for Addition on Raspberry Pi

  1. Create a hardware configuration object by using the targetHardware function in the MATLAB Command Window.

board = targetHardware('Raspberry Pi');

Verify the DeviceAddress, Username, and Password properties listed in the output. If required, change the value of the properties by using the dot notation syntax. For example, to change the device address to 172.18.185.123, enter,

board.DeviceAddress = '172.18.185.123'

Set the CoderConfig.GenCodeOnly property of the board object to true. This enables the support package to generate and run the sum function code only on the host computer.

board.CoderConfig.GenCodeOnly = true
board = 
targetHardware with properties:

               Name: 'Raspberry Pi'
      DeviceAddress: '172.18.182.234'
           Username: 'pi'
           Password: '*********'
           BuildDir: '/home/pi'
    EnableRunOnBoot: 0
        BuildAction: 'Build, load, and run'
        CoderConfig: [1×1 coder.CodeConfig]

Deploy the sum function on the hardware by using the deploy function. The deploy function initiates code generation of the simpleSum function. Once code generation is completed, MATLAB generates a code generation report. Use this report to debug the function for any errors and warnings in the generated code.

After successfully generating the code, update the main.c file to accept inputs from command line.

deploy(board,'simpleSum')
Code generation successful: View report

Step 4: Edit C Main File to Accept Input from Command Line of Raspberry Pi

This section explains how to modify the main.c file to accept inputs from command line of Raspberry Pi. A premodified main.c file is available for you to use. You can choose to use the premodified file or to update the file manually by using the steps in this section.

To use the premodified main.c file, enter these commands in the MATLAB Command Window.

copyfile('main.c',[pwd,'\codegen\exe\simpleSum'])
copyfile([pwd,'\codegen\exe\simpleSum\examples\main.h'],[pwd,'\codegen\exe\simpleSum'])

To update the file manually, copy the generated main.c and main.c files to one level higher than the current level in the file hierarchy. Open the main.c file and update the file by adding code at the points labeled in this code.

Step 5: Deploy the Function as a Standalone Executable on the Raspberry Pi

To generate and deploy the code on the hardware, set the CoderConfig.GenCodeOnly property of the board object to false and then use the deploy function. Since the deploy function will overwrite the main.c file that you edited in the previous section, disable the creation of the file before deployment.

board.CoderConfig.CustomSource = [pwd,'\codegen\exe\simpleSum\main.c'];
board.CoderConfig.GenerateExampleMain = 'DoNotGenerate';
board.CoderConfig.GenCodeOnly = false
board = 
targetHardware with properties:

               Name: 'Raspberry Pi'
      DeviceAddress: '172.18.182.234'
           Username: 'pi'
           Password: '*********'
           BuildDir: '/home/pi'
    EnableRunOnBoot: 0
        BuildAction: 'Build, load, and run'
        CoderConfig: [1×1 coder.CodeConfig]

deploy(board,'simpleSum')
Location of the generated elf : /home/pi/MATLAB_ws/R2020a/L/Work/raspi_example/Examples/raspberrypiio-ex15468149
Code generation successful: View report

The deploy function initiates code generation of the simpleSum function.

After successfully generating the code, the support package loads the sum function as a standalone executable on the hardware. The location of the executable is displayed in the MATLAB Command Window. Note the location.

Step 7: Run the Executable Program to Add Numbers

To open an SSH terminal with Raspberry Pi, use this command in the MATLAB Command Window. In the terminal, log in by entering the user name and password for the Raspberry Pi.

openShell(r)

Change the current directory to the directory where the executable was saved on the Raspberry Pi. This is the location that you noted in the previous step.

cd /home/pi/MATLAB_ws/R2020a/L/Work/raspi_example/Examples/raspberrypiio-ex15468149-20200123101332

Run the executable on the Raspberry Pi by providing inputs to the executable.

./simpleSum.elf 4 7

The executable adds the numbers and displays the output on the Linux terminal of the RaspberryPi.

See Also