Main Content

MATLAB入门Support Package for Raspberry Pi Hardware

This example shows you how to use the MATLAB® Support Package for Raspberry Pi™ Hardware to perform basic operations on the hardware such as executing shell commands, turning an on-board LED on or off and manipulating files.

Introduction

Raspberry P金宝appi硬件的MATLAB支持包使您可以从运行MATLAB的计算机远程与Raspberry Pi硬件进行通信。支持软件金宝app包包括一个MATLAB命令行接口,用于访问Raspberry Pi硬件的I/O外围设备和通信接口。使用此命令行界面,您可以从连接到Raspberry Pi硬件的传感器中收集数据,并在Raspberry Pi硬件上操纵设备。

In this example you learn how to create araspi从MATLAB内部连接到Raspberry Pi硬件。您检查了此对象的属性和方法,以了解基本外围设备的状态,例如数字I/O引脚(也称为GPIO),SPI,I2C和串行。使用此对象,您可以在Raspberry Pi硬件上执行Shell命令,并在Raspberry Pi硬件上操纵文件。

Prerequisites

  • 您必须完成Raspberry Pi硬件的固件更新,以便能够将MATLAB接口用于Raspberry Pi硬件。MATLAB通过连接到Raspberry Pi上运行的服务器来与Raspberry Pi硬件进行通信。该服务器内置在带有支持包装的固件中。金宝app要更新Raspberry Pi固件,请按照所述的步骤操作Hardware Setup

Required hardware

要运行此示例,您需要以下硬件:

  • Raspberry Pi hardware

  • 至少1A输出的电源

创建一个raspi object

创建一个raspiobject.

rpi = raspi();

RPI是RASPI对象的手柄。在创建RPI对象时,MATLAB连接到通过TCP/IP在Raspberry Pi硬件上运行的服务器。如果您在创建RASPI对象方面有任何问题,请参见诊断连接问题的故障排除指南。

The properties of the raspi object show information about your Raspberry Pi hardware and the status of some of the hardware peripherals available. Either the numeric IP address or the hostname of your Raspberry Pi hardware and the port used for TCP/IP communication are displayed in the DeviceAddress and Port properties. The raspi object detects the model and version number of your board and displays it in the BoardName property. The GPIO pin-outs and available peripherals change with the model and version of your Raspberry Pi hardware.

The AvailableLEDs property of the raspi object lists user controllable LEDs. You can turn a user LED on or off using the writeLED method.

AvailableDigitalPins, AvailableI2CBuses, and AvailableSPIChannels properties of the raspi object indicate the pins that you can use for digital I/O, I2C buses, and SPI channels that can be used to communicate with sensors and actuators supporting the I2C and SPI communication protocols. It is not an issue if nothing is listed for AvailableSPIChannels. The Raspbian Linux image shipped with MATLAB does not enable the SPI peripheral to provide you with more general purpose digital I/O pins. You can enable and disable I2C and SPI peripherals to suit your needs by loading and unloading Linux® kernel modules responsible for these peripherals.

Turn an LED on and off

There is a user LED on Raspberry Pi hardware that you can turn on and off. Execute the following command at the MATLAB prompt to turn the LED off and then turn it on again.

led = rpi.availableleds {1};写入(RPI,LED,0);写入(RPI,LED,1);

While executing the preceding commands, observe the 'ACT' (or 'OK') LED on the Raspberry Pi hardware and visually confirm the LED operation. If you are unsure where the user LED is located, execute the following command.

showLEDs(rpi);

You can make the LED blink in a loop with a period of 1 second.

为了i = 1:10 writeLED(rpi, led, 0); pause(0.5); writeLED(rpi, led, 1); pause(0.5);结尾

Execute system commands

RASPI对象具有许多方法,可让您从MATLAB内部执行Raspberry Pi硬件上的系统命令。您可以通过在Raspberry Pi硬件上执行系统命令来完成很多工作。在MATLAB®在线™中,系统功能受到限制。

Try taking a directory listing.

system(rpi,'ls -al /home /pi'

该语句执行Linux目录列表命令,并在MATLAB命令提示符处返回所得的文本输出。您可以将结果存储在MATLAB变量中以执行进一步的处理。确定谁是 /home /pi下的.profile文件的所有者。

输出=系统(RPI,'ls -al /home /pi');ret = regexp(output,'\s+[\w-]+\s+\d\s+(\w+)\s+.+\.profile\s+',,,,“令牌”);ret {1}

You can also achieve the same result using a single shell command.

system(rpi,'stat -format =“%u” /home/pi/.profile'

这次使用系统命令执行LED练习。默认情况下,用户LED配置为基于SD卡活动更改其状态。但是,您可以根据应用程序的要求重新配置LED以更改其状态。要手动控制LED,请设置systemcommand to没有任何

system(rpi,'回声“无” |sudo Tee/sys/class/led/led 0/触发');

The system commands with 0 and 1 are equivalent to the writeLED method with arguments 0 and 1 for the LED state.

system(rpi,'回声0 |sudo Tee/sys/class/led/led 0/亮度');system(rpi,'回声1 |sudo Tee/sys/class/led/led 0/亮度');

You can set the LED back to its original state of tracking the SD card activity by executing this command:

system(rpi,'echo“ mmc0” |sudo Tee/sys/class/led/led 0/触发');

You cannot execute interactive system commands using the system() method. To execute interactive commands on the Raspberry Pi hardware, you must open a terminal session.

OpenShell(RPI)

This command opens a PuTTY terminal. Log in with your user name and password. The default user name is 'pi' and the default password is 'raspberry'. After logging in, you can execute interactive shell commands like 'top'.

The openShell function is not supported in MATLAB Online. Access the command shell remotely via SSH with PuTTY, as described in远程访问

操纵文件

The raspi object provides the basic file manipulation capabilities. To transfer a file on Raspberry Pi hardware to your host computer you use the getFile() method.

getfile(rpi,'/usr/share/pixmaps/debian-logo.png');

然后,您可以在MATLAB中读取PNG文件:

img = imread('debian-logo.png');图像(IMG);

GetFile()方法采用可选的第二个参数,允许您定义文件目标。要将主机计算机上的文件传输到Raspberry Pi硬件,请使用PutFile()方法。MATLAB在线不支持此方法。金宝app

putfile(rpi,'debian-logo.png',,,,'/home/pi/debian-logo.png.copy');

Make sure that file is copied.

system(rpi,'ls -l/home/pi/debian-logo.png.copy'

您可以使用deletefile()命令在Raspberry Pi硬件上删除文件。

deleteFile(rpi,'/home/pi/debian-logo.png.copy');

Make sure that file is deleted.

system(rpi,'ls -l/home/pi/debian-logo.png.copy'

前面命令应导致错误,表明无法找到文件。

Summary

此示例介绍了使用MATLAB支持软件包用于Raspberry Pi硬件的工作流程。金宝app使用Raspberry Pi支持软件包,您将用金宝app户打开和关闭用户,执行系统命令以及Raspberry Pi硬件上的操纵文件。

也可以看看

Video series:Raspberry Pi支金宝app持MATLAB