1 #if ( _MSC_VER && _MSC_VER <= 1200 )
2 // Ce pragma ne devrait pas etre dans ce fichier car le warning ne s'affiche
3 // que si l'on compile une version DEBUG de la librairie.
4 // Il faut utiliser l'option de compilation \wd4786 mais elle n'est presente
5 // qu'à partir de VC++7
6 # pragma warning (disable : 4786)
11 #include <hkl/hkl-pseudoaxis.h>
14 #include "TangoHKLAdapter.h"
15 #include "Diffractometer.h"
16 #include "PseudoAxes.h"
17 #include "AxisAdapter.h"
18 #include "AxisAttrib.h"
19 #include "PseudoAxesAttrib.h"
23 # define STDOUT(stream) std::cout << stream
25 # define STDOUT(stream) {};
28 namespace Diffractometer_ns
30 //+----------------------------------------------------------------------------
32 // method : TangoHKLAdapter::TangoHKLAdapter(string &s)
34 // description : constructor for the adapter between HKl library and Tango
36 // in : - type : Type of diffractometer to instancate
38 //-----------------------------------------------------------------------------
39 TangoHKLAdapter::TangoHKLAdapter(Diffractometer
*device
, HklGeometryType type
) :
50 _auto_update_from_proxies
= false;
51 _lambdaAttributeProxy
= NULL
;
52 _wrong_nb_of_axis_proxies
= false;
63 // Create the hkl part
65 _diffractometer
= new HklDiffractometer
;
66 _diffractometer
->geometry_r
= hkl_geometry_factory_new(type
, 50 * HKL_DEGTORAD
);
67 _diffractometer
->geometry_w
= hkl_geometry_factory_new(type
, 50 * HKL_DEGTORAD
);
68 _diffractometer
->geometry_r_real
= hkl_geometry_factory_new(type
, 50 * HKL_DEGTORAD
);
69 _diffractometer
->geometry_w_real
= hkl_geometry_factory_new(type
, 50 * HKL_DEGTORAD
);
72 _diffractometer
->detector
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
73 _diffractometer
->detector
->idx
= 1;
75 _diffractometer
->detector_real
= hkl_detector_factory_new(HKL_DETECTOR_TYPE_0D
);
76 _diffractometer
->detector_real
->idx
= 1;
79 _diffractometer
->samples
= hkl_sample_list_new();
81 // the pseudoAxesenginesList (one per geometry)
82 _diffractometer
->engines_r
= hkl_pseudo_axis_engine_list_factory(type
);
83 _diffractometer
->engines_w
= hkl_pseudo_axis_engine_list_factory(type
);
84 _diffractometer
->engines_r_real
= hkl_pseudo_axis_engine_list_factory(type
);
85 _diffractometer
->engines_w_real
= hkl_pseudo_axis_engine_list_factory(type
);
86 this->update_pseudo_axis_engines();
89 Tango::GroupReply::enable_exception(true);
91 // fill the axisAdapter.
92 len
= HKL_LIST_LEN(_diffractometer
->geometry_r
->axes
);
93 axes_r
= _diffractometer
->geometry_r
->axes
;
94 axes_w
= _diffractometer
->geometry_w
->axes
;
95 axes_r_real
= _diffractometer
->geometry_r_real
->axes
;
96 axes_w_real
= _diffractometer
->geometry_w_real
->axes
;
98 _axes
.push_back(AxisAdapter(this, &axes_r
[i
], &axes_w
[i
], &axes_r_real
[i
], &axes_w_real
[i
]));
100 // hack to connect all axes the first time
101 _auto_update_from_proxies
= true;
102 this->connect_all_proxies();
103 _auto_update_from_proxies
= false;
105 // fill the pseudoAxesAdapters
107 omni_mutex_lock
lock(_lock
);
109 len
= HKL_LIST_LEN(_diffractometer
->engines_r
->engines
);
110 _pseudo_axes_proxies
.resize(len
, 1);
111 for(i
=0; i
<len
; ++i
){
112 PseudoAxesAdapter
*adapter
;
114 adapter
= new PseudoAxesAdapter(this, i
);
115 _pseudoAxesAdapters
.push_back(adapter
);
116 _pseudo_axes_proxies
.data
[i
] = const_cast<char *>(adapter
->_proxy_name
.c_str());
120 // create the dynamic attributes
121 this->create_axes_dynamic_attributes();
123 // set the default lambda
124 this->set_lambda(1.54);
127 TangoHKLAdapter::~TangoHKLAdapter(void)
131 this->destroy_axes_dynamic_attributes();
133 // remove all pseudo axis adapters
134 for(i
=0;i
<_pseudoAxisAdapters
.size();i
++)
135 delete _pseudoAxisAdapters
[i
];
137 // remove all pseudo axes adapters;
138 for(i
=0; i
<_pseudoAxesAdapters
.size(); ++i
)
139 delete _pseudoAxesAdapters
[i
];
141 // remove all axisAdapter
144 if (_lambdaAttributeProxy
)
145 delete _lambdaAttributeProxy
;
147 // remove the hkl part
148 hkl_geometry_free(_diffractometer
->geometry_r
);
149 hkl_geometry_free(_diffractometer
->geometry_w
);
150 hkl_geometry_free(_diffractometer
->geometry_r_real
);
151 hkl_geometry_free(_diffractometer
->geometry_w_real
);
152 hkl_detector_free(_diffractometer
->detector
);
153 hkl_detector_free(_diffractometer
->detector_real
);
154 hkl_sample_list_free(_diffractometer
->samples
);
155 hkl_pseudo_axis_engine_list_free(_diffractometer
->engines_r
);
156 hkl_pseudo_axis_engine_list_free(_diffractometer
->engines_w
);
157 hkl_pseudo_axis_engine_list_free(_diffractometer
->engines_r_real
);
158 hkl_pseudo_axis_engine_list_free(_diffractometer
->engines_w_real
);
160 if(_diffractometer
) {
161 delete _diffractometer
;
162 _diffractometer
= NULL
;
166 void TangoHKLAdapter::connect_all_proxies(void)
168 omni_mutex_lock
lock(_lock
);
170 // connect the lambda proxy
171 if ((!_lambdaAttributeProxy
&& _auto_update_from_proxies
)
172 && _device
->lambdaAttributeProxy
!= "") {
175 _lambdaAttributeProxy
= new Tango::AttributeProxy(_device
->lambdaAttributeProxy
);
181 // connect the axes proxies
182 if (!this->ready
&& _auto_update_from_proxies
) {
185 nb
= _device
->realAxisProxies
.size();
186 if (nb
!= _axes
.size()) {
187 _wrong_nb_of_axis_proxies
= true;
193 for(i
=0; i
<nb
; ++i
) {
194 AxisAdapter
& axis
= _axes
[i
];
195 if (!axis
.is_ready()){
196 // Find axis in the proxy list
197 for(j
=0; j
<nb
; ++j
) {
198 char *line
= strdup(_device
->realAxisProxies
[j
].c_str());
200 char *axis_name
= strtok_r(line
, ":", &last
);
201 char *proxy_name
= strtok_r(NULL
, ":", &last
);
202 if (axis
.get_name() == axis_name
)
203 axis
.connect(proxy_name
);
206 if (!axis
.is_ready())
215 void TangoHKLAdapter::set_lambda(double lambda
)
217 omni_mutex_lock
lock(_lock
);
219 if ((!_auto_update_from_proxies
|| _device
->lambdaAttributeProxy
== "")
222 _diffractometer
->geometry_r
->source
.wave_length
= lambda
;
223 _diffractometer
->geometry_w
->source
.wave_length
= lambda
;
224 _diffractometer
->geometry_r_real
->source
.wave_length
= lambda
;
225 _diffractometer
->geometry_w_real
->source
.wave_length
= lambda
;
229 // this method connect engines to the right geometry, detector and sample
230 void TangoHKLAdapter::update_pseudo_axis_engines(void)
232 hkl_pseudo_axis_engine_list_init(_diffractometer
->engines_r
,
233 _diffractometer
->geometry_r
,
234 _diffractometer
->detector
,
235 _diffractometer
->samples
->current
);
237 hkl_pseudo_axis_engine_list_init(_diffractometer
->engines_w
,
238 _diffractometer
->geometry_w
,
239 _diffractometer
->detector
,
240 _diffractometer
->samples
->current
);
242 hkl_pseudo_axis_engine_list_init(_diffractometer
->engines_r_real
,
243 _diffractometer
->geometry_r_real
,
244 _diffractometer
->detector
,
245 _diffractometer
->samples
->current
);
247 hkl_pseudo_axis_engine_list_init(_diffractometer
->engines_w_real
,
248 _diffractometer
->geometry_w_real
,
249 _diffractometer
->detector
,
250 _diffractometer
->samples
->current
);
253 void TangoHKLAdapter::update_lambda(void)
255 if (_lambdaAttributeProxy
&& _auto_update_from_proxies
){
257 _lambdaAttributeProxy
->read() >> _lambda
;
258 _diffractometer
->geometry_r
->source
.wave_length
= _lambda
;
259 _diffractometer
->geometry_w
->source
.wave_length
= _lambda
;
260 _diffractometer
->geometry_r_real
->source
.wave_length
= _lambda
;
261 _diffractometer
->geometry_w_real
->source
.wave_length
= _lambda
;
263 catch (Tango::DevFailed
&){
266 _lambda
= _diffractometer
->geometry_r
->source
.wave_length
;
270 * this method update the angles attribut from the engines->geometry list
272 void TangoHKLAdapter::update_angles(void)
277 HklPseudoAxisEngine
*engine
;
279 HklGeometry
**geometries
;
281 if(!_auto_update_from_proxies
)
282 engine
= hkl_pseudo_axis_engine_list_get_by_name(_diffractometer
->engines_w
, "hkl");
284 engine
= hkl_pseudo_axis_engine_list_get_by_name(_diffractometer
->engines_w_real
, "hkl");
286 // update the computed_angles part
287 xdim
= 1 + HKL_LIST_LEN(_diffractometer
->geometry_r
->axes
);
288 ydim
= HKL_LIST_LEN(engine
->engines
->geometries
->geometries
);
290 _angles
.resize(xdim
, ydim
);
295 geometries
= engine
->engines
->geometries
->geometries
;
296 for(j
=0; j
<ydim
; ++j
){
297 HklGeometry
*geom
= geometries
[j
];
300 for(i
=1; i
<xdim
; ++i
)
301 data
[i
] = hkl_axis_get_value_unit(&geom
->axes
[i
-1]);
307 * this method update the reflections_angles when we change a sample parameter
309 void TangoHKLAdapter::update_reflections_angles(void)
311 HklSample
*sample
= _diffractometer
->samples
->current
;
317 // the reflection Angles
318 rdim
= HKL_LIST_LEN(sample
->reflections
);
319 _reflections_angles
.resize(rdim
, rdim
);
320 _reflections_angles
.clear();
322 data
= _reflections_angles
.data
;
323 for(i
=0; i
<rdim
; ++i
) {
324 for(j
=0; j
<rdim
; ++j
) {
327 angle
= hkl_sample_get_reflection_theoretical_angle(sample
, i
, j
);
329 angle
= hkl_sample_get_reflection_mesured_angle(sample
, i
, j
);
331 *data
= angle
* HKL_RADTODEG
;
338 void TangoHKLAdapter::update_reflections(void)
340 HklSample
*sample
= _diffractometer
->samples
->current
;
343 // Allocation of the image
344 unsigned int xdim
= 6 + HKL_LIST_LEN(_diffractometer
->geometry_r
->axes
);
345 unsigned int ydim
= HKL_LIST_LEN(sample
->reflections
);
346 _reflections
.resize(xdim
, ydim
);
349 double *data
= _reflections
.data
;
350 for(i
=0; i
<ydim
; ++i
) {
351 HklSampleReflection
*r
;
353 r
= hkl_sample_get_ith_reflection(sample
, i
);
356 HklAxis
*axes
= r
->geometry
->axes
;
359 data
[1] = r
->hkl
.data
[0];
360 data
[2] = r
->hkl
.data
[1];
361 data
[3] = r
->hkl
.data
[2];
363 data
[5] = (double)r
->flag
;
365 for(k
=0; k
<HKL_LIST_LEN(r
->geometry
->axes
); ++k
)
366 data
[6 + k
] = hkl_axis_get_value_unit(&axes
[k
]);
373 void TangoHKLAdapter::update_ub(void)
375 HklSample
const *sample
= _diffractometer
->samples
->current
;
378 _ub
.resize(dim
, dim
);
379 _ub
.set_data_from_buffer(&(sample
->UB
.data
[0][0]), dim
, dim
);
383 void TangoHKLAdapter::update_ux_uy_uz(void)
385 HklSample
*sample
= _diffractometer
->samples
->current
;
388 hkl_matrix_to_euler(&sample
->U
, &_ux
, &_uy
, &_uz
);
396 * this method update all the AxisAdapter from the proxy every 200 ms.
397 * this from_proxy get the real part from the proxy and the "sim" part
398 * from the HklAxis in simulated mode or from the proxy in real mode
399 * else it updates them from the HklAxis.
410 * simulated -> HklAxis
412 void TangoHKLAdapter::update_axis_adapters(void)
416 // first read from the proxy.
418 if(duration
.GetDurationInMs() >= 200) {
419 for(size_t i
=0; i
<_axes
.size(); ++i
)
420 _axes
[i
].from_proxy(!_auto_update_from_proxies
);
423 for(i
=0; i
<_axes
.size(); ++i
)
424 _axes
[i
].from_HklAxis();
427 void TangoHKLAdapter::update_hkl_from_axis_adapters(void)
432 for(i
=0; i
<_axes
.size(); ++i
)
433 _axes
[i
].to_HklAxis();
435 // update the pseudo axes
436 if(_diffractometer
&& _diffractometer
->samples
){
437 hkl_pseudo_axis_engine_list_get(_diffractometer
->engines_r
);
438 hkl_pseudo_axis_engine_list_get(_diffractometer
->engines_w
);
439 hkl_pseudo_axis_engine_list_get(_diffractometer
->engines_r_real
);
440 hkl_pseudo_axis_engine_list_get(_diffractometer
->engines_w_real
);
444 void TangoHKLAdapter::update_pseudo_axis_adapters_from_hkl(void)
446 for(size_t i
=0;i
<_pseudoAxisAdapters
.size();++i
)
447 _pseudoAxisAdapters
[i
]->update();
450 void TangoHKLAdapter::update_proxies_from_axis_adapters(void)
454 // first stop motion of all axes.
455 for(i
=0; i
<_axes
.size(); ++i
)
458 // then send write values to the proxies
459 for(i
=0; i
<_axes
.size(); ++i
)
463 void TangoHKLAdapter::update_proxies_from_pseudo_axis_adapters(PseudoAxisAdapter
*adapter
)
465 adapter
->to_proxies();
468 void TangoHKLAdapter::update_pseudo_axes_adapters_from_hkl(void)
471 HklPseudoAxisEngine
*engine_r
, *engine_w
;
473 for(i
=0; i
<_pseudoAxesAdapters
.size(); ++i
){
474 PseudoAxesAdapter
*adapter
;
476 adapter
= _pseudoAxesAdapters
[i
];
477 engine_r
= _diffractometer
->engines_r
->engines
[i
];
478 engine_w
= _diffractometer
->engines_w
->engines
[i
];
479 len
= HKL_LIST_LEN(engine_r
->pseudoAxes
);
480 for(j
=0; j
<len
; ++j
){
481 adapter
->_read
.data
[j
] = hkl_parameter_get_value_unit((HklParameter
*)engine_r
->pseudoAxes
[j
]);
482 adapter
->_write
.data
[j
] = hkl_parameter_get_value_unit((HklParameter
*)engine_w
->pseudoAxes
[j
]);
489 void TangoHKLAdapter::update_state_and_status(void)
491 Tango::DevState state
;
493 std::string extra_status
;
495 state
= Tango::STANDBY
;
496 if (!_auto_update_from_proxies
)
497 status
= "AutoUpdateFromProxies OFF";
499 status
= "AutoUpdateFromProxies ON";
502 state
= Tango::FAULT
;
503 extra_status
+= "\nCan not connect to axes proxies";
504 // update the monochromator proxy status.
505 if (!_lambdaAttributeProxy
&& _auto_update_from_proxies
) {
506 extra_status
+= "\nCan not connect to the lambdaAttributeProxy";
507 extra_status
+= "\nCheck also the lambdaAttributeProxy property";
511 for(unsigned int i
=0; i
<_axes
.size(); ++i
) {
512 AxisAdapter
const & axis
= _axes
[i
];
513 std::string proxy_name
= axis
.get_proxy_name();
514 Tango::DevState tmpState
= axis
.get_state();
516 ::compose_state(state
, tmpState
);
517 if (tmpState
!= Tango::STANDBY
)
518 extra_status
+= "\n" + proxy_name
+ " is in " + Tango::DevStateName
[tmpState
];
522 state
= Tango::FAULT
;
523 extra_status
+= "\nCan not connect to axes proxies";
529 if (_diffractometer
){
530 status
+= "\nSample: ";
531 if(!_diffractometer
->samples
->current
){
532 status
+= "Not yet Set";
533 state
= Tango::FAULT
;
535 status
+= _diffractometer
->samples
->current
->name
;
536 if (state
== Tango::STANDBY
)
537 extra_status
+= "\nready to compute hkl";
539 state
= Tango::FAULT
;
540 extra_status
+= "\nhkl core not yet initialized !!!";
542 status
+= "\nDiffractometer status: ";
543 status
+= Tango::DevStateName
[state
];
544 status
+= extra_status
;
546 _diffractometerConfig
.state
= state
;
547 _diffractometerConfig
.status
= status
;
550 void TangoHKLAdapter::update(void)
552 /**********************************************
554 **********************************************/
555 omni_mutex_lock
lock(_lock
);
557 this->update_lambda();
558 this->update_axis_adapters();
559 this->update_hkl_from_axis_adapters();
560 this->update_pseudo_axis_adapters_from_hkl();
561 this->update_pseudo_axes_adapters_from_hkl();
562 this->update_state_and_status();
563 /**********************************************
564 * END OF CRITICAL SECTION
565 **********************************************/
568 /********************/
569 /* State and status */
570 /********************/
571 void TangoHKLAdapter::get_diffractometer_config(DiffractometerConfig
& config
)
573 omni_mutex_lock
lock(_lock
);
575 config
= _diffractometerConfig
;
582 short & TangoHKLAdapter::get_angles_idx(void)
584 omni_mutex_lock
lock(_lock
);
589 void TangoHKLAdapter::set_angles_idx(short idx
)
591 omni_mutex_lock
lock(_lock
);
594 if(idx
>= 0 && idx
< (int)_angles
.ydim
){
599 values
= &_angles
.data
[1 + idx
* _angles
.xdim
];
600 for(i
=0; i
<_axes
.size(); ++i
)
601 this->write_axis_i(_axes
[i
], values
[i
]);
605 void TangoHKLAdapter::load(void)
610 void TangoHKLAdapter::load_1(void)
612 unsigned long nb_properties
;
614 // Get the Crystal Attributes properties.
615 Tango::DbData properties
;
616 properties
.push_back(Tango::DbDatum("Crystal"));
617 _device
->get_db_device()->get_attribute_property(properties
);
619 // the first one is the number of properties
620 properties
[0] >> nb_properties
;
622 if (nb_properties
> 1) {
624 HklGeometry
*geometry
;
625 HklDetector
*detector
;
627 geometry
= hkl_geometry_new_copy(_diffractometer
->geometry_r
);
628 detector
= hkl_detector_new_copy(_diffractometer
->detector
);
630 hkl_sample_list_clear(_diffractometer
->samples
);
631 for(i
=1; i
<=nb_properties
; ++i
) {
632 // skip the _ver property
633 if(!strcasecmp("_ver", properties
[i
].name
.c_str()))
638 // The name of the property name is the name of a crystal.
639 sample
= hkl_sample_new(properties
[i
].name
.c_str(), HKL_SAMPLE_MONOCRYSTAL
);
641 // Extract the lines store in the property
642 std::vector
<std::string
> lines
;
643 properties
[i
] >> lines
;
645 for(j
=0; j
<lines
.size(); j
++) {
646 char *line
= strdup(lines
[j
].c_str());
648 char *key
= strtok_r(line
, "=", &last
);
650 if (!strcmp(key
,"lattice")) {
651 double a
= atof(strtok_r(NULL
, ";", &last
));
652 double b
= atof(strtok_r(NULL
, ";", &last
));
653 double c
= atof(strtok_r(NULL
, ";", &last
));
654 double alpha
= atof(strtok_r(NULL
, ";", &last
)) * HKL_DEGTORAD
;
655 double beta
= atof(strtok_r(NULL
, ";", &last
)) * HKL_DEGTORAD
;
656 double gamma
= atof(strtok_r(NULL
, ";", &last
)) * HKL_DEGTORAD
;
657 hkl_sample_set_lattice(sample
, a
, b
, c
, alpha
, beta
, gamma
);
659 sample
->lattice
->a
->fit
= atoi(strtok_r(NULL
, ";", &last
));
660 sample
->lattice
->b
->fit
= atoi(strtok_r(NULL
, ";", &last
));
661 sample
->lattice
->c
->fit
= atoi(strtok_r(NULL
, ";", &last
));
662 sample
->lattice
->alpha
->fit
= atoi(strtok_r(NULL
, ";", &last
));
663 sample
->lattice
->beta
->fit
= atoi(strtok_r(NULL
, ";", &last
));
664 sample
->lattice
->gamma
->fit
= atoi(strtok_r(NULL
, ";", &last
));
665 } else if (!strcmp(key
, "uxuyuz")){
666 double ux
= atof(strtok_r(NULL
, ";", &last
)) * HKL_DEGTORAD
;
667 double uy
= atof(strtok_r(NULL
, ";", &last
)) * HKL_DEGTORAD
;
668 double uz
= atof(strtok_r(NULL
, ";", &last
)) * HKL_DEGTORAD
;
669 hkl_sample_set_U_from_euler(sample
, ux
, uy
, uz
);
670 } else if (!strcmp(key
,"reflection")) {
671 unsigned int idx
= 0;
672 HklSampleReflection
*reflection
;
674 double wavelength
= atof(strtok_r(NULL
, ";", &last
));
675 double h
= atof(strtok_r(NULL
, ";", &last
));
676 double k
= atof(strtok_r(NULL
, ";", &last
));
677 double l
= atof(strtok_r(NULL
, ";", &last
));
678 int flag
= atoi(strtok_r(NULL
, ";", &last
));
680 // first set the geometry axes
681 while(key
= strtok_r(NULL
, ";", &last
))
682 hkl_axis_set_value_unit(&geometry
->axes
[idx
++], atof(key
));
683 geometry
->source
.wave_length
= wavelength
;
685 reflection
= hkl_sample_add_reflection(sample
, geometry
, detector
, h
, k
, l
);
686 reflection
->flag
= flag
;
689 // End of key research
690 }// End for each parameters
691 hkl_sample_list_append(_diffractometer
->samples
, sample
);
692 }// End for each property
694 hkl_detector_free(detector
);
695 hkl_geometry_free(geometry
);
697 //_device->refresh_crystal_parameters();
701 void TangoHKLAdapter::save(void)
703 omni_mutex_lock
lock(_lock
);
707 Tango::DbData crystal_prop
;
708 Tango::DbData data_put
;
710 // Step 1 : clean all properties
711 // FP Le mieux serait sans doute de ne pas effacer les propriétés d'attribut
712 // avant d'avoir crée correctement un data_put.
713 crystal_prop
.push_back(Tango::DbDatum("Crystal"));
714 _device
->get_db_device()->get_attribute_property(crystal_prop
);
715 long number_of_prop
= 0;
716 crystal_prop
[0] >> number_of_prop
;
717 if( number_of_prop
> 0)
718 _device
->get_db_device()->delete_attribute_property(crystal_prop
);
720 // Step 2 : create the Crystal properties
721 Tango::DbDatum
properties("Crystal");
722 // Put number of properties (= nb of samples + 1)
723 len
= hkl_sample_list_len(_diffractometer
->samples
);
724 properties
<< (long)(len
+ 1);
725 data_put
.push_back(properties
);
727 // first property is the format version
728 Tango::DbDatum
version("_ver");
729 version
<< (long)FORMAT_VERSION
;
730 data_put
.push_back(version
);
733 for(k
=0; k
<len
; ++k
){
735 std::vector
<std::string
> lines
;
738 // the lattices values
739 sample
= hkl_sample_list_get_ith(_diffractometer
->samples
, k
);
740 double a
= hkl_parameter_get_value_unit(sample
->lattice
->a
);
741 double b
= hkl_parameter_get_value_unit(sample
->lattice
->b
);
742 double c
= hkl_parameter_get_value_unit(sample
->lattice
->c
);
743 double alpha
= hkl_parameter_get_value_unit(sample
->lattice
->alpha
);
744 double beta
= hkl_parameter_get_value_unit(sample
->lattice
->beta
);
745 double gamma
= hkl_parameter_get_value_unit(sample
->lattice
->gamma
);
747 int a_fit
= sample
->lattice
->a
->fit
;
748 int b_fit
= sample
->lattice
->b
->fit
;
749 int c_fit
= sample
->lattice
->c
->fit
;
750 int alpha_fit
= sample
->lattice
->alpha
->fit
;
751 int beta_fit
= sample
->lattice
->beta
->fit
;
752 int gamma_fit
= sample
->lattice
->gamma
->fit
;
754 snprintf(line
, 255, "lattice=%f;%f;%f;%f;%f;%f;%d;%d;%d;%d;%d;%d",
755 a
, b
, c
, alpha
, beta
, gamma
,
756 a_fit
, b_fit
, c_fit
, alpha_fit
, beta_fit
, gamma_fit
);
757 lines
.push_back(line
);
759 // the UxUyUz parameters
761 hkl_matrix_to_euler(&sample
->U
, &ux
, &uy
, &uz
);
766 snprintf(line
, 255, "uxuyuz=%f;%f;%f", ux
, uy
, uz
);
767 lines
.push_back(line
);
770 for(i
=0; i
<HKL_LIST_LEN(sample
->reflections
); ++i
) {
771 HklSampleReflection
*reflection
;
773 reflection
= hkl_sample_get_ith_reflection(sample
, i
);
774 double wavelength
= reflection
->geometry
->source
.wave_length
;
775 double h
= reflection
->hkl
.data
[0];
776 double k
= reflection
->hkl
.data
[1];
777 double l
= reflection
->hkl
.data
[2];
778 int flag
= reflection
->flag
;
780 snprintf(line
, 255, "reflection=%f;%f;%f;%f;%d",
781 wavelength
, h
, k
, l
, flag
);
782 // Extract values of each axes
783 for(j
=0; j
<HKL_LIST_LEN(reflection
->geometry
->axes
); ++j
) {
785 double rad
= hkl_axis_get_value_unit(&reflection
->geometry
->axes
[j
]);
786 snprintf(pos
, 255, ";%f", rad
);
787 strncat(line
, pos
, 255);
789 lines
.push_back(line
);
792 // Try to create property
794 Tango::DbDatum
property(sample
->name
);
796 data_put
.push_back(property
);
799 //update database for this property
800 _device
->get_db_device()->put_attribute_property(data_put
);
807 char const *TangoHKLAdapter::get_sample_name(void)
809 omni_mutex_lock
lock(_lock
);
811 HklSample
*sample
= _diffractometer
->samples
->current
;
818 void TangoHKLAdapter::set_current_sample(char const * name
)
820 omni_mutex_lock
lock(_lock
);
824 last
= _diffractometer
->samples
->current
;
825 if (HKL_SUCCESS
== hkl_sample_list_select_current(_diffractometer
->samples
, name
))
826 if (last
!= _diffractometer
->samples
->current
){
827 this->update_pseudo_axis_engines();
829 this->update_ux_uy_uz();
830 this->update_reflections_angles();
831 this->update_reflections();
832 this->update_state_and_status();
836 void TangoHKLAdapter::get_sample_lattices(double *a
, double *b
, double *c
,
837 double *alpha
, double *beta
, double *gamma
,
838 double *a_star
, double *b_star
, double *c_star
,
839 double *alpha_star
, double *beta_star
, double *gamma_star
)
841 omni_mutex_lock
lock(_lock
);
843 HklSample
* sample
= _diffractometer
->samples
->current
;
847 HklLattice
const *lattice
= sample
->lattice
;
848 HklLattice
*reciprocal
= hkl_lattice_new_copy(lattice
);
850 hkl_lattice_reciprocal(lattice
, reciprocal
);
853 *a
= hkl_parameter_get_value_unit(lattice
->a
);
854 *b
= hkl_parameter_get_value_unit(lattice
->b
);
855 *c
= hkl_parameter_get_value_unit(lattice
->c
);
856 *alpha
= hkl_parameter_get_value_unit(lattice
->alpha
);
857 *beta
= hkl_parameter_get_value_unit(lattice
->beta
);
858 *gamma
= hkl_parameter_get_value_unit(lattice
->gamma
);
861 *a_star
= hkl_parameter_get_value_unit(reciprocal
->a
);
862 *b_star
= hkl_parameter_get_value_unit(reciprocal
->b
);
863 *c_star
= hkl_parameter_get_value_unit(reciprocal
->c
);
864 *alpha_star
= hkl_parameter_get_value_unit(reciprocal
->alpha
);
865 *beta_star
= hkl_parameter_get_value_unit(reciprocal
->beta
);
866 *gamma_star
= hkl_parameter_get_value_unit(reciprocal
->gamma
);
868 hkl_lattice_free(reciprocal
);
871 void TangoHKLAdapter::get_sample_fit(bool *afit
, bool *bfit
, bool *cfit
,
872 bool *alphafit
, bool *betafit
, bool *gammafit
,
873 bool *uxfit
, bool *uyfit
, bool *uzfit
)
875 omni_mutex_lock
lock(_lock
);
877 HklSample
* sample
= _diffractometer
->samples
->current
;
881 HklLattice
const *lattice
= sample
->lattice
;
883 *afit
= lattice
->a
->fit
;
884 *bfit
= lattice
->b
->fit
;
885 *cfit
= lattice
->c
->fit
;
886 *alphafit
= lattice
->alpha
->fit
;
887 *betafit
= lattice
->beta
->fit
;
888 *gammafit
= lattice
->gamma
->fit
;
895 void TangoHKLAdapter::set_sample_Ux(double ux
)
899 sample
= _diffractometer
->samples
->current
;
901 if (HKL_SUCCESS
== hkl_sample_set_U_from_euler(sample
,
904 _uz
* HKL_DEGTORAD
)){
908 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Can not set this Ux value",
909 "Set a correct value");
913 void TangoHKLAdapter::set_sample_Uy(double uy
)
917 sample
= _diffractometer
->samples
->current
;
919 if (HKL_SUCCESS
== hkl_sample_set_U_from_euler(sample
,
922 _uz
* HKL_DEGTORAD
)){
926 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Can not set this Uy value",
927 "Set a correct value");
931 void TangoHKLAdapter::set_sample_Uz(double uz
)
935 sample
= _diffractometer
->samples
->current
;
937 if (HKL_SUCCESS
== hkl_sample_set_U_from_euler(sample
,
944 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Can not set this Uz value",
945 "Set a correct value");
949 void TangoHKLAdapter::set_sample_AFit(bool fit
)
953 sample
= _diffractometer
->samples
->current
;
955 sample
->lattice
->a
->fit
= fit
;
958 void TangoHKLAdapter::set_sample_BFit(bool fit
)
962 sample
= _diffractometer
->samples
->current
;
964 sample
->lattice
->b
->fit
= fit
;
967 void TangoHKLAdapter::set_sample_CFit(bool fit
)
971 sample
= _diffractometer
->samples
->current
;
973 sample
->lattice
->c
->fit
= fit
;
976 void TangoHKLAdapter::set_sample_AlphaFit(bool fit
)
980 sample
= _diffractometer
->samples
->current
;
982 sample
->lattice
->alpha
->fit
= fit
;
985 void TangoHKLAdapter::set_sample_BetaFit(bool fit
)
989 sample
= _diffractometer
->samples
->current
;
991 sample
->lattice
->beta
->fit
= fit
;
994 void TangoHKLAdapter::set_sample_GammaFit(bool fit
)
998 sample
= _diffractometer
->samples
->current
;
1000 sample
->lattice
->gamma
->fit
= fit
;
1003 void TangoHKLAdapter::set_sample_UxFit(bool fit
)
1007 sample
= _diffractometer
->samples
->current
;
1009 _uxfit
= _uyfit
= _uzfit
= fit
;
1012 void TangoHKLAdapter::set_sample_UyFit(bool fit
)
1016 sample
= _diffractometer
->samples
->current
;
1018 _uxfit
= _uyfit
= _uzfit
= fit
;
1021 void TangoHKLAdapter::set_sample_UzFit(bool fit
)
1025 sample
= _diffractometer
->samples
->current
;
1027 _uxfit
= _uyfit
= _uzfit
= fit
;
1030 void TangoHKLAdapter::add_new_sample(std::string
const & name
)
1032 omni_mutex_lock
lock(_lock
);
1034 HklSample
*sample
= hkl_sample_new(name
.c_str(), HKL_SAMPLE_MONOCRYSTAL
);
1036 if (!hkl_sample_list_append(_diffractometer
->samples
, sample
)){
1037 hkl_sample_free(sample
);
1038 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Can not add a sample with this name",
1039 "A sample with the same name is already present in the sample list");
1043 void TangoHKLAdapter::copy_sample_as(Tango::DevString copy_name
)
1045 omni_mutex_lock
lock(_lock
);
1048 HklSample
const *current
;
1050 current
= _diffractometer
->samples
->current
;
1052 TANGO_EXCEPTION_THROW_WITHOUT_LOG("No current sample set",
1053 "Please set a current sample");
1055 sample
= hkl_sample_list_get_by_name(_diffractometer
->samples
, copy_name
);
1057 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Can not add a sample with this name",
1058 "A sample with the same name is already present in the sample list");
1060 sample
= hkl_sample_new_copy(current
);
1062 hkl_sample_set_name(sample
, copy_name
);
1063 hkl_sample_list_append(_diffractometer
->samples
, sample
);
1067 void TangoHKLAdapter::del_sample(void)
1069 omni_mutex_lock
lock(_lock
);
1071 HklSampleList
*samples
= _diffractometer
->samples
;
1072 hkl_sample_list_del(samples
, samples
->current
);
1074 // add a default sample if no more sample in the list
1075 if(hkl_sample_list_len(samples
) == 0){
1076 HklSample
*sample
= hkl_sample_new("default", HKL_SAMPLE_MONOCRYSTAL
);
1077 samples
->current
= hkl_sample_list_append(samples
, sample
);
1079 samples
->current
= hkl_sample_list_get_ith(samples
, 0);
1081 this->update_ux_uy_uz();
1082 this->update_reflections_angles();
1083 this->update_reflections();
1086 void TangoHKLAdapter::set_lattice(const Tango::DevVarDoubleArray
*argin
)
1088 omni_mutex_lock
lock(_lock
);
1090 if (argin
&& argin
->length() != 6)
1091 TANGO_EXCEPTION_THROW_WITHOUT_LOG("DATA_OUT_OF_RANGE",
1092 "Did not receive the exact amount of crystal parameters: A, B, C, alpha, beta, gamma");
1094 HklSample
*sample
= _diffractometer
->samples
->current
;
1095 if (HKL_FAIL
== hkl_sample_set_lattice(sample
,
1096 (*argin
)[0],(*argin
)[1], (*argin
)[2],
1097 (*argin
)[3] * HKL_DEGTORAD
,
1098 (*argin
)[4] * HKL_DEGTORAD
,
1099 (*argin
)[5] * HKL_DEGTORAD
))
1101 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Can not set this lattice combination.",
1102 "Please set a good combination");
1104 this->update_reflections_angles();
1107 void TangoHKLAdapter::add_reflection(const Tango::DevVarDoubleArray
*argin
)
1109 omni_mutex_lock
lock(_lock
);
1111 HklSample
*sample
= _diffractometer
->samples
->current
;
1115 HklSampleReflection
*ref
;
1117 if(argin
&& argin
->length() == 3){
1122 HklPseudoAxisEngine
*engine
;
1124 engine
= hkl_pseudo_axis_engine_list_get_by_name(_diffractometer
->engines_r
, "hkl");
1126 h
= hkl_parameter_get_value_unit((HklParameter
*)engine
->pseudoAxes
[0]);
1127 k
= hkl_parameter_get_value_unit((HklParameter
*)engine
->pseudoAxes
[1]);
1128 l
= hkl_parameter_get_value_unit((HklParameter
*)engine
->pseudoAxes
[2]);
1131 ref
= hkl_sample_add_reflection(sample
,
1132 _diffractometer
->geometry_r
,
1133 _diffractometer
->detector
,
1136 this->update_reflections_angles();
1137 this->update_reflections();
1142 void TangoHKLAdapter::del_reflection(Tango::DevShort argin
)
1144 omni_mutex_lock
lock(_lock
);
1146 if (HKL_FAIL
== hkl_sample_del_reflection(_diffractometer
->samples
->current
, argin
))
1147 TANGO_EXCEPTION_THROW_WITHOUT_LOG("index out of range",
1148 "change the reflection index");
1149 this->update_reflections_angles();
1150 this->update_reflections();
1153 void TangoHKLAdapter::set_reflections(Matrix
<double> const & img
)
1155 omni_mutex_lock
lock(_lock
);
1157 HklSample
*sample
= _diffractometer
->samples
->current
;
1159 && img
.xdim
== _reflections
.xdim
1160 && img
.ydim
== _reflections
.ydim
) {
1164 for(i
=0; i
<HKL_LIST_LEN(sample
->reflections
); ++i
) {
1165 HklSampleReflection
*r
;
1167 r
= hkl_sample_get_ith_reflection(sample
, i
);
1169 hkl_sample_reflection_set_hkl(r
, img
.data
[j
+1], img
.data
[j
+2], img
.data
[j
+3]);
1170 hkl_sample_reflection_set_flag(r
, (int)img
.data
[j
+5]);
1171 if(!_device
->protectReflectionAxes
){
1176 len
= HKL_LIST_LEN(r
->geometry
->axes
);
1177 axes
= &r
->geometry
->axes
[0];
1179 for(k
=6; k
<6+len
; ++k
)
1180 hkl_axis_set_value_unit(axes
++, img
.data
[j
+k
]);
1182 hkl_geometry_update(r
->geometry
);
1183 hkl_sample_reflection_set_geometry(r
, r
->geometry
);
1186 j
+= _reflections
.xdim
;
1188 this->update_reflections();
1189 this->update_reflections_angles();
1193 double TangoHKLAdapter::affine_sample(std::string name
)
1195 omni_mutex_lock
lock(_lock
);
1200 sample
= hkl_sample_list_get_by_name(_diffractometer
->samples
, name
.c_str());
1204 // check if the affine sample is already in the HklSampleList
1205 std::string name
= sample
->name
;
1207 tmp
= hkl_sample_list_get_by_name(_diffractometer
->samples
, name
.c_str());
1208 hkl_sample_list_del(_diffractometer
->samples
, tmp
);
1210 tmp
= hkl_sample_new_copy(sample
);
1211 hkl_sample_set_name(tmp
, name
.c_str());
1212 res
= hkl_sample_affine(tmp
);
1214 hkl_sample_list_append(_diffractometer
->samples
, tmp
);
1215 _diffractometer
->samples
->current
= tmp
;
1218 this->update_ux_uy_uz();
1219 this->update_reflections_angles();
1226 std::vector
<std::string
> TangoHKLAdapter::get_samples_names(void)
1228 omni_mutex_lock
lock(_lock
);
1230 std::vector
<std::string
> names
;
1232 HklSampleList
*samples
= _diffractometer
->samples
;
1234 len
= hkl_sample_list_len(samples
);
1235 for(i
=0; i
<len
; ++i
)
1236 names
.push_back(hkl_sample_list_get_ith(samples
, i
)->name
);
1242 void TangoHKLAdapter::get_sample_parameter_values(Tango::DevVarDoubleStringArray
*argout
)
1244 omni_mutex_lock
lock(_lock
);
1247 HklParameter
*parameter
= NULL
;
1251 if (argout
->svalue
.length() != 1)
1252 TANGO_EXCEPTION_THROW_WITHOUT_LOG("DATA_OUT_OF_RANGE",
1253 "only one string = parameter name");
1255 sample
= _diffractometer
->samples
->current
;
1258 name
= argout
->svalue
[0];
1260 parameter
= sample
->lattice
->a
;
1261 else if(name
== "b")
1262 parameter
= sample
->lattice
->b
;
1263 else if(name
== "c")
1264 parameter
= sample
->lattice
->c
;
1265 else if(name
== "alpha")
1266 parameter
= sample
->lattice
->alpha
;
1267 else if(name
== "beta")
1268 parameter
= sample
->lattice
->beta
;
1269 else if(name
== "gamma")
1270 parameter
= sample
->lattice
->gamma
;
1273 argout
->dvalue
[0] = parameter
->range
.min
;
1274 argout
->dvalue
[1] = parameter
->range
.max
;
1275 argout
->dvalue
[2] = parameter
->fit
;
1277 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Wrong parameter name",
1278 "Select: a, b, c, alpha, beta, gamma");
1283 void TangoHKLAdapter::set_sample_parameter_values(Tango::DevVarDoubleStringArray
const *argin
)
1285 omni_mutex_lock
lock(_lock
);
1288 HklParameter
*parameter
= NULL
;
1292 if(argin
->dvalue
.length() != 3)
1293 TANGO_EXCEPTION_THROW_WITHOUT_LOG("DATA_OUT_OF_RANGE",
1294 "set_crystal_parameter_values did not receive the right amount of scalar parameters: min, max, flag");
1295 if((argin
->svalue
.length() ) != 1)
1296 TANGO_EXCEPTION_THROW_WITHOUT_LOG("DATA_OUT_OF_RANGE",
1297 "set_crystal_parameter_values did not receive the right amount of string parameters: parameter name");
1300 sample
= _diffractometer
->samples
->current
;
1302 TANGO_EXCEPTION_THROW_WITHOUT_LOG("No current sample set",
1303 "Please set a current sample");
1305 name
= argin
->svalue
[0];
1307 parameter
= sample
->lattice
->a
;
1308 else if(name
== "b")
1309 parameter
= sample
->lattice
->b
;
1310 else if(name
== "c")
1311 parameter
= sample
->lattice
->c
;
1312 else if(name
== "alpha")
1313 parameter
= sample
->lattice
->alpha
;
1314 else if(name
== "beta")
1315 parameter
= sample
->lattice
->beta
;
1316 else if(name
== "gamma")
1317 parameter
= sample
->lattice
->gamma
;
1320 parameter
->range
.min
= argin
->dvalue
[0];
1321 parameter
->range
.max
= argin
->dvalue
[1];
1322 parameter
->fit
= argin
->dvalue
[2] != 0.;
1324 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Wrong parameter name",
1325 "Select: a, b, c, alpha, beta, gamma");
1327 this->update_ux_uy_uz();
1328 this->update_reflections_angles();
1331 void TangoHKLAdapter::compute_u(const Tango::DevVarLongArray
*argin
)
1333 omni_mutex_lock
lock(_lock
);
1337 // is parameter ok ?
1338 if (argin
->length() != 2)
1339 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Need exactly two reflections indexes",
1340 "use the right number of parameters");
1342 sample
= _diffractometer
->samples
->current
;
1344 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Can not compute the U matrix without current sample set.",
1345 "Set a current sample");
1347 if (hkl_sample_compute_UB_busing_levy(sample
, (*argin
)[0], (*argin
)[1]))
1348 TANGO_EXCEPTION_THROW_WITHOUT_LOG("can not compute the UB matrix using thoses reflections index",
1349 "Use other reflections");
1351 this->update_ux_uy_uz();
1358 void TangoHKLAdapter::read_axis(int idx
, double & read
, double & write
)
1360 omni_mutex_lock
lock(_lock
);
1362 AxisAdapter
& axis
= _axes
[idx
];
1363 read
= axis
.get_read();
1364 write
= axis
.get_write();
1367 void TangoHKLAdapter::stop_all_axis(void)
1369 #ifdef WRITE_TO_PROXY_ALLOWED
1371 for(i
=0; i
<_axes
.size(); ++i
)
1376 void TangoHKLAdapter::write_axis(AxisAdapter
& axis
, double value
)
1378 omni_mutex_lock
lock(_lock
);
1380 this->write_axis_i(axis
, value
);
1383 void TangoHKLAdapter::write_axis_i(AxisAdapter
& axis
, double value
)
1385 axis
._read
= axis
._write
= value
;
1386 hkl_axis_set_value_unit(axis
._axis_r
, value
);
1387 hkl_axis_set_value_unit(axis
._axis_w
, value
);
1390 /*******************/
1391 /* pseudoAxis part */
1392 /*******************/
1394 std::vector
<std::string
> TangoHKLAdapter::pseudo_axis_get_names(void)
1396 omni_mutex_lock
lock(_lock
);
1398 std::vector
<std::string
> names
;
1401 for(i
=0; i
<HKL_LIST_LEN(_diffractometer
->engines_r_real
->engines
); ++i
){
1402 HklPseudoAxisEngine
*engine
;
1404 engine
= _diffractometer
->engines_r_real
->engines
[i
];
1405 for(j
=0; j
<HKL_LIST_LEN(engine
->pseudoAxes
); ++j
)
1406 names
.push_back(((HklParameter
*)(&engine
->pseudoAxes
[j
]))->name
);
1412 PseudoAxisAdapter
*TangoHKLAdapter::pseudo_axis_buffer_new(char const *name
)
1414 omni_mutex_lock
lock(_lock
);
1416 PseudoAxisAdapter
* buffer
= NULL
;
1417 HklPseudoAxis
*pseudo_r
;
1418 HklPseudoAxis
*pseudo_w
;
1420 pseudo_r
= hkl_pseudo_axis_engine_list_get_pseudo_axis_by_name(
1421 _diffractometer
->engines_r_real
, name
);
1422 pseudo_w
= hkl_pseudo_axis_engine_list_get_pseudo_axis_by_name(
1423 _diffractometer
->engines_w_real
, name
);
1425 if(pseudo_r
&& pseudo_w
) {
1428 buffer
= new PseudoAxisAdapter(*this, pseudo_r
, pseudo_w
);
1429 _pseudoAxisAdapters
.push_back(buffer
);
1431 catch (Tango::DevFailed
)
1440 void TangoHKLAdapter::pseudo_axis_set_initialized(PseudoAxisAdapter
*buffer
,
1441 Tango::DevBoolean initialized
)
1446 omni_mutex_lock
lock(_lock
);
1448 hkl_pseudo_axis_engine_initialize(buffer
->_pseudo_r
->engine
);
1449 hkl_pseudo_axis_engine_initialize(buffer
->_pseudo_w
->engine
);
1453 void TangoHKLAdapter::pseudo_axis_set_position(PseudoAxisAdapter
*buffer
,
1454 const Tango::DevDouble
& position
)
1456 omni_mutex_lock
lock(_lock
);
1458 double value
= position
- buffer
->_config
.offset
;
1461 HklPseudoAxisEngineList
*engines_w_real
= _diffractometer
->engines_w_real
;
1462 HklPseudoAxisEngine
*engine
= buffer
->_pseudo_w
->engine
;
1463 HklGeometry
*geometry_w_real
= _diffractometer
->geometry_w_real
;
1465 hkl_parameter_set_value_unit((HklParameter
*)buffer
->_pseudo_w
, value
);
1466 res
= hkl_pseudo_axis_engine_set(buffer
->_pseudo_w
->engine
);
1467 if (HKL_SUCCESS
== res
){
1468 hkl_geometry_init_geometry(geometry_w_real
, engine
->engines
->geometries
->geometries
[0]);
1469 hkl_pseudo_axis_engine_list_get(engines_w_real
);
1472 this->update_angles();
1473 buffer
->to_proxies();
1474 this->update_axis_adapters();
1475 this->update_pseudo_axis_adapters_from_hkl();
1476 this->update_pseudo_axes_adapters_from_hkl();
1478 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Can not write on this pseudo axis",
1479 "Check the sample");
1483 PseudoAxisConfig
TangoHKLAdapter::pseudo_axis_get_config(PseudoAxisAdapter
*buffer
)
1487 omni_mutex_lock
lock(_lock
);
1489 return buffer
->_config
;
1492 void TangoHKLAdapter::pseudo_axis_set_mode(PseudoAxisAdapter
*buffer
, Tango::DevString
const & mode
)
1494 omni_mutex_lock
lock(_lock
);
1498 len
= HKL_LIST_LEN(buffer
->_pseudo_r
->engine
->modes
);
1499 for(i
=0; i
<len
; ++i
)
1500 if(!strcasecmp(mode
, buffer
->_pseudo_r
->engine
->modes
[i
]->name
))
1503 hkl_pseudo_axis_engine_select_mode(buffer
->_pseudo_r
->engine
, i
);
1504 hkl_pseudo_axis_engine_select_mode(buffer
->_pseudo_w
->engine
, i
);
1508 void TangoHKLAdapter::pseudo_axis_get_mode_parameters(PseudoAxisAdapter
*buffer
, Tango::DevVarDoubleStringArray
*argout
)
1510 omni_mutex_lock
lock(_lock
);
1512 HklPseudoAxisEngine
*engine
= buffer
->_pseudo_r
->engine
;
1513 if (engine
&& engine
->mode
){
1515 size_t len
= HKL_LIST_LEN(engine
->mode
->parameters
);
1517 argout
->svalue
.length(len
);
1518 argout
->dvalue
.length(len
);
1519 for(i
=0; i
<len
; ++i
){
1520 argout
->svalue
[i
] = CORBA::string_dup(engine
->mode
->parameters
[i
].name
);
1521 argout
->dvalue
[i
] = hkl_parameter_get_value_unit(&engine
->mode
->parameters
[i
]);
1524 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Can not get the current Mode parameters values without a current mode set.",
1528 void TangoHKLAdapter::pseudo_axis_set_mode_parameters(PseudoAxisAdapter
*buffer
, const Tango::DevVarDoubleStringArray
*argin
)
1530 omni_mutex_lock
lock(_lock
);
1532 if(argin
->svalue
.length() != argin
->dvalue
.length())
1533 TANGO_EXCEPTION_THROW_WITHOUT_LOG("DATA_OUT_OF_RANGE",
1534 "set_mode_parameters_values did not receive the same amount between string and double values");
1536 HklPseudoAxisEngine
*engine_r
= buffer
->_pseudo_r
->engine
;
1537 HklPseudoAxisEngine
*engine_w
= buffer
->_pseudo_w
->engine
;
1538 if (engine_r
&& engine_w
){
1542 len
= argin
->svalue
.length();
1543 if (HKL_LIST_LEN(engine_r
->mode
->parameters
) != len
)
1544 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Not the right number of parameter",
1545 "gives the right number of parameters");
1547 for(i
=0; i
<len
; ++i
){
1548 double value
= argin
->dvalue
[i
];
1549 char const *name
= argin
->svalue
[i
];
1550 if(!strcmp(name
, engine_r
->mode
->parameters
[i
].name
)){
1551 hkl_parameter_set_value_unit(&engine_r
->mode
->parameters
[i
], value
);
1552 hkl_parameter_set_value_unit(&engine_w
->mode
->parameters
[i
], value
);
1556 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Can not set the current Mode parameters values without a current mode set.",
1560 void TangoHKLAdapter::pseudo_axis_on(PseudoAxisAdapter
*buffer
)
1562 omni_mutex_lock
lock(_lock
);
1564 for(size_t i
=0; i
<buffer
->_axes
.size(); ++i
)
1565 buffer
->_axes
[i
]->on();
1568 /*******************/
1569 /* pseudoAxes part */
1570 /*******************/
1572 PseudoAxesAdapter
*TangoHKLAdapter::pseudo_axes_adapter_get_by_name(std::string
const & name
)
1574 //omni_mutex_lock lock(_lock);
1577 PseudoAxesAdapter
*adapter
= NULL
;
1579 for(i
=0; i
<_pseudoAxesAdapters
.size(); ++i
)
1580 if(_pseudoAxesAdapters
[i
]->get_name() == name
){
1581 adapter
= _pseudoAxesAdapters
[i
];
1587 PseudoAxesConfig
TangoHKLAdapter::pseudo_axes_get_config(size_t idx
)
1591 omni_mutex_lock
lock(_lock
);
1593 return _pseudoAxesAdapters
[idx
]->_config
;
1597 void TangoHKLAdapter::pseudo_axes_write(size_t idx
, Matrix
<double> const & write
) throw (Tango::DevFailed
)
1599 omni_mutex_lock
lock(_lock
);
1603 HklPseudoAxisEngineList
*engines_r
, *engines_w
;
1604 HklGeometry
*geometry_r
, *geometry_w
;
1606 engines_r
= _diffractometer
->engines_r
;
1607 engines_w
= _diffractometer
->engines_w
;
1608 len
= HKL_LIST_LEN(engines_w
->engines
[idx
]->pseudoAxes
);
1610 geometry_r
= _diffractometer
->geometry_r
;
1611 geometry_w
= _diffractometer
->geometry_w
;
1613 for(i
=0; i
<len
; ++i
)
1614 hkl_parameter_set_value_unit((HklParameter
*)(engines_w
->engines
[idx
]->pseudoAxes
[i
]), write
.data
[i
]);
1616 res
= hkl_pseudo_axis_engine_set(engines_w
->engines
[idx
]);
1618 if (HKL_SUCCESS
== res
){
1619 // force auto update OFF when we write on a PseudoAxes.
1620 _auto_update_from_proxies
= false;
1622 hkl_geometry_init_geometry(geometry_w
, engines_w
->geometries
->geometries
[0]);
1623 hkl_pseudo_axis_engine_list_get(engines_w
);
1624 hkl_geometry_init_geometry(geometry_r
, engines_w
->geometries
->geometries
[0]);
1625 hkl_pseudo_axis_engine_list_get(engines_r
);
1628 this->update_angles();
1629 this->update_axis_adapters();
1630 this->update_pseudo_axis_adapters_from_hkl();
1631 this->update_pseudo_axes_adapters_from_hkl();
1633 TANGO_EXCEPTION_THROW_WITHOUT_LOG("Can not write on this pseudo axis",
1634 "Check the sample");
1638 Tango::DevString
*TangoHKLAdapter::pseudo_axes_get_mode(size_t idx
)
1640 omni_mutex_lock
lock(_lock
);
1642 HklPseudoAxisEngine
*engine
= _diffractometer
->engines_r
->engines
[idx
];
1644 return const_cast<Tango::DevString
*>(&engine
->mode
->name
);
1649 void TangoHKLAdapter::pseudo_axes_set_mode(size_t idx
, const Tango::DevString name
)
1651 omni_mutex_lock
lock(_lock
);
1654 HklPseudoAxisEngine
*engine
= _diffractometer
->engines_r
->engines
[idx
];
1656 // check if we try to set the same mode than the current one
1658 if(!strcasecmp(name
, engine
->mode
->name
))
1661 // no so set the mode if possible
1662 size_t len
= HKL_LIST_LEN(engine
->modes
);
1663 for(i
=0; i
<len
; ++i
)
1664 if(!strcasecmp(name
, engine
->modes
[i
]->name
))
1667 hkl_pseudo_axis_engine_select_mode(_diffractometer
->engines_r
->engines
[idx
], i
);
1668 hkl_pseudo_axis_engine_select_mode(_diffractometer
->engines_w
->engines
[idx
], i
);
1669 hkl_pseudo_axis_engine_select_mode(_diffractometer
->engines_r_real
->engines
[idx
], i
);
1670 hkl_pseudo_axis_engine_select_mode(_diffractometer
->engines_w_real
->engines
[idx
], i
);
1671 _pseudoAxesAdapters
[idx
]->update_axes_i(engine
);
1672 _pseudoAxesAdapters
[idx
]->update_state_and_status_i();
1676 Matrix
<char *> & TangoHKLAdapter::pseudo_axes_get_mode_parameters_names(size_t idx
)
1678 omni_mutex_lock
lock(_lock
);
1681 HklPseudoAxisEngine
*engine
= _diffractometer
->engines_r
->engines
[idx
];
1682 size_t len
= HKL_LIST_LEN(engine
->mode
->parameters
);
1683 PseudoAxesAdapter
*adapter
= _pseudoAxesAdapters
[idx
];
1684 adapter
->_mode_parameters_names
.resize(len
, 1);
1685 for(i
=0; i
<len
; ++i
)
1686 adapter
->_mode_parameters_names
.data
[i
] = const_cast<char *>(engine
->mode
->parameters
[i
].name
);
1687 return adapter
->_mode_parameters_names
;
1690 Matrix
<double> const & TangoHKLAdapter::pseudo_axes_get_mode_parameters(size_t idx
)
1692 omni_mutex_lock
lock(_lock
);
1695 HklPseudoAxisEngine
*engine
= _diffractometer
->engines_r
->engines
[idx
];
1696 size_t len
= HKL_LIST_LEN(engine
->mode
->parameters
);
1697 PseudoAxesAdapter
*adapter
= _pseudoAxesAdapters
[idx
];
1698 adapter
->_mode_parameters
.resize(len
, 1);
1699 for(i
=0; i
<len
; ++i
)
1700 adapter
->_mode_parameters
.data
[i
] = hkl_parameter_get_value_unit(&engine
->mode
->parameters
[i
]);
1701 return adapter
->_mode_parameters
;
1704 void TangoHKLAdapter::pseudo_axes_set_mode_parameters(size_t idx
, Matrix
<double> const & values
)
1706 omni_mutex_lock
lock(_lock
);
1709 HklPseudoAxisEngine
*engine
= _diffractometer
->engines_r
->engines
[idx
];
1710 size_t len
= HKL_LIST_LEN(engine
->mode
->parameters
);
1711 if(len
== values
.xdim
)
1712 for(i
=0; i
<len
; ++i
){
1713 double & value
= values
.data
[i
];
1715 hkl_parameter_set_value_unit(&_diffractometer
->engines_r
->engines
[idx
]->mode
->parameters
[i
],
1717 hkl_parameter_set_value_unit(&_diffractometer
->engines_w
->engines
[idx
]->mode
->parameters
[i
],
1719 hkl_parameter_set_value_unit(&_diffractometer
->engines_r_real
->engines
[idx
]->mode
->parameters
[i
],
1721 hkl_parameter_set_value_unit(&_diffractometer
->engines_w_real
->engines
[idx
]->mode
->parameters
[i
],
1726 void TangoHKLAdapter::pseudo_axes_init(size_t idx
)
1728 omni_mutex_lock
lock(_lock
);
1730 HklPseudoAxisEngine
*engine_r
= _diffractometer
->engines_r
->engines
[idx
];
1731 HklPseudoAxisEngine
*engine_w
= _diffractometer
->engines_w
->engines
[idx
];
1732 HklPseudoAxisEngine
*engine_r_real
= _diffractometer
->engines_r_real
->engines
[idx
];
1733 HklPseudoAxisEngine
*engine_w_real
= _diffractometer
->engines_w_real
->engines
[idx
];
1735 hkl_pseudo_axis_engine_initialize(engine_r
);
1736 hkl_pseudo_axis_engine_initialize(engine_w
);
1737 hkl_pseudo_axis_engine_initialize(engine_r_real
);
1738 hkl_pseudo_axis_engine_initialize(engine_w_real
);
1741 void TangoHKLAdapter::pseudo_axes_add_dynamic_attributes(size_t idx
)
1743 //omni_mutex_lock lock(_lock);
1747 // check if all the PseudoAxes were instantiated
1748 for(i
=0; i
<_pseudoAxesAdapters
.size(); ++i
)
1749 if(!_pseudoAxesAdapters
[i
]->_device
)
1753 * we need to attach the dynamic attributes after all instance of
1754 * the PseudoAxes devices were started. otherwise it cause problem.
1755 * only add the attributs if they were not already added.
1757 for(i
=0; i
<_pseudoAxesAdapters
.size(); ++i
){
1758 PseudoAxesAdapter
*adapter
= _pseudoAxesAdapters
[i
];
1759 Tango::MultiAttribute
*multiattribute
= adapter
->_device
->get_device_attr();
1761 for(j
=0; j
<adapter
->_dynamic_attribute_pseudo_axes_axis
.size(); ++j
){
1763 multiattribute
->get_attr_by_name(adapter
->get_pseudo_axis_names().data
[j
]);
1764 }catch (Tango::DevFailed
){
1765 adapter
->_device
->add_attribute(adapter
->_dynamic_attribute_pseudo_axes_axis
[j
]);
1771 void TangoHKLAdapter::pseudo_axes_remove_dynamic_attributes(size_t idx
)
1774 PseudoAxesAdapter
*adapter
;
1776 adapter
= _pseudoAxesAdapters
[idx
];
1777 if(adapter
->_device
){
1778 for(i
=0; i
<adapter
->_dynamic_attribute_pseudo_axes_axis
.size(); ++i
)
1779 adapter
->_device
->remove_attribute(adapter
->_dynamic_attribute_pseudo_axes_axis
[i
], false);
1780 adapter
->_device
= NULL
;
1784 void TangoHKLAdapter::pseudo_axes_create_and_start_devices(void)
1786 omni_mutex_lock
lock(_lock
);
1790 Tango::Util
*tg
= Tango::Util::instance();
1791 Tango::Database
*db
= tg
->get_database();
1792 for(i
=0; i
<_pseudoAxesAdapters
.size(); ++i
){
1793 std::string dev_name
= _pseudoAxesAdapters
[i
]->get_proxy_name();
1795 // first check if the device is already defined in the database
1797 Tango::DbDevImportInfo my_device_import_info
;
1799 my_device_import_info
= db
->import_device(dev_name
);
1800 } catch (Tango::DevFailed
&) {
1801 Tango::DbDevInfo my_device_info
;
1803 // add the device to the database
1804 my_device_info
.name
= dev_name
.c_str();
1805 my_device_info
._class
= "PseudoAxes";
1806 my_device_info
.server
= tg
->get_ds_name().c_str();
1808 db
->add_device(my_device_info
);
1810 // add the right properties to that device
1811 Tango::DbDatum
DiffractometerProxy("DiffractometerProxy"), EngineName("EngineName");
1812 Tango::DbData properties
;
1813 DiffractometerProxy
<< _device
->name();
1814 EngineName
<< _pseudoAxesAdapters
[i
]->get_name();
1815 properties
.push_back(DiffractometerProxy
);
1816 properties
.push_back(EngineName
);
1817 db
->put_device_property(dev_name
,properties
);
1820 // now start the device
1821 const std::vector
<Tango::DeviceClass
*> *cl_list
= tg
->get_class_list();
1822 for(j
=0; j
<cl_list
->size(); ++j
){
1823 if((*cl_list
)[j
]->get_name() == "PseudoAxes"){
1825 Tango::DevVarStringArray na
;
1827 na
[0] = dev_name
.c_str();
1828 (*cl_list
)[j
]->device_factory(&na
);
1829 std::cout
<< "Started " << dev_name
<< std::endl
;
1832 catch (Tango::DevFailed
&e
)
1841 Tango::DevBoolean
TangoHKLAdapter::pseudo_axes_get_initialized(size_t idx
)
1843 omni_mutex_lock
lock(_lock
);
1845 if(!_diffractometer
->engines_r
->engines
[idx
]->mode
->initialize
)
1849 void TangoHKLAdapter::pseudo_axes_set_initialized(size_t idx
, bool initialized
)
1854 omni_mutex_lock
lock(_lock
);
1856 HklPseudoAxisEngine
*engine_r
= _diffractometer
->engines_r
->engines
[idx
];
1857 HklPseudoAxisEngine
*engine_w
= _diffractometer
->engines_w
->engines
[idx
];
1858 HklPseudoAxisEngine
*engine_r_real
= _diffractometer
->engines_r_real
->engines
[idx
];
1859 HklPseudoAxisEngine
*engine_w_real
= _diffractometer
->engines_w_real
->engines
[idx
];
1861 hkl_pseudo_axis_engine_initialize(engine_r
);
1862 hkl_pseudo_axis_engine_initialize(engine_w
);
1863 hkl_pseudo_axis_engine_initialize(engine_r_real
);
1864 hkl_pseudo_axis_engine_initialize(engine_w_real
);
1868 /**********************/
1869 /* Dynamic attributes */
1870 /**********************/
1872 void TangoHKLAdapter::create_axes_dynamic_attributes(void)
1875 size_t len
= _axes
.size();
1876 _dynamic_attribute_axes_names
.resize(len
, 1);
1877 for(i
=0; i
<len
; ++i
){
1882 // compute the diffractometer axis names
1885 name
+= _axes
[i
].get_name();
1886 name
[l
] = toupper(name
[l
]);
1888 // create the AxisAttrib to deal with the proxy connection.
1889 att
= new AxisAttrib(name
.c_str(), _axes
[i
]);
1890 _dynamic_attribute_axes
.push_back(att
);
1891 _dynamic_attribute_axes_names
.data
[i
] = const_cast<char *>(att
->get_name().c_str());
1895 void TangoHKLAdapter::destroy_axes_dynamic_attributes(void)
1898 size_t len
= _dynamic_attribute_axes
.size();
1900 for(i
=0; i
<len
; ++i
)
1901 delete _dynamic_attribute_axes
[i
];
1904 void TangoHKLAdapter::attach_dynamic_attributes_to_device(void)
1908 for(i
=0; i
<_dynamic_attribute_axes
.size(); ++i
)
1909 _device
->add_attribute(_dynamic_attribute_axes
[i
]);
1913 void TangoHKLAdapter::detach_dynamic_attributes_from_device(void)
1917 for(i
=0; i
<_dynamic_attribute_axes
.size(); ++i
)
1918 _device
->remove_attribute(_dynamic_attribute_axes
[i
], false);
1922 bool TangoHKLAdapter::get_auto_update_from_proxies(void)
1924 omni_mutex_lock
lock(_lock
);
1926 return _auto_update_from_proxies
;
1929 void TangoHKLAdapter::set_auto_update_from_proxies(bool update
)
1931 bool old
= _auto_update_from_proxies
;
1932 _auto_update_from_proxies
= update
;
1933 if(old
= false && update
== true)
1939 std::string
hkl_axes_consign_as_string(HklAxis
const **axes
, size_t axes_len
)
1944 std::ostringstream line
, tmp
;
1945 for(i
=0; i
<axes_len
; ++i
) {
1946 HklParameter
const *axis
= (HklParameter
const *)axes
[i
];
1947 double value
= hkl_parameter_get_value_unit(axis
);
1952 std::ostringstream tmp
;
1954 tmp
<< value
<< "°";