Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
//=============================================================================
//
// file : MythenPool.cpp
//
// description : Monitoring Pilatus Sensors through the device Lima Pilatus
//
// project : MythenWAXS Project
//
// $Author: noureddine $
//
// copyleft : Synchrotron SOLEIL
// L'Orme des merisiers - Saint Aubin
// BP48 - 91192 Gif sur Yvette
// FRANCE
//=============================================================================
#include <tango.h>
//- YAT stuff
#include <yat/utils/XString.h>
#include <iomanip>
#include "MythenPool.h"
//@@@@TODO
//temporary and only for simulation
const int CHANNEL_SIZE = 5;
const int PIXEL_SIZE = 0.05;
//////////////////////////////
namespace MythenWAXS_ns
{
//----------------------------------------------------------------------------------------------------------------------
//- MythenPool Ctor
//----------------------------------------------------------------------------------------------------------------------
MythenPool::MythenPool(Tango::DeviceImpl *dev, PropertyLoader prop)
: yat4tango::DeviceTask(dev),
m_device(dev),
m_prop(prop)
{
DEBUG_STREAM << "MythenPool::MythenPool() - [BEGIN]" << endl;
set_state(Tango::INIT);
set_periodic_msg_period(TASK_PERIODIC__MS);//1Hz
enable_periodic_msg(false);
DEBUG_STREAM << "MythenPool::MythenPool() - [END]" << endl;
}
//----------------------------------------------------------------------------------------------------------------------
//- MythenPool Dtor
//----------------------------------------------------------------------------------------------------------------------
MythenPool::~MythenPool()
{
DEBUG_STREAM << "MythenPool::~MythenPool() - [BEGIN]" << endl;
DEBUG_STREAM << "MythenPool::~MythenPool() - [END]" << endl;
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::set_state(Tango::DevState state)
{
{
//- AutoLock the following
yat::MutexLock scoped_lock(m_proxy_lock);
m_state = state;
}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
Tango::DevState MythenPool::get_state()
{
{
//- AutoLock the following
yat::MutexLock scoped_lock(m_proxy_lock);
compute_state_status();
return m_state;
}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::set_status(const std::string& status)
{
{
//- AutoLock the following
yat::MutexLock scoped_lock(m_proxy_lock);
m_status.str("");
m_status << status.c_str();
}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
std::string MythenPool::get_status()
{
{
//- AutoLock the following
yat::MutexLock scoped_lock(m_proxy_lock);
return (m_status.str());
}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::write_energy(double value)
{
yat::MutexLock scoped_lock(m_proxy_lock);
for (unsigned i = 0; i < m_prop.proxy_mythen_names.size(); i++)
{
long nb_channels = 0;
m_proxy_mythens.at(i)->read_attribute("nbChannels", nb_channels, m_prop.proxy_nb_retry);
for (unsigned j = 0; j < nb_channels; j++)
{
stringstream ss("");
std::string name("");
name = "energy";
ss << name << std::setfill('0') << std::setw(2) << j + 1;//index of energy start with '01'
m_proxy_mythens.at(i)->write_attribute(ss.str(), value, m_prop.proxy_nb_retry);
}
}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::write_threshold(double value)
{
yat::MutexLock scoped_lock(m_proxy_lock);
for (unsigned i = 0; i < m_prop.proxy_mythen_names.size(); i++)
{
long nb_channels = 0;
m_proxy_mythens.at(i)->read_attribute("nbChannels", nb_channels, m_prop.proxy_nb_retry);
for (unsigned j = 0; j < nb_channels; j++)
{
stringstream ss("");
std::string name("");
name = "threshold";
ss << name << std::setfill('0') << std::setw(2) << j + 1;//index of threshold start with '01'
m_proxy_mythens.at(i)->write_attribute(ss.str(), value, m_prop.proxy_nb_retry);
}
}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::write_exposure_time(double value)
{
yat::MutexLock scoped_lock(m_proxy_lock);
for (unsigned i = 0; i < m_prop.proxy_mythen_names.size(); i++)
{
m_proxy_mythens.at(i)->write_attribute("exposureTime", value, m_prop.proxy_nb_retry);
}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::write_nb_frames(long value)
{
yat::MutexLock scoped_lock(m_proxy_lock);
for (unsigned i = 0; i < m_prop.proxy_mythen_names.size(); i++)
{
m_proxy_mythens.at(i)->write_attribute("nbFrames", value, m_prop.proxy_nb_retry);
}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::snap()
{
yat::MutexLock scoped_lock(m_proxy_lock);
for (unsigned i = 0; i < m_prop.proxy_mythen_names.size(); i++)
{
////m_proxy_mythens.at(i)->command("Snap", m_prop.proxy_nb_retry);
}
enable_periodic_msg(true); //in order to compute_frame_xy()
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::stop()
{
yat::MutexLock scoped_lock(m_proxy_lock);
for (unsigned i = 0; i < m_prop.proxy_mythen_names.size(); i++)
{
////m_proxy_mythens.at(i)->command("Stop", m_prop.proxy_nb_retry);
}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
const std::vector<double>& MythenPool::get_frame_x()
{
yat::MutexLock scoped_lock(m_proxy_lock);
return m_x_values;
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
const std::vector<double>& MythenPool::get_frame_y()
{
yat::MutexLock scoped_lock(m_proxy_lock);
return m_y_values;
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::compute_frame_xy()
{
yat::MutexLock scoped_lock(m_proxy_lock);
m_x_values.clear();
m_y_values.clear();
unsigned nb_total_channel = 0;
//get the motor position tth2C
double theta_position;
m_proxy_motor->read_attribute("position", theta_position, m_prop.proxy_nb_retry);
//compute tth
for (unsigned i = 0; i < m_prop.proxy_mythen_names.size(); i++)
{
long nb_channels = 0;
m_proxy_mythens.at(i)->read_attribute("nbChannels", nb_channels, m_prop.proxy_nb_retry);
for (unsigned j = 0; j < nb_channels; j++)
{
std::vector<double> frame;
stringstream ss("");
std::string name("");
name = "frame";
ss << name << std::setfill('0') << std::setw(2) << j + 1;//index of threshold start with '01'
m_proxy_mythens.at(i)->read_attribute(ss.str(), frame, m_prop.proxy_nb_retry);
for (unsigned k = 0;k < CHANNEL_SIZE;k++)
{
double twotheta = theta_position -
m_deltas.at(nb_total_channel + j) -
atan((k - m_centers.at(nb_total_channel + j)) * PIXEL_SIZE / (m_distances.at(nb_total_channel + j)));
m_x_values.push_back(twotheta*(180.0/M_PI));
m_y_values.push_back(frame.at(k));
}
}
nb_total_channel += nb_channels;
}
// INFO_STREAM << "m_twotheta_values.size() = " << m_twotheta_values.size() << std::endl;
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::decode_equation_parameters()
{
try
{
INFO_STREAM << "\n" << endl;
INFO_STREAM << "----------------------------------------------------" << endl;
INFO_STREAM << "Decode the EquationPrameters Device Property ..." << endl;
INFO_STREAM << "----------------------------------------------------" << endl;
//there is 9 lines
//each line contains : delta=value;center=value;distance=value;
for (unsigned i = 0; i < m_prop.equation_parameters.size(); i++)
{
Tokenizer line(m_prop.equation_parameters.at(i), ';');
if (line.count() != 3)
{
Tango::Except::throw_exception( "LOGIC_ERROR",
"Check the format of the 'EquationParameters' Device Property !",
"MythenPool::decode_equation_parameters()");
}
for (int itoken = 0; itoken < line.count(); itoken++)
{
try
{
string item_name;
double item_val;
//try catch used to check the end of items
Tokenizer field(line[itoken], '=');
if (field.count() != 2)
{
Tango::Except::throw_exception( "LOGIC_ERROR",
"Check the format of the 'EquationParameters' Device Property !",
"MythenPool::decode_equation_parameters()");
}
item_name = field[0];
istringstream iss_val(field[1]);//val
iss_val >> item_val;
if (item_name == "delta")
m_deltas.push_back(item_val);
else if (item_name == "center")
m_centers.push_back(item_val);
else if (item_name == "distance")
m_distances.push_back(item_val);
}
catch (exception& e)
{
//indicate that token has not the good format or no token anymore, Nothing to do
//DEBUG_STREAM<<"token has not the good format or no token anymore ! "<<endl;
continue;
}
}
}
//display values
for (int i = 0; i < m_deltas.size(); i++)
{
INFO_STREAM << "m_deltas[i] = " << m_deltas[i] << std::endl;
}
INFO_STREAM << "\n" << std::endl;
for (int i = 0; i < m_centers.size(); i++)
{
INFO_STREAM << "m_centers[i] = " << m_centers[i] << std::endl;
}
INFO_STREAM << "\n" << std::endl;
for (int i = 0; i < m_distances.size(); i++)
{
INFO_STREAM << "m_distances[i] = " << m_distances[i] << std::endl;
}
INFO_STREAM << "\n" << std::endl;
}
catch (exception& e)
{
//indicate that token has not the good format or no token anymore
ERROR_STREAM << e.what() << endl;
ostringstream ossMsgErr;
ossMsgErr << e.what();
Tango::Except::throw_exception("LOGIC_ERROR",
ossMsgErr.str().c_str(),
"MythenPool::decode_equation_parameters()");
}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::compute_state_status()
{
{
//- AutoLock the following
yat::MutexLock scoped_lock(m_proxy_lock);
DEBUG_STREAM << "MythenPool::compute_state_status() - [BEGIN]" << endl;
unsigned proxy_num;
Tango::DevState global_state;
stringstream proxy_status;
proxy_status.str("");
vector <Tango::DevState> states;
unsigned int fault_count = 0;
unsigned int running_count = 0;
unsigned int standby_count = 0;
if (m_state != Tango::INIT)
{
//////////////////////////////////////////////////////////
try
{
//more than one proxy mythen devices
for (unsigned i = 0; i < m_prop.proxy_mythen_names.size(); i++)
{
proxy_num = i;
Tango::DevState simple_state;
m_proxy_mythens.at(i)->command_out("State", simple_state, m_prop.proxy_nb_retry);
proxy_status << "Proxy[" << proxy_num << "]\t\t" << " --> " << Tango::DevStateName[simple_state] << "\n";
states.push_back(simple_state);
}
//only one proxy motor device
Tango::DevState simple_state;
m_proxy_motor->command_out("State", simple_state, m_prop.proxy_nb_retry);
proxy_status << "Proxy[" << proxy_num << "]\t\t" << " --> " << Tango::DevStateName[simple_state] << "\n";
states.push_back(simple_state);
//counting each kind of state
for (int i = 0; i < states.size(); i++)
{
switch (states.at(i))
{
case Tango::FAULT: fault_count++;
break;
case Tango::STANDBY: standby_count++;
break;
case Tango::RUNNING: running_count++;
break;
case Tango::MOVING: running_count++;
break;
default: fault_count++;
break; //NOP
}
}
//////
if (fault_count)
{
global_state = Tango::FAULT; //if at least one device is in fault
}
else if (running_count)
{
global_state = Tango::RUNNING; //if all devices are RUNNING
}
else if (standby_count == states.size())
{
global_state = Tango::STANDBY; //if all devices are STANDBY
}
else
{
global_state = Tango::ALARM; //Otherwise ALARM
}
}
catch (Tango::DevFailed& e)
{
ERROR_STREAM << e << ENDLOG;
proxy_status << "Proxy[" << proxy_num << "]\t\t" << " --> Unable to read State !\n";
global_state = Tango::FAULT;
}
//////////////////////////////////////////////////////////
//if pilatus is in "FAULTy" state
if (global_state == Tango::FAULT || global_state == Tango::OFF || global_state == Tango::INIT)
{
set_state(Tango::FAULT);
std::stringstream status;
status.str("");
status << "At least one Device is in FAULTy state !\n";
status << "---------------------------------------------------\n";
set_status(status.str());
DEBUG_STREAM << "MythenPool::compute_state_status() - [END]" << endl;
return;
}
//if pilatus is in "RUNNING" state
if (global_state == Tango::RUNNING || global_state == Tango::MOVING)
{
set_state(Tango::RUNNING);
std::stringstream status;
status.str("");
status << "Acquisition is Running ...\n";
status << "---------------------------------------------------\n";
set_status(status.str());
DEBUG_STREAM << "MythenPool::compute_state_status() - [END]" << endl;
return;
}
if (global_state == Tango::STANDBY)
{
//STANDBY
set_state(Tango::STANDBY);
std::stringstream status;
status.str("");
status << "Waiting for request ...\n";
status << "---------------------------------------------------\n";
set_status(status.str());
DEBUG_STREAM << "MythenPool::compute_state_status() - [END]" << endl;
return;
}
if (global_state == Tango::ALARM)
{
//ALARM
set_state(Tango::ALARM);
std::stringstream status;
status.str("");
status << "At least one Device is not in the expected state !\n";
status << "---------------------------------------------------\n";
set_status(status.str());
DEBUG_STREAM << "MythenPool::compute_state_status() - [END]" << endl;
return;
}
}
}
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
void MythenPool::process_message(yat::Message& msg) throw (Tango::DevFailed)
{
try
{
switch (msg.type())
{
//-----------------------------------------------------
case yat::TASK_INIT:
{
INFO_STREAM << " " << std::endl;
INFO_STREAM << "--------------------------------------------" << std::endl;
INFO_STREAM << "-> MythenPool::TASK_INIT" << endl;
INFO_STREAM << "--------------------------------------------" << std::endl;
std::string proxy_name;
try
{
INFO_STREAM << "" << endl;
//create proxies to Mthen2Detector
for (int i = 0;i < m_prop.proxy_mythen_names.size();i++)
{
proxy_name = m_prop.proxy_mythen_names.at(i);
INFO_STREAM << "\t- Create the Proxy to the device (" << proxy_name << ")" << endl;
m_proxy_mythens.push_back(new yat4tango::DeviceProxyHelper(proxy_name, m_device));
m_proxy_mythens[i]->get_device_proxy()->ping();
}
//create proxy to the motor
proxy_name = m_prop.proxy_motor_name;
INFO_STREAM << "\t- Create the Proxy to the motor (" << proxy_name << ")" << endl;
m_proxy_motor.reset(new yat4tango::DeviceProxyHelper(proxy_name, m_device));
m_proxy_motor->get_device_proxy()->ping();
}
catch (const std::bad_alloc& e)
{
ERROR_STREAM << e.what() << ENDLOG;
std::stringstream status;
status << "Unable to create proxy to ( " << proxy_name << " ) :\n"
<< "--> Memory allocation exception.\n"
<< endl;
set_state(Tango::INIT);
set_status(status.str());
return;
}
catch (Tango::DevFailed& e)
{
ERROR_STREAM << e << ENDLOG;
std::stringstream status;
status << "Unable to create proxy to ( " << proxy_name << " ) :\n"
<< "--> check the value of the 'ProxiesNames' property.\n"
<< "--> check the state of the device ( " << proxy_name << " ).\n"
<< endl;
set_state(Tango::INIT);
set_status(status.str());
return;
}
//decode constants from equation_constants property
decode_equation_parameters();
//set state STANDBY if everything is ok
set_state(Tango::STANDBY);
//enable_periodic_msg(true);
}
break;
//-----------------------------------------------------
case yat::TASK_EXIT:
{
INFO_STREAM << " " << std::endl;
INFO_STREAM << "--------------------------------------------" << std::endl;
INFO_STREAM << "-> MythenPool::TASK_EXIT" << endl;
INFO_STREAM << "--------------------------------------------" << std::endl;
}
break;
//-----------------------------------------------------
case yat::TASK_TIMEOUT:
{
INFO_STREAM << " " << std::endl;
INFO_STREAM << "--------------------------------------------" << std::endl;
INFO_STREAM << "-> MythenPool::TASK_TIMEOUT" << endl;
INFO_STREAM << "--------------------------------------------" << std::endl;
}
break;
//-----------------------------------------------------
case yat::TASK_PERIODIC:
{
//- AutoLock the following
yat::MutexLock scoped_lock(m_proxy_lock);
Tango::DevState state = get_state();
if (state == Tango::STANDBY)
{
DEBUG_STREAM << " " << std::endl;
DEBUG_STREAM << "--------------------------------------------" << std::endl;
DEBUG_STREAM << "-> MythenPool::TASK_PERIODIC" << endl;
DEBUG_STREAM << "--------------------------------------------" << std::endl;
try
{
INFO_STREAM << "--------------------------------------------" << std::endl;
INFO_STREAM << "Compute frameX & frameY" << endl;
INFO_STREAM << "--------------------------------------------" << std::endl;
compute_frame_xy();
enable_periodic_msg(false);
}
catch (Tango::DevFailed &df)
{
ERROR_STREAM << df << endl;
std::stringstream status;
status.str("");
status << "Origin\t: " << df.errors[0].origin << endl;
status << "Desc\t: " << df.errors[0].desc << endl;
set_status(status.str());
set_state(Tango::FAULT);
}
}
}
break;
//-----------------------------------------------------
}
}
catch (yat::Exception& ex)
{
//- TODO Error Handling
ex.dump();
std::stringstream error_msg("");
error_msg << "Origin\t: " << ex.errors[0].origin << endl;
error_msg << "Desc\t: " << ex.errors[0].desc << endl;
error_msg << "Reason\t: " << ex.errors[0].reason << endl;
ERROR_STREAM << "Exception from - MythenPool::process_message() : " << error_msg.str() << endl;
throw;
}
}
} // namespace