DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
NetworkLaser.cpp
Go to the documentation of this file.
1// This file is part of DynExp.
2
3#include "stdafx.h"
4#include "NetworkLaser.h"
5
6namespace DynExpInstr
7{
9 {
11 {
12 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
13 StubPtr = InstrData->template GetStub<DynExpProto::NetworkLaser::NetworkLaser>();
14 } // InstrData unlocked here.
15
16 auto Response = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::GetDeviceInfo, {});
17
18 {
19 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
20
21 InstrData->FrequencyUnit = ToLaserUnitType(Response.frequencyunit());
22 InstrData->IntensityUnit = ToLaserUnitType(Response.intensityunit());
23 InstrData->HardwareMinFrequency = Response.hardwareminfrequency();
24 InstrData->HardwareMaxFrequency = Response.hardwaremaxfrequency();
25 InstrData->HardwareMinIntensity = Response.hardwareminintensity();
26 InstrData->HardwareMaxIntensity = Response.hardwaremaxintensity();
27 InstrData->HardwareMinScanRange = Response.hardwareminscanrange();
28 InstrData->HardwareMaxScanRange = Response.hardwaremaxscanrange();
29 InstrData->HardwareMinScanRate = Response.hardwareminscanrate();
30 InstrData->HardwareMaxScanRate = Response.hardwaremaxscanrate();
31 InstrData->HardwareModeHopFreeTuningRange = Response.hardwaremodehopfreetuningrange();
32 } // InstrData unlocked here.
33
34 // Initialize derived instrument last.
35 InitFuncImpl(dispatch_tag<InitTask>(), Instance);
36 }
37
39 {
40 // Shut down derived instrument first.
41 ExitFuncImpl(dispatch_tag<ExitTask>(), Instance);
42
43 try
44 {
45 // Stop laser emission.
47 {
48 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
49 StubPtr = InstrData->template GetStub<DynExpProto::NetworkLaser::NetworkLaser>();
50 } // InstrData unlocked here.
51
52 InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::Disable, {});
53 }
54 catch (...)
55 {
56 // Swallow any exception which might arise from instrument shutdown since a failure
57 // of this function is not considered a severe error.
58 }
59 }
60
62 {
64 {
65 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
66 StubPtr = InstrData->template GetStub<DynExpProto::NetworkLaser::NetworkLaser>();
67 } // InstrData unlocked here.
68
69 auto StateResponse = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::GetState, {});
70 auto FrequencyResponse = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::GetFrequency, {});
71 auto IntensityResponse = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::GetIntensity, {});
72 auto ScanRangeResponse = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::GetScanRange, {});
73 auto ScanRateResponse = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::GetScanRate, {});
74
75 {
76 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
77
78 InstrData->LaserState = ToLaserStateType(StateResponse.state());
79 InstrData->SetFrequencyValue(FrequencyResponse.frequency());
80 InstrData->SetIntensityValue(IntensityResponse.intensity());
81 InstrData->SetScanRangeValue(ScanRangeResponse.scanrangeinfrequnit());
82 InstrData->SetScanRateValue(ScanRateResponse.scanrateinfrequnitpersecond());
83 } // InstrData unlocked here.
84
85 // Update derived instrument.
86 UpdateFuncImpl(dispatch_tag<UpdateTask>(), Instance);
87 }
88
90 {
92 {
93 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
94 StubPtr = InstrData->template GetStub<DynExpProto::NetworkLaser::NetworkLaser>();
95 } // InstrData unlocked here.
96
97 DynExpProto::NetworkLaser::FrequencyMessage Message;
98 Message.set_frequency(Frequency);
99
100 auto Response = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::SetFrequency, Message);
101
102 static const std::string ErrorMsg = "Failed to set laser frequency.";
103 if (Response.result() == DynExpProto::NetworkLaser::ResultType::InvalidValue)
104 Instance.GetOwner().SetWarning(ErrorMsg + " Invalid frequency value.", Util::DynExpErrorCodes::ServiceFailed);
105 else if (Response.result() == DynExpProto::NetworkLaser::ResultType::InvalidMethod)
106 Instance.GetOwner().SetWarning(ErrorMsg + " Invalid method for this laser.", Util::DynExpErrorCodes::ServiceFailed);
107 else if (Response.result() != DynExpProto::NetworkLaser::ResultType::OK)
108 Instance.GetOwner().SetWarning(ErrorMsg, Util::DynExpErrorCodes::ServiceFailed);
109
110 return {};
111 }
112
114 {
116 {
117 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
118 StubPtr = InstrData->template GetStub<DynExpProto::NetworkLaser::NetworkLaser>();
119 } // InstrData unlocked here.
120
121 DynExpProto::NetworkLaser::IntensityMessage Message;
122 Message.set_intensity(Intensity);
123
124 auto Response = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::SetIntensity, Message);
125
126 static const std::string ErrorMsg = "Failed to set laser intensity.";
127 if (Response.result() == DynExpProto::NetworkLaser::ResultType::InvalidValue)
128 Instance.GetOwner().SetWarning(ErrorMsg + " Invalid intensity value.", Util::DynExpErrorCodes::ServiceFailed);
129 else if (Response.result() == DynExpProto::NetworkLaser::ResultType::InvalidMethod)
130 Instance.GetOwner().SetWarning(ErrorMsg + " Invalid method for this laser.", Util::DynExpErrorCodes::ServiceFailed);
131 else if (Response.result() != DynExpProto::NetworkLaser::ResultType::OK)
132 Instance.GetOwner().SetWarning(ErrorMsg, Util::DynExpErrorCodes::ServiceFailed);
133
134 return {};
135 }
136
138 {
140 {
141 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
142 StubPtr = InstrData->template GetStub<DynExpProto::NetworkLaser::NetworkLaser>();
143 } // InstrData unlocked here.
144
145 DynExpProto::NetworkLaser::ScanRangeMessage Message;
146 Message.set_scanrangeinfrequnit(ScanRange);
147
148 auto Response = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::SetScanRange, Message);
149
150 static const std::string ErrorMsg = "Failed to set laser scan range.";
151 if (Response.result() == DynExpProto::NetworkLaser::ResultType::InvalidValue)
152 Instance.GetOwner().SetWarning(ErrorMsg + " Invalid scan range value.", Util::DynExpErrorCodes::ServiceFailed);
153 else if (Response.result() == DynExpProto::NetworkLaser::ResultType::InvalidMethod)
154 Instance.GetOwner().SetWarning(ErrorMsg + " Invalid method for this laser.", Util::DynExpErrorCodes::ServiceFailed);
155 else if (Response.result() != DynExpProto::NetworkLaser::ResultType::OK)
156 Instance.GetOwner().SetWarning(ErrorMsg, Util::DynExpErrorCodes::ServiceFailed);
157
158 return {};
159 }
160
162 {
164 {
165 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
166 StubPtr = InstrData->template GetStub<DynExpProto::NetworkLaser::NetworkLaser>();
167 } // InstrData unlocked here.
168
169 DynExpProto::NetworkLaser::ScanRateMessage Message;
170 Message.set_scanrateinfrequnitpersecond(ScanRate);
171
172 auto Response = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::SetScanRate, Message);
173
174 static const std::string ErrorMsg = "Failed to set laser scan rate.";
175 if (Response.result() == DynExpProto::NetworkLaser::ResultType::InvalidValue)
176 Instance.GetOwner().SetWarning(ErrorMsg + " Invalid scan rate value.", Util::DynExpErrorCodes::ServiceFailed);
177 else if (Response.result() == DynExpProto::NetworkLaser::ResultType::InvalidMethod)
178 Instance.GetOwner().SetWarning(ErrorMsg + " Invalid method for this laser.", Util::DynExpErrorCodes::ServiceFailed);
179 else if (Response.result() != DynExpProto::NetworkLaser::ResultType::OK)
180 Instance.GetOwner().SetWarning(ErrorMsg, Util::DynExpErrorCodes::ServiceFailed);
181
182 return {};
183 }
184
186 {
188 {
189 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
190 StubPtr = InstrData->template GetStub<DynExpProto::NetworkLaser::NetworkLaser>();
191 } // InstrData unlocked here.
192
193 auto Response = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::Enable, {});
194
195 static const std::string ErrorMsg = "Failed to enable laser emission.";
196 if (Response.result() == DynExpProto::NetworkLaser::ResultType::InvalidMethod)
197 Instance.GetOwner().SetWarning(ErrorMsg + " Invalid method for this laser.", Util::DynExpErrorCodes::ServiceFailed);
198 else if (Response.result() != DynExpProto::NetworkLaser::ResultType::OK)
199 Instance.GetOwner().SetWarning(ErrorMsg, Util::DynExpErrorCodes::ServiceFailed);
200
201 return {};
202 }
203
205 {
207 {
208 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
209 StubPtr = InstrData->template GetStub<DynExpProto::NetworkLaser::NetworkLaser>();
210 }
211
212 InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::Disable, {});
213
214 return {};
215 }
216
218 {
220 {
221 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
222 StubPtr = InstrData->template GetStub<DynExpProto::NetworkLaser::NetworkLaser>();
223 }
224
225 auto Response = InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::ScanContinuously, {});
226
227 static const std::string ErrorMsg = "Failed to scan laser emission.";
228 if (Response.result() == DynExpProto::NetworkLaser::ResultType::InvalidMethod)
229 Instance.GetOwner().SetWarning(ErrorMsg + " Invalid method for this laser.", Util::DynExpErrorCodes::ServiceFailed);
230 else if (Response.result() != DynExpProto::NetworkLaser::ResultType::OK)
231 Instance.GetOwner().SetWarning(ErrorMsg, Util::DynExpErrorCodes::ServiceFailed);
232
233 return {};
234 }
235
237 {
239 {
240 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(Instance.InstrumentDataGetter());
241 StubPtr = InstrData->template GetStub<DynExpProto::NetworkLaser::NetworkLaser>();
242 }
243
244 InvokeStubFunc(StubPtr, &DynExpProto::NetworkLaser::NetworkLaser::Stub::DisableScan, {});
245
246 return {};
247 }
248
250 {
251 FrequencyUnit = FrequencyUnitType::Hz;
252 IntensityUnit = IntensityUnitType::Power_W;
253 HardwareMinFrequency = 0.0;
254 HardwareMaxFrequency = 0.0;
255 HardwareMinIntensity = 0.0;
256 HardwareMaxIntensity = 0.0;
257 HardwareMinScanRange = 0.0;
258 HardwareMaxScanRange = 0.0;
259 HardwareMinScanRate = 0.0;
260 HardwareMaxScanRate = 0.0;
261 HardwareModeHopFreeTuningRange = 0.0;
262
263 LaserState = LaserStateType::Ready;
264
265 ResetImpl(dispatch_tag<NetworkLaserData>());
266 }
267
268 NetworkLaser::NetworkLaser(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType&& Params)
269 : gRPCInstrument<Laser, 0, DynExpProto::NetworkLaser::NetworkLaser>(OwnerThreadID, std::move(Params))
270 {
271 }
272
274 {
275 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(GetInstrumentData());
276
277 return InstrData->GetFrequencyUnit();
278 }
279
281 {
282 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(GetInstrumentData());
283
284 return InstrData->GetIntensityUnit();
285 }
286
288 {
289 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(GetInstrumentData());
290
291 return InstrData->GetMinFrequency();
292 }
293
295 {
296 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(GetInstrumentData());
297
298 return InstrData->GetMaxFrequency();
299 }
300
302 {
303 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(GetInstrumentData());
304
305 return InstrData->GetMinIntensity();
306 }
307
309 {
310 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(GetInstrumentData());
311
312 return InstrData->GetMaxIntensity();
313 }
314
316 {
317 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(GetInstrumentData());
318
319 return InstrData->GetMinScanRange();
320 }
321
323 {
324 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(GetInstrumentData());
325
326 return InstrData->GetMaxScanRange();
327 }
328
330 {
331 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(GetInstrumentData());
332
333 return InstrData->GetMinScanRate();
334 }
335
337 {
338 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(GetInstrumentData());
339
340 return InstrData->GetMaxScanRate();
341 }
342
344 {
345 auto InstrData = DynExp::dynamic_InstrumentData_cast<NetworkLaser>(GetInstrumentData());
346
347 return InstrData->GetModeHopFreeTuningRange();
348 }
349
354}
Implementation of a gRPC client instrument to access a remote laser meta instrument.
FrequencyUnitType
Supported laser frequency units.
Definition Laser.h:74
IntensityUnitType
Supported laser intensity units.
Definition Laser.h:85
Meta instrument for a laser.
Definition Laser.h:189
void ResetImpl(dispatch_tag< gRPCInstrumentData< Laser, 0, DynExpProto::NetworkLaser::NetworkLaser > >) override final
virtual DynExp::TaskResultType RunChild(DynExp::InstrumentInstance &Instance) override
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
virtual DynExp::TaskResultType RunChild(DynExp::InstrumentInstance &Instance) override
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
virtual DynExp::TaskResultType RunChild(DynExp::InstrumentInstance &Instance) override
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
void ExitFuncImpl(dispatch_tag< gRPCInstrumentTasks::ExitTask< Laser, 0, DynExpProto::NetworkLaser::NetworkLaser > >, DynExp::InstrumentInstance &Instance) override final
void InitFuncImpl(dispatch_tag< gRPCInstrumentTasks::InitTask< Laser, 0, DynExpProto::NetworkLaser::NetworkLaser > >, DynExp::InstrumentInstance &Instance) override final
virtual DynExp::TaskResultType RunChild(DynExp::InstrumentInstance &Instance) override
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
virtual DynExp::TaskResultType RunChild(DynExp::InstrumentInstance &Instance) override
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
virtual DynExp::TaskResultType RunChild(DynExp::InstrumentInstance &Instance) override
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
virtual DynExp::TaskResultType RunChild(DynExp::InstrumentInstance &Instance) override
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
virtual DynExp::TaskResultType RunChild(DynExp::InstrumentInstance &Instance) override
Runs the task. Override RunChild() to define a derived task's action(s). Any exception leaving RunChi...
void UpdateFuncImpl(dispatch_tag< gRPCInstrumentTasks::UpdateTask< Laser, 0, DynExpProto::NetworkLaser::NetworkLaser > >, DynExp::InstrumentInstance &Instance) override final
virtual double GetMinScanRate() const override
Determines the minimal frequency scan rate. The default implementation returns 0.0,...
virtual double GetMinFrequency() const override
Determines the minimal emission frequency. The default implementation returns GetMaxFrequency() which...
virtual LaserData::FrequencyUnitType GetFrequencyUnit() const override
Determines the frequency unit.
virtual double GetMaxScanRange() const override
Determines the maximal frequency scan range. The default implementation returns 0....
virtual double GetMaxScanRate() const override
Determines the maximal frequency scan rate. The default implementation returns 0.0,...
virtual double GetMinScanRange() const override
Determines the minimal frequency scan range. The default implementation returns 0....
NetworkLaser(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType &&Params)
virtual LaserData::IntensityUnitType GetIntensityUnit() const override
Determines the intensity unit.
virtual double GetModeHopFreeTuningRange() const override
Determines the mode hop free tuning range. The default implementation returns 0.0,...
virtual double GetMinIntensity() const override
Determines the minimal emission intensity. The default implementation returns GetMaxIntensity() which...
virtual double GetMaxIntensity() const override
Determines the maximal emission intensity.
virtual double GetMaxFrequency() const override
Determines the maximal emission frequency.
void ResetImpl(dispatch_tag< gRPCInstrument< Laser, 0, DynExpProto::NetworkLaser::NetworkLaser > >) override final
Data class for gRPCInstrument.
Defines a task for deinitializing an instrument within an instrument inheritance hierarchy....
Defines a task for initializing an instrument within an instrument inheritance hierarchy....
Defines a task for updating an instrument within an instrument inheritance hierarchy....
Meta instrument template for transforming meta instruments into network instruments,...
InstrumentDataTypeSyncPtrType GetInstrumentData(const std::chrono::milliseconds Timeout=GetInstrumentDataTimeoutDefault)
Locks the mutex of the instrument data class instance InstrumentData assigned to this InstrumentBase ...
Defines data for a thread belonging to a InstrumentBase instance. Refer to RunnableInstance.
Definition Instrument.h:813
const InstrumentBase::InstrumentDataGetterType InstrumentDataGetter
Getter for instrument's data. Refer to InstrumentBase::InstrumentDataGetterType.
Definition Instrument.h:832
Refer to ParamsBase::dispatch_tag.
Definition Object.h:2018
const auto & GetOwner() const noexcept
Returns Owner.
Definition Object.h:3556
Defines the return type of task functions.
Definition Instrument.h:865
DynExp's instrument namespace contains the implementation of DynExp instruments which extend DynExp's...
ResponseMsgType InvokeStubFunc(StubPtrType< gRPCStub > StubPtr, StubFuncPtrType< gRPCStub, RequestMsgType, ResponseMsgType > StubFunc, const RequestMsgType &RequestMsg)
Invokes a gRPC stub function as a remote procedure call. Waits for a fixed amount of time (2 seconds)...
std::shared_ptr< typename gRPCStub::Stub > StubPtrType
Alias for a pointer to a gRPC stub.
constexpr LaserData::FrequencyUnitType ToLaserUnitType(DynExpProto::Common::FrequencyUnitType Unit)
constexpr LaserData::LaserStateType ToLaserStateType(DynExpProto::NetworkLaser::StateType State)
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
Definition Object.h:1807
Accumulates include statements to provide a precompiled header.