-
Notifications
You must be signed in to change notification settings - Fork 4
/
MP285.m
113 lines (101 loc) · 4.03 KB
/
MP285.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
classdef MP285 < handle
% MP285 - create object to interface with Sutter Instrument Micromanipulator
properties
uSteps_per_um % [x y z] microsteps per micrometer
hPort
end
properties (Dependent = true)
status
velocity
end
methods
%% initiation
function obj = MP285(comport, uSteps_per_um)
obj.hPort = instrfind('Port', comport);
if ~isempty(obj.hPort) && any(strcmp(obj.hPort.Status, 'open'))
error(['Port ', comport, ' busy! Type instrfind(''Port'', ''', comport,''');' ])
%try fclose(obj.hPort); delete(obj.hPort), end
end
obj.hPort = serial(comport);
set(obj.hPort, 'BaudRate', 9600, 'Parity', 'none' , 'Terminator', {'CR', ''}, ...
'StopBits', 1, 'Timeout', 5, 'Name', 'MP285', 'ErrorFcn', @(varargin) disp('MP285 error!'));
fopen(obj.hPort);
if nargin < 2
uSteps_per_um = [25 25 25];
warning('defaulting to 25 usteps/um')
end
obj.uSteps_per_um = uSteps_per_um;
obj.getPos;
obj.velocity = 200;
end
% set properties
function set.velocity(obj, val)
vel = ['V' typecast(bitset(uint16(val),16,0),'uint8')];
obj.write(vel);
end
% basic control and communication
function out = flush(obj)
nBytes = get(obj.hPort, 'BytesAvailable');
if nBytes > 0
out = fread(obj.hPort, nBytes);
else
out = [];
end
end
function status = write(obj,in)
obj.flush();
fwrite(obj.hPort, [in 13]);
if nargout > 0, status = fread(obj.hPort,1); end
end
function status = moveAbs(obj,xyz)
if nargout > 0
status = obj.write(['m' obj.toChar(int32(xyz.*obj.uSteps_per_um))]);
else
obj.write(['m' obj.toChar(int32(xyz.*obj.uSteps_per_um))]);
end
end
function status = moveRel(obj,xyz) % in microns [x y z]'
[xyzOld] = obj.getPos;
% pause(0.05);
if nargout > 0
status = obj.moveAbs(xyz + xyzOld);
else
obj.moveAbs(xyz + xyzOld);
end
end
function out = get.status(obj) % in microns
obj.write('s');
out.flags = dec2binvec(fread(obj.hPort, 1, 'uint8'), 8);
out.udirXYZ = fread(obj.hPort, 3, 'uint8')';
out.roe_vari = fread(obj.hPort, 1, 'uint16');
out.uoffset = fread(obj.hPort, 1, 'uint16');
out.range = fread(obj.hPort, 1, 'uint16');
out.pulse = fread(obj.hPort, 1, 'uint16');
out.uspeed = fread(obj.hPort, 1, 'uint16');
out.indevice = fread(obj.hPort, 1, 'uint8');
out.flags_2 = dec2binvec(fread(obj.hPort, 1, 'uint8'), 8);
out.jumpspd = fread(obj.hPort, 1, 'uint16');
out.highspd = fread(obj.hPort, 1, 'uint16');
out.dead = fread(obj.hPort, 1, 'uint16');
out.watch_dog = fread(obj.hPort, 1, 'uint16');
out.step_div = fread(obj.hPort, 1, 'uint16');
out.step_mul = fread(obj.hPort, 1, 'uint16');
out.xspeed = fread(obj.hPort, 1, 'uint16');
out.version = fread(obj.hPort, 1, 'uint16');
obj.flush();
end
function [xyz, status] = getPos(obj) % in microns
obj.write('c');
xyz = fread(obj.hPort, 3, 'long')' ./ obj.uSteps_per_um;
if nargout > 1, status = fread(obj.hPort, 1);end
end
%
function delete(obj) % class destructor
if ~isempty(obj.hPort), fclose(obj.hPort); delete(obj.hPort); end
end
% Helper functions
function c = toChar(obj,in)
c = typecast(in, 'uint8');
end
end
end