-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maestro.m
63 lines (52 loc) · 1.77 KB
/
Maestro.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
classdef Maestro < MaestroInterface
%MAESTRO Summary of this class goes here
% Detailed explanation goes here
properties
BaudIndicator = hex2dec('AA')
DeviceId = hex2dec('c')
end
methods (Static)
function res = lsb(x)
res = binvec2dec(bitget(x, 1:7));
end
function res = msb(x)
res = binvec2dec(bitget(x, 8:14));
end
end
methods (Hidden)
function sendCommand(obj, cmd)
cmd = cat(2, [obj.BaudIndicator, obj.DeviceId], cmd);
fwrite(obj.Port, cmd);
end
function sendAction(obj, action, chan, target)
assert(isnumeric(chan), 'Channel cannot be non-numeric');
assert(isnumeric(target), 'Target cannot be non-numeric');
assert(isnumeric(action), 'Action cannot be non-numeric');
cmd = [action, chan, obj.lsb(target), obj.msb(target);];
obj.sendCommand(cmd);
end
end
methods
function gobj = Maestro(port)
gobj.Port = serial(port);
set(gobj.Port, 'InputBufferSize', 2048);
set(gobj.Port, 'BaudRate', 9600);
set(gobj.Port, 'DataBits', 8);
set(gobj.Port, 'Parity', 'none');
set(gobj.Port, 'StopBits', 1);
fopen(gobj.Port);
end
function close(obj)
fclose(obj.Port);
end
function setTarget(obj, chan, target)
obj.sendAction(4, chan, target);
end
function setSpeed(obj, chan, target)
obj.sendAction(7, chan, target);
end
function setAccel(obj, chan, target)
obj.sendAction(9, chan, target);
end
end
end