DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Loading...
Searching...
No Matches
SmarAct.cpp
Go to the documentation of this file.
1// This file is part of DynExp.
2
3#include "stdafx.h"
4#include "SmarAct.h"
5
6namespace DynExpInstr
7{
9 {
10 {
11 auto InstrParams = DynExp::dynamic_Params_cast<SmarAct>(Instance.ParamsGetter());
12 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
13
14 InstrData->Channel = InstrParams->Channel;
15 Instance.LockObject(InstrParams->HardwareAdapter, InstrData->HardwareAdapter);
16 } // InstrParams and InstrData unlocked here.
17
19 }
20
22 {
23 ExitFuncImpl(dispatch_tag<ExitTask>(), Instance);
24
25 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
26
27 try
28 {
29 // Abort motion.
30 InstrData->HardwareAdapter->StopMotion(InstrData->GetChannel());
31 }
32 catch (...)
33 {
34 // Swallow any exception which might arise from HardwareAdapter->StopMotion() since a failure
35 // of this function is not considered a severe error.
36 }
37
38 Instance.UnlockObject(InstrData->HardwareAdapter);
39 }
40
42 {
43 try
44 {
45 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
46 bool UpdateError = false;
47
48 try
49 {
50 InstrData->SetCurrentPosition(InstrData->HardwareAdapter->GetCurrentPosition(InstrData->GetChannel()));
51 InstrData->SetVelocity(InstrData->HardwareAdapter->GetVelocity(InstrData->GetChannel()));
52 InstrData->SmarActChannelStatus.Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
53 }
54 catch ([[maybe_unused]] const DynExpHardware::SmarActException& e)
55 {
56 UpdateError = true;
57
58 // Swallow if just one or two subsequent updates failed.
59 if (InstrData->NumFailedStatusUpdateAttempts++ >= 3)
60 throw;
61 }
62
63 if (!UpdateError)
64 InstrData->NumFailedStatusUpdateAttempts = 0;
65 }
66 // Issued if a mutex is blocked by another operation.
67 catch (const Util::TimeoutException& e)
68 {
69 Instance.GetOwner().SetWarning(e);
70
71 return;
72 }
73
74 UpdateFuncImpl(dispatch_tag<UpdateTask>(), Instance);
75 }
76
78 {
79 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
80
81 InstrData->SetHomePosition(InstrData->GetCurrentPosition());
82
83 return {};
84 }
85
87 {
88 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
89
90 InstrData->HardwareAdapter->Reference(InstrData->GetChannel());
91 InstrData->GetSmarActChannelStatus().Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
92
93 return {};
94 }
95
97 {
98 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
99
100 InstrData->HardwareAdapter->Calibrate(InstrData->GetChannel());
101 InstrData->GetSmarActChannelStatus().Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
102
103 return {};
104 }
105
107 {
108 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
109
110 InstrData->HardwareAdapter->SetVelocity(InstrData->GetChannel(), Velocity);
111
112 return {};
113 }
114
116 {
117 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
118
119 InstrData->HardwareAdapter->MoveAbsolute(InstrData->GetChannel(), InstrData->GetHomePosition());
120 InstrData->GetSmarActChannelStatus().Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
121
122 return {};
123 }
124
126 {
127 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
128
129 InstrData->HardwareAdapter->MoveAbsolute(InstrData->GetChannel(), Position);
130 InstrData->GetSmarActChannelStatus().Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
131
132 return {};
133 }
134
136 {
137 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
138
139 InstrData->HardwareAdapter->MoveRelative(InstrData->GetChannel(), Position);
140 InstrData->GetSmarActChannelStatus().Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
141
142 return {};
143 }
144
146 {
147 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
148
149 InstrData->HardwareAdapter->StopMotion(InstrData->GetChannel());
150
151 return DynExp::TaskResultType();
152 }
153
155 {
156 Channel = 0;
157 SmarActChannelStatus.Set(0);
158 NumFailedStatusUpdateAttempts = 0;
159 HomePosition = 0;
160
161 ResetImpl(dispatch_tag<SmarActData>());
162 }
163
164 SmarAct::SmarAct(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType&& Params)
165 : PositionerStage(OwnerThreadID, std::move(Params))
166 {
167 }
168
170 {
171 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(GetInstrumentData());
172
173 // Abort motion.
174 InstrData->HardwareAdapter->StopMotion(InstrData->GetChannel());
175 }
176
181}
Implementation of an instrument to control a single positioner stage connected to the SmarAct MCS2.
Implementation of a meta instrument to control single-axis positioner stages.
Definition Stage.h:155
void ResetImpl(dispatch_tag< PositionerStageData >) override final
Refer to DynExp::InstrumentDataBase::Reset(). Using tag dispatch mechanism to ensure that ResetImpl()...
Definition SmarAct.cpp:154
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...
Definition SmarAct.cpp:96
void ExitFuncImpl(dispatch_tag< PositionerStageTasks::ExitTask >, DynExp::InstrumentInstance &Instance) override final
Deinitializes the respective instrument within the instrument inheritance hierarchy....
Definition SmarAct.cpp:21
void InitFuncImpl(dispatch_tag< PositionerStageTasks::InitTask >, DynExp::InstrumentInstance &Instance) override final
Initializes the respective instrument within the instrument inheritance hierarchy....
Definition SmarAct.cpp:8
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...
Definition SmarAct.cpp:125
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...
Definition SmarAct.cpp:135
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...
Definition SmarAct.cpp:115
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...
Definition SmarAct.cpp:86
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...
Definition SmarAct.cpp:77
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...
Definition SmarAct.cpp:106
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...
Definition SmarAct.cpp:145
void UpdateFuncImpl(dispatch_tag< PositionerStageTasks::UpdateTask >, DynExp::InstrumentInstance &Instance) override final
Updates the respective instrument within the instrument inheritance hierarchy. Call UpdateFuncImpl() ...
Definition SmarAct.cpp:41
virtual void OnErrorChild() const override
Derived classes can perform critical shutdown actions after an error has occurred....
Definition SmarAct.cpp:169
SmarAct(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType &&Params)
Definition SmarAct.cpp:164
void ResetImpl(dispatch_tag< PositionerStage >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
Definition SmarAct.cpp:177
Refer to DynExp::ParamsBase::dispatch_tag.
Refer to DynExp::ParamsBase::dispatch_tag.
InstrumentDataTypeSyncPtrType GetInstrumentData(const std::chrono::milliseconds Timeout=GetInstrumentDataTimeoutDefault)
Locks the mutex of the instrument data class instance InstrumentData assigned to this InstrumentBase ...
Refer to ParamsBase::dispatch_tag.
Definition Instrument.h:146
Defines data for a thread belonging to a InstrumentBase instance. Refer to RunnableInstance.
Definition Instrument.h:772
const InstrumentBase::InstrumentDataGetterType InstrumentDataGetter
Getter for instrument's data. Refer to InstrumentBase::InstrumentDataGetterType.
Definition Instrument.h:791
Refer to ParamsBase::dispatch_tag.
Definition Object.h:2018
const Object::ParamsGetterType ParamsGetter
Invoke to obtain the parameters (derived from ParamsBase) of Owner.
Definition Object.h:3671
void UnlockObject(LinkedObjectWrapperContainer< ObjectT > &ObjectWrapperContainer)
Unlocks an Object instance stored in the LinkedObjectWrapperContainer ObjectWrapperContainer....
Definition Object.h:3570
void LockObject(const ParamsBase::Param< ObjectLink< ObjectT > > &LinkParam, LinkedObjectWrapperContainer< ObjectT > &ObjectWrapperContainer, std::chrono::milliseconds Timeout=ObjectLinkBase::LockObjectTimeoutDefault)
Locks an Object instance referenced by a parameter LinkParam of type ParamsBase::Param< ObjectLink< O...
Definition Object.h:3554
const auto & GetOwner() const noexcept
Returns Owner.
Definition Object.h:3524
Defines the return type of task functions.
Definition Instrument.h:824
Refer to DynExp::ParamsBase::dispatch_tag.
Thrown when an operation timed out before it could be completed, especially used for locking shared d...
Definition Exception.h:261
DynExp's instrument namespace contains the implementation of DynExp instruments which extend DynExp's...
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.