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
17 if (InstrParams->HoldTime > -2)
18 InstrData->HardwareAdapter->SetHoldTime(InstrData->GetChannel(), std::chrono::milliseconds(InstrParams->HoldTime));
19 } // InstrParams and InstrData unlocked here.
20
22 }
23
25 {
26 ExitFuncImpl(dispatch_tag<ExitTask>(), Instance);
27
28 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
29
30 try
31 {
32 // Abort motion.
33 InstrData->HardwareAdapter->StopMotion(InstrData->GetChannel());
34 }
35 catch (...)
36 {
37 // Swallow any exception which might arise from HardwareAdapter->StopMotion() since a failure
38 // of this function is not considered a severe error.
39 }
40
41 Instance.UnlockObject(InstrData->HardwareAdapter);
42 }
43
45 {
46 try
47 {
48 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
49 bool UpdateError = false;
50
51 try
52 {
53 InstrData->SetCurrentPosition(InstrData->HardwareAdapter->GetCurrentPosition(InstrData->GetChannel()));
54 InstrData->SetVelocity(InstrData->HardwareAdapter->GetVelocity(InstrData->GetChannel()));
55 InstrData->SmarActChannelStatus.Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
56 }
57 catch ([[maybe_unused]] const DynExpHardware::SmarActException& e)
58 {
59 UpdateError = true;
60
61 // Swallow if just one or two subsequent updates failed.
62 if (InstrData->NumFailedStatusUpdateAttempts++ >= 3)
63 throw;
64 }
65
66 if (!UpdateError)
67 InstrData->NumFailedStatusUpdateAttempts = 0;
68 }
69 // Issued if a mutex is blocked by another operation.
70 catch (const Util::TimeoutException& e)
71 {
72 Instance.GetOwner().SetWarning(e);
73
74 return;
75 }
76
77 UpdateFuncImpl(dispatch_tag<UpdateTask>(), Instance);
78 }
79
81 {
82 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
83
84 InstrData->SetHomePosition(InstrData->GetCurrentPosition());
85
86 return {};
87 }
88
90 {
91 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
92
93 InstrData->HardwareAdapter->Reference(InstrData->GetChannel());
94 InstrData->GetSmarActChannelStatus().Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
95
96 return {};
97 }
98
100 {
101 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
102
103 InstrData->HardwareAdapter->Calibrate(InstrData->GetChannel());
104 InstrData->GetSmarActChannelStatus().Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
105
106 return {};
107 }
108
110 {
111 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
112
113 InstrData->HardwareAdapter->SetVelocity(InstrData->GetChannel(), Velocity);
114
115 return {};
116 }
117
119 {
120 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
121
122 InstrData->HardwareAdapter->MoveAbsolute(InstrData->GetChannel(), InstrData->GetHomePosition());
123 InstrData->GetSmarActChannelStatus().Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
124
125 return {};
126 }
127
129 {
130 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
131
132 InstrData->HardwareAdapter->MoveAbsolute(InstrData->GetChannel(), Position);
133 InstrData->GetSmarActChannelStatus().Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
134
135 return {};
136 }
137
139 {
140 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
141
142 InstrData->HardwareAdapter->MoveRelative(InstrData->GetChannel(), Position);
143 InstrData->GetSmarActChannelStatus().Set(InstrData->HardwareAdapter->GetChannelState(InstrData->GetChannel()));
144
145 return {};
146 }
147
149 {
150 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(Instance.InstrumentDataGetter());
151
152 InstrData->HardwareAdapter->StopMotion(InstrData->GetChannel());
153
154 return DynExp::TaskResultType();
155 }
156
158 {
159 Channel = 0;
160 SmarActChannelStatus.Set(0);
161 NumFailedStatusUpdateAttempts = 0;
162 HomePosition = 0;
163
164 ResetImpl(dispatch_tag<SmarActData>());
165 }
166
167 SmarAct::SmarAct(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType&& Params)
168 : PositionerStage(OwnerThreadID, std::move(Params))
169 {
170 }
171
173 {
174 auto InstrData = DynExp::dynamic_InstrumentData_cast<SmarAct>(GetInstrumentData());
175
176 // Abort motion.
177 InstrData->HardwareAdapter->StopMotion(InstrData->GetChannel());
178 }
179
184}
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:157
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:99
void ExitFuncImpl(dispatch_tag< PositionerStageTasks::ExitTask >, DynExp::InstrumentInstance &Instance) override final
Deinitializes the respective instrument within the instrument inheritance hierarchy....
Definition SmarAct.cpp:24
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:128
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:138
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:118
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:89
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:80
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:109
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:148
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:44
virtual void OnErrorChild() const override
Derived classes can perform critical shutdown actions after an error has occurred....
Definition SmarAct.cpp:172
SmarAct(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType &&Params)
Definition SmarAct.cpp:167
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:180
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: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 Object::ParamsGetterType ParamsGetter
Invoke to obtain the parameters (derived from ParamsBase) of Owner.
Definition Object.h:3710
void UnlockObject(LinkedObjectWrapperContainer< ObjectT > &ObjectWrapperContainer)
Unlocks an Object instance stored in the LinkedObjectWrapperContainer ObjectWrapperContainer....
Definition Object.h:3609
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:3593
const auto & GetOwner() const noexcept
Returns Owner.
Definition Object.h:3556
Defines the return type of task functions.
Definition Instrument.h:865
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:262
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.