| 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 |
|
|---|
| 11 | #include "tools.h"
|
|---|
| 12 |
|
|---|
| 13 | #include "LocalControl.h"
|
|---|
| 14 |
|
|---|
| 15 | #include "HeadersFTM.h"
|
|---|
| 16 | #include "HeadersRateScan.h"
|
|---|
| 17 | #include "HeadersRateControl.h"
|
|---|
| 18 |
|
|---|
| 19 | namespace ba = boost::asio;
|
|---|
| 20 | namespace bs = boost::system;
|
|---|
| 21 | namespace dummy = ba::placeholders;
|
|---|
| 22 |
|
|---|
| 23 | using namespace std;
|
|---|
| 24 |
|
|---|
| 25 | // ------------------------------------------------------------------------
|
|---|
| 26 |
|
|---|
| 27 | #include "DimDescriptionService.h"
|
|---|
| 28 | #include "DimState.h"
|
|---|
| 29 |
|
|---|
| 30 | // ------------------------------------------------------------------------
|
|---|
| 31 |
|
|---|
| 32 | class StateMachineRateControl : public StateMachineDim//, public DimInfoHandler
|
|---|
| 33 | {
|
|---|
| 34 | private:
|
|---|
| 35 | bool fTriggerOn;
|
|---|
| 36 |
|
|---|
| 37 | vector<bool> fBlock;
|
|---|
| 38 |
|
|---|
| 39 | DimVersion fDim;
|
|---|
| 40 | DimDescribedState fDimFTM;
|
|---|
| 41 | DimDescribedState fDimRS;
|
|---|
| 42 |
|
|---|
| 43 | DimDescribedService fDimThreshold;
|
|---|
| 44 |
|
|---|
| 45 | float fTargetRate;
|
|---|
| 46 | float fTriggerRate;
|
|---|
| 47 |
|
|---|
| 48 | uint16_t fThresholdMin;
|
|---|
| 49 | uint16_t fThresholdReference;
|
|---|
| 50 |
|
|---|
| 51 | bool fVerbose;
|
|---|
| 52 |
|
|---|
| 53 | uint64_t fCounter;
|
|---|
| 54 |
|
|---|
| 55 | Time fCalibrationTimeStart;
|
|---|
| 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 |
|
|---|
| 288 | const RateControl::DimThreshold data = { fThresholdMin, fCalibrationTimeStart.Mjd(), Time().Mjd() };
|
|---|
| 289 | fDimThreshold.setQuality(0);
|
|---|
| 290 | fDimThreshold.Update(data);
|
|---|
| 291 |
|
|---|
| 292 | ostringstream out;
|
|---|
| 293 | out << setprecision(3);
|
|---|
| 294 | out << "Measured rate " << fTriggerRate << "Hz below target rate " << fTargetRate << "... mininum threshold set to " << fThresholdMin;
|
|---|
| 295 | Info(out);
|
|---|
| 296 |
|
|---|
| 297 | return RateControl::State::kGlobalThresholdSet;
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | // This is a step towards a threshold at which the NSB rate is equal the target rate
|
|---|
| 301 | // +1 to avoid getting a step of 0
|
|---|
| 302 | const float step = (log10(fTriggerRate)-log10(fTargetRate))/0.039 + 1;
|
|---|
| 303 |
|
|---|
| 304 | const uint16_t diff = fThresholdMin+int16_t(truncf(step));
|
|---|
| 305 | if (diff<=fThresholdMin)
|
|---|
| 306 | {
|
|---|
| 307 | const RateControl::DimThreshold data = { fThresholdMin, fCalibrationTimeStart.Mjd(), Time().Mjd() };
|
|---|
| 308 | fDimThreshold.setQuality(1);
|
|---|
| 309 | fDimThreshold.Update(data);
|
|---|
| 310 |
|
|---|
| 311 | ostringstream out;
|
|---|
| 312 | out << setprecision(3);
|
|---|
| 313 | out << "Next step would be 0... mininum threshold set to " << fThresholdMin;
|
|---|
| 314 | Info(out);
|
|---|
| 315 |
|
|---|
| 316 | return RateControl::State::kGlobalThresholdSet;
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | if (fVerbose)
|
|---|
| 320 | {
|
|---|
| 321 | //Out() << idx/40 << "|" << (idx/4)%10 << "|" << idx%4;
|
|---|
| 322 | Out() << fThresholdMin;
|
|---|
| 323 | Out() << (step>0 ? " += " : " -= ");
|
|---|
| 324 | Out() << step << " (" << diff << ")" << endl;
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | const uint32_t val[2] = { -1, diff };
|
|---|
| 328 | DimClient::sendCommandNB("FTM_CONTROL/SET_THRESHOLD", (void*)val, 8);
|
|---|
| 329 |
|
|---|
| 330 | fThresholdMin = diff;
|
|---|
| 331 |
|
|---|
| 332 | return GetCurrentState();
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | int HandleStaticData(const EventImp &evt)
|
|---|
| 336 | {
|
|---|
| 337 | if (!CheckEventSize(evt, sizeof(FTM::DimStaticData)))
|
|---|
| 338 | return GetCurrentState();
|
|---|
| 339 |
|
|---|
| 340 | const FTM::DimStaticData &sdata = *static_cast<const FTM::DimStaticData*>(evt.GetData());
|
|---|
| 341 | fTriggerOn = sdata.HasTrigger();
|
|---|
| 342 |
|
|---|
| 343 | PrintThresholds(sdata);
|
|---|
| 344 |
|
|---|
| 345 | fThresholds.assign(sdata.fThreshold, sdata.fThreshold+160);
|
|---|
| 346 |
|
|---|
| 347 | return GetCurrentState();
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | int HandleTriggerRates(const EventImp &evt)
|
|---|
| 351 | {
|
|---|
| 352 | if (fThresholds.size()==0)
|
|---|
| 353 | return GetCurrentState();
|
|---|
| 354 |
|
|---|
| 355 | if (GetCurrentState()<=RateControl::State::kConnected ||
|
|---|
| 356 | GetCurrentState()==RateControl::State::kGlobalThresholdSet)
|
|---|
| 357 | return GetCurrentState();
|
|---|
| 358 |
|
|---|
| 359 | if (!CheckEventSize(evt, sizeof(FTM::DimTriggerRates)))
|
|---|
| 360 | return GetCurrentState();
|
|---|
| 361 |
|
|---|
| 362 | const FTM::DimTriggerRates &sdata = *static_cast<const FTM::DimTriggerRates*>(evt.GetData());
|
|---|
| 363 |
|
|---|
| 364 | if (GetCurrentState()==RateControl::State::kSettingGlobalThreshold)
|
|---|
| 365 | return ProcessCamera(sdata);
|
|---|
| 366 |
|
|---|
| 367 | if (GetCurrentState()==RateControl::State::kInProgress)
|
|---|
| 368 | ProcessPatches(sdata);
|
|---|
| 369 |
|
|---|
| 370 | return GetCurrentState();
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | int Calibrate()
|
|---|
| 374 | {
|
|---|
| 375 | if (!fTriggerOn)
|
|---|
| 376 | {
|
|---|
| 377 | Info("Physics trigger not enabled... CALIBRATE command ignored.");
|
|---|
| 378 | return RateControl::State::kGlobalThresholdSet;
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | const int32_t val[2] = { -1, fThresholdReference };
|
|---|
| 382 | Dim::SendCommand("FTM_CONTROL/SET_THRESHOLD", val);
|
|---|
| 383 |
|
|---|
| 384 | fThresholds.assign(160, fThresholdReference);
|
|---|
| 385 |
|
|---|
| 386 | fThresholdMin = fThresholdReference;
|
|---|
| 387 | fTriggerRate = -1;
|
|---|
| 388 | fCounter = 0;
|
|---|
| 389 |
|
|---|
| 390 | fCalibrationTimeStart = Time();
|
|---|
| 391 |
|
|---|
| 392 | ostringstream out;
|
|---|
| 393 | out << "Rate calibration started at a threshold of " << fThresholdReference << " with a target rate of " << fTargetRate << " Hz";
|
|---|
| 394 | Info(out);
|
|---|
| 395 |
|
|---|
| 396 | return RateControl::State::kSettingGlobalThreshold;
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | int StopRC()
|
|---|
| 400 | {
|
|---|
| 401 | return RateControl::State::kConnected;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | int SetMinThreshold(const EventImp &evt)
|
|---|
| 405 | {
|
|---|
| 406 | if (!CheckEventSize(evt, 4))
|
|---|
| 407 | return kSM_FatalError;
|
|---|
| 408 |
|
|---|
| 409 | // FIXME: Check missing
|
|---|
| 410 |
|
|---|
| 411 | fThresholdReference = evt.GetUShort();
|
|---|
| 412 |
|
|---|
| 413 | return GetCurrentState();
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | int SetTargetRate(const EventImp &evt)
|
|---|
| 417 | {
|
|---|
| 418 | if (!CheckEventSize(evt, 4))
|
|---|
| 419 | return kSM_FatalError;
|
|---|
| 420 |
|
|---|
| 421 | fTargetRate = evt.GetFloat();
|
|---|
| 422 |
|
|---|
| 423 | return GetCurrentState();
|
|---|
| 424 | }
|
|---|
| 425 |
|
|---|
| 426 | int Print() const
|
|---|
| 427 | {
|
|---|
| 428 | Out() << fDim << endl;
|
|---|
| 429 | Out() << fDimFTM << endl;
|
|---|
| 430 | Out() << fDimRS << endl;
|
|---|
| 431 |
|
|---|
| 432 | return GetCurrentState();
|
|---|
| 433 | }
|
|---|
| 434 |
|
|---|
| 435 | int SetVerbosity(const EventImp &evt)
|
|---|
| 436 | {
|
|---|
| 437 | if (!CheckEventSize(evt, 1))
|
|---|
| 438 | return kSM_FatalError;
|
|---|
| 439 |
|
|---|
| 440 | fVerbose = evt.GetBool();
|
|---|
| 441 |
|
|---|
| 442 | return GetCurrentState();
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | int Execute()
|
|---|
| 446 | {
|
|---|
| 447 | if (!fDim.online())
|
|---|
| 448 | return RateControl::State::kDimNetworkNA;
|
|---|
| 449 |
|
|---|
| 450 | // All subsystems are not connected
|
|---|
| 451 | if (fDimFTM.state()<FTM::State::kConnected)
|
|---|
| 452 | return RateControl::State::kDisconnected;
|
|---|
| 453 |
|
|---|
| 454 | const bool inprog = fTriggerOn && fDimRS.state()<RateScan::State::kConfiguring;
|
|---|
| 455 |
|
|---|
| 456 | switch (GetCurrentState())
|
|---|
| 457 | {
|
|---|
| 458 | case RateControl::State::kSettingGlobalThreshold:
|
|---|
| 459 | return RateControl::State::kSettingGlobalThreshold;
|
|---|
| 460 |
|
|---|
| 461 | case RateControl::State::kGlobalThresholdSet:
|
|---|
| 462 | if (!inprog)
|
|---|
| 463 | return RateControl::State::kGlobalThresholdSet;
|
|---|
| 464 |
|
|---|
| 465 | case RateControl::State::kInProgress:
|
|---|
| 466 | if (inprog)
|
|---|
| 467 | return RateControl::State::kInProgress;
|
|---|
| 468 | }
|
|---|
| 469 |
|
|---|
| 470 | return RateControl::State::kConnected;
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | public:
|
|---|
| 474 | StateMachineRateControl(ostream &out=cout) : StateMachineDim(out, "RATE_CONTROL"),
|
|---|
| 475 | fTriggerOn(false), fBlock(40),
|
|---|
| 476 | fDimFTM("FTM_CONTROL"),
|
|---|
| 477 | fDimRS("RATE_SCAN"),
|
|---|
| 478 |
|
|---|
| 479 | fDimThreshold("RATE_CONTROL/THRESHOLD", "S:1;D:1;D:1",
|
|---|
| 480 | "Resulting threshold after calibration"
|
|---|
| 481 | "|threshold[dac]:Resulting threshold from calibration"
|
|---|
| 482 | "|begin[mjd]:Start time of calibration"
|
|---|
| 483 | "|end[mjd]:End time of calibration")
|
|---|
| 484 | {
|
|---|
| 485 | // ba::io_service::work is a kind of keep_alive for the loop.
|
|---|
| 486 | // It prevents the io_service to go to stopped state, which
|
|---|
| 487 | // would prevent any consecutive calls to run()
|
|---|
| 488 | // or poll() to do nothing. reset() could also revoke to the
|
|---|
| 489 | // previous state but this might introduce some overhead of
|
|---|
| 490 | // deletion and creation of threads and more.
|
|---|
| 491 |
|
|---|
| 492 | fDim.Subscribe(*this);
|
|---|
| 493 | fDimFTM.Subscribe(*this);
|
|---|
| 494 | fDimRS.Subscribe(*this);
|
|---|
| 495 |
|
|---|
| 496 | Subscribe("FTM_CONTROL/TRIGGER_RATES")
|
|---|
| 497 | (bind(&StateMachineRateControl::HandleTriggerRates, this, placeholders::_1));
|
|---|
| 498 | Subscribe("FTM_CONTROL/STATIC_DATA")
|
|---|
| 499 | (bind(&StateMachineRateControl::HandleStaticData, this, placeholders::_1));
|
|---|
| 500 |
|
|---|
| 501 | // State names
|
|---|
| 502 | AddStateName(RateControl::State::kDimNetworkNA, "DimNetworkNotAvailable",
|
|---|
| 503 | "The Dim DNS is not reachable.");
|
|---|
| 504 |
|
|---|
| 505 | AddStateName(RateControl::State::kDisconnected, "Disconnected",
|
|---|
| 506 | "The Dim DNS is reachable, but the required subsystems are not available.");
|
|---|
| 507 |
|
|---|
| 508 | AddStateName(RateControl::State::kConnected, "Connected",
|
|---|
| 509 | "All needed subsystems are connected to their hardware, no action is performed.");
|
|---|
| 510 |
|
|---|
| 511 | AddStateName(RateControl::State::kSettingGlobalThreshold, "Calibrating",
|
|---|
| 512 | "A global minimum thrshold is currently determined.");
|
|---|
| 513 |
|
|---|
| 514 | AddStateName(RateControl::State::kGlobalThresholdSet, "GlobalThresholdSet",
|
|---|
| 515 | "A global threshold has ben set, waiting for the trigger to be switched on.");
|
|---|
| 516 |
|
|---|
| 517 | AddStateName(RateControl::State::kInProgress, "InProgress",
|
|---|
| 518 | "Rate control in progress.");
|
|---|
| 519 |
|
|---|
| 520 | AddEvent("CALIBRATE")
|
|---|
| 521 | (bind(&StateMachineRateControl::Calibrate, this))
|
|---|
| 522 | ("Start a search for a reasonable minimum global threshold");
|
|---|
| 523 |
|
|---|
| 524 | AddEvent("STOP", RateControl::State::kSettingGlobalThreshold, RateControl::State::kGlobalThresholdSet, RateControl::State::kInProgress)
|
|---|
| 525 | (bind(&StateMachineRateControl::StopRC, this))
|
|---|
| 526 | ("Stop a calibration or ratescan in progress");
|
|---|
| 527 |
|
|---|
| 528 | AddEvent("SET_MIN_THRESHOLD", "I:1")
|
|---|
| 529 | (bind(&StateMachineRateControl::SetMinThreshold, this, placeholders::_1))
|
|---|
| 530 | ("Set a minimum threshold at which th rate control starts calibrating");
|
|---|
| 531 |
|
|---|
| 532 | AddEvent("SET_TARGET_RATE", "F:1")
|
|---|
| 533 | (bind(&StateMachineRateControl::SetTargetRate, this, placeholders::_1))
|
|---|
| 534 | ("Set a target trigger rate for the calibration");
|
|---|
| 535 |
|
|---|
| 536 | AddEvent("PRINT")
|
|---|
| 537 | (bind(&StateMachineRateControl::Print, this))
|
|---|
| 538 | ("Print current status");
|
|---|
| 539 |
|
|---|
| 540 | AddEvent("SET_VERBOSE", "B")
|
|---|
| 541 | (bind(&StateMachineRateControl::SetVerbosity, this, placeholders::_1))
|
|---|
| 542 | ("set verbosity state"
|
|---|
| 543 | "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
|
|---|
| 544 |
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | int EvalOptions(Configuration &conf)
|
|---|
| 548 | {
|
|---|
| 549 | fVerbose = !conf.Get<bool>("quiet");
|
|---|
| 550 |
|
|---|
| 551 | fThresholdReference = 300;
|
|---|
| 552 | fThresholdMin = 300;
|
|---|
| 553 | fTargetRate = 75;
|
|---|
| 554 |
|
|---|
| 555 | return -1;
|
|---|
| 556 | }
|
|---|
| 557 | };
|
|---|
| 558 |
|
|---|
| 559 | // ------------------------------------------------------------------------
|
|---|
| 560 |
|
|---|
| 561 | #include "Main.h"
|
|---|
| 562 |
|
|---|
| 563 | template<class T>
|
|---|
| 564 | int RunShell(Configuration &conf)
|
|---|
| 565 | {
|
|---|
| 566 | return Main::execute<T, StateMachineRateControl>(conf);
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | void SetupConfiguration(Configuration &conf)
|
|---|
| 570 | {
|
|---|
| 571 | po::options_description control("Rate control options");
|
|---|
| 572 | control.add_options()
|
|---|
| 573 | ("quiet,q", po_bool(), "Disable printing more informations during rate control.")
|
|---|
| 574 | //("max-wait", var<uint16_t>(150), "The maximum number of seconds to wait to get the anticipated resolution for a point.")
|
|---|
| 575 | // ("resolution", var<double>(0.05) , "The minimum resolution required for a single data point.")
|
|---|
| 576 | ;
|
|---|
| 577 |
|
|---|
| 578 | conf.AddOptions(control);
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 | /*
|
|---|
| 582 | Extract usage clause(s) [if any] for SYNOPSIS.
|
|---|
| 583 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
|---|
| 584 | are used to match the usage synopsis in program output. An example from cp
|
|---|
| 585 | (GNU coreutils) which contains both strings:
|
|---|
| 586 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
|---|
| 587 | or: cp [OPTION]... SOURCE... DIRECTORY
|
|---|
| 588 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
|---|
| 589 | */
|
|---|
| 590 | void PrintUsage()
|
|---|
| 591 | {
|
|---|
| 592 | cout <<
|
|---|
| 593 | "The ratecontrol program is a keep the rate reasonable low.\n"
|
|---|
| 594 | "\n"
|
|---|
| 595 | "Usage: ratecontrol [-c type] [OPTIONS]\n"
|
|---|
| 596 | " or: ratecontrol [OPTIONS]\n";
|
|---|
| 597 | cout << endl;
|
|---|
| 598 | }
|
|---|
| 599 |
|
|---|
| 600 | void PrintHelp()
|
|---|
| 601 | {
|
|---|
| 602 | Main::PrintHelp<StateMachineRateControl>();
|
|---|
| 603 |
|
|---|
| 604 | /* Additional help text which is printed after the configuration
|
|---|
| 605 | options goes here */
|
|---|
| 606 |
|
|---|
| 607 | /*
|
|---|
| 608 | cout << "bla bla bla" << endl << endl;
|
|---|
| 609 | cout << endl;
|
|---|
| 610 | cout << "Environment:" << endl;
|
|---|
| 611 | cout << "environment" << endl;
|
|---|
| 612 | cout << endl;
|
|---|
| 613 | cout << "Examples:" << endl;
|
|---|
| 614 | cout << "test exam" << endl;
|
|---|
| 615 | cout << endl;
|
|---|
| 616 | cout << "Files:" << endl;
|
|---|
| 617 | cout << "files" << endl;
|
|---|
| 618 | cout << endl;
|
|---|
| 619 | */
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | int main(int argc, const char* argv[])
|
|---|
| 623 | {
|
|---|
| 624 | Configuration conf(argv[0]);
|
|---|
| 625 | conf.SetPrintUsage(PrintUsage);
|
|---|
| 626 | Main::SetupConfiguration(conf);
|
|---|
| 627 | SetupConfiguration(conf);
|
|---|
| 628 |
|
|---|
| 629 | if (!conf.DoParse(argc, argv, PrintHelp))
|
|---|
| 630 | return 127;
|
|---|
| 631 |
|
|---|
| 632 | if (!conf.Has("console"))
|
|---|
| 633 | return RunShell<LocalStream>(conf);
|
|---|
| 634 |
|
|---|
| 635 | if (conf.Get<int>("console")==0)
|
|---|
| 636 | return RunShell<LocalShell>(conf);
|
|---|
| 637 | else
|
|---|
| 638 | return RunShell<LocalConsole>(conf);
|
|---|
| 639 |
|
|---|
| 640 | return 0;
|
|---|
| 641 | }
|
|---|