| 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 "externals/PixelMap.h"
|
|---|
| 11 |
|
|---|
| 12 | #include "tools.h"
|
|---|
| 13 |
|
|---|
| 14 | #include "LocalControl.h"
|
|---|
| 15 |
|
|---|
| 16 | #include "HeadersFTM.h"
|
|---|
| 17 | #include "HeadersDrive.h"
|
|---|
| 18 | #include "HeadersRateScan.h"
|
|---|
| 19 | #include "HeadersRateControl.h"
|
|---|
| 20 |
|
|---|
| 21 | namespace ba = boost::asio;
|
|---|
| 22 | namespace bs = boost::system;
|
|---|
| 23 | namespace dummy = ba::placeholders;
|
|---|
| 24 |
|
|---|
| 25 | using namespace std;
|
|---|
| 26 |
|
|---|
| 27 | // ------------------------------------------------------------------------
|
|---|
| 28 |
|
|---|
| 29 | #include "DimDescriptionService.h"
|
|---|
| 30 | #include "DimState.h"
|
|---|
| 31 |
|
|---|
| 32 | // ------------------------------------------------------------------------
|
|---|
| 33 |
|
|---|
| 34 | class StateMachineRateControl : public StateMachineDim//, public DimInfoHandler
|
|---|
| 35 | {
|
|---|
| 36 | private:
|
|---|
| 37 | struct config
|
|---|
| 38 | {
|
|---|
| 39 | uint16_t fCalibrationType;
|
|---|
| 40 | uint16_t fTargetRate;
|
|---|
| 41 | uint16_t fMinThreshold;
|
|---|
| 42 | uint16_t fAverageTime;
|
|---|
| 43 | uint16_t fRequiredEvents;
|
|---|
| 44 | };
|
|---|
| 45 |
|
|---|
| 46 | map<string, config> fRunTypes;
|
|---|
| 47 |
|
|---|
| 48 | PixelMap fMap;
|
|---|
| 49 |
|
|---|
| 50 | bool fPhysTriggerEnabled;
|
|---|
| 51 | bool fTriggerOn;
|
|---|
| 52 |
|
|---|
| 53 | vector<bool> fBlock;
|
|---|
| 54 |
|
|---|
| 55 | DimVersion fDim;
|
|---|
| 56 | DimDescribedState fDimFTM;
|
|---|
| 57 | DimDescribedState fDimRS;
|
|---|
| 58 | DimDescribedState fDimDrive;
|
|---|
| 59 |
|
|---|
| 60 | DimDescribedService fDimThreshold;
|
|---|
| 61 |
|
|---|
| 62 | float fTargetRate;
|
|---|
| 63 | float fTriggerRate;
|
|---|
| 64 |
|
|---|
| 65 | uint16_t fThresholdMin;
|
|---|
| 66 | uint16_t fThresholdReference;
|
|---|
| 67 |
|
|---|
| 68 | uint16_t fAverageTime;
|
|---|
| 69 | uint16_t fRequiredEvents;
|
|---|
| 70 |
|
|---|
| 71 | list<pair<Time,float>> fCurrentsMed;
|
|---|
| 72 | list<pair<Time,float>> fCurrentsDev;
|
|---|
| 73 | list<pair<Time,vector<float>>> fCurrentsVec;
|
|---|
| 74 |
|
|---|
| 75 | bool fVerbose;
|
|---|
| 76 | bool fCalibrateByCurrent;
|
|---|
| 77 |
|
|---|
| 78 | uint64_t fCounter;
|
|---|
| 79 |
|
|---|
| 80 | Time fCalibrationTimeStart;
|
|---|
| 81 |
|
|---|
| 82 | bool CheckEventSize(const EventImp &evt, size_t size)
|
|---|
| 83 | {
|
|---|
| 84 | if (size_t(evt.GetSize())==size)
|
|---|
| 85 | return true;
|
|---|
| 86 |
|
|---|
| 87 | if (evt.GetSize()==0)
|
|---|
| 88 | return false;
|
|---|
| 89 |
|
|---|
| 90 | ostringstream msg;
|
|---|
| 91 | msg << evt.GetName() << " - Received event has " << evt.GetSize() << " bytes, but expected " << size << ".";
|
|---|
| 92 | Fatal(msg);
|
|---|
| 93 | return false;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | vector<uint32_t> fThresholds;
|
|---|
| 97 |
|
|---|
| 98 | void PrintThresholds(const FTM::DimStaticData &sdata)
|
|---|
| 99 | {
|
|---|
| 100 | if (!fVerbose)
|
|---|
| 101 | return;
|
|---|
| 102 |
|
|---|
| 103 | if (fThresholds.empty())
|
|---|
| 104 | return;
|
|---|
| 105 |
|
|---|
| 106 | if (GetCurrentState()<=RateControl::State::kConnected)
|
|---|
| 107 | return;
|
|---|
| 108 |
|
|---|
| 109 | Out() << "Min. DAC=" << fThresholdMin << endl;
|
|---|
| 110 |
|
|---|
| 111 | for (int j=0; j<10; j++)
|
|---|
| 112 | {
|
|---|
| 113 | for (int k=0; k<4; k++)
|
|---|
| 114 | {
|
|---|
| 115 | for (int i=0; i<4; i++)
|
|---|
| 116 | {
|
|---|
| 117 | const int p = i + k*4 + j*16;
|
|---|
| 118 |
|
|---|
| 119 | if (fThresholds[p]!=fThresholdMin)
|
|---|
| 120 | Out() << setw(3) << fThresholds[p];
|
|---|
| 121 | else
|
|---|
| 122 | Out() << " - ";
|
|---|
| 123 |
|
|---|
| 124 | if (fThresholds[p]!=sdata.fThreshold[p])
|
|---|
| 125 | Out() << "!";
|
|---|
| 126 | else
|
|---|
| 127 | Out() << " ";
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | Out() << " ";
|
|---|
| 131 | }
|
|---|
| 132 | Out() << endl;
|
|---|
| 133 | }
|
|---|
| 134 | Out() << endl;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | // RETUNR VALUE
|
|---|
| 138 | bool Step(int idx, float step)
|
|---|
| 139 | {
|
|---|
| 140 | uint32_t diff = fThresholds[idx]+int16_t(truncf(step));
|
|---|
| 141 | if (diff<fThresholdMin)
|
|---|
| 142 | diff=fThresholdMin;
|
|---|
| 143 | if (diff>0xffff)
|
|---|
| 144 | diff = 0xffff;
|
|---|
| 145 |
|
|---|
| 146 | if (diff==fThresholds[idx])
|
|---|
| 147 | return false;
|
|---|
| 148 |
|
|---|
| 149 | if (fVerbose)
|
|---|
| 150 | {
|
|---|
| 151 | Out() << "Apply: Patch " << setw(3) << idx << " [" << idx/40 << "|" << (idx/4)%10 << "|" << idx%4 << "]";
|
|---|
| 152 | Out() << (step>0 ? " += " : " -= ");
|
|---|
| 153 | Out() << fabs(step) << " (old=" << fThresholds[idx] << ", new=" << diff << ")" << endl;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | fThresholds[idx] = diff;
|
|---|
| 157 | fBlock[idx/4] = true;
|
|---|
| 158 |
|
|---|
| 159 | return true;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | void ProcessPatches(const FTM::DimTriggerRates &sdata)
|
|---|
| 163 | {
|
|---|
| 164 |
|
|---|
| 165 | // Caluclate Median and deviation
|
|---|
| 166 | vector<float> medb(sdata.fBoardRate, sdata.fBoardRate+40);
|
|---|
| 167 | vector<float> medp(sdata.fPatchRate, sdata.fPatchRate+160);
|
|---|
| 168 |
|
|---|
| 169 | sort(medb.begin(), medb.end());
|
|---|
| 170 | sort(medp.begin(), medp.end());
|
|---|
| 171 |
|
|---|
| 172 | vector<float> devb(40);
|
|---|
| 173 | for (int i=0; i<40; i++)
|
|---|
| 174 | devb[i] = fabs(sdata.fBoardRate[i]-medb[i]);
|
|---|
| 175 |
|
|---|
| 176 | vector<float> devp(160);
|
|---|
| 177 | for (int i=0; i<160; i++)
|
|---|
| 178 | devp[i] = fabs(sdata.fPatchRate[i]-medp[i]);
|
|---|
| 179 |
|
|---|
| 180 | sort(devb.begin(), devb.end());
|
|---|
| 181 | sort(devp.begin(), devp.end());
|
|---|
| 182 |
|
|---|
| 183 | const double mb = (medb[19]+medb[20])/2;
|
|---|
| 184 | const double mp = (medp[79]+medp[80])/2;
|
|---|
| 185 |
|
|---|
| 186 | const double db = devb[27];
|
|---|
| 187 | const double dp = devp[109];
|
|---|
| 188 |
|
|---|
| 189 | // If any is zero there is something wrong
|
|---|
| 190 | if (mb==0 || mp==0 || db==0 || dp==0)
|
|---|
| 191 | return;
|
|---|
| 192 |
|
|---|
| 193 | if (fVerbose)
|
|---|
| 194 | Out() << Tools::Form("Boards: Med=%3.1f +- %3.1f Hz Patches: Med=%3.1f +- %3.1f Hz", mb, db, mp, dp) << endl;
|
|---|
| 195 |
|
|---|
| 196 | bool changed = false;
|
|---|
| 197 |
|
|---|
| 198 | for (int i=0; i<40; i++)
|
|---|
| 199 | {
|
|---|
| 200 | if (fBlock[i])
|
|---|
| 201 | {
|
|---|
| 202 | fBlock[i] = false;
|
|---|
| 203 | continue;
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | int maxi = -1;
|
|---|
| 207 |
|
|---|
| 208 | const float dif = fabs(sdata.fBoardRate[i]-mb)/db;
|
|---|
| 209 | if (dif>3)
|
|---|
| 210 | {
|
|---|
| 211 | if (fVerbose)
|
|---|
| 212 | Out() << "Board " << setw(3) << i << ": " << dif << " dev away from med" << endl;
|
|---|
| 213 |
|
|---|
| 214 | float max = sdata.fPatchRate[i*4];
|
|---|
| 215 | maxi = 0;
|
|---|
| 216 |
|
|---|
| 217 | for (int j=1; j<4; j++)
|
|---|
| 218 | if (sdata.fPatchRate[i*4+j]>max)
|
|---|
| 219 | {
|
|---|
| 220 | max = sdata.fPatchRate[i*4+j];
|
|---|
| 221 | maxi = j;
|
|---|
| 222 | }
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | for (int j=0; j<4; j++)
|
|---|
| 226 | {
|
|---|
| 227 | // For the noise pixel correct down to median+3*deviation
|
|---|
| 228 | if (maxi==j)
|
|---|
| 229 | {
|
|---|
| 230 | // This is the step which has to be performed to go from
|
|---|
| 231 | // a NSB rate of sdata.fPatchRate[i*4+j]
|
|---|
| 232 |
|
|---|
| 233 |
|
|---|
| 234 | const float step = (log10(sdata.fPatchRate[i*4+j])-log10(mp+3.5*dp))/0.039;
|
|---|
| 235 | // * (dif-5)/dif
|
|---|
| 236 | changed |= Step(i*4+j, step);
|
|---|
| 237 | continue;
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | // For pixels below the median correct also back to median+3*deviation
|
|---|
| 241 | if (sdata.fPatchRate[i*4+j]<mp)
|
|---|
| 242 | {
|
|---|
| 243 | const float step = (log10(sdata.fPatchRate[i*4+j])-log10(mp+3.5*dp))/0.039;
|
|---|
| 244 | changed |= Step(i*4+j, step);
|
|---|
| 245 | continue;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | const float step = -1.5*(log10(mp+dp)-log10(mp))/0.039;
|
|---|
| 249 | changed |= Step(i*4+j, step);
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | if (changed)
|
|---|
| 254 | Dim::SendCommandNB("FTM_CONTROL/SET_SELECTED_THRESHOLDS", fThresholds);
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | int ProcessCamera(const FTM::DimTriggerRates &sdata)
|
|---|
| 258 | {
|
|---|
| 259 | if (fCounter++==0)
|
|---|
| 260 | return GetCurrentState();
|
|---|
| 261 |
|
|---|
| 262 | // Caluclate Median and deviation
|
|---|
| 263 | vector<float> medb(sdata.fBoardRate, sdata.fBoardRate+40);
|
|---|
| 264 |
|
|---|
| 265 | sort(medb.begin(), medb.end());
|
|---|
| 266 |
|
|---|
| 267 | vector<float> devb(40);
|
|---|
| 268 | for (int i=0; i<40; i++)
|
|---|
| 269 | devb[i] = fabs(sdata.fBoardRate[i]-medb[i]);
|
|---|
| 270 |
|
|---|
| 271 | sort(devb.begin(), devb.end());
|
|---|
| 272 |
|
|---|
| 273 | double mb = (medb[19]+medb[20])/2;
|
|---|
| 274 | double db = devb[27];
|
|---|
| 275 |
|
|---|
| 276 | // If any is zero there is something wrong
|
|---|
| 277 | if (mb==0 || db==0)
|
|---|
| 278 | {
|
|---|
| 279 | Warn("The median or the deviation of all board rates is zero... cannot calibrate.");
|
|---|
| 280 | return GetCurrentState();
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | double avg = 0;
|
|---|
| 284 | int num = 0;
|
|---|
| 285 |
|
|---|
| 286 | for (int i=0; i<40; i++)
|
|---|
| 287 | {
|
|---|
| 288 | if ( fabs(sdata.fBoardRate[i]-mb)<2.5*db)
|
|---|
| 289 | {
|
|---|
| 290 | avg += sdata.fBoardRate[i];
|
|---|
| 291 | num++;
|
|---|
| 292 | }
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | fTriggerRate = avg/num * 40;
|
|---|
| 296 |
|
|---|
| 297 | if (fVerbose)
|
|---|
| 298 | {
|
|---|
| 299 | Out() << "Board: Median=" << mb << " Dev=" << db << endl;
|
|---|
| 300 | Out() << "Camera: " << fTriggerRate << " (" << sdata.fTriggerRate << ", n=" << num << ")" << endl;
|
|---|
| 301 | Out() << "Target: " << fTargetRate << endl;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | if (sdata.fTriggerRate<fTriggerRate)
|
|---|
| 305 | fTriggerRate = sdata.fTriggerRate;
|
|---|
| 306 |
|
|---|
| 307 | // ----------------------
|
|---|
| 308 |
|
|---|
| 309 | /*
|
|---|
| 310 | if (avg>0 && avg<fTargetRate)
|
|---|
| 311 | {
|
|---|
| 312 | // I am assuming here (and at other places) the the answer from the FTM when setting
|
|---|
| 313 | // the new threshold always arrives faster than the next rate update.
|
|---|
| 314 | fThresholdMin = fThresholds[0];
|
|---|
| 315 | Out() << "Setting fThresholdMin to " << fThresholds[0] << endl;
|
|---|
| 316 | }
|
|---|
| 317 | */
|
|---|
| 318 |
|
|---|
| 319 | if (fTriggerRate>0 && fTriggerRate<fTargetRate)
|
|---|
| 320 | {
|
|---|
| 321 | fThresholds.assign(160, fThresholdMin);
|
|---|
| 322 |
|
|---|
| 323 | const RateControl::DimThreshold data = { fThresholdMin, fCalibrationTimeStart.Mjd(), Time().Mjd() };
|
|---|
| 324 | fDimThreshold.setQuality(0);
|
|---|
| 325 | fDimThreshold.Update(data);
|
|---|
| 326 |
|
|---|
| 327 | ostringstream out;
|
|---|
| 328 | out << setprecision(3);
|
|---|
| 329 | out << "Measured rate " << fTriggerRate << "Hz below target rate " << fTargetRate << "... mininum threshold set to " << fThresholdMin;
|
|---|
| 330 | Info(out);
|
|---|
| 331 |
|
|---|
| 332 | fTriggerOn = false;
|
|---|
| 333 | fPhysTriggerEnabled = false;
|
|---|
| 334 | return RateControl::State::kGlobalThresholdSet;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | // This is a step towards a threshold at which the NSB rate is equal the target rate
|
|---|
| 338 | // +1 to avoid getting a step of 0
|
|---|
| 339 | const float step = (log10(fTriggerRate)-log10(fTargetRate))/0.039 + 1;
|
|---|
| 340 |
|
|---|
| 341 | const uint16_t diff = fThresholdMin+int16_t(truncf(step));
|
|---|
| 342 | if (diff<=fThresholdMin)
|
|---|
| 343 | {
|
|---|
| 344 | const RateControl::DimThreshold data = { fThresholdMin, fCalibrationTimeStart.Mjd(), Time().Mjd() };
|
|---|
| 345 | fDimThreshold.setQuality(1);
|
|---|
| 346 | fDimThreshold.Update(data);
|
|---|
| 347 |
|
|---|
| 348 | ostringstream out;
|
|---|
| 349 | out << setprecision(3);
|
|---|
| 350 | out << "Next step would be 0... mininum threshold set to " << fThresholdMin;
|
|---|
| 351 | Info(out);
|
|---|
| 352 |
|
|---|
| 353 | fTriggerOn = false;
|
|---|
| 354 | fPhysTriggerEnabled = false;
|
|---|
| 355 | return RateControl::State::kGlobalThresholdSet;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | if (fVerbose)
|
|---|
| 359 | {
|
|---|
| 360 | //Out() << idx/40 << "|" << (idx/4)%10 << "|" << idx%4;
|
|---|
| 361 | Out() << fThresholdMin;
|
|---|
| 362 | Out() << (step>0 ? " += " : " -= ");
|
|---|
| 363 | Out() << step << " (" << diff << ")" << endl;
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | const uint32_t val[2] = { -1, diff };
|
|---|
| 367 | Dim::SendCommandNB("FTM_CONTROL/SET_THRESHOLD", val);
|
|---|
| 368 |
|
|---|
| 369 | fThresholdMin = diff;
|
|---|
| 370 |
|
|---|
| 371 | return GetCurrentState();
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | int HandleStaticData(const EventImp &evt)
|
|---|
| 375 | {
|
|---|
| 376 | if (!CheckEventSize(evt, sizeof(FTM::DimStaticData)))
|
|---|
| 377 | return GetCurrentState();
|
|---|
| 378 |
|
|---|
| 379 | const FTM::DimStaticData &sdata = *static_cast<const FTM::DimStaticData*>(evt.GetData());
|
|---|
| 380 | fPhysTriggerEnabled = sdata.HasTrigger();
|
|---|
| 381 | fTriggerOn = (evt.GetQoS()&FTM::kFtmStates)==FTM::kFtmRunning;
|
|---|
| 382 |
|
|---|
| 383 | Out() << "\n" << evt.GetTime() << ": " << (bool)fTriggerOn << " " << (bool)fPhysTriggerEnabled << endl;
|
|---|
| 384 | PrintThresholds(sdata);
|
|---|
| 385 |
|
|---|
| 386 | if (GetCurrentState()==RateControl::State::kSettingGlobalThreshold && fCalibrateByCurrent)
|
|---|
| 387 | {
|
|---|
| 388 | if (fThresholds.empty())
|
|---|
| 389 | return RateControl::State::kSettingGlobalThreshold;
|
|---|
| 390 |
|
|---|
| 391 | if (!std::equal(sdata.fThreshold, sdata.fThreshold+160, fThresholds.begin()))
|
|---|
| 392 | return RateControl::State::kSettingGlobalThreshold;
|
|---|
| 393 |
|
|---|
| 394 | return RateControl::State::kGlobalThresholdSet;
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | fThresholds.assign(sdata.fThreshold, sdata.fThreshold+160);
|
|---|
| 398 |
|
|---|
| 399 | return GetCurrentState();
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | int HandleTriggerRates(const EventImp &evt)
|
|---|
| 403 | {
|
|---|
| 404 | fTriggerOn = (evt.GetQoS()&FTM::kFtmStates)==FTM::kFtmRunning;
|
|---|
| 405 |
|
|---|
| 406 | if (fThresholds.empty())
|
|---|
| 407 | return GetCurrentState();
|
|---|
| 408 |
|
|---|
| 409 | if (GetCurrentState()<=RateControl::State::kConnected ||
|
|---|
| 410 | GetCurrentState()==RateControl::State::kGlobalThresholdSet)
|
|---|
| 411 | return GetCurrentState();
|
|---|
| 412 |
|
|---|
| 413 | if (!CheckEventSize(evt, sizeof(FTM::DimTriggerRates)))
|
|---|
| 414 | return GetCurrentState();
|
|---|
| 415 |
|
|---|
| 416 | const FTM::DimTriggerRates &sdata = *static_cast<const FTM::DimTriggerRates*>(evt.GetData());
|
|---|
| 417 |
|
|---|
| 418 | if (GetCurrentState()==RateControl::State::kSettingGlobalThreshold && !fCalibrateByCurrent)
|
|---|
| 419 | return ProcessCamera(sdata);
|
|---|
| 420 |
|
|---|
| 421 | if (GetCurrentState()==RateControl::State::kInProgress)
|
|---|
| 422 | ProcessPatches(sdata);
|
|---|
| 423 |
|
|---|
| 424 | return GetCurrentState();
|
|---|
| 425 | }
|
|---|
| 426 |
|
|---|
| 427 | int HandleCalibratedCurrents(const EventImp &evt)
|
|---|
| 428 | {
|
|---|
| 429 | // Check if received event is valid
|
|---|
| 430 | if (!CheckEventSize(evt, (2*416+8)*4))
|
|---|
| 431 | return GetCurrentState();
|
|---|
| 432 |
|
|---|
| 433 | // Record only currents when the drive is tracking to avoid
|
|---|
| 434 | // bias from the movement
|
|---|
| 435 | if (fDimDrive.state()<Drive::State::kTracking)
|
|---|
| 436 | return GetCurrentState();
|
|---|
| 437 |
|
|---|
| 438 | // Get time and median current (FIXME: check N?)
|
|---|
| 439 | const Time &time = evt.GetTime();
|
|---|
| 440 | const float med = evt.Get<float>(416*4+4+4);
|
|---|
| 441 | const float dev = evt.Get<float>(416*4+4+4+4);
|
|---|
| 442 | const float *cur = evt.Ptr<float>();
|
|---|
| 443 |
|
|---|
| 444 | // Keep all median currents of the past 10 seconds
|
|---|
| 445 | fCurrentsMed.emplace_back(time, med);
|
|---|
| 446 | fCurrentsDev.emplace_back(time, dev);
|
|---|
| 447 | fCurrentsVec.emplace_back(time, vector<float>(cur, cur+320));
|
|---|
| 448 | while (!fCurrentsMed.empty())
|
|---|
| 449 | {
|
|---|
| 450 | if (time-fCurrentsMed.front().first<boost::posix_time::seconds(fAverageTime))
|
|---|
| 451 | break;
|
|---|
| 452 |
|
|---|
| 453 | fCurrentsMed.pop_front();
|
|---|
| 454 | fCurrentsDev.pop_front();
|
|---|
| 455 | fCurrentsVec.pop_front();
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | // If we are not doing a calibration no further action necessary
|
|---|
| 459 | if (!fCalibrateByCurrent)
|
|---|
| 460 | return GetCurrentState();
|
|---|
| 461 |
|
|---|
| 462 | // We are not setting thresholds at all
|
|---|
| 463 | if (GetCurrentState()!=RateControl::State::kSettingGlobalThreshold)
|
|---|
| 464 | return GetCurrentState();
|
|---|
| 465 |
|
|---|
| 466 | // Target thresholds have been assigned already
|
|---|
| 467 | if (!fThresholds.empty())
|
|---|
| 468 | return GetCurrentState();
|
|---|
| 469 |
|
|---|
| 470 | // We want at least 8 values for averaging
|
|---|
| 471 | if (fCurrentsMed.size()<fRequiredEvents)
|
|---|
| 472 | return GetCurrentState();
|
|---|
| 473 |
|
|---|
| 474 | // Calculate avera and rms of median
|
|---|
| 475 | double avg = 0;
|
|---|
| 476 | double rms = 0;
|
|---|
| 477 | for (auto it=fCurrentsMed.begin(); it!=fCurrentsMed.end(); it++)
|
|---|
| 478 | {
|
|---|
| 479 | avg += it->second;
|
|---|
| 480 | rms += it->second*it->second;
|
|---|
| 481 | }
|
|---|
| 482 | avg /= fCurrentsMed.size();
|
|---|
| 483 | rms /= fCurrentsMed.size();
|
|---|
| 484 | rms -= avg*avg;
|
|---|
| 485 | rms = rms<0 ? 0 : sqrt(rms);
|
|---|
| 486 |
|
|---|
| 487 | double avg_dev = 0;
|
|---|
| 488 | for (auto it=fCurrentsDev.begin(); it!=fCurrentsDev.end(); it++)
|
|---|
| 489 | avg_dev += it->second;
|
|---|
| 490 | avg_dev /= fCurrentsMed.size();
|
|---|
| 491 |
|
|---|
| 492 | // One could recalculate the median of all pixels including the
|
|---|
| 493 | // correction for the three crazy pixels, but that is three out
|
|---|
| 494 | // of 320. The effect on the median should be negligible anyhow.
|
|---|
| 495 | vector<double> vec(160);
|
|---|
| 496 | for (auto it=fCurrentsVec.begin(); it!=fCurrentsVec.end(); it++)
|
|---|
| 497 | for (int i=0; i<320; i++)
|
|---|
| 498 | {
|
|---|
| 499 | const PixelMapEntry &hv = fMap.hv(i);
|
|---|
| 500 | if (hv)
|
|---|
| 501 | vec[hv.hw()/9] += it->second[i]*hv.count();
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | //fThresholdMin = max(uint16_t(36.0833*pow(avg, 0.638393)+184.037), fThresholdReference);
|
|---|
| 505 | //fThresholdMin = max(uint16_t(42.4*pow(avg, 0.642)+182), fThresholdReference);
|
|---|
| 506 | //fThresholdMin = max(uint16_t(41.6*pow(avg+1, 0.642)+175), fThresholdReference);
|
|---|
| 507 | //fThresholdMin = max(uint16_t(42.3*pow(avg, 0.655)+190), fThresholdReference);
|
|---|
| 508 | fThresholdMin = max(uint16_t(46.6*pow(avg, 0.627)+187), fThresholdReference);
|
|---|
| 509 | //fThresholdMin = max(uint16_t(41.6*pow(avg, 0.642)+175), fThresholdReference);
|
|---|
| 510 | fThresholds.assign(160, fThresholdMin);
|
|---|
| 511 |
|
|---|
| 512 | int counter = 1;
|
|---|
| 513 |
|
|---|
| 514 | double avg2 = 0;
|
|---|
| 515 | for (int i=0; i<160; i++)
|
|---|
| 516 | {
|
|---|
| 517 | vec[i] /= fCurrentsVec.size()*9;
|
|---|
| 518 |
|
|---|
| 519 | avg2 += vec[i];
|
|---|
| 520 |
|
|---|
| 521 | if (vec[i]>avg+3.5*avg_dev)
|
|---|
| 522 | {
|
|---|
| 523 | fThresholds[i] = max(uint16_t(40.5*pow(vec[i], 0.642)+164), fThresholdMin);
|
|---|
| 524 |
|
|---|
| 525 | counter++;
|
|---|
| 526 | }
|
|---|
| 527 | }
|
|---|
| 528 | avg2 /= 160;
|
|---|
| 529 |
|
|---|
| 530 |
|
|---|
| 531 | Dim::SendCommandNB("FTM_CONTROL/SET_ALL_THRESHOLDS", fThresholds);
|
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 | const RateControl::DimThreshold data = { fThresholdMin, fCalibrationTimeStart.Mjd(), Time().Mjd() };
|
|---|
| 535 | fDimThreshold.setQuality(2);
|
|---|
| 536 | fDimThreshold.Update(data);
|
|---|
| 537 |
|
|---|
| 538 | //Info("Sent a total of "+to_string(counter)+" commands for threshold setting");
|
|---|
| 539 |
|
|---|
| 540 | ostringstream out;
|
|---|
| 541 | out << setprecision(3);
|
|---|
| 542 | out << "Measured average current " << avg << "uA +- " << rms << "uA [N=" << fCurrentsMed.size() << "]... mininum threshold set to " << fThresholdMin;
|
|---|
| 543 | Info(out);
|
|---|
| 544 | Info("Set "+to_string(counter)+" individual thresholds.");
|
|---|
| 545 |
|
|---|
| 546 | fTriggerOn = false;
|
|---|
| 547 | fPhysTriggerEnabled = false;
|
|---|
| 548 |
|
|---|
| 549 | return RateControl::State::kSettingGlobalThreshold;
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | int Calibrate()
|
|---|
| 553 | {
|
|---|
| 554 | if (!fPhysTriggerEnabled)
|
|---|
| 555 | {
|
|---|
| 556 | Info("Physics trigger not enabled... CALIBRATE command ignored.");
|
|---|
| 557 |
|
|---|
| 558 | fTriggerOn = false;
|
|---|
| 559 | fPhysTriggerEnabled = false;
|
|---|
| 560 | return RateControl::State::kGlobalThresholdSet;
|
|---|
| 561 | }
|
|---|
| 562 |
|
|---|
| 563 | const int32_t val[2] = { -1, fThresholdReference };
|
|---|
| 564 | Dim::SendCommandNB("FTM_CONTROL/SET_THRESHOLD", val);
|
|---|
| 565 |
|
|---|
| 566 | fThresholds.assign(160, fThresholdReference);
|
|---|
| 567 |
|
|---|
| 568 | fThresholdMin = fThresholdReference;
|
|---|
| 569 | fTriggerRate = -1;
|
|---|
| 570 | fCounter = 0;
|
|---|
| 571 | fBlock.assign(160, false);
|
|---|
| 572 |
|
|---|
| 573 | fCalibrateByCurrent = false;
|
|---|
| 574 | fCalibrationTimeStart = Time();
|
|---|
| 575 |
|
|---|
| 576 | ostringstream out;
|
|---|
| 577 | out << "Rate calibration started at a threshold of " << fThresholdReference << " with a target rate of " << fTargetRate << " Hz";
|
|---|
| 578 | Info(out);
|
|---|
| 579 |
|
|---|
| 580 | return RateControl::State::kSettingGlobalThreshold;
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | int CalibrateByCurrent()
|
|---|
| 584 | {
|
|---|
| 585 | if (!fPhysTriggerEnabled)
|
|---|
| 586 | {
|
|---|
| 587 | Info("Physics trigger not enabled... CALIBRATE command ignored.");
|
|---|
| 588 |
|
|---|
| 589 | fTriggerOn = false;
|
|---|
| 590 | fPhysTriggerEnabled = false;
|
|---|
| 591 | return RateControl::State::kGlobalThresholdSet;
|
|---|
| 592 | }
|
|---|
| 593 |
|
|---|
| 594 | if (fDimDrive.state()<Drive::State::kMoving)
|
|---|
| 595 | Warn("Drive not even moving...");
|
|---|
| 596 |
|
|---|
| 597 | fCounter = 0;
|
|---|
| 598 | fCalibrateByCurrent = true;
|
|---|
| 599 | fCalibrationTimeStart = Time();
|
|---|
| 600 | fBlock.assign(160, false);
|
|---|
| 601 |
|
|---|
| 602 | fThresholds.clear();
|
|---|
| 603 |
|
|---|
| 604 | ostringstream out;
|
|---|
| 605 | out << "Rate calibration by current with min. threshold of " << fThresholdReference << ".";
|
|---|
| 606 | Info(out);
|
|---|
| 607 |
|
|---|
| 608 | return RateControl::State::kSettingGlobalThreshold;
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 | int CalibrateRun(const EventImp &evt)
|
|---|
| 612 | {
|
|---|
| 613 | const string name = evt.GetText();
|
|---|
| 614 |
|
|---|
| 615 | auto it = fRunTypes.find(name);
|
|---|
| 616 | if (it==fRunTypes.end())
|
|---|
| 617 | {
|
|---|
| 618 | Info("CalibrateRun - Run-type '"+name+"' not found... trying 'default'.");
|
|---|
| 619 |
|
|---|
| 620 | it = fRunTypes.find("default");
|
|---|
| 621 | if (it==fRunTypes.end())
|
|---|
| 622 | {
|
|---|
| 623 | Error("CalibrateRun - Run-type 'default' not found.");
|
|---|
| 624 | return GetCurrentState();
|
|---|
| 625 | }
|
|---|
| 626 | }
|
|---|
| 627 |
|
|---|
| 628 | const config &conf = it->second;
|
|---|
| 629 |
|
|---|
| 630 | switch (conf.fCalibrationType)
|
|---|
| 631 | {
|
|---|
| 632 | case 0:
|
|---|
| 633 | Info("No calibration requested.");
|
|---|
| 634 | fTriggerOn = false;
|
|---|
| 635 | fPhysTriggerEnabled = false;
|
|---|
| 636 | return RateControl::State::kGlobalThresholdSet;
|
|---|
| 637 | break;
|
|---|
| 638 |
|
|---|
| 639 | case 1:
|
|---|
| 640 | fThresholdReference = conf.fMinThreshold;
|
|---|
| 641 | fTargetRate = conf.fTargetRate;
|
|---|
| 642 | return Calibrate();
|
|---|
| 643 |
|
|---|
| 644 | case 2:
|
|---|
| 645 | fThresholdReference = conf.fMinThreshold;
|
|---|
| 646 | fAverageTime = conf.fAverageTime;
|
|---|
| 647 | fRequiredEvents = conf.fRequiredEvents;
|
|---|
| 648 | return CalibrateByCurrent();
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | Error("CalibrateRun - Calibration type "+to_string(conf.fCalibrationType)+" unknown.");
|
|---|
| 652 | return GetCurrentState();
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | int StopRC()
|
|---|
| 656 | {
|
|---|
| 657 | Info("Stop received.");
|
|---|
| 658 | return RateControl::State::kConnected;
|
|---|
| 659 | }
|
|---|
| 660 |
|
|---|
| 661 | int SetMinThreshold(const EventImp &evt)
|
|---|
| 662 | {
|
|---|
| 663 | if (!CheckEventSize(evt, 4))
|
|---|
| 664 | return kSM_FatalError;
|
|---|
| 665 |
|
|---|
| 666 | // FIXME: Check missing
|
|---|
| 667 |
|
|---|
| 668 | fThresholdReference = evt.GetUShort();
|
|---|
| 669 |
|
|---|
| 670 | return GetCurrentState();
|
|---|
| 671 | }
|
|---|
| 672 |
|
|---|
| 673 | int SetTargetRate(const EventImp &evt)
|
|---|
| 674 | {
|
|---|
| 675 | if (!CheckEventSize(evt, 4))
|
|---|
| 676 | return kSM_FatalError;
|
|---|
| 677 |
|
|---|
| 678 | fTargetRate = evt.GetFloat();
|
|---|
| 679 |
|
|---|
| 680 | return GetCurrentState();
|
|---|
| 681 | }
|
|---|
| 682 |
|
|---|
| 683 | int Print() const
|
|---|
| 684 | {
|
|---|
| 685 | Out() << fDim << endl;
|
|---|
| 686 | Out() << fDimFTM << endl;
|
|---|
| 687 | Out() << fDimRS << endl;
|
|---|
| 688 | Out() << fDimDrive << endl;
|
|---|
| 689 |
|
|---|
| 690 | return GetCurrentState();
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | int SetVerbosity(const EventImp &evt)
|
|---|
| 694 | {
|
|---|
| 695 | if (!CheckEventSize(evt, 1))
|
|---|
| 696 | return kSM_FatalError;
|
|---|
| 697 |
|
|---|
| 698 | fVerbose = evt.GetBool();
|
|---|
| 699 |
|
|---|
| 700 | return GetCurrentState();
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 | int Execute()
|
|---|
| 704 | {
|
|---|
| 705 | if (!fDim.online())
|
|---|
| 706 | return RateControl::State::kDimNetworkNA;
|
|---|
| 707 |
|
|---|
| 708 | // All subsystems are not connected
|
|---|
| 709 | if (fDimFTM.state()<FTM::State::kConnected || fDimDrive.state()<Drive::State::kConnected)
|
|---|
| 710 | return RateControl::State::kDisconnected;
|
|---|
| 711 |
|
|---|
| 712 | // Do not allow any action while a ratescan is configured or in progress
|
|---|
| 713 | if (fDimRS.state()>=RateScan::State::kConfiguring)
|
|---|
| 714 | return RateControl::State::kConnected;
|
|---|
| 715 |
|
|---|
| 716 | switch (GetCurrentState())
|
|---|
| 717 | {
|
|---|
| 718 | case RateControl::State::kSettingGlobalThreshold:
|
|---|
| 719 | return RateControl::State::kSettingGlobalThreshold;
|
|---|
| 720 |
|
|---|
| 721 | case RateControl::State::kGlobalThresholdSet:
|
|---|
| 722 |
|
|---|
| 723 | // Wait for the trigger to get switched on before starting control loop
|
|---|
| 724 | if (fTriggerOn && fPhysTriggerEnabled)
|
|---|
| 725 | return RateControl::State::kInProgress;
|
|---|
| 726 |
|
|---|
| 727 | return RateControl::State::kGlobalThresholdSet;
|
|---|
| 728 |
|
|---|
| 729 | case RateControl::State::kInProgress:
|
|---|
| 730 |
|
|---|
| 731 | // Go back to connected when the trigger has been switched off
|
|---|
| 732 | if (!fTriggerOn || !fPhysTriggerEnabled)
|
|---|
| 733 | return RateControl::State::kConnected;
|
|---|
| 734 |
|
|---|
| 735 | return RateControl::State::kInProgress;
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | return RateControl::State::kConnected;
|
|---|
| 739 | }
|
|---|
| 740 |
|
|---|
| 741 | public:
|
|---|
| 742 | StateMachineRateControl(ostream &out=cout) : StateMachineDim(out, "RATE_CONTROL"),
|
|---|
| 743 | fPhysTriggerEnabled(false), fTriggerOn(false), fBlock(40),
|
|---|
| 744 | fDimFTM("FTM_CONTROL"),
|
|---|
| 745 | fDimRS("RATE_SCAN"),
|
|---|
| 746 | fDimDrive("DRIVE_CONTROL"),
|
|---|
| 747 | fDimThreshold("RATE_CONTROL/THRESHOLD", "S:1;D:1;D:1",
|
|---|
| 748 | "Resulting threshold after calibration"
|
|---|
| 749 | "|threshold[dac]:Resulting threshold from calibration"
|
|---|
| 750 | "|begin[mjd]:Start time of calibration"
|
|---|
| 751 | "|end[mjd]:End time of calibration")
|
|---|
| 752 | {
|
|---|
| 753 | // ba::io_service::work is a kind of keep_alive for the loop.
|
|---|
| 754 | // It prevents the io_service to go to stopped state, which
|
|---|
| 755 | // would prevent any consecutive calls to run()
|
|---|
| 756 | // or poll() to do nothing. reset() could also revoke to the
|
|---|
| 757 | // previous state but this might introduce some overhead of
|
|---|
| 758 | // deletion and creation of threads and more.
|
|---|
| 759 |
|
|---|
| 760 | fDim.Subscribe(*this);
|
|---|
| 761 | fDimFTM.Subscribe(*this);
|
|---|
| 762 | fDimRS.Subscribe(*this);
|
|---|
| 763 | fDimDrive.Subscribe(*this);
|
|---|
| 764 |
|
|---|
| 765 | Subscribe("FTM_CONTROL/TRIGGER_RATES")
|
|---|
| 766 | (bind(&StateMachineRateControl::HandleTriggerRates, this, placeholders::_1));
|
|---|
| 767 | Subscribe("FTM_CONTROL/STATIC_DATA")
|
|---|
| 768 | (bind(&StateMachineRateControl::HandleStaticData, this, placeholders::_1));
|
|---|
| 769 | Subscribe("FEEDBACK/CALIBRATED_CURRENTS")
|
|---|
| 770 | (bind(&StateMachineRateControl::HandleCalibratedCurrents, this, placeholders::_1));
|
|---|
| 771 |
|
|---|
| 772 | // State names
|
|---|
| 773 | AddStateName(RateControl::State::kDimNetworkNA, "DimNetworkNotAvailable",
|
|---|
| 774 | "The Dim DNS is not reachable.");
|
|---|
| 775 |
|
|---|
| 776 | AddStateName(RateControl::State::kDisconnected, "Disconnected",
|
|---|
| 777 | "The Dim DNS is reachable, but the required subsystems are not available.");
|
|---|
| 778 |
|
|---|
| 779 | AddStateName(RateControl::State::kConnected, "Connected",
|
|---|
| 780 | "All needed subsystems are connected to their hardware, no action is performed.");
|
|---|
| 781 |
|
|---|
| 782 | AddStateName(RateControl::State::kSettingGlobalThreshold, "Calibrating",
|
|---|
| 783 | "A global minimum thrshold is currently determined.");
|
|---|
| 784 |
|
|---|
| 785 | AddStateName(RateControl::State::kGlobalThresholdSet, "GlobalThresholdSet",
|
|---|
| 786 | "A global threshold has ben set, waiting for the trigger to be switched on.");
|
|---|
| 787 |
|
|---|
| 788 | AddStateName(RateControl::State::kInProgress, "InProgress",
|
|---|
| 789 | "Rate control in progress.");
|
|---|
| 790 |
|
|---|
| 791 | AddEvent("CALIBRATE")
|
|---|
| 792 | (bind(&StateMachineRateControl::Calibrate, this))
|
|---|
| 793 | ("Start a search for a reasonable minimum global threshold");
|
|---|
| 794 |
|
|---|
| 795 | AddEvent("CALIBRATE_BY_CURRENT")
|
|---|
| 796 | (bind(&StateMachineRateControl::CalibrateByCurrent, this))
|
|---|
| 797 | ("Set the global threshold from the median current");
|
|---|
| 798 |
|
|---|
| 799 | AddEvent("CALIBRATE_RUN", "C")
|
|---|
| 800 | (bind(&StateMachineRateControl::CalibrateRun, this, placeholders::_1))
|
|---|
| 801 | ("Start a threshold calibration as defined in the setup for this run-type, state change to InProgress is delayed until trigger enabled");
|
|---|
| 802 |
|
|---|
| 803 | AddEvent("STOP", RateControl::State::kSettingGlobalThreshold, RateControl::State::kGlobalThresholdSet, RateControl::State::kInProgress)
|
|---|
| 804 | (bind(&StateMachineRateControl::StopRC, this))
|
|---|
| 805 | ("Stop a calibration or ratescan in progress");
|
|---|
| 806 |
|
|---|
| 807 | AddEvent("SET_MIN_THRESHOLD", "I:1")
|
|---|
| 808 | (bind(&StateMachineRateControl::SetMinThreshold, this, placeholders::_1))
|
|---|
| 809 | ("Set a minimum threshold at which th rate control starts calibrating");
|
|---|
| 810 |
|
|---|
| 811 | AddEvent("SET_TARGET_RATE", "F:1")
|
|---|
| 812 | (bind(&StateMachineRateControl::SetTargetRate, this, placeholders::_1))
|
|---|
| 813 | ("Set a target trigger rate for the calibration");
|
|---|
| 814 |
|
|---|
| 815 | AddEvent("PRINT")
|
|---|
| 816 | (bind(&StateMachineRateControl::Print, this))
|
|---|
| 817 | ("Print current status");
|
|---|
| 818 |
|
|---|
| 819 | AddEvent("SET_VERBOSE", "B")
|
|---|
| 820 | (bind(&StateMachineRateControl::SetVerbosity, this, placeholders::_1))
|
|---|
| 821 | ("set verbosity state"
|
|---|
| 822 | "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
|
|---|
| 823 |
|
|---|
| 824 | }
|
|---|
| 825 |
|
|---|
| 826 | bool GetConfig(Configuration &conf, const string &name, const string &sub, uint16_t &rc)
|
|---|
| 827 | {
|
|---|
| 828 | if (conf.HasDef(name, sub))
|
|---|
| 829 | {
|
|---|
| 830 | rc = conf.GetDef<uint16_t>(name, sub);
|
|---|
| 831 | return true;
|
|---|
| 832 | }
|
|---|
| 833 |
|
|---|
| 834 | Error("Neither "+name+"default nor "+name+sub+" found.");
|
|---|
| 835 | return false;
|
|---|
| 836 | }
|
|---|
| 837 |
|
|---|
| 838 | int EvalOptions(Configuration &conf)
|
|---|
| 839 | {
|
|---|
| 840 | fVerbose = !conf.Get<bool>("quiet");
|
|---|
| 841 |
|
|---|
| 842 | if (!fMap.Read(conf.Get<string>("pixel-map-file")))
|
|---|
| 843 | {
|
|---|
| 844 | Error("Reading mapping table from "+conf.Get<string>("pixel-map-file")+" failed.");
|
|---|
| 845 | return 1;
|
|---|
| 846 | }
|
|---|
| 847 |
|
|---|
| 848 | fThresholdReference = 300;
|
|---|
| 849 | fThresholdMin = 300;
|
|---|
| 850 | fTargetRate = 75;
|
|---|
| 851 |
|
|---|
| 852 | fAverageTime = 10;
|
|---|
| 853 | fRequiredEvents = 8;
|
|---|
| 854 |
|
|---|
| 855 | // ---------- Setup run types ---------
|
|---|
| 856 | const vector<string> types = conf.Vec<string>("run-type");
|
|---|
| 857 | if (types.empty())
|
|---|
| 858 | Warn("No run-types defined.");
|
|---|
| 859 | else
|
|---|
| 860 | Message("Defining run-types");
|
|---|
| 861 |
|
|---|
| 862 | for (auto it=types.begin(); it!=types.end(); it++)
|
|---|
| 863 | {
|
|---|
| 864 | Message(" -> "+ *it);
|
|---|
| 865 |
|
|---|
| 866 | if (fRunTypes.count(*it)>0)
|
|---|
| 867 | {
|
|---|
| 868 | Error("Run-type "+*it+" defined twice.");
|
|---|
| 869 | return 1;
|
|---|
| 870 | }
|
|---|
| 871 |
|
|---|
| 872 | config &c = fRunTypes[*it];
|
|---|
| 873 | if (!GetConfig(conf, "calibration-type.", *it, c.fCalibrationType) ||
|
|---|
| 874 | !GetConfig(conf, "target-rate.", *it, c.fTargetRate) ||
|
|---|
| 875 | !GetConfig(conf, "min-threshold.", *it, c.fMinThreshold) ||
|
|---|
| 876 | !GetConfig(conf, "average-time.", *it, c.fAverageTime) ||
|
|---|
| 877 | !GetConfig(conf, "required-events.", *it, c.fRequiredEvents))
|
|---|
| 878 | return 2;
|
|---|
| 879 | }
|
|---|
| 880 |
|
|---|
| 881 | return -1;
|
|---|
| 882 | }
|
|---|
| 883 | };
|
|---|
| 884 |
|
|---|
| 885 | // ------------------------------------------------------------------------
|
|---|
| 886 |
|
|---|
| 887 | #include "Main.h"
|
|---|
| 888 |
|
|---|
| 889 | template<class T>
|
|---|
| 890 | int RunShell(Configuration &conf)
|
|---|
| 891 | {
|
|---|
| 892 | return Main::execute<T, StateMachineRateControl>(conf);
|
|---|
| 893 | }
|
|---|
| 894 |
|
|---|
| 895 | void SetupConfiguration(Configuration &conf)
|
|---|
| 896 | {
|
|---|
| 897 | po::options_description control("Rate control options");
|
|---|
| 898 | control.add_options()
|
|---|
| 899 | ("quiet,q", po_bool(), "Disable printing more informations during rate control.")
|
|---|
| 900 | ("pixel-map-file", var<string>()->required(), "Pixel mapping file. Used here to get the default reference voltage.")
|
|---|
| 901 | //("max-wait", var<uint16_t>(150), "The maximum number of seconds to wait to get the anticipated resolution for a point.")
|
|---|
| 902 | // ("resolution", var<double>(0.05) , "The minimum resolution required for a single data point.")
|
|---|
| 903 | ;
|
|---|
| 904 |
|
|---|
| 905 | conf.AddOptions(control);
|
|---|
| 906 |
|
|---|
| 907 | po::options_description runtype("Run type configuration");
|
|---|
| 908 | runtype.add_options()
|
|---|
| 909 | ("run-type", vars<string>(), "Name of run-types (replace the * in the following configuration by the case-sensitive names defined here)")
|
|---|
| 910 | ("calibration-type.*", var<uint16_t>(), "Calibration type (0: none, 1: by rate, 2: by current)")
|
|---|
| 911 | ("target-rate.*", var<uint16_t>(), "Target rate for calibration by rate")
|
|---|
| 912 | ("min-threshold.*", var<uint16_t>(), "Minimum threshold which can be applied in a calibration")
|
|---|
| 913 | ("average-time.*", var<uint16_t>(), "Time in seconds to average the currents for a calibration by current.")
|
|---|
| 914 | ("required-events.*", var<uint16_t>(), "Number of required current events to start a calibration by current.");
|
|---|
| 915 | ;
|
|---|
| 916 |
|
|---|
| 917 | conf.AddOptions(runtype);
|
|---|
| 918 | }
|
|---|
| 919 |
|
|---|
| 920 | /*
|
|---|
| 921 | Extract usage clause(s) [if any] for SYNOPSIS.
|
|---|
| 922 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
|---|
| 923 | are used to match the usage synopsis in program output. An example from cp
|
|---|
| 924 | (GNU coreutils) which contains both strings:
|
|---|
| 925 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
|---|
| 926 | or: cp [OPTION]... SOURCE... DIRECTORY
|
|---|
| 927 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
|---|
| 928 | */
|
|---|
| 929 | void PrintUsage()
|
|---|
| 930 | {
|
|---|
| 931 | cout <<
|
|---|
| 932 | "The ratecontrol program is a keep the rate reasonable low.\n"
|
|---|
| 933 | "\n"
|
|---|
| 934 | "Usage: ratecontrol [-c type] [OPTIONS]\n"
|
|---|
| 935 | " or: ratecontrol [OPTIONS]\n";
|
|---|
| 936 | cout << endl;
|
|---|
| 937 | }
|
|---|
| 938 |
|
|---|
| 939 | void PrintHelp()
|
|---|
| 940 | {
|
|---|
| 941 | Main::PrintHelp<StateMachineRateControl>();
|
|---|
| 942 |
|
|---|
| 943 | /* Additional help text which is printed after the configuration
|
|---|
| 944 | options goes here */
|
|---|
| 945 |
|
|---|
| 946 | /*
|
|---|
| 947 | cout << "bla bla bla" << endl << endl;
|
|---|
| 948 | cout << endl;
|
|---|
| 949 | cout << "Environment:" << endl;
|
|---|
| 950 | cout << "environment" << endl;
|
|---|
| 951 | cout << endl;
|
|---|
| 952 | cout << "Examples:" << endl;
|
|---|
| 953 | cout << "test exam" << endl;
|
|---|
| 954 | cout << endl;
|
|---|
| 955 | cout << "Files:" << endl;
|
|---|
| 956 | cout << "files" << endl;
|
|---|
| 957 | cout << endl;
|
|---|
| 958 | */
|
|---|
| 959 | }
|
|---|
| 960 |
|
|---|
| 961 | int main(int argc, const char* argv[])
|
|---|
| 962 | {
|
|---|
| 963 | Configuration conf(argv[0]);
|
|---|
| 964 | conf.SetPrintUsage(PrintUsage);
|
|---|
| 965 | Main::SetupConfiguration(conf);
|
|---|
| 966 | SetupConfiguration(conf);
|
|---|
| 967 |
|
|---|
| 968 | if (!conf.DoParse(argc, argv, PrintHelp))
|
|---|
| 969 | return 127;
|
|---|
| 970 |
|
|---|
| 971 | if (!conf.Has("console"))
|
|---|
| 972 | return RunShell<LocalStream>(conf);
|
|---|
| 973 |
|
|---|
| 974 | if (conf.Get<int>("console")==0)
|
|---|
| 975 | return RunShell<LocalShell>(conf);
|
|---|
| 976 | else
|
|---|
| 977 | return RunShell<LocalConsole>(conf);
|
|---|
| 978 |
|
|---|
| 979 | return 0;
|
|---|
| 980 | }
|
|---|