Changeset 13498 for trunk/FACT++/src
- Timestamp:
- 05/01/12 15:01:06 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/smartfact.cc
r13497 r13498 1 #include <valarray>2 3 1 #include "Dim.h" 4 2 #include "Event.h" … … 19 17 #include "HeadersFAD.h" 20 18 #include "HeadersBIAS.h" 19 #include "HeadersFTM.h" 21 20 22 21 namespace ba = boost::asio; … … 41 40 }; 42 41 42 // ------------------------- Internal variables ----------------------- 43 43 44 PixelMap fPixelMap; 45 46 Time fLastUpdate; 47 48 // ----------------------------- Data storage ------------------------- 49 50 enum weather_t { kTemp = 0, kDew, kHum, kPress, kWind, kGusts, kDir }; 51 float fMagicWeatherData[7]; 52 53 vector<float> fFeedbackCalibration; 54 vector<float> fBiasControlVoltageVec; 55 56 float fBiasControlVoltageMed; 57 float fBiasControlCurrentMed; 58 float fBiasControlCurrentMax; 59 60 deque<float> fBiasControlCurrentHist; 61 62 float fDriveControlPointingZd; 63 string fDriveControlPointingAz; 64 float fDriveControlTrackingDev; 65 string fDriveControlSourceName; 66 67 float fFtmControlTriggerRateCam; 68 deque<float> fFtmControlTriggerRateHist; 69 70 uint8_t fFadControlEventCounter; 71 72 // ------------- Initialize variables before the Dim stuff ------------ 44 73 45 74 DimServiceInfoList fNetwork; … … 50 79 pair<Time, int> fStatusFeedback; 51 80 pair<Time, int> fStatusBiasControl; 81 pair<Time, int> fStatusFtmControl; 52 82 pair<Time, int> fStatusFadControl; 53 83 … … 69 99 DimStampedInfo fDimBiasControlCurrent; 70 100 101 DimStampedInfo fDimFtmControl; 102 DimStampedInfo fDimFtmControlTriggerRates; 103 71 104 DimStampedInfo fDimFadControl; 72 105 DimStampedInfo *fDimFadControlEventData; 73 74 Time fLastUpdate;75 76 enum weather_t { kTemp = 0, kDew, kHum, kPress, kWind, kGusts, kDir };77 float fMagicWeatherData[7];78 79 vector<float> fFeedbackCalibration;80 vector<float> fBiasControlVoltageVec;81 82 float fBiasControlVoltageMed;83 float fBiasControlCurrentMed;84 float fBiasControlCurrentMax;85 86 float fDriveControlPointingZd;87 string fDriveControlPointingAz;88 float fDriveControlTrackingDev;89 string fDriveControlSourceName;90 106 91 107 // ------------------------------------------------------------------- … … 133 149 } 134 150 151 152 // ------------------------------------------------------------------- 153 154 template<class T> 155 void WriteBinary(const string &fname, const T &t, double scale, double offset=0) 156 { 157 vector<uint8_t> val(t.size(), 0); 158 for (uint64_t i=0; i<t.size(); i++) 159 { 160 float range = nearbyint(128*(t[i]+offset)/scale); // [-2V; 2V] 161 if (range>127) 162 range=127; 163 if (range<0) 164 range=0; 165 val[i] = (uint8_t)range; 166 } 167 168 const char *ptr = reinterpret_cast<char*>(val.data()); 169 170 ofstream fout("www/"+fname+".bin"); 171 fout.write(ptr, val.size()*sizeof(uint8_t)); 172 } 135 173 136 174 // ------------------------------------------------------------------- … … 271 309 return; 272 310 311 // Convert dac counts to uA 273 312 vector<float> v(320); 274 313 for (int i=0; i<320; i++) 275 v[i] = d.ptr<uint16_t>()[i]; 276 314 v[i] = d.ptr<uint16_t>()[i] * 5000./4096; 315 316 // Calibrate the data (subtract offset) 277 317 if (fFeedbackCalibration.size()>0 && fBiasControlVoltageVec.size()>0) 278 318 for (int i=0; i<320; i++) 279 319 v[i] -= fBiasControlVoltageVec[i]/fFeedbackCalibration[i]*1e6; 280 320 281 vector<uint8_t> val(160, 0); 321 // Get the maximum of each patch 322 vector<float> val(160, 0); 282 323 for (int i=0; i<160; i++) 283 { 284 const float I = max(v[i*2], v[i*2+1]); 285 286 float range = nearbyint(128*I/1000); // [0, 1000uA] 287 if (range>127) 288 range=127; 289 if (range<0) 290 range=0; 291 val[i] = (uint8_t)range; 292 } 293 324 val[i] = max(v[i*2], v[i*2+1]); 325 326 // Write the 160 patch values to a file 327 WriteBinary("biascontrol-current", val, 1000); 328 329 // Now sort them to determine the median 294 330 sort(v.begin(), v.end()); 295 331 296 332 // Exclude the three crazy channels 297 fBiasControlCurrentMed = (v[159]+v[160])/2 * 5000./4096; 298 fBiasControlCurrentMax = v[316] * 5000./4096; 299 300 const char *ptr = reinterpret_cast<char*>(val.data()); 301 302 ofstream fout("www/biascontrol-current.bin"); 303 fout.write(ptr, 160*sizeof(uint8_t)); 304 } 305 306 uint8_t fEventCounter; 333 fBiasControlCurrentMed = (v[159]+v[160])/2; 334 fBiasControlCurrentMax = v[316]; 335 336 // Store a history of the last 60 entries 337 fBiasControlCurrentHist.push_back(fBiasControlCurrentMed); 338 if (fBiasControlCurrentHist.size()>60) 339 fBiasControlCurrentHist.pop_front(); 340 341 // write the history to a file 342 WriteBinary("biascontrol-current-hist", fBiasControlCurrentHist, 1000); 343 } 344 345 void HandleFtmControlTriggerRates(const DimData &d) 346 { 347 if (!CheckDataSize(d, "FtmControl:TriggerRates", 24+160+640+8)) 348 return; 349 350 fFtmControlTriggerRateCam = d.get<float>(20); 351 352 const float *brates = d.ptr<float>(24); // Board rate 353 const float *prates = d.ptr<float>(24+160); // Patch rate 354 355 // Store a history of the last 60 entries 356 fFtmControlTriggerRateHist.push_back(fFtmControlTriggerRateCam); 357 if (fFtmControlTriggerRateHist.size()>60) 358 fFtmControlTriggerRateHist.pop_front(); 359 360 WriteBinary("ftmcontrol-triggerrate-hist", 361 fFtmControlTriggerRateHist, 100); 362 WriteBinary("ftmcontrol-boardrates", 363 vector<float>(brates, brates+40), 50); 364 WriteBinary("ftmcontrol-patchrates", 365 vector<float>(prates, prates+160), 10); 366 } 307 367 308 368 void HandleFadControlEventData(const DimData &d) … … 311 371 return; 312 372 313 if (f EventCounter++%30)373 if (fFadControlEventCounter++%30) 314 374 return; 315 375 … … 328 388 } 329 389 330 vector<uint8_t> val(160, 0); 331 for (int i=0; i<160; i++) 332 { 333 float range = nearbyint(64*dat[i]/2000)+64; // [-2V; 2V] 334 if (range>127) 335 range=127; 336 if (range<0) 337 range=0; 338 val[i] = (uint8_t)range; 339 } 340 341 const char *ptr = reinterpret_cast<char*>(val.data()); 342 343 ofstream fout("www/fadcontrol-eventdata.bin"); 344 fout.write(ptr, 160*sizeof(int8_t)); 390 WriteBinary("fadcontrol-eventdata", dat, 4000, 2000); 345 391 } 346 392 … … 353 399 return; 354 400 355 if (HandleService(curr, fDimMagicWeatherData, &StateMachineSmartFACT::HandleMagicWeatherData)) 356 return; 357 if (HandleService(curr, fDimFeedbackCalibration, &StateMachineSmartFACT::HandleFeedbackCalibration)) 358 return; 359 if (HandleService(curr, fDimBiasControlVoltage, &StateMachineSmartFACT::HandleBiasControlVoltage)) 360 return; 361 if (HandleService(curr, fDimBiasControlCurrent, &StateMachineSmartFACT::HandleBiasControlCurrent)) 362 return; 363 if (HandleService(curr, *fDimFadControlEventData, &StateMachineSmartFACT::HandleFadControlEventData)) 401 if (HandleService(curr, fDimMagicWeatherData, &StateMachineSmartFACT::HandleMagicWeatherData)) 402 return; 403 if (HandleService(curr, fDimFeedbackCalibration, &StateMachineSmartFACT::HandleFeedbackCalibration)) 404 return; 405 if (HandleService(curr, fDimBiasControlVoltage, &StateMachineSmartFACT::HandleBiasControlVoltage)) 406 return; 407 if (HandleService(curr, fDimBiasControlCurrent, &StateMachineSmartFACT::HandleBiasControlCurrent)) 408 return; 409 if (HandleService(curr, fDimFtmControlTriggerRates, &StateMachineSmartFACT::HandleFtmControlTriggerRates)) 410 return; 411 if (HandleService(curr, *fDimFadControlEventData, &StateMachineSmartFACT::HandleFadControlEventData)) 364 412 return; 365 413 … … 369 417 return; 370 418 if (UpdateState(curr, fDimBiasControl, fStatusBiasControl)) 419 return; 420 if (UpdateState(curr, fDimFtmControl, fStatusFtmControl)) 371 421 return; 372 422 if (UpdateState(curr, fDimFadControl, fStatusFadControl)) … … 468 518 } 469 519 else 470 out << kWhite << "\ t\t\t\t\t\n";520 out << kWhite << "\n"; 471 521 472 522 // --------------- MagicWeather ------------- … … 494 544 } 495 545 else 496 out << kWhite << "\t\t\n\n"; 546 out << kWhite << "\n\n"; 547 548 // --------------- FtmControl ------------- 549 if (fStatusFtmControl.second>=FTM::kIdle) 550 { 551 string col = kGreen; 552 if (fFtmControlTriggerRateCam<15) 553 col = kYellow; 554 if (fFtmControlTriggerRateCam>100) 555 col = kRed; 556 557 out << col << '\t' << fFtmControlTriggerRateCam << '\n'; 558 } 559 else 560 out << kWhite << '\n'; 497 561 498 562 // --------------- BiasControl ------------- … … 516 580 } 517 581 else 518 out << kWhite << "\ t\t\t\n";582 out << kWhite << "\n"; 519 583 520 584 … … 528 592 public: 529 593 StateMachineSmartFACT(ostream &out=cout) : StateMachineDim(out, "SMART_FACT"), 594 fFadControlEventCounter(0), 595 //--- 530 596 fStatusDim (make_pair(Time(), -2)), 531 597 fStatusDriveControl(make_pair(Time(), -2)), … … 533 599 fStatusFeedback (make_pair(Time(), -2)), 534 600 fStatusBiasControl (make_pair(Time(), -2)), 601 fStatusFtmControl (make_pair(Time(), -2)), 535 602 fStatusFadControl (make_pair(Time(), -2)), 536 603 //--- 537 fDim ("DIS_DNS/VERSION_NUMBER", (void*)NULL, 0, this),604 fDim ("DIS_DNS/VERSION_NUMBER", (void*)NULL, 0, this), 538 605 //--- 539 fDimDriveControl ("DRIVE_CONTROL/STATE", (void*)NULL, 0, this),540 fDimDriveControlPointing ("DRIVE_CONTROL/POINTING_POSITION", (void*)NULL, 0, this),541 fDimDriveControlTracking ("DRIVE_CONTROL/TRACKING_POSITION", (void*)NULL, 0, this),542 fDimDriveControlSource ("DRIVE_CONTROL/SOURCE_POSITION", (void*)NULL, 0, this),606 fDimDriveControl ("DRIVE_CONTROL/STATE", (void*)NULL, 0, this), 607 fDimDriveControlPointing ("DRIVE_CONTROL/POINTING_POSITION", (void*)NULL, 0, this), 608 fDimDriveControlTracking ("DRIVE_CONTROL/TRACKING_POSITION", (void*)NULL, 0, this), 609 fDimDriveControlSource ("DRIVE_CONTROL/SOURCE_POSITION", (void*)NULL, 0, this), 543 610 //--- 544 fDimMagicWeather ("MAGIC_WEATHER/STATE", (void*)NULL, 0, this),545 fDimMagicWeatherData ("MAGIC_WEATHER/DATA", (void*)NULL, 0, this),611 fDimMagicWeather ("MAGIC_WEATHER/STATE", (void*)NULL, 0, this), 612 fDimMagicWeatherData ("MAGIC_WEATHER/DATA", (void*)NULL, 0, this), 546 613 //--- 547 fDimFeedback ("FEEDBACK/STATE", (void*)NULL, 0, this),548 fDimFeedbackCalibration ("FEEDBACK/CALIBRATION", (void*)NULL, 0, this),614 fDimFeedback ("FEEDBACK/STATE", (void*)NULL, 0, this), 615 fDimFeedbackCalibration ("FEEDBACK/CALIBRATION", (void*)NULL, 0, this), 549 616 //--- 550 fDimBiasControl ("BIAS_CONTROL/STATE", (void*)NULL, 0, this),551 fDimBiasControlVoltage ("BIAS_CONTROL/VOLTAGE", (void*)NULL, 0, this),552 fDimBiasControlCurrent ("BIAS_CONTROL/CURRENT", (void*)NULL, 0, this),617 fDimBiasControl ("BIAS_CONTROL/STATE", (void*)NULL, 0, this), 618 fDimBiasControlVoltage ("BIAS_CONTROL/VOLTAGE", (void*)NULL, 0, this), 619 fDimBiasControlCurrent ("BIAS_CONTROL/CURRENT", (void*)NULL, 0, this), 553 620 //--- 554 fDimFadControl ("FAD_CONTROL/STATE", (void*)NULL, 0, this), 555 fDimFadControlEventData(0), 556 //--- 557 fEventCounter(0) 621 fDimFtmControl ("FTM_CONTROL/STATE", (void*)NULL, 0, this), 622 fDimFtmControlTriggerRates("FTM_CONTROL/TRIGGER_RATES", (void*)NULL, 0, this), 623 //- 624 fDimFadControl ("FAD_CONTROL/STATE", (void*)NULL, 0, this), 625 fDimFadControlEventData(0) 558 626 { 559 627 // State names
Note:
See TracChangeset
for help on using the changeset viewer.