* idem for the state and status.
[diffractometer.git] / src / AxisAdapter.cpp
blob59447b14e952b90b3deccf42e101be367283d8fe
1 #include "AxisAdapter.h"
2 #include "TangoHKLAdapter.h"
3 #include "macros.h"
5 namespace Diffractometer_ns {
7 AxisAdapter::AxisAdapter(TangoHKLAdapter * hklAdapter, HklAxis *axis_r, HklAxis *axis_w,
8 HklAxis *axis_r_real, HklAxis *axis_w_real) :
9 _hklAdapter(hklAdapter),
10 _axis_r(axis_r),
11 _axis_w(axis_w),
12 _axis_r_real(axis_r_real),
13 _axis_w_real(axis_w_real)
15 _proxy_name = "";
16 _proxy_stop_command_name = "Stop";
17 _proxy_on_command_name = "On";
18 _proxy = NULL;
19 this->from_HklAxis();
20 _state = Tango::UNKNOWN;
23 AxisAdapter::~AxisAdapter(void)
27 std::string const AxisAdapter::get_name(void) const
29 return ((HklParameter *)(_axis_r))->name;
32 std::string const AxisAdapter::get_proxy_name(void) const
34 return _proxy_name;
37 bool AxisAdapter::connect(char *proxy_name)
39 bool res = false;
41 _proxy_name = proxy_name;
42 try{
43 _proxy = new Tango::AttributeProxy(proxy_name);
44 if(_proxy){
45 std::vector<double> positions;
47 res = true;
49 // read the read/write position
50 _proxy->read() >> positions;
51 hkl_axis_set_value_unit(_axis_r, positions[0]);
52 hkl_axis_set_value_unit(_axis_w, positions[1]);
53 _read = positions[0];
54 _write = positions[1];
56 // now read the range
57 std::string attr_name = AXIS_ATTRIBUTE_POSITION_NAME;
58 Tango::AttributeInfo info = _proxy->get_config();
60 // Check if min and max value have been set
61 char const *token;
62 token = info.min_value.c_str();
63 if (!strstr(token, "Not specified"))
64 _min = atof(token);
66 token = info.max_value.c_str();
67 if (!strstr(token, "Not specified"))
68 _max = atof(token);
70 }catch(Tango::DevFailed &){
71 _state = Tango::FAULT;
73 return res;
76 bool const AxisAdapter::is_ready(void) const
78 if (_proxy)
79 return true;
80 else
81 return false;
84 void AxisAdapter::write(double value)
86 _hklAdapter->write_axis(*this, value);
89 void AxisAdapter::from_HklAxis(void)
91 _read = hkl_axis_get_value_unit(_axis_r);
92 _write = hkl_axis_get_value_unit(_axis_w);
93 _read_real = hkl_axis_get_value_unit(_axis_r_real);
94 _write_real = hkl_axis_get_value_unit(_axis_w_real);
95 hkl_axis_get_range_unit(_axis_r, &_min, &_max);
98 void AxisAdapter::to_HklAxis(void)
100 hkl_axis_set_value_unit(_axis_r, _read);
101 hkl_axis_set_value_unit(_axis_w, _write);
102 hkl_axis_set_range_unit(_axis_r, _min, _max);
103 hkl_axis_set_range_unit(_axis_w, _min, _max);
104 hkl_axis_set_value_unit(_axis_r_real, _read_real);
105 hkl_axis_set_value_unit(_axis_w_real, _write_real);
106 hkl_axis_set_range_unit(_axis_r_real, _min, _max);
107 hkl_axis_set_range_unit(_axis_w_real, _min, _max);
110 void AxisAdapter::from_proxy(bool simulated)
112 try{
113 if(this->is_ready()){
114 std::vector<double> positions;
116 // read the position attribute
117 _proxy->read() >> positions;
118 _read_real = positions[0];
119 _write_real = positions[1];
121 Tango::AttributeInfo info = _proxy->get_config();
123 // Check if min and max value have been set
124 char const *token;
125 token = info.min_value.c_str();
126 if (!strstr(token, "Not specified"))
127 _min = atof(token);
129 token = info.max_value.c_str();
130 if (!strstr(token, "Not specified"))
131 _max = atof(token);
133 // read the state
134 _state = _proxy->state();
136 if(!simulated){
137 _read = _read_real;
138 _write = _write_real;
139 }else{
140 _read = hkl_axis_get_value_unit(_axis_r);
141 _write = hkl_axis_get_value_unit(_axis_w);
145 catch(Tango::DevFailed &)
147 _state = Tango::FAULT;
151 void AxisAdapter::to_proxy(void)
153 #ifdef WRITE_TO_PROXY_ALLOWED
154 try{
155 if(this->is_ready()){
156 Tango::DeviceAttribute attr(_proxy_name, _write_real);
157 _state = Tango::MOVING;
158 _proxy->write(attr);
161 catch(Tango::DevFailed &)
163 _state = Tango::FAULT;
165 #endif
168 void AxisAdapter::stop(void)
170 // let the error propagate if stop failed.
171 if(this->is_ready())
172 _proxy->get_device_proxy()->command_inout(_proxy_stop_command_name);
175 void AxisAdapter::on(void)
177 if(this->is_ready())
178 try{
179 _proxy->get_device_proxy()->command_inout(_proxy_on_command_name);
181 catch (Tango::DevFailed &)
183 _state = Tango::FAULT;
184 // do nothing if the command is not present.