MotionProxyHelper
Helper library for accessing motors Tango devices
MotionProxyHelper.h
1 #ifndef _MotionProxyHelper_H_
2 #define _MotionProxyHelper_H_
3 // ============================================================================
4 //
5 // = CONTEXT
6 // Utility class - Implementation of a class encapsulating creation and control
7 // of a Tango device proxy to a standard soleil motor
8 //
9 // = File
10 // MotionProxyHelper.h
11 //
12 // = AUTHOR
13 // Arafat NOUREDDINE - Synchrotron SOLEIL
14 // F. Langlois - Synchrotron SOLEIL
15 //
16 // ============================================================================
17 #include <tango.h>
18 #include <TangoExceptionsHelper.h>
19 #include <DeviceProxyHelper.h>
20 #include <yat/threading/Mutex.h>
21 
22 using namespace std;
23 namespace mph
24 {
25 
26 class MotionProxyHelper : public Tango::LogAdapter
27 {
28 public:
29 
30  //- ctor
31  MotionProxyHelper( const string sProxyName,
32  const string sDescription,
33  Tango::DeviceImpl * _device)
34  throw (Tango::DevFailed) ;
35 
36  //- ctor
37  MotionProxyHelper( const string sProxyName,
38  const string sDescription,
39  const string sAttributePositionName ,
40  const string sCommandStateName,
41  const string sCommandStopName,
42  Tango::DeviceImpl * _device)
43  throw (Tango::DevFailed) ;
44 
45  //- dtor
46  virtual ~MotionProxyHelper();
47 
48  //- Move motor to a new position
49  virtual void Move(double)
50  throw (Tango::DevFailed);
51 
52  //- Stop the motor
53  virtual void Stop(void)
54  throw (Tango::DevFailed);
55 
56  //- Forward the motor
57  virtual void Forward(void)
58  throw (Tango::DevFailed);
59 
60  //- Backward the motor
61  virtual void Backward(void)
62  throw (Tango::DevFailed);
63 
64  //- Read the read part of the attribute "attributePositionName" associated to the motor (call read_attribute)
65  virtual double Read(void)
66  throw (Tango::DevFailed);
67 
68  //- Read the write part of the attribute "attributePositionName" associated to the motor (call read_attribute_w)
69  virtual double ReadW(void)
70  throw (Tango::DevFailed);
71 
72  //- Read the the attribute "attributeBLSWName" associated to the motor (call read_attribute)
73  bool ReadBLSW(void)
74  throw (Tango::DevFailed);
75 
76  //- Read the the attribute "attributeFLSWName" associated to the motor (call read_attribute)
77  bool ReadFLSW(void)
78  throw (Tango::DevFailed);
79 
80  //- Read the state of the motor
81  virtual Tango::DevState State(void)
82  throw (Tango::DevFailed);
83 
84  //- Read the status of the motor
85  string Status(void)
86  throw (Tango::DevFailed);
87 
88  void AllowMove(const bool bAllowMove);
89 
90  //---------------------------------------------------------------------------------------------------------------
91  // generic methods, intented to manipulate any command/attribute
92  //---------------------------------------------------------------------------------------------------------------
93 
94  //- Execute any simple command without arg
95  void Command(const string & cmd)
96  throw (Tango::DevFailed);
97 
98  //- Execute any simple command with arg
99  template <typename T>
100  void Command(const string & cmd, T value)
101  throw (Tango::DevFailed);
102 
103  //- Write on any attribute
104  template <typename T>
105  void WriteAttribute(const string & cmd, T value)
106  throw (Tango::DevFailed);
107 
108  //- Read any attribute
109  template <typename T>
110  T ReadAttribute(const string & cmd)
111  throw (Tango::DevFailed);
112 
113 
114 protected:
115  Tango::DeviceProxyHelper* _mProxy;
116  string _sProxyName;
117  string _sDescription;
118  string _sAttributePositionName;
119  string _sAttributeBLSWName;
120  string _sAttributeFLSWName;
121  string _sCommandStateName;
122  string _sCommandStatusName;
123  string _sCommandStopName;
124  string _sCommandForwardName;
125  string _sCommandBackwardName;
126 private:
127  bool _bAllowMove;
128  yat::Mutex _lock;
129 
130 };
131 
133 //-
135 template <typename T>
136 void MotionProxyHelper::Command(const string & cmd_name, T arg)
137 throw (Tango::DevFailed)
138 {
139  DEBUG_STREAM << "MotionProxyHelper::Command("<<cmd_name<<","<<arg<<") [ "<< _sDescription<< " ] entering... " << endl;
140  try
141  {
142  if(_mProxy!=0)
143  _mProxy->command_in(cmd_name.c_str(),arg);
144  }
145  catch (Tango::DevFailed& e)
146  {
147  //ERROR_STREAM << e << ENDLOG;
148  TangoSys_OMemStream o;
149  o << "Unable to execute command "<<cmd_name<<" with arg "<<arg <<" on the device " << _sDescription << " !" << endl;
150  Tango::Except::re_throw_exception
151  ( e,
152  static_cast<const char*>("TANGO_DEVICE_ERROR"),
153  static_cast<const char*>(o.str().c_str()),
154  static_cast<const char*>("MotionProxyHelper::Command()")
155  );
156  }
157  catch(...)
158  {
159  //ERROR_STREAM << "Unknown Exception" << ENDLOG;
160  TangoSys_OMemStream o;
161  o << "Unable to execute command "<<cmd_name<<" with arg "<<arg <<" on the device " << _sDescription << " !" << endl;
162  Tango::Except::throw_exception
163  (
164  static_cast<const char*>("UNKNOWN_ERROR"),
165  static_cast<const char*>(o.str().c_str()),
166  static_cast<const char*>("MotionProxyHelper::Command()")
167  );
168  }
169 }
170 
172 //-
174 template <typename T>
175 void MotionProxyHelper::WriteAttribute(const string & attr_name, T value)
176 throw (Tango::DevFailed)
177 {
178  DEBUG_STREAM << "MotionProxyHelper::WriteAttribute("<<attr_name<<","<<value<<") [ "<< _sDescription<< " ] entering... " << endl;
179  try
180  {
181  if(_mProxy!=0)
182  _mProxy->write_attribute(attr_name.c_str(),value);
183  }
184  catch (Tango::DevFailed& e)
185  {
186  //ERROR_STREAM << e << ENDLOG;
187  TangoSys_OMemStream o;
188  o << "Unable to write "<<value<<" on "<<attr_name <<"attribute of the device " << _sDescription << " !" << endl;
189  Tango::Except::re_throw_exception
190  ( e,
191  static_cast<const char*>("TANGO_DEVICE_ERROR"),
192  static_cast<const char*>(o.str().c_str()),
193  static_cast<const char*>("MotionProxyHelper::WriteAttribute()")
194  );
195  }
196  catch(...)
197  {
198  //ERROR_STREAM << "Unknown Exception" << ENDLOG;
199  TangoSys_OMemStream o;
200  o << "Unable to write "<<value<<" on "<<attr_name <<" attribute of the device " << _sDescription << " !" << endl;
201  Tango::Except::throw_exception
202  (
203  static_cast<const char*>("UNKNOWN_ERROR"),
204  static_cast<const char*>(o.str().c_str()),
205  static_cast<const char*>("MotionProxyHelper::WriteAttribute()")
206  );
207  }
208 }
209 
211 //-
213 template <typename T>
214 T MotionProxyHelper::ReadAttribute(const string & attr_name)
215 throw (Tango::DevFailed)
216 {
217  DEBUG_STREAM << "MotionProxyHelper::ReadAttribute("<<attr_name<<") [ "<< _sDescription<< " ] entering... " << endl;
218  T value = 0.;
219  try
220  {
221  if(_mProxy!=0)
222  _mProxy->read_attribute(attr_name.c_str(),value);
223  }
224  catch (Tango::DevFailed& e)
225  {
226  //ERROR_STREAM << e << ENDLOG;
227  TangoSys_OMemStream o;
228  o << "Unable to read "<<attr_name <<"attribute of the device " << _sDescription << " !" << endl;
229  Tango::Except::re_throw_exception
230  ( e,
231  static_cast<const char*>("TANGO_DEVICE_ERROR"),
232  static_cast<const char*>(o.str().c_str()),
233  static_cast<const char*>("MotionProxyHelper::ReadAttribute()")
234  );
235  }
236  catch(...)
237  {
238  //ERROR_STREAM << "Unknown Exception" << ENDLOG;
239  TangoSys_OMemStream o;
240  o << "Unable to read "<<attr_name <<" attribute of the device " << _sDescription << " !" << endl;
241  Tango::Except::throw_exception
242  (
243  static_cast<const char*>("UNKNOWN_ERROR"),
244  static_cast<const char*>(o.str().c_str()),
245  static_cast<const char*>("MotionProxyHelper::ReadAttribute()")
246  );
247  }
248  return value;
249 }
250 }
251 
253 
254 #endif // _MotionProxyHelper_H_
Definition: MotionProxyHelper.h:26
Definition: MotionProxyHelper.cpp:18