* add a dist-hook to generate the changelog from the git repositoy.
[diffractometer.git] / src / PseudoAxis / PseudoAxis.cpp
blobf8bf7a299198527af7b7999625d62bc54697fe86
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_IsInitialised_read);
91 DELETE_SCALAR_ATTRIBUTE(attr_initialized_read);
92 DELETE_SCALAR_ATTRIBUTE(attr_offset_read);
93 DELETE_DEVSTRING_ATTRIBUTE(attr_mode_read);
95 _buffer = NULL;
96 _hklAdapter = NULL;
99 //+----------------------------------------------------------------------------
101 // method : PseudoAxis::init_device()
103 // description : will be called at device initialization.
105 //-----------------------------------------------------------------------------
106 void PseudoAxis::init_device()
108 DEBUG_STREAM << "PseudoAxis::init_device() entering... " << std::endl;
110 // Initialise variables to default values
111 //--------------------------------------------
112 this->get_device_property();
114 CREATE_SCALAR_ATTRIBUTE(attr_position_read);
115 CREATE_SCALAR_ATTRIBUTE(attr_IsInitialised_read);
116 CREATE_SCALAR_ATTRIBUTE(attr_initialized_read);
117 CREATE_SCALAR_ATTRIBUTE(attr_offset_read);
118 CREATE_DEVSTRING_ATTRIBUTE(attr_mode_read, 1, "");
120 _buffer = NULL;
121 _hklAdapter = NULL;
124 //+----------------------------------------------------------------------------
126 // method : PseudoAxis::get_device_property()
128 // description : Read the device properties from database.
130 //-----------------------------------------------------------------------------
131 void PseudoAxis::get_device_property()
133 DEBUG_STREAM << "PseudoAxis::get_device_property() entering... " << std::endl;
135 // Initialize your default values here (if not done with POGO).
136 //------------------------------------------------------------------
138 // Read device properties from database.(Automatic code generation)
139 //------------------------------------------------------------------
140 Tango::DbData dev_prop;
141 dev_prop.push_back(Tango::DbDatum("DiffractometerProxy"));
142 dev_prop.push_back(Tango::DbDatum("PseudoAxisName"));
144 // Call database and extract values
145 //--------------------------------------------
146 if (Tango::Util::instance()->_UseDb==true)
147 get_db_device()->get_property(dev_prop);
148 Tango::DbDatum def_prop, cl_prop;
149 PseudoAxisClass *ds_class =
150 (static_cast<PseudoAxisClass *>(get_device_class()));
151 int i = -1;
153 // Try to initialize DiffractometerProxy from class property
154 cl_prop = ds_class->get_class_property(dev_prop[++i].name);
155 if (cl_prop.is_empty()==false) cl_prop >> diffractometerProxy;
156 else {
157 // Try to initialize DiffractometerProxy from default device value
158 def_prop = ds_class->get_default_device_property(dev_prop[i].name);
159 if (def_prop.is_empty()==false) def_prop >> diffractometerProxy;
161 // And try to extract DiffractometerProxy value from database
162 if (dev_prop[i].is_empty()==false) dev_prop[i] >> diffractometerProxy;
164 // Try to initialize PseudoAxisName from class property
165 cl_prop = ds_class->get_class_property(dev_prop[++i].name);
166 if (cl_prop.is_empty()==false) cl_prop >> pseudoAxisName;
167 else {
168 // Try to initialize PseudoAxisName from default device value
169 def_prop = ds_class->get_default_device_property(dev_prop[i].name);
170 if (def_prop.is_empty()==false) def_prop >> pseudoAxisName;
172 // And try to extract PseudoAxisName value from database
173 if (dev_prop[i].is_empty()==false) dev_prop[i] >> pseudoAxisName;
177 // End of Automatic code generation
178 //------------------------------------------------------------------
183 //+------------------------------------------------------------------
185 * method: PseudoAxe::dev_state
187 * description: method to execute "State"
188 * This command gets the device state (stored in its <i>device_state</i> data member) and returns it to the caller.
190 * @return State Code
192 * TODO to Adapter
195 //+------------------------------------------------------------------
196 Tango::DevState PseudoAxis::dev_state()
198 DEBUG_STREAM << "PseudoAxis::dev_state(): entering... !" << endl;
200 Tango::DevState state;
201 std::string status;
203 if (!_hklAdapter || !_buffer)
204 state = Tango::FAULT;
205 else {
206 _hklAdapter->update();
207 state = _buffer->get_state();
208 status = _buffer->get_status();
210 this->set_state(state);
211 this->set_status(status);
212 return this->device_state;
215 //+------------------------------------------------------------------
217 * method: PseudoAxe::dev_status
219 * description: method to execute "Status"
220 * This command gets the device state (stored in its <i>device_state</i> data member) and returns it to the caller.
222 * @return State Code
224 * TODO to Adapter
227 //+------------------------------------------------------------------
228 Tango::ConstDevString PseudoAxis::dev_status()
230 DEBUG_STREAM << "PseudoAxis::dev_status(): entering... !" << endl;
232 if (!_hklAdapter) {
233 this->device_state = Tango::FAULT;
234 if (diffractometerProxy == "")
235 this->device_status = "Please set a correct \"diffractometerProxy\" propertie";
236 else {
237 this->device_status = "The Diffractometer device " + diffractometerProxy + " is not ready or do not existe!\n";
238 this->device_status += "Please start the Diffractometer or check the \"diffractometerProxy\" Propertie";
240 } else {
241 if(!_buffer) {
242 this->device_state = Tango::FAULT;
243 if (pseudoAxisName == "")
244 this->device_status = "Please set a correct \"pseudoAxisName\" : ";
245 else {
246 this->device_status = "Cannot find the pseudoAxe \"" + pseudoAxisName + "\" in the " + diffractometerProxy + " Diffractometer device : ";
248 this->device_status += "Set \"pseudoAxisName\" propertie with one of these possible values : ";
249 std::vector<std::string> const names = _hklAdapter->pseudo_axis_get_names();
250 std::vector<std::string>::const_iterator iter = names.begin();
251 std::vector<std::string>::const_iterator end = names.end();
252 this->device_status += " " + *iter;
253 ++iter;
254 while(iter != end) {
255 this->device_status += ", " + *iter;
256 ++iter;
258 } else {
261 _hklAdapter->update();
262 this->device_state = _buffer->get_state();
263 this->device_status = _buffer->get_status();
265 catch (...)
267 this->device_state = Tango::FAULT;
268 this->device_status = " One motor Device does not respond ";
272 return this->device_status.c_str();
275 //+----------------------------------------------------------------------------
277 // method : PseudoAxis::always_executed_hook()
279 // description : method always executed before any command is executed
281 //-----------------------------------------------------------------------------
282 void PseudoAxis::always_executed_hook()
284 DEBUG_STREAM << "PseudoAxis::always_executed_hook() entering... " << std::endl;
285 // here we are looking for the intance of the related Diffractometer to get the right hkladapter instance.
286 if(!_hklAdapter)
287 _hklAdapter = Diffractometer_ns::TangoHKLAdapterFactory::instance()->get_diffractometer_adapter(diffractometerProxy);
289 if(_hklAdapter && !_buffer)
290 _buffer = _hklAdapter->pseudo_axis_buffer_new(pseudoAxisName.c_str());
293 //+----------------------------------------------------------------------------
295 // method : PseudoAxis::read_attr_hardware
297 // description : Hardware acquisition for attributes.
299 //-----------------------------------------------------------------------------
300 void PseudoAxis::read_attr_hardware(vector<long> &attr_list)
302 DEBUG_STREAM << "PseudoAxis::read_attr_hardware(vector<long> &attr_list) entering... "<< endl;
303 // Add your own code here
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_initialized_read = _buffer->get_initialized();
319 attr.set_value(attr_initialized_read);
322 //+----------------------------------------------------------------------------
324 // method : PseudoAxis::write_initialized
326 // description : Write initialized attribute values to hardware.
328 //-----------------------------------------------------------------------------
329 void PseudoAxis::write_initialized(Tango::WAttribute &attr)
331 DEBUG_STREAM << "PseudoAxis::write_initialized(Tango::WAttribute &attr) entering... "<< endl;
332 attr.get_write_value(attr_initialized_write);
333 if(_buffer)
334 _buffer->set_initialized(attr_initialized_write);
336 // need to after the internals before initializing the pseudo
337 if (_hklAdapter)
338 _hklAdapter->update();
341 //+----------------------------------------------------------------------------
343 // method : PseudoAxis::read_mode
345 // description : Extract real attribute values for mode acquisition result.
347 //-----------------------------------------------------------------------------
348 void PseudoAxis::read_mode(Tango::Attribute &attr)
350 DEBUG_STREAM << "PseudoAxis::read_mode(Tango::Attribute &attr) entering... "<< endl;
352 if(_buffer)
353 attr.set_value(_buffer->get_mode());
356 //+----------------------------------------------------------------------------
358 // method : PseudoAxis::write_mode
360 // description : Write mode attribute values to hardware.
362 //-----------------------------------------------------------------------------
363 void PseudoAxis::write_mode(Tango::WAttribute &attr)
365 DEBUG_STREAM << "PseudoAxis::write_mode(Tango::WAttribute &attr) entering... "<< endl;
366 attr.get_write_value(attr_mode_write);
367 if(_buffer)
368 _buffer->set_mode(attr_mode_write);
371 //+----------------------------------------------------------------------------
373 // method : PseudoAxis::read_modeNames
375 // description : Extract real attribute values for modeNames acquisition result.
377 //-----------------------------------------------------------------------------
378 void PseudoAxis::read_modeNames(Tango::Attribute &attr)
380 DEBUG_STREAM << "PseudoAxis::read_modeNames(Tango::Attribute &attr) entering... "<< endl;
381 if(_buffer){
382 Matrix<char *> const & img = _buffer->get_mode_names();
383 attr.set_value(img.data, img.xdim);
387 //+----------------------------------------------------------------------------
389 // method : PseudoAxis::write_IsInitialised
391 // description : Write IsInitialised attribute values to hardware.
393 //-----------------------------------------------------------------------------
394 void PseudoAxis::write_IsInitialised(Tango::WAttribute &attr)
396 DEBUG_STREAM << "PseudoAxis::write_IsInitialised(Tango::WAttribute &attr) entering... "<< endl;
397 // Add your own code here
398 attr.get_write_value(attr_IsInitialised_write);
399 if(_buffer)
400 _buffer->set_initialized(attr_IsInitialised_write);
402 // need to after the internals before initializing the pseudo
403 if (_hklAdapter)
404 _hklAdapter->update();
408 //+----------------------------------------------------------------------------
410 // method : PseudoAxis::read_position
412 // description : Extract real attribute values for position acquisition result.
414 //-----------------------------------------------------------------------------
415 void PseudoAxis::read_position(Tango::Attribute &attr)
417 DEBUG_STREAM << "PseudoAxis::read_position(Tango::Attribute &attr) entering... "<< endl;
418 // Add your own code here
420 if (_hklAdapter && _buffer) {
421 _hklAdapter->update();
423 // first the read part
424 *attr_position_read = _buffer->get_read();
425 *attr_position_read += *attr_offset_read;
426 // second the write part
427 attr_position_write = _buffer->get_write();
428 attr_position_write += *attr_offset_read;
429 // now use Tango to aknowledge for the write part.
430 Tango::WAttribute & att = dev_attr->get_w_attr_by_name(attr.get_name().c_str());
431 att.set_write_value(attr_position_write);
433 attr.set_value(attr_position_read);
436 //+----------------------------------------------------------------------------
438 // method : PseudoAxis::write_position
440 // description : Write position attribute values to hardware.
442 //-----------------------------------------------------------------------------
443 void PseudoAxis::write_position(Tango::WAttribute &attr)
445 DEBUG_STREAM << "PseudoAxis::write_position(Tango::WAttribute &attr) entering... "<< endl;
446 // Add your own code here
448 attr.get_write_value(attr_position_write);
449 if (_hklAdapter && _buffer)
450 _hklAdapter->pseudo_axis_write(_buffer, attr_position_write - *attr_offset_read);
453 //+----------------------------------------------------------------------------
455 // method : PseudoAxis::read_offset
457 // description : Extract real attribute values for offset acquisition result.
459 //-----------------------------------------------------------------------------
460 void PseudoAxis::read_offset(Tango::Attribute &attr)
462 DEBUG_STREAM << "PseudoAxis::read_offset(Tango::Attribute &attr) entering... "<< endl;
463 // Add your own code here
464 attr.set_value(attr_offset_read);
467 //+----------------------------------------------------------------------------
469 // method : PseudoAxis::write_offset
471 // description : Write offset attribute values to hardware.
473 //-----------------------------------------------------------------------------
474 void PseudoAxis::write_offset(Tango::WAttribute &attr)
476 DEBUG_STREAM << "PseudoAxis::write_offset(Tango::WAttribute &attr) entering... "<< endl;
477 // Add your own code here
478 attr.get_write_value(attr_offset_write);
479 *attr_offset_read = attr_offset_write;
482 //+----------------------------------------------------------------------------
484 // method : PseudoAxis::read_relativeMove
486 // description : Extract real attribute values for relativeMove acquisition result.
488 //-----------------------------------------------------------------------------
489 void PseudoAxis::read_relativeMove(Tango::Attribute &attr)
491 DEBUG_STREAM << "PseudoAxis::read_relativeMove(Tango::Attribute &attr) entering... "<< endl;
492 // Add your own code here
493 attr.set_value(&attr_relativeMove_write);
496 //+----------------------------------------------------------------------------
498 // method : PseudoAxis::write_relativeMove
500 // description : Write relativeMove attribute values to hardware.
502 //-----------------------------------------------------------------------------
503 void PseudoAxis::write_relativeMove(Tango::WAttribute &attr)
505 DEBUG_STREAM << "PseudoAxis::write_relativeMove(Tango::WAttribute &attr) entering... "<< endl;
506 // Add your own code here
507 attr.get_write_value(attr_relativeMove_write);
508 double new_position = *attr_position_read + attr_relativeMove_write;
510 // Write to the "position" attribute to move the axe.
511 Tango::DeviceProxyHelper moi(this->device_name, this);
512 moi.write_attribute("position", new_position);
516 //+----------------------------------------------------------------------------
518 // method : PseudoAxis::read_IsInitialised
520 // description : Extract real attribute values for IsInitialised acquisition result.
522 //-----------------------------------------------------------------------------
523 void PseudoAxis::read_IsInitialised(Tango::Attribute &attr)
525 DEBUG_STREAM << "PseudoAxis::read_IsInitialised(Tango::Attribute &attr) entering... "<< endl;
526 // Add your own code here
528 if(_buffer)
529 *attr_IsInitialised_read = _buffer->get_initialized();
530 attr.set_value(attr_IsInitialised_read);
533 //+------------------------------------------------------------------
535 * method: PseudoAxis::motor_off
537 * description: method to execute "MotorOFF"
538 * DEPRECATED
542 //+------------------------------------------------------------------
543 void PseudoAxis::motor_off()
545 DEBUG_STREAM << "PseudoAxis::motor_off(): entering... !" << endl;
547 // Add your own code to control device here
550 //+------------------------------------------------------------------
552 * method: PseudoAxis::motor_on
554 * description: method to execute "MotorON"
555 * DEPRECATED
559 //+------------------------------------------------------------------
560 void PseudoAxis::motor_on()
562 DEBUG_STREAM << "PseudoAxis::motor_on(): entering... !" << endl;
564 // Add your own code to control device here
565 if(_buffer)
566 _buffer->on();
569 //+------------------------------------------------------------------
571 * method: PseudoAxis::compute_new_offset
573 * description: method to execute "ComputeNewOffset"
574 * The so smart program computes the offset to goal the user position.
576 * @param argin The new user position
579 //+------------------------------------------------------------------
580 void PseudoAxis::compute_new_offset(Tango::DevDouble argin)
582 DEBUG_STREAM << "PseudoAxis::compute_new_offset(): entering... !" << endl;
584 // Add your own code to control device here
585 if (_hklAdapter && _buffer) {
586 _hklAdapter->update();
587 Tango::DeviceProxyHelper moi(this->device_name, this);
588 moi.write_attribute("offset", argin - _buffer->get_read());
592 //+------------------------------------------------------------------
594 * method: PseudoAxis::stop
596 * description: method to execute "Stop"
597 * Stop the pseudoAxes and all related axes.
601 //+------------------------------------------------------------------
602 void PseudoAxis::stop()
604 DEBUG_STREAM << "PseudoAxis::stop(): entering... !" << endl;
606 // Add your own code to control device here
607 // FRED here use the _pseudoAxe->relatedAxes to stop the right axes.
608 _hklAdapter->stop_all_axis();
612 //+------------------------------------------------------------------
614 * method: PseudoAxis::motor_init
616 * description: method to execute "MotorInit"
617 * DEPRECATED
621 //+------------------------------------------------------------------
622 void PseudoAxis::motor_init()
624 DEBUG_STREAM << "PseudoAxis::motor_init(): entering... !" << endl;
626 // DEPRECATED
627 // Add your own code to control device here
628 if (_buffer)
629 _buffer->set_initialized(true);
630 if(_hklAdapter)
631 _hklAdapter->update();
634 //+------------------------------------------------------------------
636 * method: PseudoAxis::backward
638 * description: method to execute "Backward"
639 * dummy command for V2 interface
643 //+------------------------------------------------------------------
644 void PseudoAxis::backward()
646 DEBUG_STREAM << "PseudoAxis::backward(): entering... !" << endl;
648 // Add your own code to control device here
652 //+------------------------------------------------------------------
654 * method: PseudoAxis::forward
656 * description: method to execute "Forward"
657 * dummy command for V2 interface
661 //+------------------------------------------------------------------
662 void PseudoAxis::forward()
664 DEBUG_STREAM << "PseudoAxis::forward(): entering... !" << endl;
666 // Add your own code to control device here
670 //+------------------------------------------------------------------
672 * method: PseudoAxis::define_position
674 * description: method to execute "DefinePosition"
675 * dummy command for V2 interface
677 * @param argin
680 //+------------------------------------------------------------------
681 void PseudoAxis::define_position(Tango::DevDouble argin)
683 DEBUG_STREAM << "PseudoAxis::define_position(): entering... !" << endl;
685 // Add your own code to control device here
689 //+------------------------------------------------------------------
691 * method: PseudoAxis::initialize_reference_position
693 * description: method to execute "InitializeReferencePosition"
694 * dummy command for V2 interface
698 //+------------------------------------------------------------------
699 void PseudoAxis::initialize_reference_position()
701 DEBUG_STREAM << "PseudoAxis::initialize_reference_position(): entering... !" << endl;
703 // Add your own code to control device here
708 //+------------------------------------------------------------------
710 * method: PseudoAxis::get_mode_parameters
712 * description: method to execute "GetModeParameters"
713 * this command return for the current mode, its parameters.
715 * @return parameters names and values
718 //+------------------------------------------------------------------
719 Tango::DevVarDoubleStringArray *PseudoAxis::get_mode_parameters()
721 // POGO has generated a method core with argout allocation.
722 // If you would like to use a static reference without copying,
723 // See "TANGO Device Server Programmer's Manual"
724 // (chapter : Writing a TANGO DS / Exchanging data)
725 //------------------------------------------------------------
726 Tango::DevVarDoubleStringArray *argout = new Tango::DevVarDoubleStringArray();
727 argout->dvalue.length(1);
728 argout->dvalue[0] = 0.0;
729 argout->svalue.length(1);
730 argout->svalue[0] = CORBA::string_dup("dummy");
731 DEBUG_STREAM << "PseudoAxis::get_mode_parameters(): entering... !" << endl;
733 // Add your own code to control device here
734 if(_buffer)
735 _buffer->get_mode_parameters(argout);
737 return argout;
740 //+------------------------------------------------------------------
742 * method: PseudoAxis::set_mode_parameters
744 * description: method to execute "SetModeParameters"
746 * @param argin The parameters to set
749 //+------------------------------------------------------------------
750 void PseudoAxis::set_mode_parameters(const Tango::DevVarDoubleStringArray *argin)
752 DEBUG_STREAM << "PseudoAxis::set_mode_parameters(): entering... !" << endl;
754 // Add your own code to control device here
755 if(_buffer)
756 _buffer->set_mode_parameters(argin);
763 //+------------------------------------------------------------------
765 * method: PseudoAxis::on
767 * description: method to execute "On"
768 * Activate the pseudoAxis and all related motors.
772 //+------------------------------------------------------------------
773 void PseudoAxis::on()
775 DEBUG_STREAM << "PseudoAxis::on(): entering... !" << endl;
777 // Add your own code to control device here
778 if(_buffer)
779 _buffer->on();
782 } // namespace