-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSingleAxisStage.cpp
359 lines (285 loc) · 10.9 KB
/
SingleAxisStage.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
350
351
352
353
354
355
356
357
358
359
// Thorlabs Kinesis device adapter for Micro-Manager
// Author: Mark A. Tsuchida
//
// Copyright 2019-2020 The Board of Regents of the University of Wisconsin
// System
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#define NOMINMAX
#include "SingleAxisStage.h"
#include "Connections.h"
#include "DeviceEnumeration.h"
#include "Errors.h"
#include <algorithm>
#include <cmath>
#include <limits>
namespace {
char const* const PROP_StageType = "StageType";
char const* const PROPVAL_StageTypeLinear = "Linear";
char const* const PROPVAL_StageTypeRotational = "Rotational";
char const* const PROP_DeviceUnitsPerMillimeter = "DeviceUnitsPerMillimeter";
char const* const PROP_DeviceUnitsPerRevolution = "DeviceUnitsPerRevolution";
}
namespace {
// TODO C++17 will have std::clamp()
template <typename T>
inline T clamp(T const& value, T const& lower, T const& upper) {
return std::max(lower, std::min(upper, value));
}
template <typename T>
inline int clamp_int(T const& value) {
T const lower{ std::numeric_limits<int>::min() };
T const upper{ std::numeric_limits<int>::max() };
T ret = clamp(value, lower, upper);
return static_cast<int>(ret);
}
}
SingleAxisStage::SingleAxisStage(std::string const& name,
std::string const& serialNo, short channel,
std::shared_ptr<KinesisDeviceConnection> connection) :
serialNo_{ serialNo },
channel_{ channel },
givenName_{ name },
retainedConnection_{ connection }
{
for (auto const& item : KinesisErrorCodes()) {
SetErrorText(ERR_OFFSET + item.first, item.second.c_str());
}
// In general, the user must tell us whether the stage is linear or
// rotational. (As far as I can tell, *_GetMotorTravelMode() doesn't work
// as expected.)
switch (TypeIDOfSerialNo(serialNo)) {
case TypeIDCageRotator:
isRotational_ = true;
break;
}
CreateStringProperty(PROP_StageType,
isRotational_ ? PROPVAL_StageTypeRotational : PROPVAL_StageTypeLinear,
false, nullptr, true);
AddAllowedValue(PROP_StageType, PROPVAL_StageTypeLinear);
AddAllowedValue(PROP_StageType, PROPVAL_StageTypeRotational);
// In general, the user must tell us how to convert from physical to device
// units. (As far as I can tell, there is no API to query the actuator lead
// screw pitch, or even the device units per motor revolution (device units
// are not equal to "steps"). And *_GetRealValueFromDeviceUnit() and
// *_GetDeviceUnitFromRealValue() seem to always return an error.)
// The defaults below are set to small values to prevent accidents. Known
// defaults (taken from the Kinesis app) are given for integrated devices.
double defaultDeviceUnitsPerMm = 1.0;
switch (TypeIDOfSerialNo(serialNo)) {
case TypeIDLabJack050: defaultDeviceUnitsPerMm = 1228800.0; break;
case TypeIDLabJack490: defaultDeviceUnitsPerMm = 134737.0; break;
case TypeIDLongTravelStage: defaultDeviceUnitsPerMm = 409600.0; break;
case TypeIDVerticalStage: defaultDeviceUnitsPerMm = 25050.0; break;
}
CreateFloatProperty(PROP_DeviceUnitsPerMillimeter,
defaultDeviceUnitsPerMm, false, nullptr, true);
double defaultDeviceUnitsPerRevolution = 360.0;
switch (TypeIDOfSerialNo(serialNo)) {
case TypeIDCageRotator:
defaultDeviceUnitsPerRevolution = 49152000.0;
break;
}
CreateFloatProperty(PROP_DeviceUnitsPerRevolution,
defaultDeviceUnitsPerRevolution, false, nullptr, true);
}
SingleAxisStage::~SingleAxisStage() = default;
int
SingleAxisStage::Initialize() {
auto motorDrive = Connect();
if (!motorDrive) // Shouldn't happen
return DEVICE_ERR;
if (!motorDrive->GetConnection()->IsValid()) {
return ERR_OFFSET + motorDrive->GetConnection()->ConnectionError();
}
motorDrive_ = std::move(motorDrive);
short err;
// For what it's worth (doesn't seem to change anything)
err = motorDrive_->RequestSettings();
if (err)
return ERR_OFFSET + err;
char stageType[MM::MaxStrLength];
GetProperty(PROP_StageType, stageType);
isRotational_ = stageType != std::string{ PROPVAL_StageTypeLinear };
if (isRotational_) {
double deviceUnitsPerRevolution;
GetProperty(PROP_DeviceUnitsPerRevolution, deviceUnitsPerRevolution);
deviceUnitsPerUm_ = deviceUnitsPerRevolution / 360.0;
}
else {
double deviceUnitsPerMm;
GetProperty(PROP_DeviceUnitsPerMillimeter, deviceUnitsPerMm);
deviceUnitsPerUm_ = deviceUnitsPerMm / 1000.0;
}
// Start polling, which will keep position and status bits up to date.
// Ensure we are immediately up to date by first requesting position and
// status bits.
err = motorDrive_->RequestPosition();
if (err)
return ERR_OFFSET + err;
err = motorDrive_->RequestStatusBits();
if (err)
return ERR_OFFSET + err;
bool ok = motorDrive_->StartPolling(pollingIntervalMs_);
if (!ok) {
LogMessage(("Failed to start polling for serial no " + serialNo_).c_str());
}
CDeviceUtils::SleepMs(100); // Ensure the above requests finished (100 ms cycle)
if (!motorDrive_->IsChannelEnabled()) {
// A call to XXX_EnableChannel was added to Thorlabs example code at
// some point, but only for some devices. If this causes errors, we may
// need to branch depending on device type. At least some devices
// always start up disabled, so enabling _is_ necessary for those.
err = motorDrive_->SetChannelEnabled(true);
if (err)
return ERR_OFFSET + err;
didEnable_ = true;
}
return DEVICE_OK;
}
int
SingleAxisStage::Shutdown() {
if (didEnable_)
motorDrive_->SetChannelEnabled(false);
if (motorDrive_)
motorDrive_->StopPolling();
motorDrive_.reset();
return DEVICE_OK;
}
void
SingleAxisStage::GetName(char* name) const {
// Name format is ModelNo_SerialNo or ModelNo_SerialNo-Channel
// There are two situations in which GetName() is called before
// Initialize(): (1) During hardware configuration after
// DetectInstalledDevices() and (2) During normal config loading.
//
// In case (1) we don't know the model number yet, so we make a temporary
// connection (which we can because the Hub has already initialized the
// Kinesis API).
//
// In case (2), the Hub has not been initialized yet, so we echo back the
// name used to create this device.
std::string n;
if (!givenName_.empty()) {
n = givenName_;
}
else {
auto tmpMotorDrive = Connect();
n = MakeName(tmpMotorDrive.get());
}
CDeviceUtils::CopyLimitedString(name, n.c_str());
}
bool
SingleAxisStage::Busy() {
// We are busy if the motor is moving, which we get from the status bits.
// However, the status bits are only updated every polling interval, so
// they do not immediately indicate movement after we kick off a move. So
// we need to unconditionally report "busy" for one polling interval after
// starting a movement.
auto now = GetCurrentMMTime();
auto msSinceMovementStart = (now - lastMovementStart_).getMsec();
// Add a little extra to minimize the chance of a race due to jitter in the
// polling.
if (msSinceMovementStart <= pollingIntervalMs_ + 10.0)
return true;
DWORD status = motorDrive_->GetStatusBits();
return status & (
MotorDrive::StatusBitsMovingCW |
MotorDrive::StatusBitsMovingCCW |
MotorDrive::StatusBitsJoggingCW |
MotorDrive::StatusBitsJoggingCCW |
MotorDrive::StatusBitsHoming
);
}
int
SingleAxisStage::GetPositionUm(double& pos) {
long steps;
int err = GetPositionSteps(steps);
if (err != DEVICE_OK)
return err;
pos = steps / deviceUnitsPerUm_;
return DEVICE_OK;
}
int
SingleAxisStage::SetPositionUm(double pos) {
double dSteps = std::round(pos * deviceUnitsPerUm_);
int steps = clamp_int(dSteps);
return SetPositionSteps(steps);
}
int
SingleAxisStage::GetPositionSteps(long& steps) {
// TODO Does it make sense to use encoder position for non-stepper?
steps = motorDrive_->GetPositionCounter();
return DEVICE_OK;
}
int
SingleAxisStage::SetPositionSteps(long steps) {
int iSteps = sizeof(steps) > sizeof(iSteps) ?
clamp_int(steps) : static_cast<int>(steps);
short err = motorDrive_->MoveToPosition(iSteps);
if (err)
return ERR_OFFSET + err;
lastMovementStart_ = GetCurrentMMTime();
return DEVICE_OK;
}
int
SingleAxisStage::Home() {
if (!motorDrive_->CanHome())
return DEVICE_UNSUPPORTED_COMMAND;
short err = motorDrive_->Home();
if (err)
return ERR_OFFSET + err;
lastMovementStart_ = GetCurrentMMTime();
return DEVICE_OK;
}
std::unique_ptr<MotorDrive>
SingleAxisStage::Connect() const {
auto connection = MakeConnection(serialNo_);
if (!connection) // Shouldn't happen
return {};
return MakeKinesisMotorDrive(connection, channel_);
}
std::string
SingleAxisStage::MakeName(MotorDrive* motorDrive) const {
std::string name;
if (motorDrive && motorDrive->GetConnection()->IsValid()) {
name += motorDrive->GetModelNo();
}
else {
name += "Error";
if (motorDrive) {
name += std::to_string(motorDrive->GetConnection()->ConnectionError());
}
}
name += '_';
name += serialNo_;
if (channel_ > 0) {
name += '-';
name += std::to_string(channel_);
}
return name;
}