The VisAO Camera
VisAOSimpleMotorCtrl.cpp
Go to the documentation of this file.
1 /************************************************************
2 * VisAOSimpleMotorCtrl.cpp
3 *
4 * Author: Jared R. Males (jrmales@email.arizona.edu)
5 *
6 * Declarations for the VisAOSimpleMotorCtrl class, which adapts
7 * the SimpleMotorCtrl class for use as a VisAO App.
8 *
9 * Developed as part of the Magellan Adaptive Optics system.
10 ************************************************************/
11 
12 /** \file VisAOSimpleMotorCtrl.cpp
13  * \author Jared R. Males
14  * \brief Declarations for the VisAOSimpleMotorCtrl class.
15  *
16 */
17 
18 #include "VisAOSimpleMotorCtrl.h"
19 
20 /// Mutex to lock out other threads during complex operations.
21 /** Defined in SimpleMotorCtrl.cpp, we use it here for \ref VisAOSimpleMotorCtrl::CModeReqChanged.
22  */
23 extern pthread_mutex_t threadMutex;
24 
25 namespace VisAO
26 {
27 
28 //Static fields
29 int VisAOSimpleMotorCtrl::control_mode = VisAOSimpleMotorCtrl::CMODE_REMOTE;
30 
32 
33 VisAOSimpleMotorCtrl::VisAOSimpleMotorCtrl( std::string name, const std::string &conffile) throw (AOException) : SimpleMotorCtrl( name, conffile)
34 {
35  control_mode = CMODE_REMOTE;
36  return;
37 }
38 
39 VisAOSimpleMotorCtrl::VisAOSimpleMotorCtrl( int argc, char **argv) throw (AOException) : SimpleMotorCtrl( argc, argv)
40 {
41  control_mode = CMODE_REMOTE;
42  return;
43 }
44 
46 {
47  //Add the normal motor variables to RTDB
48  SimpleMotorCtrl::SetupVars();
49 
50  //Add the control mode variables to RTDB.
51  try
52  {
53  var_cmode_cur = RTDBvar( MyFullName(), "ConMode", CUR_VAR, INT_VARIABLE, 1,1);
55  var_cmode_req = RTDBvar( MyFullName(), "ConMode", REQ_VAR, INT_VARIABLE, 1,1);
56  }
57  catch (AOVarException &e)
58  {
59  Logger::get()->log(Logger::LOG_LEV_FATAL, "%s:%s: %s", __FILE__, __LINE__, e.what().c_str());
60  throw AOException("Error creating RTDB variables");
61  }
62 
63  //Reset the RTDB notifier to our handler.
64  this->Notify( var_cmode_req, ((VisAOSimpleMotorCtrl *)this)->CModeReqChanged);
65 
67 }
68 
70 {
71  SimpleMotor *motor = NULL;
72 
73  if ((string)ConfigDictionary()["MotorType"] == string("VisAOfilterwheel"))
74  {
75  //std::cout << "Creating VisAOfilterwheel\n";
76  _logger->log( Logger::LOG_LEV_DEBUG, "This is a VisAO filterwheel");
77  motor = new VisAOFilterWheel( this, ConfigDictionary());
78 
79  //After motor is created, re-Notify so the VisAO version of PosReqChange gets called
80  Notify( *(((VisAOFilterWheel *)motor)->get_var_pos_req()), VisAO_PosReqChanged);
81  Notify( ((VisAOFilterWheel *)motor)->var_pos_local_req, VisAO_PosLocalReqChanged);
82  Notify( ((VisAOFilterWheel *)motor)->var_pos_script_req, VisAO_PosScriptReqChanged);
83 
84  }
85  else motor = SimpleMotorCtrl::CreateMotor();
86 
87  return motor;
88 }
89 
90 
92 {
93  double pos = motor->GetPosition( force); // This will update the POS.CUR value too.
94 
95  std::string customPos = GetPosName(pos);
96 
97  motor->SetPosName(customPos);
98 }
99 
100 std::string VisAOSimpleMotorCtrl::GetPosName(double pos)
101 {
102  double np, ul, ll;
103  std::string name;
104 
105  pos = fmod(pos, _customPos.size());
106  if(pos < 0) pos += _customPos.size();
107 
108  cpos_it = _customPos.begin();
109  while(cpos_it != _customPos.end())
110  {
111  name = (*cpos_it).first;
112  np = (*cpos_it).second;
113  ll = fmod(np-.25, _customPos.size());
114  if(ll < 0) ll += _customPos.size();
115  ul = fmod(np+.25, _customPos.size());
116  if(ul < 0) ul += _customPos.size();
117  if((pos > ll && pos < ul) || (ul-ll < 0 && (pos < ul || pos > ll)))
118  {
119  return name;
120  }
121 
122  cpos_it++;
123  }
124 
125  return "intermediate";
126 }
127 
129 {
130 
131  double np, ul, ll;
132  int type;
133 
134  pos = fmod(pos, _customTypes.size());
135  if(pos < 0) pos += _customTypes.size();
136 
137  ctype_it = _customTypes.begin();
138  while(ctype_it != _customTypes.end())
139  {
140  type = (*ctype_it).first;
141  np = (*ctype_it).second;
142  ll = fmod(np-.25, _customTypes.size());
143  if(ll < 0) ll += _customTypes.size();
144  ul = fmod(np+.25, _customTypes.size());
145  if(ul < 0) ul += _customTypes.size();
146  if((pos > ll && pos < ul) || (ul-ll < 0 && (pos < ul || pos > ll)))
147  {
148  //std::cout << type << "\n";
149  return type;
150  break;
151  }
152 
153  ctype_it++;
154  }
155 
156  return 0;
157 }
158 
160 {
161  return request_control(cmode, 0);
162 }
163 
164 int VisAOSimpleMotorCtrl::request_control(int cmode, bool override)
165 {
166  //std::cout << "Requesting control\n";
167 
168 
169  if(cmode >= control_mode || override || cmode == CMODE_NONE)
170  {
171  control_mode = cmode;
172 
173  var_cmode_cur.Set(control_mode, 0);
174  return control_mode;
175  }
176  else return -1;
177 }
178 
179 
180 
181 int VisAOSimpleMotorCtrl::CModeReqChanged(void *pt, Variable *msgb)
182 {
183 
184  int newstate;
185 
186  //std::cout << "In CModeReqChanged\n";
188 
189  /*** Don't lock threadMutex here, it is done in request_control ****/
190 
191  newstate = msgb->Value.Lv[0];
192 
193  int cmode;
194  bool orride = false;
195  switch(newstate)
196  {
197  case 1:
198  cmode = CMODE_REMOTE;
199  break;
200  case 10:
201  cmode = CMODE_REMOTE;
202  orride = true;
203  break;
204  case 2:
205  cmode = CMODE_LOCAL;
206  break;
207  case 20:
208  cmode = CMODE_LOCAL;
209  orride = true;
210  break;
211  case 3:
212  cmode = CMODE_SCRIPT;
213  break;
214  case 30:
215  cmode = CMODE_SCRIPT;
216  orride = true;
217  break;
218  default:
219  cmode = CMODE_NONE;
220  break;
221  }
222 
223  return vmc->request_control(cmode, orride);
224 
225 
226 
227 }
228 
229 int VisAOSimpleMotorCtrl::VisAO_PosReqChanged( void *pt, Variable *var)
230 {
231  int stat;
232  double pos;
233 
234  //std::cout << "In Remote version of PosReqChanged\n";
235  _logger->log(Logger::LOG_LEV_TRACE, "In Remote version of PosReqChanged");
236 
237  /***** Don't lock the mutex! it will get locked by PosReqChanged *******/
238 
239  pos = var->Value.Dv[0];
240 
241  //First check if control type supports, or if it is an abort
242  if(control_mode == CMODE_REMOTE || pos == ((VisAOSimpleMotorCtrl*)pt)->getAbortPos())
243  {
244  try
245  {
246  stat = SimpleMotorCtrl::PosReqChanged(pt, var);
247  }
248  catch(AOVarException &e)
249  {
250  _logger->log(Logger::LOG_LEV_ERROR, "Exception in %s %i: %s", __FILE__, __LINE__, e.what().c_str());
251  pthread_mutex_unlock(&threadMutex);
252  stat = -1;
253  }
254 
255  }
256  else
257  {
258  stat = NO_ERROR; //Otherwise do nothing, and that's ok.
259  }
260 
261  return stat;
262 }
263 
265 {
266  int stat;
267  double pos;
268  //std::cout << "In Local version of PosReqChanged\n";
269 
270  _logger->log(Logger::LOG_LEV_TRACE, "In Local version of PosReqChanged");
271 
272  /***** Don't lock the mutex! it will get locked by PosReqChanged *******/
273 
274  pos = var->Value.Dv[0];
275 
276  //First check if control type supports, or if it is an abort
277  if(control_mode == CMODE_LOCAL || pos == ((VisAOSimpleMotorCtrl*)pt)->getAbortPos())
278  {
279  try
280  {
281  stat = SimpleMotorCtrl::PosReqChanged(pt, var);
282  }
283  catch(AOVarException &e)
284  {
285  _logger->log(Logger::LOG_LEV_ERROR, "Exception in %s %i: %s", __FILE__, __LINE__, e.what().c_str());
286  pthread_mutex_unlock(&threadMutex);
287  stat = -1;
288  }
289 
290  }
291  else
292  {
293  stat = NO_ERROR; //Otherwise do nothing, and that's ok.
294  }
295 
296  return stat;
297 }
298 
300 {
301  int stat;
302  double pos;
303 
304  //std::cout << "In Script version of PosReqChanged\n";
305  _logger->log(Logger::LOG_LEV_TRACE, "In Script version of PosReqChanged");
306 
307  /***** Don't lock the mutex! it will get locked by PosReqChanged *******/
308 
309  pos = var->Value.Dv[0];
310 
311  //First check if control type supports, or if it is an abort
312  if(control_mode == CMODE_SCRIPT || pos == ((VisAOSimpleMotorCtrl*)pt)->getAbortPos())
313  {
314  try
315  {
316  stat = SimpleMotorCtrl::PosReqChanged(pt, var);
317  }
318  catch(AOVarException &e)
319  {
320  _logger->log(Logger::LOG_LEV_ERROR, "Exception in %s %i: %s", __FILE__, __LINE__, e.what().c_str());
321  pthread_mutex_unlock(&threadMutex);
322  stat = -1;
323  }
324 
325  }
326  else
327  {
328  stat = NO_ERROR; //Otherwise do nothing, and that's ok.
329  }
330 
331  return stat;
332 }
333 
334 } //namespace VisAO
335 
map< std::string, float >::iterator cpos_it
An iterator for accessing the custom pos list.
multimap< int, float > _customTypes
Custom filter types list.
static int control_mode
The current control mode.
virtual SimpleMotor * CreateMotor()
Overridden to test if this is a VisAOFilterWheel.
double getAbortPos()
Exposes the abort Position (_abortPos) for use in PosReqChanged (which is static) ...
static int CModeReqChanged(void *pt, Variable *msgb)
RTDB handler for a control mode change request from the AO Supervisor.
pthread_mutex_t threadMutex
Mutex to lock out other threads during complex operations.
virtual void updatePos(bool force=false)
Update position variables in RTDB.
static int VisAO_PosLocalReqChanged(void *pt, Variable *msgb)
RTDB handler for a position change by the AO Supervisor.
static int VisAO_PosReqChanged(void *pt, Variable *msgb)
RTDB handler for a position change by the AO Supervisor.
int GetType(double pos)
Get the position type from the wheel position.
static int VisAO_PosScriptReqChanged(void *pt, Variable *msgb)
RTDB handler for a position change by the AO Supervisor.
RTDBvar var_cmode_req
The requested control mode in the RTDB.
std::string GetPosName(double pos)
Get the position name from wheel position.
A class to provide VisAO functionality to the adopt motor controller.
pthread_mutex_t threadMutex
Mutex to lock out other threads during complex operations.
Definition: VisAOIClient.cpp:8
void SetupVars()
Virtual function to setup variables in RTDB.
VisAOSimpleMotorCtrl(std::string name, const std::string &conffile)
Standard adopt style config file constructor.
multimap< int, float >::iterator ctype_it
An iterator for accessing the custom pos list.
virtual int request_control(int cmode)
Calls request_control(cmode, 0).
Declarations for the VisAOSimpleMotorCtrl class.
The namespace of VisAO software.
RTDBvar var_cmode_cur
The current control mode in the RTDB.
static int default_control_mode
The default control mode.