matlab添加arduino类库
参照官网教程:create a helloworld addon
教程结果检验代码:
将自己写的类库导入,首先需要有个写好的传感器驱动库,以电机驱动模块L298N为例,具体步骤如下:
1.写好库文件,L298N.cpp和L298N.h将其放入文件夹L298n中.
2.找到matlab安装的arduino硬件扩展包源文件所在的位置,我的在:C:MATLABSupportPackagesR2016a ,里边有个文件夹arduino-1.6.1(没错,这个就是arduino的官方编译器),打开它把,L298N文件夹放入其中的libraries文件夹中。
3.回到,在create a helloworld addon教程中创建好的“+arduinoioaddons”文件夹中,与之类似,创建“+L298NAddon”文件夹,包括:“srcL298N_.h”,L298N_.m。
L298N_.h代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60#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代码如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47classdef 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类库给出的文件代码,当你需要添加别库的时还需要具体问题,具体分析。在下水平有限,还是去参照官网教程为好!
最后
以上就是优美帆布鞋最近收集整理的关于matlab添加arduino类库的全部内容,更多相关matlab添加arduino类库内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复