-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZaber.cpp
349 lines (292 loc) · 8.81 KB
/
Zaber.cpp
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright 2015 Eileen Mazzochette, et al <[email protected]>
*/
#include "StdAfx.h"
#include "Zaber.h"
#include "ImageControl.h"
#using <System.dll>
// Sets duration to wait for when polling for available bytes to read
#define SLEEP_DURATION_MS 1
// Some Binary message command numbers
#define CMD_HOME 1
#define CMD_LIMIT_ACTIVE 9
#define CMD_MOVE_ABSOLUTE 20
#define CMD_MOVE_RELATIVE 21
#define CMD_UNEXPECTED_POS 13
#define CMD_RETURN_CURRENT_POSITION 60
#define CMD_ERROR 255
#define CMD_RETURN_STATUS 54
#define STAGE_RESOLUTION 0.15625e-6
#define ACTUATOR_RESOLUTION 0.047625e-6
using namespace System;
using namespace System::IO::Ports;
using namespace System::Threading;
using namespace System::Text;
using namespace cv;
Zaber::Zaber()
{
// Set up serial port
port = gcnew SerialPort();
port->PortName = "COM1";
port->BaudRate = 9600;
port->DataBits = 8;
port->Parity = Parity::None;
port->StopBits = StopBits::One;
port->Handshake = Handshake::None;
moveUnit = MICROSTEP;
homePosition = 0;
hoverPosition = 0;
scanPosition = 0;
}
int Zaber::openPort(void)
{
if (!port->IsOpen) {
port->Open();
}
if (port->IsOpen) {
return 0;
} else {
return -1;
}
}
int Zaber::closePort(void)
{
if (port->IsOpen) {
port->Close();
}
if (!port->IsOpen) {
return 0;
} else {
return -1;
}
}
Movement determineStageMovement(Point target, Point cantilever)
{
Movement result;
result.x = -((double)(cantilever.x - target.x))/(PIXELS_PER_UM * IMAGE_RESIZE_SCALE);
result.y = ((double)(cantilever.y - target.y))/(PIXELS_PER_UM * IMAGE_RESIZE_SCALE);
return result;
}
void Zaber::moveStage(Movement move)
{
microstepsX = (unsigned int)convertToStageMicrosteps(move.x);
microstepsY = (unsigned int)convertToStageMicrosteps(move.y);
device = 1;
command = CMD_MOVE_RELATIVE;
data = microstepsX;
sendCommand();
device = 2;
command = CMD_MOVE_RELATIVE;
data = microstepsY;
sendCommand();
}
void Zaber::moveActuator(double distance)
{
microstepsZ = (unsigned int)convertToActuatorMicrosteps(distance);
device = 3;
command = CMD_MOVE_RELATIVE;
data = microstepsZ;
sendCommand();
waitForReply(3, CMD_MOVE_RELATIVE);
}
void Zaber::moveActuatorToPosition(double position)
{
device = 3;
command = CMD_MOVE_ABSOLUTE;
data = (unsigned int)convertToActuatorMicrosteps(position);
sendCommand();
waitForReply(3, CMD_MOVE_ABSOLUTE);
}
void Zaber::moveActuatorHome(void)
{
device = 3;
command = CMD_MOVE_ABSOLUTE;
data = 0;
sendCommand();
}
void Zaber::waitForStaticActuator(void)
{
while (true) { //wait for zaber to stabilize
int zaberStatus = requestZaberActuatorStatus();
if (zaberStatus == 0) break;
}
}
void Zaber::waitForStaticStage(void)
{
while (true) { //wait for zaber to stabilize
int zaberStatus = requestZaberStatus();
if (zaberStatus == 0) break;
}
}
int Zaber::requestZaberStatus(void)
{
device = 1;
command = CMD_RETURN_STATUS;
data = 0;
sendCommand();
waitForReply(1, CMD_RETURN_STATUS);
if (data !=0) {
return 1; //x-axis is moving
}
device = 2;
command = CMD_RETURN_STATUS;
data = 0;
sendCommand();
waitForReply(2, CMD_RETURN_STATUS);
if (data != 0) {
return 2; //y-axis is moving
}
return 0; //zaber is stable
}
int Zaber::requestZaberActuatorStatus(void)
{
device = 3;
command = CMD_RETURN_STATUS;
data = 0;
sendCommand();
waitForReply(device,command);
if (data !=0) {
return 1; //x-axis is moving
}
return 0;
}
Point2d Zaber::getStagePosition(void)
{
Point2d position; //placeholder
// Send command to request x position
device = 1;
command = CMD_RETURN_CURRENT_POSITION;
data = 0;
sendCommand();
//Wait for device to reply with x position
waitForReply(1, CMD_RETURN_CURRENT_POSITION);
position.x = convertFromStageMicrosteps(data);
//Send command to request y position.
device = 2;
command = CMD_RETURN_CURRENT_POSITION;
data = 0;
sendCommand();
//Wait for device to reply with y position
waitForReply(2, CMD_RETURN_CURRENT_POSITION);
position.y = convertFromStageMicrosteps(data);
return position;
}
double Zaber::getActuatorPosition(void)
{
double position;
// Send command to request z position
device = 3;
command = CMD_RETURN_CURRENT_POSITION;
data = 0;
sendCommand();
//Wait for device to reply with x position
waitForReply(3, CMD_RETURN_CURRENT_POSITION);
position = convertFromStageMicrosteps(data);
return position;
}
void Zaber::receiveReply(void)
{
while (port->BytesToRead < 6) {
Thread::Sleep(SLEEP_DURATION_MS);
}
array<Byte>^ packet = gcnew array<Byte>(6);
port->Read(packet, 0, 6);
device = packet[0];
command = packet[1];
data = packet[2] + (packet[3] << 8) + (packet[4] << 16) + (packet[5] << 24);
}
void Zaber::waitForReply(Byte expectDevice, Byte expectCommand)
{
do {
receiveReply();
} while ( ! (device == expectDevice
&& (command == expectCommand
|| command == CMD_HOME // Auto-home routine has been triggered
|| command == CMD_LIMIT_ACTIVE // Device has reached its travel limit
|| command == CMD_UNEXPECTED_POS // Device has recently stalled or displaced
|| command == CMD_ERROR) ) ); // Device is sending an error reply
}
void Zaber::waitForReplyEitherDevice(Byte expectDevice1, Byte expectCommand1, Byte expectDevice2, Byte expectCommand2)
{
do {
receiveReply();
} while ( !( (device == expectDevice1)
&& (command == expectCommand1 // Reply matches device and command 1
|| command == CMD_HOME // Auto-home routinehas been triggered
|| command == CMD_LIMIT_ACTIVE // Device has reached its travel limit
|| command == CMD_UNEXPECTED_POS // Device has recently stalled or displaced
|| command == CMD_ERROR)) // Device is sending an error reply
|| (device == expectDevice2 // OR reply matches device and command 2
&& (command == expectCommand2
|| command == CMD_HOME // Auto-home routine has been triggered
|| command == CMD_LIMIT_ACTIVE // Device has reached its travel limit
|| command == CMD_UNEXPECTED_POS // Device has recently stalled or displaced
|| command == CMD_ERROR)) ); // Device is sending an error reply
}
int Zaber::convertToStageMicrosteps(double distanceInMoveUnit)
{
int result;
if (moveUnit == CENTIMETER) {
result = (int)(distanceInMoveUnit * (1e-2) / STAGE_RESOLUTION);
} else if (moveUnit == MILLIMETER) {
result = (int)(distanceInMoveUnit * (1e-3) / STAGE_RESOLUTION);
} else if (moveUnit == MICROMETER) {
result = (int)(distanceInMoveUnit * (1e-6) / STAGE_RESOLUTION);
} else if (moveUnit == MICROSTEP) {
result = (int)(distanceInMoveUnit);
}
return result;
}
double Zaber::convertFromStageMicrosteps(int distanceInMicroSteps)
{
double result;
if (moveUnit == CENTIMETER) {
result = (double)distanceInMicroSteps * (1e2) * STAGE_RESOLUTION;
} else if (moveUnit == MILLIMETER) {
result = (double)distanceInMicroSteps * (1e3) * STAGE_RESOLUTION;
} else if (moveUnit == MICROMETER) {
result = (double)distanceInMicroSteps * (1e6) * STAGE_RESOLUTION;
} else if (moveUnit == MICROSTEP) {
result = (double)distanceInMicroSteps;
}
return result;
}
int Zaber::convertToActuatorMicrosteps(double distanceInMoveUnit)
{
int result;
if (moveUnit == CENTIMETER) {
result = (int)(distanceInMoveUnit * (1e-2) / ACTUATOR_RESOLUTION);
} else if (moveUnit == MILLIMETER) {
result = (int)(distanceInMoveUnit * (1e-3) / ACTUATOR_RESOLUTION);
} else if (moveUnit == MICROMETER) {
result = (int)(distanceInMoveUnit * (1e-6) / ACTUATOR_RESOLUTION);
} else if (moveUnit == MICROSTEP) {
result = (int)(distanceInMoveUnit);
}
return result;
}
double Zaber::convertFromActuatorMicrosteps(int distanceInMicroSteps)
{
double result;
if (moveUnit == CENTIMETER) {
result = (double)distanceInMicroSteps * (1e2) * ACTUATOR_RESOLUTION;
} else if (moveUnit == MILLIMETER) {
result = (double)distanceInMicroSteps * (1e3) * ACTUATOR_RESOLUTION;
} else if (moveUnit == MICROMETER) {
result = (double)distanceInMicroSteps * (1e6) * ACTUATOR_RESOLUTION;
} else if (moveUnit == MICROSTEP) {
result = (double)distanceInMicroSteps;
}
return result;
}
void Zaber::sendCommand(void)
{
array<Byte>^ packet = gcnew array<Byte>(6);
packet[0] = device;
packet[1] = command;
packet[2] = data & 0xFF;
packet[3] = (data >> 8) & 0xFF;
packet[4] = (data >> 16) & 0xFF;
packet[5] = (data >> 24) & 0xFF;
port->Write(packet, 0, 6);
}