* switch the initialized attribut to the config struct
[diffractometer.git] / src / PseudoAxis / PseudoAxis.cpp
blob7158613eeeddf54e185b5adbf78bdc7fb5bbc868
1 //===================================================================
2 //
3 // The following table gives the correspondence
4 // between commands and method name.
5 //
6 // Command name | Method name
7 // ----------------------------------------
8 // State | dev_state()
9 // Status | dev_status()
10 // Stop | stop()
11 // On | on()
12 // MotorOFF | motor_off()
13 // MotorON | motor_on()
14 // MotorInit | motor_init()
15 // ComputeNewOffset | compute_new_offset()
16 // Backward | backward()
17 // Forward | forward()
18 // DefinePosition | define_position()
19 // InitializeReferencePosition | initialize_reference_position()
20 // GetModeParameters | get_mode_parameters()
21 // SetModeParameters | set_mode_parameters()
23 //===================================================================
25 #if defined (WIN32)
26 #include <PseudoAxis.h>
27 #include <PseudoAxisClass.h>
28 #include <PogoHelper.h>
29 #include <tango.h>
30 #include <TangoExceptionsHelper.h>
31 #else
32 #include <tango.h>
33 #include <TangoExceptionsHelper.h>
34 #include <PseudoAxis.h>
35 #include <PseudoAxisClass.h>
36 #include <PogoHelper.h>
37 #endif
39 #include <DeviceProxyHelper.h>
41 #include <hkl/hkl-pseudoaxis.h>
43 #include "macros.h"
44 #include "PseudoAxis.h"
45 #include "PseudoAxisClass.h"
46 #include "TangoHKLAdapterFactory.h"
48 namespace PseudoAxis_ns
51 //+----------------------------------------------------------------------------
53 // method : PseudoAxis::PseudoAxis(string &s)
54 //
55 // description : constructor for simulated PseudoAxis
57 // in : - cl : Pointer to the DeviceClass object
58 // - s : Device name
60 //-----------------------------------------------------------------------------
61 PseudoAxis::PseudoAxis(Tango::DeviceClass *cl,string &s)
62 :Tango::Device_3Impl(cl,s.c_str())
64 init_device();
67 PseudoAxis::PseudoAxis(Tango::DeviceClass *cl,const char *s)
68 :Tango::Device_3Impl(cl,s)
70 init_device();
73 PseudoAxis::PseudoAxis(Tango::DeviceClass *cl,const char *s,const char *d)
74 :Tango::Device_3Impl(cl,s,d)
76 init_device();
78 //+----------------------------------------------------------------------------
80 // method : PseudoAxis::delete_device()
81 //
82 // description : will be called at device destruction or at init command.
84 //-----------------------------------------------------------------------------
85 void PseudoAxis::delete_device()
87 DEBUG_STREAM << "PseudoAxis::delete_device() entering... " << std::endl;
89 DELETE_SCALAR_ATTRIBUTE(attr_position_read);
90 DELETE_SCALAR_ATTRIBUTE(attr_offset_read);
92 _buffer = NULL;
93 _hklAdapter = NULL;
96 //+----------------------------------------------------------------------------
98 // method : PseudoAxis::init_device()
99 //
100 // description : will be called at device initialization.
102 //-----------------------------------------------------------------------------
103 void PseudoAxis::init_device()
105 DEBUG_STREAM << "PseudoAxis::init_device() entering... " << std::endl;
107 // Initialise variables to default values
108 //--------------------------------------------
109 this->get_device_property();
111 CREATE_SCALAR_ATTRIBUTE(attr_position_read);
112 CREATE_SCALAR_ATTRIBUTE(attr_offset_read);
114 _buffer = NULL;
115 _hklAdapter = NULL;
117 // initialize the config
118 _config.mode = "";
119 _config.initialized = false;
122 //+----------------------------------------------------------------------------
124 // method : PseudoAxis::get_device_property()
126 // description : Read the device properties from database.
128 //-----------------------------------------------------------------------------
129 void PseudoAxis::get_device_property()
131 DEBUG_STREAM << "PseudoAxis::get_device_property() entering... " << std::endl;
133 // Initialize your default values here (if not done with POGO).
134 //------------------------------------------------------------------
136 // Read device properties from database.(Automatic code generation)
137 //------------------------------------------------------------------
138 Tango::DbData dev_prop;
139 dev_prop.push_back(Tango::DbDatum("DiffractometerProxy"));
140 dev_prop.push_back(Tango::DbDatum("PseudoAxisName"));
142 // Call database and extract values
143 //--------------------------------------------
144 if (Tango::Util::instance()->_UseDb==true)
145 get_db_device()->get_property(dev_prop);
146 Tango::DbDatum def_prop, cl_prop;
147 PseudoAxisClass *ds_class =
148 (static_cast<PseudoAxisClass *>(get_device_class()));
149 int i = -1;
151 // Try to initialize DiffractometerProxy from class property
152 cl_prop = ds_class->get_class_property(dev_prop[++i].name);
153 if (cl_prop.is_empty()==false) cl_prop >> diffractometerProxy;
154 else {
155 // Try to initialize DiffractometerProxy from default device value
156 def_prop = ds_class->get_default_device_property(dev_prop[i].name);
157 if (def_prop.is_empty()==false) def_prop >> diffractometerProxy;
159 // And try to extract DiffractometerProxy value from database
160 if (dev_prop[i].is_empty()==false) dev_prop[i] >> diffractometerProxy;
162 // Try to initialize PseudoAxisName from class property
163 cl_prop = ds_class->get_class_property(dev_prop[++i].name);
164 if (cl_prop.is_empty()==false) cl_prop >> pseudoAxisName;
165 else {
166 // Try to initialize PseudoAxisName from default device value
167 def_prop = ds_class->get_default_device_property(dev_prop[i].name);
168 if (def_prop.is_empty()==false) def_prop >> pseudoAxisName;
170 // And try to extract PseudoAxisName value from database
171 if (dev_prop[i].is_empty()==false) dev_prop[i] >> pseudoAxisName;
175 // End of Automatic code generation
176 //------------------------------------------------------------------
181 //+------------------------------------------------------------------
183 * method: PseudoAxe::dev_state
185 * description: method to execute "State"
186 * This command gets the device state (stored in its <i>device_state</i> data member) and returns it to the caller.
188 * @return State Code
190 * TODO to Adapter
193 //+------------------------------------------------------------------
194 Tango::DevState PseudoAxis::dev_state()
196 DEBUG_STREAM << "PseudoAxis::dev_state(): entering... !" << endl;
198 Tango::DevState state;
199 std::string status;
201 if (!_hklAdapter || !_buffer)
202 state = Tango::FAULT;
203 else {
204 _hklAdapter->update();
205 state = _buffer->get_state();
206 status = _buffer->get_status();
208 this->set_state(state);
209 this->set_status(status);
210 return this->device_state;
213 //+------------------------------------------------------------------
215 * method: PseudoAxe::dev_status
217 * description: method to execute "Status"
218 * This command gets the device state (stored in its <i>device_state</i> data member) and returns it to the caller.
220 * @return State Code
222 * TODO to Adapter
225 //+------------------------------------------------------------------
226 Tango::ConstDevString PseudoAxis::dev_status()
228 DEBUG_STREAM << "PseudoAxis::dev_status(): entering... !" << endl;
230 if (!_hklAdapter) {
231 this->device_state = Tango::FAULT;
232 if (diffractometerProxy == "")
233 this->device_status = "Please set a correct \"diffractometerProxy\" propertie";
234 else {
235 this->device_status = "The Diffractometer device " + diffractometerProxy + " is not ready or do not existe!\n";
236 this->device_status += "Please start the Diffractometer or check the \"diffractometerProxy\" Propertie";
238 } else {
239 if(!_buffer) {
240 this->device_state = Tango::FAULT;
241 if (pseudoAxisName == "")
242 this->device_status = "Please set a correct \"pseudoAxisName\" : ";
243 else {
244 this->device_status = "Cannot find the pseudoAxe \"" + pseudoAxisName + "\" in the " + diffractometerProxy + " Diffractometer device : ";
246 this->device_status += "Set \"pseudoAxisName\" propertie with one of these possible values : ";
247 std::vector<std::string> const names = _hklAdapter->pseudo_axis_get_names();
248 std::vector<std::string>::const_iterator iter = names.begin();
249 std::vector<std::string>::const_iterator end = names.end();
250 this->device_status += " " + *iter;
251 ++iter;
252 while(iter != end) {
253 this->device_status += ", " + *iter;
254 ++iter;
256 } else {
259 _hklAdapter->update();
260 this->device_state = _buffer->get_state();
261 this->device_status = _buffer->get_status();
263 catch (...)
265 this->device_state = Tango::FAULT;
266 this->device_status = " One motor Device does not respond ";
270 return this->device_status.c_str();
273 //+----------------------------------------------------------------------------
275 // method : PseudoAxis::always_executed_hook()
277 // description : method always executed before any command is executed
279 //-----------------------------------------------------------------------------
280 void PseudoAxis::always_executed_hook()
282 DEBUG_STREAM << "PseudoAxis::always_executed_hook() entering... " << std::endl;
283 // here we are looking for the intance of the related Diffractometer to get the right hkladapter instance.
284 if(!_hklAdapter)
285 _hklAdapter = Diffractometer_ns::TangoHKLAdapterFactory::instance()->get_diffractometer_adapter(diffractometerProxy);
287 if(_hklAdapter && !_buffer)
288 _buffer = _hklAdapter->pseudo_axis_buffer_new(pseudoAxisName.c_str());
291 //+----------------------------------------------------------------------------
293 // method : PseudoAxis::read_attr_hardware
295 // description : Hardware acquisition for attributes.
297 //-----------------------------------------------------------------------------
298 void PseudoAxis::read_attr_hardware(vector<long> &attr_list)
300 DEBUG_STREAM << "PseudoAxis::read_attr_hardware(vector<long> &attr_list) entering... "<< endl;
301 // Add your own code here
302 if(_buffer)
303 _config = _buffer->get_config();
305 //+----------------------------------------------------------------------------
307 // method : PseudoAxis::read_initialized
309 // description : Extract real attribute values for initialized acquisition result.
311 //-----------------------------------------------------------------------------
312 void PseudoAxis::read_initialized(Tango::Attribute &attr)
314 DEBUG_STREAM << "PseudoAxis::read_initialized(Tango::Attribute &attr) entering... "<< endl;
315 // Add your own code here
317 if(_buffer)
318 attr.set_value(&_config.initialized);
321 //+----------------------------------------------------------------------------
323 // method : PseudoAxis::write_initialized
325 // description : Write initialized attribute values to hardware.
327 //-----------------------------------------------------------------------------
328 void PseudoAxis::write_initialized(Tango::WAttribute &attr)
330 DEBUG_STREAM << "PseudoAxis::write_initialized(Tango::WAttribute &attr) entering... "<< endl;
331 attr.get_write_value(attr_initialized_write);
332 if(_buffer)
333 _buffer->set_initialized(attr_initialized_write);
335 // need to after the internals before initializing the pseudo
336 if (_hklAdapter)
337 _hklAdapter->update();
340 //+----------------------------------------------------------------------------
342 // method : PseudoAxis::read_mode
344 // description : Extract real attribute values for mode acquisition result.
346 //-----------------------------------------------------------------------------
347 void PseudoAxis::read_mode(Tango::Attribute &attr)
349 DEBUG_STREAM << "PseudoAxis::read_mode(Tango::Attribute &attr) entering... "<< endl;
351 if(_buffer)
352 attr.set_value(&_config.mode);
355 //+----------------------------------------------------------------------------
357 // method : PseudoAxis::write_mode
359 // description : Write mode attribute values to hardware.
361 //-----------------------------------------------------------------------------
362 void PseudoAxis::write_mode(Tango::WAttribute &attr)
364 DEBUG_STREAM << "PseudoAxis::write_mode(Tango::WAttribute &attr) entering... "<< endl;
365 attr.get_write_value(attr_mode_write);
366 if(_buffer)
367 _buffer->set_mode(attr_mode_write);
370 //+----------------------------------------------------------------------------
372 // method : PseudoAxis::read_modeNames
374 // description : Extract real attribute values for modeNames acquisition result.
376 //-----------------------------------------------------------------------------
377 void PseudoAxis::read_modeNames(Tango::Attribute &attr)
379 DEBUG_STREAM << "PseudoAxis::read_modeNames(Tango::Attribute &attr) entering... "<< endl;
380 if(_buffer){
381 Matrix<char *> const & img = _buffer->get_mode_names();
382 attr.set_value(img.data, img.xdim);
386 //+----------------------------------------------------------------------------
388 // method : PseudoAxis::write_IsInitialised
390 // description : Write IsInitialised attribute values to hardware.
392 //-----------------------------------------------------------------------------
393 void PseudoAxis::write_IsInitialised(Tango::WAttribute &attr)
395 DEBUG_STREAM << "PseudoAxis::write_IsInitialised(Tango::WAttribute &attr) entering... "<< endl;
396 // Add your own code here
397 attr.get_write_value(attr_IsInitialised_write);
398 if(_buffer)
399 _buffer->set_initialized(attr_IsInitialised_write);
401 // need to after the internals before initializing the pseudo
402 if (_hklAdapter)
403 _hklAdapter->update();
407 //+----------------------------------------------------------------------------
409 // method : PseudoAxis::read_position
411 // description : Extract real attribute values for position acquisition result.
413 //-----------------------------------------------------------------------------
414 void PseudoAxis::read_position(Tango::Attribute &attr)
416 DEBUG_STREAM << "PseudoAxis::read_position(Tango::Attribute &attr) entering... "<< endl;
417 // Add your own code here
419 if (_hklAdapter && _buffer) {
420 _hklAdapter->update();
422 // first the read part
423 *attr_position_read = _buffer->get_read();
424 *attr_position_read += *attr_offset_read;
425 // second the write part
426 attr_position_write = _buffer->get_write();
427 attr_position_write += *attr_offset_read;
428 // now use Tango to aknowledge for the write part.
429 Tango::WAttribute & att = dev_attr->get_w_attr_by_name(attr.get_name().c_str());
430 att.set_write_value(attr_position_write);
432 attr.set_value(attr_position_read);
435 //+----------------------------------------------------------------------------
437 // method : PseudoAxis::write_position
439 // description : Write position attribute values to hardware.
441 //-----------------------------------------------------------------------------
442 void PseudoAxis::write_position(Tango::WAttribute &attr)
444 DEBUG_STREAM << "PseudoAxis::write_position(Tango::WAttribute &attr) entering... "<< endl;
445 // Add your own code here
447 attr.get_write_value(attr_position_write);
448 if (_hklAdapter && _buffer)
449 _hklAdapter->pseudo_axis_write(_buffer, attr_position_write - *attr_offset_read);
452 //+----------------------------------------------------------------------------
454 // method : PseudoAxis::read_offset
456 // description : Extract real attribute values for offset acquisition result.
458 //-----------------------------------------------------------------------------
459 void PseudoAxis::read_offset(Tango::Attribute &attr)
461 DEBUG_STREAM << "PseudoAxis::read_offset(Tango::Attribute &attr) entering... "<< endl;
462 // Add your own code here
463 attr.set_value(attr_offset_read);
466 //+----------------------------------------------------------------------------
468 // method : PseudoAxis::write_offset
470 // description : Write offset attribute values to hardware.
472 //-----------------------------------------------------------------------------
473 void PseudoAxis::write_offset(Tango::WAttribute &attr)
475 DEBUG_STREAM << "PseudoAxis::write_offset(Tango::WAttribute &attr) entering... "<< endl;
476 // Add your own code here
477 attr.get_write_value(attr_offset_write);
478 *attr_offset_read = attr_offset_write;
481 //+----------------------------------------------------------------------------
483 // method : PseudoAxis::read_relativeMove
485 // description : Extract real attribute values for relativeMove acquisition result.
487 //-----------------------------------------------------------------------------
488 void PseudoAxis::read_relativeMove(Tango::Attribute &attr)
490 DEBUG_STREAM << "PseudoAxis::read_relativeMove(Tango::Attribute &attr) entering... "<< endl;
491 // Add your own code here
492 attr.set_value(&attr_relativeMove_write);
495 //+----------------------------------------------------------------------------
497 // method : PseudoAxis::write_relativeMove
499 // description : Write relativeMove attribute values to hardware.
501 //-----------------------------------------------------------------------------
502 void PseudoAxis::write_relativeMove(Tango::WAttribute &attr)
504 DEBUG_STREAM << "PseudoAxis::write_relativeMove(Tango::WAttribute &attr) entering... "<< endl;
505 // Add your own code here
506 attr.get_write_value(attr_relativeMove_write);
507 double new_position = *attr_position_read + attr_relativeMove_write;
509 // Write to the "position" attribute to move the axe.
510 Tango::DeviceProxyHelper moi(this->device_name, this);
511 moi.write_attribute("position", new_position);
515 //+----------------------------------------------------------------------------
517 // method : PseudoAxis::read_IsInitialised
519 // description : Extract real attribute values for IsInitialised acquisition result.
521 //-----------------------------------------------------------------------------
522 void PseudoAxis::read_IsInitialised(Tango::Attribute &attr)
524 DEBUG_STREAM << "PseudoAxis::read_IsInitialised(Tango::Attribute &attr) entering... "<< endl;
525 // Add your own code here
527 if(_buffer)
528 attr.set_value(&_config.initialized);
531 //+------------------------------------------------------------------
533 * method: PseudoAxis::motor_off
535 * description: method to execute "MotorOFF"
536 * DEPRECATED
540 //+------------------------------------------------------------------
541 void PseudoAxis::motor_off()
543 DEBUG_STREAM << "PseudoAxis::motor_off(): entering... !" << endl;
545 // Add your own code to control device here
548 //+------------------------------------------------------------------
550 * method: PseudoAxis::motor_on
552 * description: method to execute "MotorON"
553 * DEPRECATED
557 //+------------------------------------------------------------------
558 void PseudoAxis::motor_on()
560 DEBUG_STREAM << "PseudoAxis::motor_on(): entering... !" << endl;
562 // Add your own code to control device here
563 if(_buffer)
564 _buffer->on();
567 //+------------------------------------------------------------------
569 * method: PseudoAxis::compute_new_offset
571 * description: method to execute "ComputeNewOffset"
572 * The so smart program computes the offset to goal the user position.
574 * @param argin The new user position
577 //+------------------------------------------------------------------
578 void PseudoAxis::compute_new_offset(Tango::DevDouble argin)
580 DEBUG_STREAM << "PseudoAxis::compute_new_offset(): entering... !" << endl;
582 // Add your own code to control device here
583 if (_hklAdapter && _buffer) {
584 _hklAdapter->update();
585 Tango::DeviceProxyHelper moi(this->device_name, this);
586 moi.write_attribute("offset", argin - _buffer->get_read());
590 //+------------------------------------------------------------------
592 * method: PseudoAxis::stop
594 * description: method to execute "Stop"
595 * Stop the pseudoAxes and all related axes.
599 //+------------------------------------------------------------------
600 void PseudoAxis::stop()
602 DEBUG_STREAM << "PseudoAxis::stop(): entering... !" << endl;
604 // Add your own code to control device here
605 // FRED here use the _pseudoAxe->relatedAxes to stop the right axes.
606 _hklAdapter->stop_all_axis();
610 //+------------------------------------------------------------------
612 * method: PseudoAxis::motor_init
614 * description: method to execute "MotorInit"
615 * DEPRECATED
619 //+------------------------------------------------------------------
620 void PseudoAxis::motor_init()
622 DEBUG_STREAM << "PseudoAxis::motor_init(): entering... !" << endl;
624 // DEPRECATED
625 // Add your own code to control device here
626 if (_buffer)
627 _buffer->set_initialized(true);
628 if(_hklAdapter)
629 _hklAdapter->update();
632 //+------------------------------------------------------------------
634 * method: PseudoAxis::backward
636 * description: method to execute "Backward"
637 * dummy command for V2 interface
641 //+------------------------------------------------------------------
642 void PseudoAxis::backward()
644 DEBUG_STREAM << "PseudoAxis::backward(): entering... !" << endl;
646 // Add your own code to control device here
650 //+------------------------------------------------------------------
652 * method: PseudoAxis::forward
654 * description: method to execute "Forward"
655 * dummy command for V2 interface
659 //+------------------------------------------------------------------
660 void PseudoAxis::forward()
662 DEBUG_STREAM << "PseudoAxis::forward(): entering... !" << endl;
664 // Add your own code to control device here
668 //+------------------------------------------------------------------
670 * method: PseudoAxis::define_position
672 * description: method to execute "DefinePosition"
673 * dummy command for V2 interface
675 * @param argin
678 //+------------------------------------------------------------------
679 void PseudoAxis::define_position(Tango::DevDouble argin)
681 DEBUG_STREAM << "PseudoAxis::define_position(): entering... !" << endl;
683 // Add your own code to control device here
687 //+------------------------------------------------------------------
689 * method: PseudoAxis::initialize_reference_position
691 * description: method to execute "InitializeReferencePosition"
692 * dummy command for V2 interface
696 //+------------------------------------------------------------------
697 void PseudoAxis::initialize_reference_position()
699 DEBUG_STREAM << "PseudoAxis::initialize_reference_position(): entering... !" << endl;
701 // Add your own code to control device here
706 //+------------------------------------------------------------------
708 * method: PseudoAxis::get_mode_parameters
710 * description: method to execute "GetModeParameters"
711 * this command return for the current mode, its parameters.
713 * @return parameters names and values
716 //+------------------------------------------------------------------
717 Tango::DevVarDoubleStringArray *PseudoAxis::get_mode_parameters()
719 // POGO has generated a method core with argout allocation.
720 // If you would like to use a static reference without copying,
721 // See "TANGO Device Server Programmer's Manual"
722 // (chapter : Writing a TANGO DS / Exchanging data)
723 //------------------------------------------------------------
724 Tango::DevVarDoubleStringArray *argout = new Tango::DevVarDoubleStringArray();
725 argout->dvalue.length(1);
726 argout->dvalue[0] = 0.0;
727 argout->svalue.length(1);
728 argout->svalue[0] = CORBA::string_dup("dummy");
729 DEBUG_STREAM << "PseudoAxis::get_mode_parameters(): entering... !" << endl;
731 // Add your own code to control device here
732 if(_buffer)
733 _buffer->get_mode_parameters(argout);
735 return argout;
738 //+------------------------------------------------------------------
740 * method: PseudoAxis::set_mode_parameters
742 * description: method to execute "SetModeParameters"
744 * @param argin The parameters to set
747 //+------------------------------------------------------------------
748 void PseudoAxis::set_mode_parameters(const Tango::DevVarDoubleStringArray *argin)
750 DEBUG_STREAM << "PseudoAxis::set_mode_parameters(): entering... !" << endl;
752 // Add your own code to control device here
753 if(_buffer)
754 _buffer->set_mode_parameters(argin);
761 //+------------------------------------------------------------------
763 * method: PseudoAxis::on
765 * description: method to execute "On"
766 * Activate the pseudoAxis and all related motors.
770 //+------------------------------------------------------------------
771 void PseudoAxis::on()
773 DEBUG_STREAM << "PseudoAxis::on(): entering... !" << endl;
775 // Add your own code to control device here
776 if(_buffer)
777 _buffer->on();
780 } // namespace