matlab添加arduino类库

matlab添加arduino类库
参照官网教程:create a helloworld addon
教程结果检验代码:
将自己写的类库导入,首先需要有个写好的传感器驱动库,以电机驱动模块L298N为例,具体步骤如下:

1.写好库文件,L298N.cpp和L298N.h将其放入文件夹L298n中.

2.找到matlab安装的arduino硬件扩展包源文件所在的位置,我的在:C:\MATLAB\SupportPackages\R2016a ,里边有个文件夹arduino-1.6.1(没错,这个就是arduino的官方编译器),打开它把,L298N文件夹放入其中的libraries文件夹中。

3.回到,在create a helloworld addon教程中创建好的“+arduinoioaddons”文件夹中,与之类似,创建“+L298NAddon”文件夹,包括:“src\L298N_.h”,L298N_.m。

L298N_.h代码如下:

#include "LibrayBase.h"
#include "L298N.h"

#define CREATE_L298N 0x01
#define DELETE_L298N 0x02
#define MOTOR_RUN_FORWARD   0x03//测试

class L298N_ :public LibraryBase 
{
public:

	//构造函数
	L298N_(MWArduinoClass& a) 
	{
		libName = "L298NAddon/L298N";
		a.regitsterLibrary(this);
	}

	//覆盖命令处理程序
	//为加载项在Arduino设备上每个命令创建一个switch case
	//在L298N_.m中用sendCommand来执行命令
	void commandHandler(byte cmdID, byte *inputs, unsigned int payload_size)
	{
		switch (cmdID)
		{
		case CREATE_L298N: 
		{	
			if (sizeof(inputs) / sizeof(inputs[0]) == 4) 
			{
				debugPrint("L298N::mysensor=new L298N(%d,%d,%d,%d) ");
				mysensor = new L298N(intputs[0], intputs[1], intputs[2], intputs[3]);
			}
			if (sizeof(inputs) / sizeof(inputs[0]) == 6) 
			{
				debugPrint("L298N::mysensor=new L298N(%d,%d,%d,%d,%d,%d) ");
				mysensor = new L298N(intputs[0], intputs[1], intputs[2], intputs[3], intputs[4], intputs[5]);
			}
			break;
		}
		case DELETE_L298N: 
		{
			delete mysensor;
			mysensor = NULL;
			break;
		}
		case MOTOR_RUN_FORWARD: 
		{
			mysensor->motorRun(1);
			sendResendResponseMsg(cmdID,"forward", 7);
			break;
		}
		default: 
		{
			//do nothing
		}
		}
		//命令ID必须与添加到matlab的附加库操作相匹配
		//debugPrint用于将其它消息从Arduino设备中传递到MATLAB命令行
};

L298N_.m代码如下:

classdef L298N_< arduinoio.LibraryBase
    %命令与L298N_.h中的命令对应
    properties(Access=private,Constant=true)
        CREATE_L298N=hex2dec('01')
        DELETE_L298N=hex2dec('02')
        MOTOR_RUN_FORWARD=hex2dec('03')
    end
    
    %配置
    properties(Access=protected,Constant=true)
        LibraryName = 'L298NAddon/L298N'
        DependentLibraries = {}
        ArduinoLibraryHeaderFiles = {'L298N/L298N.h'}%原始库文件的头文件
        CppHeaderFile = fullfile(arduinoio.FilePath(mfilename('fullpath')), 'src', 'L298N_.h')
        CppClassName = 'L298N_'
    end
    
  
    methods
        function obj = L298N_(parentObj,PIN)%构造函数,PIN为对应的引脚
            obj.Parent = parentObj;         %PIN=[...]
            obj.Pins = PIN;
        end 
        
    end
        
    %函数与voidCommandHandler中的case对应
    methods
        function createL298NSensor(obj)
            cmdID = obj.CREATE_L298N;
            data = getTerminalsFromPins(obj.Parent, obj.Pins);
            sendCommand(obj, obj.LibraryName, cmdID, data);
        end
        
        function deleteL298NSensor(obj)
            cmdID = obj.DELETE_L298N;
            sendCommand(obj, obj.LibraryName, cmdID, []);
        end
        
        function motor_run_forward(obj)        %case motor_run_forward
            cmdID = obj.MOTOR_RUN_FORWARD;
            sendCommand(obj, obj.LibraryName, cmdID, inputs);
        end
    end
    
end

注意以上代码是我根据自己写的L298N类库给出的文件代码,当你需要添加别库的时还需要具体问题,具体分析。在下水平有限,还是去参照官网教程为好!
在这里插入图片描述

posted @ 2018-12-14 15:16  秦星二  阅读(240)  评论(0编辑  收藏  举报