-
Notifications
You must be signed in to change notification settings - Fork 1
/
hexapod_pi_connect.m
147 lines (115 loc) · 5.08 KB
/
hexapod_pi_connect.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function hexapod_pi_connect()
global STL;
if ~strcmp(STL.motors.special, 'hex_pi')
return;
end
%% Loading the PI_MATLAB_Driver_GCS2
if (strfind(evalc('ver'), 'Windows XP'))
if (~exist('C:\Documents and Settings\All Users\PI\PI_MATLAB_Driver_GCS2','dir'))
error('The PI_MATLAB_Driver_GCS2 was not found on your system. Probably it is not installed. Please run PI_MATLAB_Driver_GCS2_Setup.exe to install the driver.');
else
addpath('C:\Documents and Settings\All Users\PI\PI_MATLAB_Driver_GCS2');
end
elseif (strfind(evalc('ver'), 'Windows'))
if (~exist('C:\Users\Public\PI\PI_MATLAB_Driver_GCS2','dir'))
error('The PI_MATLAB_Driver_GCS2 was not found on your system. Probably it is not installed. Please run PI_MATLAB_Driver_GCS2_Setup.exe to install the driver.');
else
addpath('C:\Users\Public\PI\PI_MATLAB_Driver_GCS2');
end
end
if~isfield(STL, 'motors') | ~isfield(STL.motors, 'hex') | ~isfield(STL.motors.hex, 'Controller')
STL.motors.hex.Controller = PI_GCS_Controller();
end;
if(~isa(STL.motors.hex.Controller, 'PI_GCS_Controller'))
STL.motors.hex.Controller = PI_GCS_Controller();
end
%% Connecting to the C887
devicesTcpIp = STL.motors.hex.Controller.EnumerateTCPIPDevices()
nPI = length(devicesTcpIp);
if nPI ~= 1
error('%d PI controllers were found on the network. Choose one.');
end
disp(devicesTcpIp);
% Parameters
% You MUST EDIT AND ACITVATE the parameters to make your system run properly:
% 1. Activate the connection type
% 2. Set the connection settings
% Connection settings
STL.motors.hex.use_RS232_Connection = false;
STL.motors.hex.use_TCPIP_Connection = true;
if (STL.motors.hex.use_RS232_Connection)
STL.motors.hex.comPort = 1; % Look at the device manager to get the right COM port.
STL.motors.hex.baudRate = 115200; % Look at the manual to get the right baud rate for your controller.
end
if (STL.motors.hex.use_TCPIP_Connection)
%devicesTcpIp = Controller.EnumerateTCPIPDevices('')
STL.motors.hex.ip = STL.motors.hex.ip_address; % Use "devicesTcpIp = Controller.EnumerateTCPIPDevices('')" to get all PI controller available on the network.
STL.motors.hex.port = 50000; % Is 50000 for almost all PI controllers
end
%if ~isfield(STL.motors.hex, 'connected') | ~STL.motors.hex.connected
%try
% hexapod_pi_disconnect();
%catch ME
%end
if (isfield(STL.motors.hex, 'C887')) & STL.motors.hex.C887.IsConnected
STL.motors.hex.connected = true;
end
if (~STL.motors.hex.connected)
if (STL.motors.hex.use_RS232_Connection)
STL.motors.hex.C887 = STL.motors.hex.Controller.ConnectRS232(STL.motors.hex.comPort, STL.motors.hex.baudRate);
end
if (STL.motors.hex.use_TCPIP_Connection)
STL.motors.hex.C887 = STL.motors.hex.Controller.ConnectTCPIP(STL.motors.hex.ip, STL.motors.hex.port);
end
end
%% Configuration and referencing
% Query controller identification
STL.motors.hex.C887.qIDN()
% Query controller axes
availableaxes = STL.motors.hex.C887.qSAI_ALL();
if(isempty(availableaxes))
error('No axes available');
end
all_axes = 'X Y Z U V W';
% Reference stage
fprintf('Referencing hexapod axes... ');
if any(STL.motors.hex.C887.qFRF(all_axes) == 0)
STL.motors.hex.C887.FRF(all_axes);
end
while any(STL.motors.hex.C887.qFRF(all_axes) == 0)
pause(0.1);
end
% This is required to get the range?!?
STL.motors.hex.C887.CCL(1, 'advanced');
STL.motors.hex.C887.KEN('zero');
STL.motors.hex.range = [STL.motors.hex.C887.qTMN(all_axes) STL.motors.hex.C887.qTMX(all_axes)];
STL.motors.hex.C887.KLD('level', all_axes, STL.motors.hex.leveling);
STL.motors.hex.C887.KEN('level');
STL.motors.hex.C887.KEN('PI_Base');
STL.motors.hex.C887.CCL(0, 'advanced');
fprintf('done.\n');
% Looks like everything is in order:
STL.motors.hex.connected = true;
hexapos = hexapod_get_position_um();
if any(abs(hexapos(1:3)) > 1)
%set(handles.messages, 'String', 'Hexapod position is [%s ], not [ 0 0 0 ].');
mompos = move('mom');
%error('Ben just added this code. Test it first!');
% mompos(3): > is lower, hexapos(3): > is higher
mompos(3) = mompos(3) + hexapos(3);
if hexapos(3) < 0
% Move the hexapod up AFTER the lens makes room
move('mom', mompos);
move('hex', [0 0 0], 5);
else
% Move the hexapod down BEFORE the lens follows
move('hex', [0 0 0], 5);
move('mom', mompos);
end
else
%set(handles.messages, 'String', '');
end
STL.motors.hex.C887.VLS(5);
hexapod_set_leveling(STL.motors.hex.leveling);
hexapod_reset_to_zero_rotation();
end