Repository navigation
Expand file tree
/
Copy pathSimXY.h
More file actions
253 lines (224 loc) · 9.29 KB
/
Copy pathSimXY.h
File metadata and controls
253 lines (224 loc) · 9.29 KB
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
// Copyright 2025-2026 Board of Regents of the University of Wisconsin System
// SPDX-License-Identifier: BSD-2-Clause
#pragma once
#include "DelayedNotifier.h"
#include "ProcessModel.h"
#include "SimHub.h"
#include "DeviceBase.h"
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <mutex>
#include <string>
#include <utility>
template <typename ProcModel>
class SimXY : public CXYStageBase<SimXY<ProcModel>> {
// The process model operates in steps (only set to integers; round upon
// readout). X and Y use the same step size.
static constexpr double umPerStep_ = 0.1;
std::string name_;
SimHub *hub_ = nullptr;
DelayedNotifier delayer_;
std::mutex notificationMut_;
bool notificationsEnabled_ = false;
// Declared last so that it is destroyed first: its slew thread's update
// function uses the members above.
ProcModel model_;
public:
explicit SimXY(std::string name)
: name_(std::move(name)), model_([this](std::array<double, 2> pv) {
const long ix = std::lround(pv[0]);
const long iy = std::lround(pv[1]);
this->LogMessage(
("PV = " + std::to_string(ix) + ", " + std::to_string(iy))
.c_str(),
true);
{
std::lock_guard<std::mutex> lock(notificationMut_);
if (!notificationsEnabled_) {
return;
}
}
delayer_.Schedule([this, ix, iy] {
this->LogMessage(("Notifying: PV = " + std::to_string(ix) +
", " + std::to_string(iy))
.c_str(),
true);
this->OnXYStagePositionChanged(umPerStep_ * ix,
umPerStep_ * iy);
});
}) {
if constexpr (ProcModel::isAsync) {
// Adjust default for stage-like velocity (100 um/s).
model_.ReciprocalSlewRateSeconds(umPerStep_ / 100.0);
}
}
int Initialize() final {
hub_ = static_cast<SimHub *>(this->GetParentHub());
if (!hub_) {
return DEVICE_COMM_HUB_MISSING;
}
int ret = this->CreateFloatProperty("UmPerStep", umPerStep_, true);
assert(ret == DEVICE_OK);
ret = this->CreateStringProperty(
"NotificationsEnabled", notificationsEnabled_ ? "Yes" : "No",
false,
new MM::ActionLambda(
[this](MM::PropertyBase *pProp, MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
const bool enabled = [&] {
std::lock_guard<std::mutex> lock(notificationMut_);
return notificationsEnabled_;
}();
pProp->Set(enabled ? "Yes" : "No");
} else if (eAct == MM::AfterSet) {
std::string value;
pProp->Get(value);
const bool enable = (value == "Yes");
std::lock_guard<std::mutex> lock(notificationMut_);
notificationsEnabled_ = enable;
}
return DEVICE_OK;
}));
assert(ret == DEVICE_OK);
ret = this->AddAllowedValue("NotificationsEnabled", "No");
assert(ret == DEVICE_OK);
ret = this->AddAllowedValue("NotificationsEnabled", "Yes");
assert(ret == DEVICE_OK);
if constexpr (ProcModel::isAsync) {
ret = this->CreateFloatProperty(
"SlewTimePerStep_s", model_.ReciprocalSlewRateSeconds(), false,
new MM::ActionLambda(
[this](MM::PropertyBase *pProp, MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
pProp->Set(model_.ReciprocalSlewRateSeconds());
} else if (eAct == MM::AfterSet) {
double seconds{};
pProp->Get(seconds);
model_.ReciprocalSlewRateSeconds(seconds);
}
return DEVICE_OK;
}));
assert(ret == DEVICE_OK);
ret = this->SetPropertyLimits("SlewTimePerStep_s", 0.0001, 10.0);
assert(ret == DEVICE_OK);
ret = this->CreateFloatProperty(
"UpdateInterval_s", model_.UpdateIntervalSeconds(), false,
new MM::ActionLambda(
[this](MM::PropertyBase *pProp, MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
pProp->Set(model_.UpdateIntervalSeconds());
} else if (eAct == MM::AfterSet) {
double seconds{};
pProp->Get(seconds);
model_.UpdateIntervalSeconds(seconds);
}
return DEVICE_OK;
}));
assert(ret == DEVICE_OK);
ret = this->SetPropertyLimits("UpdateInterval_s", 0.0001, 10.0);
assert(ret == DEVICE_OK);
using std::chrono::duration_cast;
using std::chrono::microseconds;
using FPSeconds = std::chrono::duration<double>;
ret = this->CreateFloatProperty(
"NotificationDelay_s",
duration_cast<FPSeconds>(delayer_.Delay()).count(), false,
new MM::ActionLambda([this](MM::PropertyBase *pProp,
MM::ActionType eAct) {
if (eAct == MM::BeforeGet) {
pProp->Set(duration_cast<FPSeconds>(delayer_.Delay())
.count());
} else if (eAct == MM::AfterSet) {
double seconds{};
pProp->Get(seconds);
const auto delay = FPSeconds{seconds};
delayer_.Delay(duration_cast<microseconds>(delay));
}
return DEVICE_OK;
}));
assert(ret == DEVICE_OK);
ret = this->SetPropertyLimits("NotificationDelay_s", 0.0, 1.0);
assert(ret == DEVICE_OK);
}
(void)ret;
hub_->SetGetXYUmFunction([this] {
// We do _not_ call GetPositionUm() here, because those um coords
// may be flipped wrt the specimen due to TransposeMirrorX/Y.
// Instead, we use a pure scaling of the steps here.
long x_steps, y_steps;
(void)this->GetPositionSteps(x_steps, y_steps);
const double xUmPerSteps = GetStepSizeXUm();
const double yUmPerSteps = GetStepSizeYUm();
const double x_um = x_steps * xUmPerSteps;
const double y_um = y_steps * yUmPerSteps;
return std::make_pair(x_um, y_um);
});
return DEVICE_OK;
}
int Shutdown() final {
if (hub_) {
hub_->SetGetXYUmFunction([] { return std::make_pair(0.0, 0.0); });
hub_ = nullptr;
}
model_.Halt();
delayer_.CancelAll();
return DEVICE_OK;
}
bool Busy() final { return model_.IsSlewing(); }
void GetName(char *name) const final {
CDeviceUtils::CopyLimitedString(name, name_.c_str());
}
int GetLimitsUm(double &xMin, double &xMax, double &yMin,
double &yMax) final {
long xLo, xHi, yLo, yHi;
int ret = GetStepLimits(xLo, xHi, yLo, yHi);
if (ret != DEVICE_OK) {
return ret;
}
xMin = xLo * umPerStep_;
xMax = xHi * umPerStep_;
yMin = yLo * umPerStep_;
yMax = yHi * umPerStep_;
return DEVICE_OK;
}
int SetPositionSteps(long x, long y) final {
this->LogMessage(
("sp = " + std::to_string(x) + ", " + std::to_string(y)).c_str(),
true);
model_.Setpoint({double(x), double(y)});
return DEVICE_OK;
}
int GetPositionSteps(long &x, long &y) final {
const auto pv = model_.ProcessVariable();
x = std::lround(pv[0]);
y = std::lround(pv[1]);
return DEVICE_OK;
}
int Home() final {
// We could simulate homing, but it is not currently clear what
// notifications mean during a home (application code should explicitly
// read the position after a home). For now, pretend that the stage
// doesn't support homing.
return DEVICE_UNSUPPORTED_COMMAND;
}
int Stop() final {
model_.Halt();
return DEVICE_OK;
}
int SetOrigin() final { return DEVICE_UNSUPPORTED_COMMAND; }
int GetStepLimits(long &xMin, long &xMax, long &yMin, long &yMax) final {
// Conservatively keep steps within 32-bit range (we want exact integer
// steps to be preserved by the double-based process model).
xMin = yMin = -2147483648;
xMax = yMax = +2147483647;
return DEVICE_OK;
}
double GetStepSizeXUm() final { return umPerStep_; }
double GetStepSizeYUm() final { return umPerStep_; }
int IsXYStageSequenceable(bool &flag) const final {
flag = false;
return DEVICE_OK;
}
};