source: trunk/FACT++/src/ratecontrol.cc@ 14553

Last change on this file since 14553 was 14493, checked in by tbretz, 12 years ago
The sign was displayed twice -- fixed.
File size: 17.8 KB
Line 
1#include <valarray>
2
3#include "Dim.h"
4#include "Event.h"
5#include "Shell.h"
6#include "StateMachineDim.h"
7#include "Connection.h"
8#include "Configuration.h"
9#include "Console.h"
10#include "Converter.h"
11//#include "PixelMap.h"
12
13#include "tools.h"
14
15#include "LocalControl.h"
16
17#include "HeadersFTM.h"
18#include "HeadersRateScan.h"
19#include "HeadersRateControl.h"
20
21namespace ba = boost::asio;
22namespace bs = boost::system;
23namespace dummy = ba::placeholders;
24
25using namespace std;
26
27// ------------------------------------------------------------------------
28
29#include "DimDescriptionService.h"
30#include "DimState.h"
31
32// ------------------------------------------------------------------------
33
34class StateMachineRateControl : public StateMachineDim//, public DimInfoHandler
35{
36private:
37 bool fTriggerOn;
38
39 vector<bool> fBlock;
40
41 DimVersion fDim;
42 DimDescribedState fDimFTM;
43 DimDescribedState fDimRS;
44
45 DimDescribedService fDimThreshold;
46
47 float fTargetRate;
48 float fTriggerRate;
49
50 uint16_t fThresholdMin;
51 uint16_t fThresholdReference;
52
53 bool fVerbose;
54
55 uint64_t fCounter;
56
57 bool CheckEventSize(const EventImp &evt, size_t size)
58 {
59 if (size_t(evt.GetSize())==size)
60 return true;
61
62 if (evt.GetSize()==0)
63 return false;
64
65 ostringstream msg;
66 msg << evt.GetName() << " - Received event has " << evt.GetSize() << " bytes, but expected " << size << ".";
67 Fatal(msg);
68 return false;
69 }
70
71 vector<uint16_t> fThresholds;
72
73 void PrintThresholds(const FTM::DimStaticData &sdata)
74 {
75 //if (!fVerbose)
76 // return;
77
78 if (fThresholds.size()==0)
79 return;
80
81 Out() << "Min. DAC=" << fThresholdMin << endl;
82
83 int t=0;
84 for (t=0; t<160; t++)
85 if (sdata.fThreshold[t]!=fThresholds[t])
86 break;
87
88 if (t==160)
89 return;
90
91 for (int j=0; j<10; j++)
92 {
93 for (int k=0; k<4; k++)
94 {
95 for (int i=0; i<4; i++)
96 if (fThresholds[i+k*4+j*16]!=fThresholdMin)
97 Out() << setw(3) << fThresholds[i+k*4+j*16] << " ";
98 else
99 Out() << " - ";
100 Out() << " ";
101 }
102 Out() << endl;
103 }
104 Out() << endl;
105 }
106
107 void Step(int idx, float step)
108 {
109 uint16_t diff = fThresholds[idx]+int16_t(truncf(step));
110 if (diff<fThresholdMin)
111 diff=fThresholdMin;
112
113 if (diff==fThresholds[idx])
114 return;
115
116 if (fVerbose)
117 {
118 Out() << idx/40 << "|" << (idx/4)%10 << "|" << idx%4;
119 Out() << (step>0 ? " += " : " -= ");
120 Out() << fabs(step) << " (" << diff << ")" << endl;
121 }
122
123 const uint32_t val[2] = { idx, diff };
124 DimClient::sendCommandNB("FTM_CONTROL/SET_THRESHOLD", (void*)val, 8);
125
126 fBlock[idx/4] = true;
127 }
128
129 void ProcessPatches(const FTM::DimTriggerRates &sdata)
130 {
131
132 // Caluclate Median and deviation
133 vector<float> medb(sdata.fBoardRate, sdata.fBoardRate+40);
134 vector<float> medp(sdata.fPatchRate, sdata.fPatchRate+160);
135
136 sort(medb.begin(), medb.end());
137 sort(medp.begin(), medp.end());
138
139 vector<float> devb(40);
140 for (int i=0; i<40; i++)
141 devb[i] = fabs(sdata.fBoardRate[i]-medb[i]);
142
143 vector<float> devp(160);
144 for (int i=0; i<160; i++)
145 devp[i] = fabs(sdata.fPatchRate[i]-medp[i]);
146
147 sort(devb.begin(), devb.end());
148 sort(devp.begin(), devp.end());
149
150 double mb = (medb[19]+medb[20])/2;
151 double mp = (medp[79]+medp[80])/2;
152
153 double db = devb[27];
154 double dp = devp[109];
155
156 // If any is zero there is something wrong
157 if (mb==0 || mp==0 || db==0 || dp==0)
158 return;
159
160 if (fVerbose)
161 {
162 Out() << "Patch: Median=" << mp << " Dev=" << dp << endl;
163 Out() << "Board: Median=" << mb << " Dev=" << db << endl;
164 }
165
166 for (int i=0; i<40; i++)
167 {
168 if (fBlock[i])
169 {
170 fBlock[i] = false;
171 continue;
172 }
173
174 int maxi = -1;
175
176 const float dif = fabs(sdata.fBoardRate[i]-mb)/db;
177 if (dif>5)
178 {
179 if (fVerbose)
180 Out() << "B" << i << ": " << dif << endl;
181
182 float max = sdata.fPatchRate[i*4];
183 maxi = 0;
184
185 for (int j=1; j<4; j++)
186 if (sdata.fPatchRate[i*4+j]>max)
187 {
188 max = sdata.fPatchRate[i*4+j];
189 maxi = j;
190 }
191 }
192
193 for (int j=0; j<4; j++)
194 {
195 // For the noise pixel correct down to median+3*deviation
196 if (maxi==j)
197 {
198 // This is the step which has to be performed to go from
199 // a NSB rate of sdata.fPatchRate[i*4+j]
200
201
202 const float step = (log10(sdata.fPatchRate[i*4+j])-log10(mp+5*dp))/0.039;
203 // * (dif-5)/dif
204 Step(i*4+j, step);
205 continue;
206 }
207
208 // For pixels below the meadian correct also back to median+3*deviation
209 if (sdata.fPatchRate[i*4+j]<mp)
210 {
211 const float step = (log10(sdata.fPatchRate[i*4+j])-log10(mp+3*dp))/0.039;
212 Step(i*4+j, step);
213 continue;
214 }
215
216 const float step = -1.5*(log10(mp+dp)-log10(mp))/0.039;
217 Step(i*4+j, step);
218 }
219 }
220 }
221
222 int ProcessCamera(const FTM::DimTriggerRates &sdata)
223 {
224 if (fCounter++==0)
225 return GetCurrentState();
226
227 // Caluclate Median and deviation
228 vector<float> medb(sdata.fBoardRate, sdata.fBoardRate+40);
229
230 sort(medb.begin(), medb.end());
231
232 vector<float> devb(40);
233 for (int i=0; i<40; i++)
234 devb[i] = fabs(sdata.fBoardRate[i]-medb[i]);
235
236 sort(devb.begin(), devb.end());
237
238 double mb = (medb[19]+medb[20])/2;
239 double db = devb[27];
240
241 // If any is zero there is something wrong
242 if (mb==0 || db==0)
243 {
244 Warn("The median or the deviation of all board rates is zero... cannot calibrate.");
245 return GetCurrentState();
246 }
247
248 double avg = 0;
249 int num = 0;
250
251 for (int i=0; i<40; i++)
252 {
253 if ( fabs(sdata.fBoardRate[i]-mb)<2.5*db)
254 {
255 avg += sdata.fBoardRate[i];
256 num++;
257 }
258 }
259
260 fTriggerRate = avg/num * 40;
261
262 if (fVerbose)
263 {
264 Out() << "Board: Median=" << mb << " Dev=" << db << endl;
265 Out() << "Camera: " << fTriggerRate << " (" << sdata.fTriggerRate << ", n=" << num << ")" << endl;
266 Out() << "Target: " << fTargetRate << endl;
267 }
268
269 if (sdata.fTriggerRate<fTriggerRate)
270 fTriggerRate = sdata.fTriggerRate;
271
272 // ----------------------
273
274 /*
275 if (avg>0 && avg<fTargetRate)
276 {
277 // I am assuming here (and at other places) the the answer from the FTM when setting
278 // the new threshold always arrives faster than the next rate update.
279 fThresholdMin = fThresholds[0];
280 Out() << "Setting fThresholdMin to " << fThresholds[0] << endl;
281 }
282 */
283
284 if (fTriggerRate>0 && fTriggerRate<fTargetRate)
285 {
286 fThresholds.assign(160, fThresholdMin);
287 fDimThreshold.Update(fThresholdMin);
288 return RateControl::State::kGlobalThresholdSet;
289 }
290
291 // This is a step towards a threshold at which the NSB rate is equal the target rate
292 // +1 to avoid getting a step of 0
293 const float step = (log10(fTriggerRate)-log10(fTargetRate))/0.039 + 1;
294
295 const uint16_t diff = fThresholdMin+int16_t(truncf(step));
296 if (diff<=fThresholdMin)
297 {
298 fDimThreshold.Update(fThresholdMin);
299 return RateControl::State::kGlobalThresholdSet;
300 }
301
302 if (fVerbose)
303 {
304 //Out() << idx/40 << "|" << (idx/4)%10 << "|" << idx%4;
305 Out() << fThresholdMin;
306 Out() << (step>0 ? " += " : " -= ");
307 Out() << step << " (" << diff << ")" << endl;
308 }
309
310 const uint32_t val[2] = { -1, diff };
311 DimClient::sendCommandNB("FTM_CONTROL/SET_THRESHOLD", (void*)val, 8);
312
313 fThresholdMin = diff;
314
315 return GetCurrentState();
316 }
317
318 int HandleStaticData(const EventImp &evt)
319 {
320 if (!CheckEventSize(evt, sizeof(FTM::DimStaticData)))
321 return GetCurrentState();
322
323 const FTM::DimStaticData &sdata = *static_cast<const FTM::DimStaticData*>(evt.GetData());
324 fTriggerOn = sdata.HasTrigger();
325
326 PrintThresholds(sdata);
327
328 fThresholds.assign(sdata.fThreshold, sdata.fThreshold+160);
329
330 return GetCurrentState();
331 }
332
333 int HandleTriggerRates(const EventImp &evt)
334 {
335 if (fThresholds.size()==0)
336 return GetCurrentState();
337
338 if (GetCurrentState()<=RateControl::State::kConnected ||
339 GetCurrentState()==RateControl::State::kGlobalThresholdSet)
340 return GetCurrentState();
341
342 if (!CheckEventSize(evt, sizeof(FTM::DimTriggerRates)))
343 return GetCurrentState();
344
345 const FTM::DimTriggerRates &sdata = *static_cast<const FTM::DimTriggerRates*>(evt.GetData());
346
347 if (GetCurrentState()==RateControl::State::kSettingGlobalThreshold)
348 return ProcessCamera(sdata);
349
350 if (GetCurrentState()==RateControl::State::kInProgress)
351 ProcessPatches(sdata);
352
353 return GetCurrentState();
354 }
355
356 int Calibrate()
357 {
358 if (!fTriggerOn)
359 {
360 Info("Trigger not switched on... CALIBRATE command ignored.");
361 return RateControl::State::kGlobalThresholdSet;
362 }
363
364 const int32_t val[2] = { -1, fThresholdReference };
365 Dim::SendCommand("FTM_CONTROL/SET_THRESHOLD", val);
366
367 fThresholds.assign(160, fThresholdReference);
368
369 fThresholdMin = fThresholdReference;
370 fTriggerRate = -1;
371 fCounter = 0;
372
373 ostringstream out;
374 out << "Rate calibration started at a threshold of " << fThresholdReference << " with a target rate of " << fTargetRate << " Hz";
375 Info(out);
376
377 return RateControl::State::kSettingGlobalThreshold;
378 }
379
380 int StopRC()
381 {
382 return RateControl::State::kConnected;
383 }
384
385 int SetMinThreshold(const EventImp &evt)
386 {
387 if (!CheckEventSize(evt, 4))
388 return kSM_FatalError;
389
390 // FIXME: Check missing
391
392 fThresholdReference = evt.GetUShort();
393
394 return GetCurrentState();
395 }
396
397 int SetTargetRate(const EventImp &evt)
398 {
399 if (!CheckEventSize(evt, 4))
400 return kSM_FatalError;
401
402 fTargetRate = evt.GetFloat();
403
404 return GetCurrentState();
405 }
406
407 int Print() const
408 {
409 Out() << fDim << endl;
410 Out() << fDimFTM << endl;
411 Out() << fDimRS << endl;
412
413 return GetCurrentState();
414 }
415
416 int SetVerbosity(const EventImp &evt)
417 {
418 if (!CheckEventSize(evt, 1))
419 return kSM_FatalError;
420
421 fVerbose = evt.GetBool();
422
423 return GetCurrentState();
424 }
425
426 int Execute()
427 {
428 if (!fDim.online())
429 return RateControl::State::kDimNetworkNA;
430
431 // All subsystems are not connected
432 if (fDimFTM.state()<FTM::State::kConnected)
433 return RateControl::State::kDisconnected;
434
435 const bool inprog = fTriggerOn && fDimRS.state()<RateScan::State::kConfiguring;
436
437 switch (GetCurrentState())
438 {
439 case RateControl::State::kSettingGlobalThreshold:
440 return RateControl::State::kSettingGlobalThreshold;
441
442 case RateControl::State::kGlobalThresholdSet:
443 if (!inprog)
444 return RateControl::State::kGlobalThresholdSet;
445
446 case RateControl::State::kInProgress:
447 if (inprog)
448 return RateControl::State::kInProgress;
449 }
450
451 return RateControl::State::kConnected;
452 }
453
454public:
455 StateMachineRateControl(ostream &out=cout) : StateMachineDim(out, "RATE_CONTROL"),
456 fTriggerOn(false), fBlock(40),
457 fDimFTM("FTM_CONTROL"),
458 fDimRS("RATE_SCAN"),
459 fDimThreshold("RATE_CONTROL/THRESHOLD", "S:1", "Resulting threshold after calibration|threshold[dac]:Resulting threshold from calibration")
460 {
461 // ba::io_service::work is a kind of keep_alive for the loop.
462 // It prevents the io_service to go to stopped state, which
463 // would prevent any consecutive calls to run()
464 // or poll() to do nothing. reset() could also revoke to the
465 // previous state but this might introduce some overhead of
466 // deletion and creation of threads and more.
467
468 fDim.Subscribe(*this);
469 fDimFTM.Subscribe(*this);
470 fDimRS.Subscribe(*this);
471
472 Subscribe("FTM_CONTROL/TRIGGER_RATES")
473 (bind(&StateMachineRateControl::HandleTriggerRates, this, placeholders::_1));
474 Subscribe("FTM_CONTROL/STATIC_DATA")
475 (bind(&StateMachineRateControl::HandleStaticData, this, placeholders::_1));
476
477 // State names
478 AddStateName(RateControl::State::kDimNetworkNA, "DimNetworkNotAvailable",
479 "The Dim DNS is not reachable.");
480
481 AddStateName(RateControl::State::kDisconnected, "Disconnected",
482 "The Dim DNS is reachable, but the required subsystems are not available.");
483
484 AddStateName(RateControl::State::kConnected, "Connected",
485 "All needed subsystems are connected to their hardware, no action is performed.");
486
487 AddStateName(RateControl::State::kSettingGlobalThreshold, "Calibrating",
488 "A global minimum thrshold is currently determined.");
489
490 AddStateName(RateControl::State::kGlobalThresholdSet, "GlobalThresholdSet",
491 "A global threshold has ben set, waiting for the trigger to be switched on.");
492
493 AddStateName(RateControl::State::kInProgress, "InProgress",
494 "Rate control in progress.");
495
496 AddEvent("CALIBRATE")
497 (bind(&StateMachineRateControl::Calibrate, this))
498 ("Start a search for a reasonable minimum global threshold");
499
500 AddEvent("STOP", "", RateControl::State::kSettingGlobalThreshold, RateControl::State::kInProgress)
501 (bind(&StateMachineRateControl::StopRC, this))
502 ("Stop a calibration or ratescan in progress");
503
504 AddEvent("SET_MIN_THRESHOLD", "I:1")
505 (bind(&StateMachineRateControl::SetMinThreshold, this, placeholders::_1))
506 ("Set a minimum threshold at which th rate control starts calibrating");
507
508 AddEvent("SET_TARGET_RATE", "F:1")
509 (bind(&StateMachineRateControl::SetTargetRate, this, placeholders::_1))
510 ("Set a target trigger rate for the calibration");
511
512 AddEvent("PRINT")
513 (bind(&StateMachineRateControl::Print, this))
514 ("Print current status");
515
516 AddEvent("SET_VERBOSE", "B")
517 (bind(&StateMachineRateControl::SetVerbosity, this, placeholders::_1))
518 ("set verbosity state"
519 "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
520
521 }
522
523 int EvalOptions(Configuration &conf)
524 {
525 fVerbose = !conf.Get<bool>("quiet");
526
527 fThresholdReference = 300;
528 fThresholdMin = 300;
529 fTargetRate = 75;
530
531 return -1;
532 }
533};
534
535// ------------------------------------------------------------------------
536
537#include "Main.h"
538
539template<class T>
540int RunShell(Configuration &conf)
541{
542 return Main::execute<T, StateMachineRateControl>(conf);
543}
544
545void SetupConfiguration(Configuration &conf)
546{
547 po::options_description control("Rate control options");
548 control.add_options()
549 ("quiet,q", po_bool(), "Disable printing more informations during rate control.")
550 //("max-wait", var<uint16_t>(150), "The maximum number of seconds to wait to get the anticipated resolution for a point.")
551 // ("resolution", var<double>(0.05) , "The minimum resolution required for a single data point.")
552 ;
553
554 conf.AddOptions(control);
555}
556
557/*
558 Extract usage clause(s) [if any] for SYNOPSIS.
559 Translators: "Usage" and "or" here are patterns (regular expressions) which
560 are used to match the usage synopsis in program output. An example from cp
561 (GNU coreutils) which contains both strings:
562 Usage: cp [OPTION]... [-T] SOURCE DEST
563 or: cp [OPTION]... SOURCE... DIRECTORY
564 or: cp [OPTION]... -t DIRECTORY SOURCE...
565 */
566void PrintUsage()
567{
568 cout <<
569 "The ratecontrol program is a keep the rate reasonable low.\n"
570 "\n"
571 "Usage: ratecontrol [-c type] [OPTIONS]\n"
572 " or: ratecontrol [OPTIONS]\n";
573 cout << endl;
574}
575
576void PrintHelp()
577{
578 Main::PrintHelp<StateMachineRateControl>();
579
580 /* Additional help text which is printed after the configuration
581 options goes here */
582
583 /*
584 cout << "bla bla bla" << endl << endl;
585 cout << endl;
586 cout << "Environment:" << endl;
587 cout << "environment" << endl;
588 cout << endl;
589 cout << "Examples:" << endl;
590 cout << "test exam" << endl;
591 cout << endl;
592 cout << "Files:" << endl;
593 cout << "files" << endl;
594 cout << endl;
595 */
596}
597
598int main(int argc, const char* argv[])
599{
600 Configuration conf(argv[0]);
601 conf.SetPrintUsage(PrintUsage);
602 Main::SetupConfiguration(conf);
603 SetupConfiguration(conf);
604
605 if (!conf.DoParse(argc, argv, PrintHelp))
606 return 127;
607
608 if (!conf.Has("console"))
609 return RunShell<LocalStream>(conf);
610
611 if (conf.Get<int>("console")==0)
612 return RunShell<LocalShell>(conf);
613 else
614 return RunShell<LocalConsole>(conf);
615
616 return 0;
617}
Note: See TracBrowser for help on using the repository browser.