DynExp
Highly flexible laboratory automation for dynamically changing experiments.
NenionLeakvalveF3.cpp
Go to the documentation of this file.
1 // This file is part of DynExp.
2 
3 #include "stdafx.h"
4 #include "NenionLeakvalveF3.h"
5 
6 namespace DynExpInstr
7 {
9  {
10  {
11  auto InstrParams = DynExp::dynamic_Params_cast<NenionLeakvalveF3>(Instance.ParamsGetter());
12  auto InstrData = DynExp::dynamic_InstrumentData_cast<NenionLeakvalveF3>(Instance.InstrumentDataGetter());
13 
14  Instance.LockObject(InstrParams->HardwareAdapter, InstrData->HardwareAdapter);
15  InstrData->HardwareAdapter->Clear();
16 
17  // Enable motor current.
18  *InstrData->HardwareAdapter << "E";
19  } // InstrParams and InstrData unlocked here.
20 
22  }
23 
25  {
26  ExitFuncImpl(dispatch_tag<ExitTask>(), Instance);
27 
28  auto InstrData = DynExp::dynamic_InstrumentData_cast<NenionLeakvalveF3>(Instance.InstrumentDataGetter());
29 
30  try
31  {
32  // Abort motion.
33  *InstrData->HardwareAdapter << "H";
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<NenionLeakvalveF3>(Instance.InstrumentDataGetter());
49  bool UpdateError = false;
50 
51  try
52  {
53  std::string StateString;
54 
55  // Empty read buffer. The valve behaves strangly if this is not done.
56  do
57  {
58  StateString = InstrData->HardwareAdapter->WaitForLine(5);
59  } while (!StateString.empty());
60 
61  *InstrData->HardwareAdapter << "S";
62 
63  // Sometimes, the valve echos a sent goto command back. Repeat reading data in this case.
64  do
65  {
66  StateString = InstrData->HardwareAdapter->WaitForLine(5);
67  } while (!StateString.empty() && StateString[0] == 'G');
68  if (StateString.size() < 3 || StateString[1] != 'P')
69  throw Util::InvalidDataException("Received an unexpected answer.");
70 
71  switch (StateString[0])
72  {
73  case 'R':
74  InstrData->NenionLeakvalveF3Status = NenionLeakvalveF3Data::NenionLeakvalveF3StatusType::Moving;
75  break;
76  case 'I':
77  InstrData->NenionLeakvalveF3Status = NenionLeakvalveF3Data::NenionLeakvalveF3StatusType::Arrived;
78  break;
79  case 'S':
80  InstrData->NenionLeakvalveF3Status = NenionLeakvalveF3Data::NenionLeakvalveF3StatusType::Ready;
81  break;
82  case 'L':
83  InstrData->NenionLeakvalveF3Status = NenionLeakvalveF3Data::NenionLeakvalveF3StatusType::LocalMode;
84  break;
85  case 'A':
86  InstrData->NenionLeakvalveF3Status = NenionLeakvalveF3Data::NenionLeakvalveF3StatusType::AnalogMode;
87  break;
88  case 'D':
89  InstrData->NenionLeakvalveF3Status = NenionLeakvalveF3Data::NenionLeakvalveF3StatusType::Disabled;
90  break;
91  default:
92  throw Util::InvalidDataException("Received an invalid state descriptor.");
93  }
94 
95  InstrData->SetCurrentPosition(Util::StrToT<PositionerStageData::PositionType>(StateString.substr(2)));
96  }
97  catch ([[maybe_unused]] const Util::InvalidDataException& e)
98  {
99  UpdateError = true;
100 
101  // Swallow if just one or two subsequent updates failed.
102  if (InstrData->NumFailedStatusUpdateAttempts++ >= 3)
103  throw;
104  }
105 
106  if (!UpdateError)
107  InstrData->NumFailedStatusUpdateAttempts = 0;
108  }
109  // Issued if a mutex is blocked by another operation.
110  catch (const Util::TimeoutException& e)
111  {
112  Instance.GetOwner().SetWarning(e);
113 
114  return;
115  }
116  // Issued here or by StrToT() if unexpected or no data has been received.
117  catch (const Util::InvalidDataException& e)
118  {
119  Instance.GetOwner().SetWarning(e);
120 
121  return;
122  }
123 
124  UpdateFuncImpl(dispatch_tag<UpdateTask>(), Instance);
125  }
126 
128  {
129  auto InstrData = DynExp::dynamic_InstrumentData_cast<NenionLeakvalveF3>(Instance.InstrumentDataGetter());
130 
131  InstrData->SetHomePosition(InstrData->GetCurrentPosition());
132 
133  return {};
134  }
135 
137  {
138  auto InstrData = DynExp::dynamic_InstrumentData_cast<NenionLeakvalveF3>(Instance.InstrumentDataGetter());
139 
140  *InstrData->HardwareAdapter << "N";
141 
142  return {};
143  }
144 
146  {
147  auto InstrData = DynExp::dynamic_InstrumentData_cast<NenionLeakvalveF3>(Instance.InstrumentDataGetter());
148 
149  InstrData->SetVelocity(Velocity);
150 
151  return {};
152  }
153 
155  {
156  auto InstrData = DynExp::dynamic_InstrumentData_cast<NenionLeakvalveF3>(Instance.InstrumentDataGetter());
157 
158  *InstrData->HardwareAdapter << "G" + Util::ToStr(InstrData->GetHomePosition());
159 
160  return {};
161  }
162 
164  {
165  auto InstrData = DynExp::dynamic_InstrumentData_cast<NenionLeakvalveF3>(Instance.InstrumentDataGetter());
166 
167  const auto NewPos = static_cast<const NenionLeakvalveF3&>(Instance.GetOwner()).EnforcePositionLimits(Position);
168  *InstrData->HardwareAdapter << "G" + Util::ToStr(NewPos);
169 
170  return {};
171  }
172 
174  {
175  auto InstrData = DynExp::dynamic_InstrumentData_cast<NenionLeakvalveF3>(Instance.InstrumentDataGetter());
176 
177  const auto NewPos = static_cast<const NenionLeakvalveF3&>(Instance.GetOwner()).EnforcePositionLimits(InstrData->GetCurrentPosition() + Position);
178  *InstrData->HardwareAdapter << "G" + Util::ToStr(NewPos);
179 
180  return {};
181  }
182 
184  {
185  auto InstrData = DynExp::dynamic_InstrumentData_cast<NenionLeakvalveF3>(Instance.InstrumentDataGetter());
186 
187  // Abort motion.
188  *InstrData->HardwareAdapter << "H";
189 
190  return DynExp::TaskResultType();
191  }
192 
194  {
195  NenionLeakvalveF3Status = NenionLeakvalveF3StatusType::Ready;
196  NumFailedStatusUpdateAttempts = 0;
197  HomePosition = 0;
198 
200  }
201 
203  {
204  return !(NenionLeakvalveF3Status == NenionLeakvalveF3StatusType::Ready ||
205  NenionLeakvalveF3Status == NenionLeakvalveF3StatusType::Moving ||
206  NenionLeakvalveF3Status == NenionLeakvalveF3StatusType::Arrived);
207  }
208 
209  NenionLeakvalveF3::NenionLeakvalveF3(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType&& Params)
210  : PositionerStage(OwnerThreadID, std::move(Params))
211  {
212  }
213 
215  {
216  return std::min(GetMaxPosition(), std::max(GetMinPosition(), Position));
217  }
218 
220  {
221  auto InstrData = DynExp::dynamic_InstrumentData_cast<NenionLeakvalveF3>(GetInstrumentData());
222 
223  // Abort motion.
224  *InstrData->HardwareAdapter << "H";
225  }
226 
228  {
230  }
231 }
Implementation of an instrument to control the positioner stage of the Nenion Leakvalve F3 valve.
void ResetImpl(dispatch_tag< PositionerStageData >) override final
Refer to DynExp::InstrumentDataBase::Reset(). Using tag dispatch mechanism to ensure that ResetImpl()...
virtual bool HasFailedChild() const noexcept override
Returns whether the stage is in an error state, i.e. moving has failed (result of HasFailedChild())
void ExitFuncImpl(dispatch_tag< PositionerStageTasks::ExitTask >, DynExp::InstrumentInstance &Instance) override final
Deinitializes the respective instrument within the instrument inheritance hierarchy....
void InitFuncImpl(dispatch_tag< PositionerStageTasks::InitTask >, DynExp::InstrumentInstance &Instance) override final
Initializes the respective instrument within the instrument inheritance hierarchy....
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...
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< PositionerStageTasks::UpdateTask >, DynExp::InstrumentInstance &Instance) override final
Updates the respective instrument within the instrument inheritance hierarchy. Call UpdateFuncImpl() ...
PositionerStageData::PositionType EnforcePositionLimits(PositionerStageData::PositionType Position) const
NenionLeakvalveF3(const std::thread::id OwnerThreadID, DynExp::ParamsBasePtrType &&Params)
virtual void OnErrorChild() const override
Derived classes can perform critical shutdown actions after an error has occurred....
virtual PositionerStageData::PositionType GetMaxPosition() const noexcept override
Returns the maximal position the stage can move to in nm if the stage supports SI units,...
void ResetImpl(dispatch_tag< PositionerStage >) override final
Refer to DynExp::Object::Reset(). Using tag dispatch mechanism to ensure that ResetImpl() of every de...
virtual PositionerStageData::PositionType GetMinPosition() const noexcept override
Returns the minimal position the stage can move to in nm if the stage supports SI units,...
signed long long PositionType
Numeric type to store the stage positions.
Definition: Stage.h:71
Implementation of a meta instrument to control single-axis positioner stages.
Definition: Stage.h:155
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1151
Refer to DynExp::ParamsBase::dispatch_tag.
Definition: Instrument.h:1120
InstrumentDataTypeSyncPtrType GetInstrumentData(const std::chrono::milliseconds Timeout=GetInstrumentDataTimeoutDefault)
Locks the mutex of the instrument data class instance InstrumentData assigned to this InstrumentBase ...
Definition: Instrument.cpp:222
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.
Definition: Instrument.h:1182
Data to operate on is invalid for a specific purpose. This indicates a corrupted data structure or fu...
Definition: Exception.h:163
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...
Definition: Instrument.h:1254
constexpr auto Ready
std::unique_ptr< ParamsBase > ParamsBasePtrType
Alias for a pointer to the parameter system base class ParamsBase.
Definition: Object.h:1807
std::string ToStr(const T &Value, int Precision=-1)
Converts a (numeric) value of type T to a std::string using operator<< of std::stringstream.
Definition: Util.h:625
Accumulates include statements to provide a precompiled header.