主要内容

创建Helloworld附加组件

此示例显示了如何从arduino发送字符串®UNO板到MATLAB®command line and create all the necessary files in your custom library using MATLAB and C++. Files for this example are located in your Arduino support package installation folder in\ toolbox \ matlab \ hard金宝appware \ supportpackages \ arduinoio \ arduinoioeexamples

创造Folder Structure

创造a folder package to contain all the files for your custom library, and add it to the MATLAB path. For this example:

  1. 添加一个名称的文件夹+arduinoioaddonsin your working folder.

  2. +arduinoioaddons, add a+ExampleAddon子文件夹包含您的MATLAB类文件。例如C:\工作

  3. 在里面+ExampleAddonsubfolder, add asrc文件夹包含您的C++header files.

创建C ++代码

For this example, create a C++ header file namedHelloworld.H, and save it in the+arduinoioaddons/+ExampleAddon/srcfolder. This file wraps methods to expose to the Arduino library.

  1. 在clude header files, includingLibraryBase.Hand any other third-party header file that the add-on library depends on.

    #include“ librarybase.h”
  2. 创建一个从LibraryBaseclass, which defines all the necessary interfaces.

  3. 在构造函数中,定义了library name, and register the library to the server.

    class HelloWorld : public LibraryBase { public: HelloWorld(MWArduinoClass& a) { libName = "ExampleAddon/HelloWorld"; a.registerLibrary(this); }

    这custom class and library names must have this format:

    shield(vendor)/device(library)
  4. 确定从MATLAB发出的命令调用。

  5. Override the command handler, and create a switch case for each command that the add-on executes on the Arduino device:

    public: void commandHandler(byte cmdID, byte* inputs, unsigned int payload_size) { switch (cmdID){ case 0x01:{ byte val [13] = "Hello World"; sendResponseMsg(cmdID, val, 13); break; } default:{ // Do nothing } } } };

    这command IDs must match up with the operations that you add to the MATLAB add-on library. For more information, seeCommand Handler

  6. (Optional) UseDebugprint将其他消息从Arduino设备传递到MATLAB命令行。

创造MATLABWrapper

这MATLAB add-on wrapper class that defines your library must inherit frommatlabshared.addon.LibraryBase。这matlabshared.addon.LibraryBase类定义了您必须在MATLAB类中覆盖的几个常数属性。该类还包含内部实用程序功能,使您可以从Arduino板上运行的服务器发送和检索数据。

  1. 创建一个MATLAB类,并为每个命令的命令ID定义发送到板上服务器的命令ID。

    classdefHelloWorld < matlabshared.addon.LibraryBase特性(Access = private, Constant = true) READ_COMMAND = hex2dec('01')end。。。end
  2. 覆盖类中的常数属性,以指定源头文件的位置。

    classdefHelloWorld < matlabshared.addon.LibraryBase。。。特性(Access = protected, Constant = true) LibraryName ='ExampleAddon/HelloWorld'DependentLibraries = {} LibraryHeaderFiles = {} CppHeaderFile = fullfile(arduinoio.FilePath(mfilename('fullpath')),'src','helloworld.h') CppClassName ='HelloWorld'end。。。end

    Define the class constructor, set the methods to call the class constructor, and set the parent property.

    classdefHelloWorld < matlabshared.addon.LibraryBase。。。方法功能obj = helloworld(parentobj)obj.parent = parentobj;end。。。endend

    始终将第一个输入参数分配给obj.Parent

    这support package auto-detects the class only if you have redefined all the properties. If you do not need a value, leave the field empty.

  3. Define the method to read the data back.

    classdefHelloWorld < matlabshared.addon.LibraryBase。。。方法。。。功能out = read(obj) cmdID = obj.READ_COMMAND; inputs = []; output = sendCommand(obj, obj.LibraryName, cmdID, inputs); out = char(output');endendend

有关使用MATLAB编程语言的帮助,请参阅

Register Add-On

要注册您的附加库,请添加包含的工作文件夹+arduinoioaddonsto the MATLAB path:

addpathC:\工作

Once you add the folder to the path, you should see the files listed in the MATLAB Current Folder browser.

确保Exippleaddon/HelloWorldlibrary is available.

ListArduinolibrories
listArduinolibraries ans ='adafruit/motorshieldv2''i2c'spi'servo'servo''expleaddon/helloworld'

Tip

If you do not see your add-on library in the list, seeCustom Arduino Library Issuesfor more information.

RunMATLABCode

此示例显示了如何从Arduino库返回数据commandHandler到MATLAB命令行。

创造anArduinoobject and include the new library. SetForceBuildOnto真的to reprogram the board.

ArduinoObj = arduino('COM3', 'Uno', 'Libraries', 'ExampleAddon/HelloWorld', 'ForceBuildOn', true);

Arduino devices reuse cached code if the specified library matches a library name in the source code. Reprogramming forces the device to newly download the header file, ensuring current and accurate information.

使用Exippleaddon图书馆。

dev = addon(arduinoObj,'ExampleAddon/HelloWorld');

在服务器上执行命令,然后将数据读回MATLAB。

read (dev)
ans = Hello World