| 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 "DimServiceInfoList.h"
|
|---|
| 12 | #include "PixelMap.h"
|
|---|
| 13 |
|
|---|
| 14 | #include "tools.h"
|
|---|
| 15 |
|
|---|
| 16 | #include "LocalControl.h"
|
|---|
| 17 |
|
|---|
| 18 | #include "HeadersFAD.h"
|
|---|
| 19 | #include "HeadersBIAS.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 |
|
|---|
| 31 | // ------------------------------------------------------------------------
|
|---|
| 32 |
|
|---|
| 33 | class StateMachineFeedback : public StateMachineDim, public DimInfoHandler
|
|---|
| 34 | {
|
|---|
| 35 | /*
|
|---|
| 36 | int Wrap(boost::function<void()> f)
|
|---|
| 37 | {
|
|---|
| 38 | f();
|
|---|
| 39 | return T::GetCurrentState();
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | boost::function<int(const EventImp &)> Wrapper(boost::function<void()> func)
|
|---|
| 43 | {
|
|---|
| 44 | return bind(&StateMachineMCP::Wrap, this, func);
|
|---|
| 45 | }*/
|
|---|
| 46 |
|
|---|
| 47 | private:
|
|---|
| 48 | enum states_t
|
|---|
| 49 | {
|
|---|
| 50 | kStateDimNetworkNA = 1,
|
|---|
| 51 | kStateDisconnected,
|
|---|
| 52 | kStateConnecting,
|
|---|
| 53 | kStateConnectedFSC,
|
|---|
| 54 | kStateConnectedFAD,
|
|---|
| 55 | kStateConnected,
|
|---|
| 56 | kStateTempCtrlIdle, // 7
|
|---|
| 57 | kStateFeedbackCtrlIdle, // 8
|
|---|
| 58 | kStateCurrentCtrlIdle, // 9
|
|---|
| 59 | kStateTempCtrlRunning, // 9->10
|
|---|
| 60 | kStateFeedbackCtrlRunning, // 10->11
|
|---|
| 61 | kStateCurrentCtrlRunning, // 12
|
|---|
| 62 | kStateCalibrating, // 11->13
|
|---|
| 63 | };
|
|---|
| 64 |
|
|---|
| 65 | enum control_t
|
|---|
| 66 | {
|
|---|
| 67 | kIdle,
|
|---|
| 68 | kTemp,
|
|---|
| 69 | kFeedback,
|
|---|
| 70 | kFeedbackGlobal,
|
|---|
| 71 | kCurrents,
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | control_t fControlType;
|
|---|
| 75 |
|
|---|
| 76 | PixelMap fMap;
|
|---|
| 77 |
|
|---|
| 78 | DimServiceInfoList fNetwork;
|
|---|
| 79 |
|
|---|
| 80 | pair<Time, int> fStatusDim;
|
|---|
| 81 | pair<Time, int> fStatusFAD;
|
|---|
| 82 | pair<Time, int> fStatusFSC;
|
|---|
| 83 | pair<Time, int> fStatusBias;
|
|---|
| 84 |
|
|---|
| 85 | DimStampedInfo fDim;
|
|---|
| 86 | DimStampedInfo fFAD;
|
|---|
| 87 | DimStampedInfo fFSC;
|
|---|
| 88 | DimStampedInfo fBias;
|
|---|
| 89 | DimStampedInfo fBiasA;
|
|---|
| 90 |
|
|---|
| 91 | DimStampedInfo fBiasData;
|
|---|
| 92 | DimStampedInfo fBiasNom;
|
|---|
| 93 | DimStampedInfo fCameraTemp;
|
|---|
| 94 |
|
|---|
| 95 | DimDescribedService fDimReference;
|
|---|
| 96 | DimDescribedService fDimDeviation;
|
|---|
| 97 | DimDescribedService fDimCalibration;
|
|---|
| 98 |
|
|---|
| 99 | vector<int64_t> fCurrentsAvg;
|
|---|
| 100 | vector<int64_t> fCurrentsRms;
|
|---|
| 101 |
|
|---|
| 102 | vector<float> fCalibration;
|
|---|
| 103 | vector<float> fVoltGapd;
|
|---|
| 104 |
|
|---|
| 105 | vector<vector<float>> fData;
|
|---|
| 106 |
|
|---|
| 107 | int64_t fCursorCur;
|
|---|
| 108 | uint64_t fCursorAmpl;
|
|---|
| 109 | uint64_t fCursorTemp;
|
|---|
| 110 |
|
|---|
| 111 | Time fBiasLast;
|
|---|
| 112 | Time fStartTime;
|
|---|
| 113 |
|
|---|
| 114 | valarray<double> fPV[3]; // Process variable (intgerated/averaged amplitudes)
|
|---|
| 115 | valarray<double> fSP; // Set point (target amplitudes)
|
|---|
| 116 |
|
|---|
| 117 | double fKp; // Proportional constant
|
|---|
| 118 | double fKi; // Integral constant
|
|---|
| 119 | double fKd; // Derivative constant
|
|---|
| 120 | double fT; // Time constant (cycle time)
|
|---|
| 121 | double fGain; // Gain (conversion from a DRS voltage deviation into a BIAS voltage change at G-APD reference voltage)
|
|---|
| 122 |
|
|---|
| 123 | double fT21;
|
|---|
| 124 |
|
|---|
| 125 | double fBiasOffset;
|
|---|
| 126 | double fCalibrationOffset;
|
|---|
| 127 | double fAppliedOffset;
|
|---|
| 128 |
|
|---|
| 129 | uint16_t fCurrentRequestInterval;
|
|---|
| 130 | uint16_t fNumCalibIgnore;
|
|---|
| 131 | uint16_t fNumCalibRequests;
|
|---|
| 132 |
|
|---|
| 133 | bool fOutputEnabled;
|
|---|
| 134 |
|
|---|
| 135 | pair<Time, int> GetNewState(DimStampedInfo &info) const
|
|---|
| 136 | {
|
|---|
| 137 | const bool disconnected = info.getSize()==0;
|
|---|
| 138 |
|
|---|
| 139 | // Make sure getTimestamp is called _before_ getTimestampMillisecs
|
|---|
| 140 | const int tsec = info.getTimestamp();
|
|---|
| 141 | const int tms = info.getTimestampMillisecs();
|
|---|
| 142 |
|
|---|
| 143 | return make_pair(Time(tsec, tms*1000),
|
|---|
| 144 | disconnected ? -2 : info.getQuality());
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | void HandleCameraTemp()
|
|---|
| 148 | {
|
|---|
| 149 | if (fCameraTemp.getSize()!=60*sizeof(float))
|
|---|
| 150 | return;
|
|---|
| 151 |
|
|---|
| 152 | const float *ptr = static_cast<float*>(fCameraTemp.getData());
|
|---|
| 153 |
|
|---|
| 154 | double avgt = 0;
|
|---|
| 155 | int numt = 0;
|
|---|
| 156 | for (int i=1; i<32; i++)
|
|---|
| 157 | if (ptr[i]!=0)
|
|---|
| 158 | {
|
|---|
| 159 | avgt += ptr[i];
|
|---|
| 160 | numt++;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | if (numt==0)
|
|---|
| 164 | return;
|
|---|
| 165 |
|
|---|
| 166 | avgt /= numt; // [deg C]
|
|---|
| 167 |
|
|---|
| 168 | const double dUt = (avgt-25)*4./70; // [V]
|
|---|
| 169 |
|
|---|
| 170 | if (GetCurrentState()==kStateCalibrating && fBiasOffset>dUt-1.2)
|
|---|
| 171 | {
|
|---|
| 172 | ostringstream msg;
|
|---|
| 173 | msg << " (applied calibration offset " << fBiasOffset << "V exceeds temperature correction " << avgt << "V - 1.2V.";
|
|---|
| 174 | Warn("Trying to calibrate above G-APD breakdown volatge!");
|
|---|
| 175 | Warn(msg);
|
|---|
| 176 | return;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | // FIXME: If calibrating do not wait for the temperature!
|
|---|
| 180 | fAppliedOffset = fBiasOffset;
|
|---|
| 181 | if (GetCurrentState()!=kStateCalibrating)
|
|---|
| 182 | fAppliedOffset += dUt;
|
|---|
| 183 |
|
|---|
| 184 | vector<float> vec(2*BIAS::kNumChannels+2);
|
|---|
| 185 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 186 | vec[i+BIAS::kNumChannels] = fAppliedOffset;
|
|---|
| 187 |
|
|---|
| 188 | vec[BIAS::kNumChannels*2] = dUt;
|
|---|
| 189 | vec[BIAS::kNumChannels*2+1] = fBiasOffset;
|
|---|
| 190 |
|
|---|
| 191 | double avg[2] = { 0, 0 };
|
|---|
| 192 | double min[2] = { 90, 90 };
|
|---|
| 193 | double max[2] = { -90, -90 };
|
|---|
| 194 | int num[2] = { 0, 0 };
|
|---|
| 195 |
|
|---|
| 196 | vector<double> med[2];
|
|---|
| 197 | med[0].resize(416);
|
|---|
| 198 | med[1].resize(416);
|
|---|
| 199 |
|
|---|
| 200 | if (fControlType==kCurrents)
|
|---|
| 201 | {
|
|---|
| 202 | if (fCursorCur==0)
|
|---|
| 203 | {
|
|---|
| 204 | //DimClient::sendCommandNB("BIAS_CONTROL/REQUEST_STATUS", NULL, 0);
|
|---|
| 205 | return;
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | // Convert from DAC counts to uA
|
|---|
| 209 | const double conv = 5000./4096;
|
|---|
| 210 |
|
|---|
| 211 | // 3900 Ohm/n + 1000 Ohm + 1100 Ohm (with n=4 or n=5)
|
|---|
| 212 | const double R[2] = { 3075, 2870 };
|
|---|
| 213 |
|
|---|
| 214 | const float *Iavg = fCalibration.data(); // Offset at U=fCalibrationOffset
|
|---|
| 215 | const float *Ravg = fCalibration.data()+BIAS::kNumChannels*2; // Measured resistance
|
|---|
| 216 |
|
|---|
| 217 | // U0 = fCalibrationOffset
|
|---|
| 218 | // dT = fAppliedVoltage
|
|---|
| 219 |
|
|---|
| 220 | // Ifeedback = Im[i] - (U[i]-U0)/Ravg[i] - Iavg[i];
|
|---|
| 221 | // dUapplied[i] + dUneu[i] = R[g] * (Im[i] - (dUapplied[i]+dUneu[i]-U0+dT)/Ravg[i] - Iavg[i])
|
|---|
| 222 |
|
|---|
| 223 | // The assumption here is that the offset calculated from the temperature
|
|---|
| 224 | // does not significanly change within a single step
|
|---|
| 225 |
|
|---|
| 226 | // dU[i] := dUtotal[i] = dUapplied[i] + dUneu[i]
|
|---|
| 227 | // dU[i] / R[g] = Im[i] - (dU[i]+dT-U0)/Ravg[i] - Iavg[i]
|
|---|
| 228 | // dU[i]/R[g] + dU[i]/Ravg[i] = Im[i] + U0/Ravg[i] - dT/Ravg[i] - Iavg[i]
|
|---|
| 229 | // dU[i]*(1/R[g]+1/Ravg[i]) = Im[i] - Iavg[i] + U0/Ravg[i] - dT/Ravg[i]
|
|---|
| 230 | // dU[i] = (Im[i] - Iavg[i] + U0/Ravg[i] - dT/Ravg[i]) / (1/R[g]+1/Ravg[i])
|
|---|
| 231 | // dU[i] = { Im[i] - Iavg[i] + (U0-dT)/Ravg[i] } * r with r := 1 / (1/R[g]+1/Ravg[i])
|
|---|
| 232 |
|
|---|
| 233 | const double U0 = fAppliedOffset-fCalibrationOffset;
|
|---|
| 234 |
|
|---|
| 235 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 236 | {
|
|---|
| 237 | const PixelMapEntry &hv = fMap.hv(i);
|
|---|
| 238 | if (!hv)
|
|---|
| 239 | continue;
|
|---|
| 240 |
|
|---|
| 241 | // Average measured current
|
|---|
| 242 | const double Im = double(fCurrentsAvg[i])/fCursorCur * conv; // [uA]
|
|---|
| 243 |
|
|---|
| 244 | // Group index (0 or 1) of the of the pixel (4 or 5 pixel patch)
|
|---|
| 245 | const int g = hv.group();
|
|---|
| 246 |
|
|---|
| 247 | // Serial resistors in front of the G-APD
|
|---|
| 248 | double Rg = R[g];
|
|---|
| 249 |
|
|---|
| 250 | // This is assuming that the broken pixels have a 390 Ohm instead of 3900 Ohm serial resistor
|
|---|
| 251 | if (i==66) // Pixel 830(66)
|
|---|
| 252 | Rg = 2400; // 2400 = (3/3900 + 1/390) + 1000 + 1100
|
|---|
| 253 | if (i==191 || i==193) // Pixel 583(191) / Pixel 1401(193)
|
|---|
| 254 | Rg = 2379; // 2379 = (4/3900 + 1/390) + 1000 + 1100
|
|---|
| 255 |
|
|---|
| 256 | const double r = 1./(1./Rg + 1./Ravg[i]); // [Ohm]
|
|---|
| 257 |
|
|---|
| 258 | // Offset induced by the voltage above the calibration point
|
|---|
| 259 | const double dI = U0/Ravg[i]; // [V/Ohm]
|
|---|
| 260 |
|
|---|
| 261 | // Offset at the calibration point (make sure that the calibration is
|
|---|
| 262 | // valid (Im[i]>Iavg[i]) and we operate above the calibration point)
|
|---|
| 263 | const double I = Im>Iavg[i] ? Im - Iavg[i] : 0; // [A]
|
|---|
| 264 |
|
|---|
| 265 | // Make sure that the averaged resistor is valid
|
|---|
| 266 | const double dU = Ravg[i]>10000 ? r*(I*1e-6 - dI) : 0;
|
|---|
| 267 |
|
|---|
| 268 | vec[i+BIAS::kNumChannels] += dU;
|
|---|
| 269 |
|
|---|
| 270 | // Calculate statistics only for channels with a valid calibration
|
|---|
| 271 | if (Iavg[i]>0)
|
|---|
| 272 | {
|
|---|
| 273 | med[g][num[g]] = dU;
|
|---|
| 274 | avg[g] += dU;
|
|---|
| 275 | num[g]++;
|
|---|
| 276 |
|
|---|
| 277 | if (dU<min[g])
|
|---|
| 278 | min[g] = dU;
|
|---|
| 279 | if (dU>max[g])
|
|---|
| 280 | max[g] = dU;
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 |
|
|---|
| 284 | sort(med[0].begin(), med[0].begin()+num[0]);
|
|---|
| 285 | sort(med[1].begin(), med[1].begin()+num[1]);
|
|---|
| 286 |
|
|---|
| 287 | fCurrentsAvg.assign(BIAS::kNumChannels, 0);
|
|---|
| 288 | fCursorCur = 0;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | fDimDeviation.setQuality(fControlType);
|
|---|
| 292 | fDimDeviation.Update(vec);
|
|---|
| 293 |
|
|---|
| 294 | if (!fOutputEnabled || fStatusBias.second!=BIAS::kVoltageOn)
|
|---|
| 295 | return;
|
|---|
| 296 |
|
|---|
| 297 | // Trigger calibration
|
|---|
| 298 | if (GetCurrentState()==kStateCalibrating && fCursorTemp==1)
|
|---|
| 299 | {
|
|---|
| 300 | DimClient::sendCommandNB("BIAS_CONTROL/REQUEST_STATUS", NULL, 0);
|
|---|
| 301 | return;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | ostringstream msg;
|
|---|
| 305 | msg << setprecision(4) << "Sending new absolute offset (" << fAppliedOffset << "V+" << (num[0]+num[1]>0?(avg[0]+avg[1])/(num[0]+num[1]):0) << "V) to biasctrl.";
|
|---|
| 306 | Info(msg);
|
|---|
| 307 |
|
|---|
| 308 | if (fControlType==kCurrents && num[0]>0 && num[1]>0)
|
|---|
| 309 | {
|
|---|
| 310 | msg.str("");
|
|---|
| 311 | msg << " Avg0=" << setw(7) << avg[0]/num[0] << " | Avg1=" << setw(7) << avg[1]/num[1];
|
|---|
| 312 | Debug(msg);
|
|---|
| 313 |
|
|---|
| 314 | msg.str("");
|
|---|
| 315 | msg << " Med0=" << setw(7) << med[0][num[0]/2] << " | Med1=" << setw(7) << med[1][num[1]/2];
|
|---|
| 316 | Debug(msg);
|
|---|
| 317 |
|
|---|
| 318 | msg.str("");
|
|---|
| 319 | msg << " Min0=" << setw(7) << min[0] << " | Min1=" << setw(7) << min[1];
|
|---|
| 320 | Debug(msg);
|
|---|
| 321 |
|
|---|
| 322 | msg.str("");
|
|---|
| 323 | msg << " Max0=" << setw(7) << max[0] << " | Max1=" << setw(7) << max[1];
|
|---|
| 324 | Debug(msg);
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | DimClient::sendCommandNB("BIAS_CONTROL/SET_ALL_CHANNELS_OFFSET",
|
|---|
| 328 | vec.data()+BIAS::kNumChannels, BIAS::kNumChannels*sizeof(float));
|
|---|
| 329 |
|
|---|
| 330 | fCursorTemp++;
|
|---|
| 331 | }
|
|---|
| 332 |
|
|---|
| 333 | int AverageCurrents()
|
|---|
| 334 | {
|
|---|
| 335 | if (fBiasA.getSize()!=BIAS::kNumChannels*sizeof(int16_t))
|
|---|
| 336 | return -1;
|
|---|
| 337 |
|
|---|
| 338 | if (fStatusBias.second!=BIAS::kVoltageOn)
|
|---|
| 339 | return false;
|
|---|
| 340 |
|
|---|
| 341 | if (fCursorCur++<0)
|
|---|
| 342 | return true;
|
|---|
| 343 |
|
|---|
| 344 | const int16_t *ptr = static_cast<int16_t*>(fBiasA.getData());
|
|---|
| 345 |
|
|---|
| 346 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 347 | {
|
|---|
| 348 | fCurrentsAvg[i] += ptr[i];
|
|---|
| 349 | fCurrentsRms[i] += ptr[i]*ptr[i];
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | return true;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 | void HandleCalibration()
|
|---|
| 357 | {
|
|---|
| 358 | const int rc = AverageCurrents();
|
|---|
| 359 | if (rc<0)
|
|---|
| 360 | return;
|
|---|
| 361 |
|
|---|
| 362 | if (fCursorCur<fNumCalibRequests)
|
|---|
| 363 | {
|
|---|
| 364 | if (fStatusBias.second==BIAS::kVoltageOn)
|
|---|
| 365 | DimClient::sendCommandNB("BIAS_CONTROL/REQUEST_STATUS", NULL, 0);
|
|---|
| 366 | return;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | if (rc==0)
|
|---|
| 370 | return;
|
|---|
| 371 |
|
|---|
| 372 | fCalibration.resize(BIAS::kNumChannels*3);
|
|---|
| 373 |
|
|---|
| 374 | float *avg = fCalibration.data();
|
|---|
| 375 | float *rms = fCalibration.data()+BIAS::kNumChannels;
|
|---|
| 376 | float *res = fCalibration.data()+BIAS::kNumChannels*2;
|
|---|
| 377 |
|
|---|
| 378 | const double conv = 5000./4096;
|
|---|
| 379 |
|
|---|
| 380 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 381 | {
|
|---|
| 382 | const double I = double(fCurrentsAvg[i])/fCursorCur;
|
|---|
| 383 |
|
|---|
| 384 | res[i] = (fVoltGapd[i]+fCalibrationOffset)/I / conv * 1e6;
|
|---|
| 385 | avg[i] = conv * I;
|
|---|
| 386 | rms[i] = conv * sqrt(double(fCurrentsRms[i])/fCursorCur-I*I);
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | fDimCalibration.Update(fCalibration);
|
|---|
| 390 |
|
|---|
| 391 | fOutputEnabled = false;
|
|---|
| 392 | fControlType = kIdle;
|
|---|
| 393 |
|
|---|
| 394 | Info("Calibration successfully done.");
|
|---|
| 395 |
|
|---|
| 396 | if (fStatusBias.second==BIAS::kVoltageOn)
|
|---|
| 397 | DimClient::sendCommandNB("BIAS_CONTROL/REQUEST_STATUS", NULL, 0);
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | void HandleFeedback()
|
|---|
| 401 | {
|
|---|
| 402 | if (fBiasData.getSize()!=1440*sizeof(float))
|
|---|
| 403 | return;
|
|---|
| 404 |
|
|---|
| 405 | // -------- Check age of last stored event --------
|
|---|
| 406 |
|
|---|
| 407 | // Must be called in this order
|
|---|
| 408 | const int tsec = fBiasData.getTimestamp();
|
|---|
| 409 | const int tms = fBiasData.getTimestampMillisecs();
|
|---|
| 410 |
|
|---|
| 411 | const Time tm(tsec, tms*1000);
|
|---|
| 412 |
|
|---|
| 413 | if (Time()-fBiasLast>boost::posix_time::seconds(30))
|
|---|
| 414 | {
|
|---|
| 415 | Warn("Last received event data older than 30s... resetting average calculation.");
|
|---|
| 416 | ResetData();
|
|---|
| 417 | }
|
|---|
| 418 | fBiasLast = tm;
|
|---|
| 419 |
|
|---|
| 420 | // -------- Store new event --------
|
|---|
| 421 |
|
|---|
| 422 | fData[fCursorAmpl%fData.size()].assign(reinterpret_cast<float*>(fBiasData.getData()),
|
|---|
| 423 | reinterpret_cast<float*>(fBiasData.getData())+1440);
|
|---|
| 424 |
|
|---|
| 425 | if (++fCursorAmpl<fData.size())
|
|---|
| 426 | return;
|
|---|
| 427 |
|
|---|
| 428 | // -------- Calculate statistics --------
|
|---|
| 429 |
|
|---|
| 430 | valarray<double> med(1440);
|
|---|
| 431 |
|
|---|
| 432 | for (int ch=0; ch<1440; ch++)
|
|---|
| 433 | {
|
|---|
| 434 | vector<float> arr(fData.size());
|
|---|
| 435 | for (size_t i=0; i<fData.size(); i++)
|
|---|
| 436 | arr[i] = fData[i][ch];
|
|---|
| 437 |
|
|---|
| 438 | sort(arr.begin(), arr.end());
|
|---|
| 439 |
|
|---|
| 440 | med[ch] = arr[arr.size()/2];
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | /*
|
|---|
| 444 | vector<float> med(1440);
|
|---|
| 445 | vector<float> rms(1440);
|
|---|
| 446 | for (size_t i=0; i<fData.size(); i++)
|
|---|
| 447 | {
|
|---|
| 448 | if (fData[i].size()==0)
|
|---|
| 449 | return;
|
|---|
| 450 |
|
|---|
| 451 | for (int j=0; j<1440; j++)
|
|---|
| 452 | {
|
|---|
| 453 | med[j] += fData[i][j];
|
|---|
| 454 | rms[j] += fData[i][j]*fData[i][j];
|
|---|
| 455 | }
|
|---|
| 456 | }
|
|---|
| 457 | */
|
|---|
| 458 |
|
|---|
| 459 | vector<double> avg(BIAS::kNumChannels);
|
|---|
| 460 | vector<int> num(BIAS::kNumChannels);
|
|---|
| 461 | for (int i=0; i<1440; i++)
|
|---|
| 462 | {
|
|---|
| 463 | const PixelMapEntry &ch = fMap.hw(i);
|
|---|
| 464 |
|
|---|
| 465 | // FIXME: Add a consistency check if the median makes sense...
|
|---|
| 466 | // FIXME: Add a consistency check to remove pixels with bright stars (median?)
|
|---|
| 467 |
|
|---|
| 468 | avg[ch.hv()] += med[i];
|
|---|
| 469 | num[ch.hv()]++;
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 473 | {
|
|---|
| 474 | if (num[i])
|
|---|
| 475 | avg[i] /= num[i];
|
|---|
| 476 |
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | // -------- Calculate correction --------
|
|---|
| 480 |
|
|---|
| 481 | // http://bestune.50megs.com/typeABC.htm
|
|---|
| 482 |
|
|---|
| 483 | // CO: Controller output
|
|---|
| 484 | // PV: Process variable
|
|---|
| 485 | // SP: Set point
|
|---|
| 486 | // T: Sampling period (loop update period)
|
|---|
| 487 | // e = SP - PV
|
|---|
| 488 | //
|
|---|
| 489 | // Kp : No units
|
|---|
| 490 | // Ki : per seconds
|
|---|
| 491 | // Kd : seconds
|
|---|
| 492 |
|
|---|
| 493 | // CO(k)-CO(k-1) = - Kp[ PV(k) - PV(k-1) ] + Ki * T * (SP(k)-PV(k)) - Kd/T [ PV(k) - 2PV(k-1) + PV(k-2) ]
|
|---|
| 494 |
|
|---|
| 495 | if (fCursorAmpl%fData.size()>0)
|
|---|
| 496 | return;
|
|---|
| 497 |
|
|---|
| 498 | // FIXME: Take out broken / dead boards.
|
|---|
| 499 |
|
|---|
| 500 | const Time tm0 = Time();
|
|---|
| 501 |
|
|---|
| 502 | /*const*/ double T21 = fT>0 ? fT : (tm0-fStartTime).total_microseconds()/1000000.;
|
|---|
| 503 | const double T10 = fT21;
|
|---|
| 504 | fT21 = T21;
|
|---|
| 505 |
|
|---|
| 506 | fStartTime = tm0;
|
|---|
| 507 |
|
|---|
| 508 | ostringstream out;
|
|---|
| 509 | out << "New " << fData.size() << " event received: " << fCursorAmpl << " / " << setprecision(3) << T21 << "s";
|
|---|
| 510 | Info(out);
|
|---|
| 511 |
|
|---|
| 512 | if (fPV[0].size()==0)
|
|---|
| 513 | {
|
|---|
| 514 | fPV[0].resize(avg.size());
|
|---|
| 515 | fPV[0] = valarray<double>(avg.data(), avg.size());
|
|---|
| 516 | return;
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | if (fPV[1].size()==0)
|
|---|
| 520 | {
|
|---|
| 521 | fPV[1].resize(avg.size());
|
|---|
| 522 | fPV[1] = valarray<double>(avg.data(), avg.size());
|
|---|
| 523 | return;
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | if (fPV[2].size()==0)
|
|---|
| 527 | {
|
|---|
| 528 | fPV[2].resize(avg.size());
|
|---|
| 529 | fPV[2] = valarray<double>(avg.data(), avg.size());
|
|---|
| 530 | return;
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | fPV[0] = fPV[1];
|
|---|
| 534 | fPV[1] = fPV[2];
|
|---|
| 535 |
|
|---|
| 536 | fPV[2].resize(avg.size());
|
|---|
| 537 | fPV[2] = valarray<double>(avg.data(), avg.size());
|
|---|
| 538 |
|
|---|
| 539 | if (T10<=0 || T21<=0)
|
|---|
| 540 | return;
|
|---|
| 541 |
|
|---|
| 542 | //cout << "Calculating (" << fCursor << ":" << T21 << ")... " << endl;
|
|---|
| 543 |
|
|---|
| 544 | // fKi[j] = response[j]*gain;
|
|---|
| 545 | // Kp = 0;
|
|---|
| 546 | // Kd = 0;
|
|---|
| 547 |
|
|---|
| 548 | // => Kp = 0.01 * gain = 0.00005
|
|---|
| 549 | // => Ki = 0.8 * gain/20s = 0.00025
|
|---|
| 550 | // => Kd = 0.1 * gain/20s = 0.00003
|
|---|
| 551 |
|
|---|
| 552 | /*
|
|---|
| 553 | fKp = 0;
|
|---|
| 554 | fKd = 0;
|
|---|
| 555 | fKi = 0.00003*20;
|
|---|
| 556 | T21 = 1;
|
|---|
| 557 | */
|
|---|
| 558 |
|
|---|
| 559 | //valarray<double> correction = - Kp*(PV[2] - PV[1]) + Ki * dT * (SP-PV[2]) - Kd/dT * (PV[2] - 2*PV[1] + PV[0]);
|
|---|
| 560 | //valarray<double> correction =
|
|---|
| 561 | // - Kp * (PV[2] - PV[1])
|
|---|
| 562 | // + dT * Ki * (SP - PV[2])
|
|---|
| 563 | // - Kd / dT * (PV[2] - 2*PV[1] + PV[0]);
|
|---|
| 564 | //
|
|---|
| 565 | // - (Kp+Kd/dT1) * (PV[2] - PV[1])
|
|---|
| 566 | // + dT2 * Ki * (SP - PV[2])
|
|---|
| 567 | // + Kd / dT1 * (PV[1] - PV[0]);
|
|---|
| 568 | //
|
|---|
| 569 | // - Kp * (PV[2] - PV[1])
|
|---|
| 570 | // + Ki * (SP - PV[2])*dT
|
|---|
| 571 | // - Kd * (PV[2] - PV[1])/dT
|
|---|
| 572 | // + Kd * (PV[1] - PV[0])/dT;
|
|---|
| 573 | //
|
|---|
| 574 | //valarray<double> correction =
|
|---|
| 575 | // - Kp*(PV[2] - PV[1]) + Ki * T21 * (SP-PV[2]) - Kd*(PV[2]-PV[1])/T21 - Kd*(PV[0]-PV[1])/T01;
|
|---|
| 576 | const valarray<double> correction = 1./fGain/1000*
|
|---|
| 577 | (
|
|---|
| 578 | - (fKp+fKd/T21)*(fPV[2] - fPV[1])
|
|---|
| 579 | + fKi*T21*(fSP-fPV[2])
|
|---|
| 580 | + fKd/T10*(fPV[1]-fPV[0])
|
|---|
| 581 | );
|
|---|
| 582 |
|
|---|
| 583 | /*
|
|---|
| 584 | integral = 0
|
|---|
| 585 | start:
|
|---|
| 586 | integral += (fSP - fPV[2])*dt
|
|---|
| 587 |
|
|---|
| 588 | output = Kp*(fSP - fPV[2]) + Ki*integral - Kd*(fPV[2] - fPV[1])/dt
|
|---|
| 589 |
|
|---|
| 590 | wait(dt)
|
|---|
| 591 |
|
|---|
| 592 | goto start
|
|---|
| 593 | */
|
|---|
| 594 |
|
|---|
| 595 | vector<float> vec(2*BIAS::kNumChannels+2);
|
|---|
| 596 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 597 | vec[i] = fPV[2][i]-fSP[i];
|
|---|
| 598 |
|
|---|
| 599 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 600 | vec[i+BIAS::kNumChannels] = avg[i]<5*2.5 ? 0 : correction[i];
|
|---|
| 601 |
|
|---|
| 602 | fDimDeviation.setQuality(fControlType);
|
|---|
| 603 | fDimDeviation.Update(vec);
|
|---|
| 604 |
|
|---|
| 605 | if (!fOutputEnabled || fStatusBias.second!=BIAS::kVoltageOn)
|
|---|
| 606 | return;
|
|---|
| 607 |
|
|---|
| 608 | Info("Sending new relative offset to biasctrl.");
|
|---|
| 609 |
|
|---|
| 610 | DimClient::sendCommandNB("BIAS_CONTROL/INCREASE_ALL_CHANNELS_VOLTAGE",
|
|---|
| 611 | vec.data()+BIAS::kNumChannels, BIAS::kNumChannels*sizeof(float));
|
|---|
| 612 | }
|
|---|
| 613 |
|
|---|
| 614 | void HandleGlobalFeedback()
|
|---|
| 615 | {
|
|---|
| 616 | if (fBiasData.getSize()!=1440*sizeof(float))
|
|---|
| 617 | return;
|
|---|
| 618 |
|
|---|
| 619 | // -------- Store new event --------
|
|---|
| 620 |
|
|---|
| 621 | vector<float> arr(reinterpret_cast<float*>(fBiasData.getData()),
|
|---|
| 622 | reinterpret_cast<float*>(fBiasData.getData())+1440);
|
|---|
| 623 |
|
|---|
| 624 | sort(arr.begin(), arr.end());
|
|---|
| 625 |
|
|---|
| 626 | const float med = arr[arr.size()/2];
|
|---|
| 627 |
|
|---|
| 628 | fData[fCursorAmpl%fData.size()].resize(1); //assign(&med, &med);
|
|---|
| 629 | fData[fCursorAmpl%fData.size()][0] = med; //assign(&med, &med);
|
|---|
| 630 |
|
|---|
| 631 | if (++fCursorAmpl<fData.size())
|
|---|
| 632 | return;
|
|---|
| 633 |
|
|---|
| 634 | // -------- Calculate statistics --------
|
|---|
| 635 |
|
|---|
| 636 | double avg=0;
|
|---|
| 637 | double rms=0;
|
|---|
| 638 | for (size_t i=0; i<fData.size(); i++)
|
|---|
| 639 | {
|
|---|
| 640 | avg += fData[i][0];
|
|---|
| 641 | rms += fData[i][0]*fData[i][0];
|
|---|
| 642 | }
|
|---|
| 643 |
|
|---|
| 644 | avg /= fData.size();
|
|---|
| 645 | rms /= fData.size();
|
|---|
| 646 |
|
|---|
| 647 | rms = sqrt(rms-avg*avg);
|
|---|
| 648 |
|
|---|
| 649 | // -------- Calculate correction --------
|
|---|
| 650 |
|
|---|
| 651 | if (fCursorAmpl%fData.size()!=0)
|
|---|
| 652 | return;
|
|---|
| 653 |
|
|---|
| 654 | Out() << "Amplitude: " << avg << " +- " << rms << endl;
|
|---|
| 655 |
|
|---|
| 656 | // FIXME: Take out broken / dead boards.
|
|---|
| 657 |
|
|---|
| 658 | /*
|
|---|
| 659 | ostringstream out;
|
|---|
| 660 | out << "New " << fData.size() << " event received: " << fCursor << " / " << setprecision(3) << T21 << "s";
|
|---|
| 661 | Info(out);
|
|---|
| 662 | */
|
|---|
| 663 |
|
|---|
| 664 | if (fPV[0].size()==0)
|
|---|
| 665 | {
|
|---|
| 666 | fPV[0].resize(1);
|
|---|
| 667 | fPV[0] = valarray<double>(&avg, 1);
|
|---|
| 668 | return;
|
|---|
| 669 | }
|
|---|
| 670 |
|
|---|
| 671 | if (fPV[1].size()==0)
|
|---|
| 672 | {
|
|---|
| 673 | fPV[1].resize(1);
|
|---|
| 674 | fPV[1] = valarray<double>(&avg, 1);
|
|---|
| 675 | return;
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | if (fPV[2].size()==0)
|
|---|
| 679 | {
|
|---|
| 680 | fPV[2].resize(1);
|
|---|
| 681 | fPV[2] = valarray<double>(&avg, 1);
|
|---|
| 682 | return;
|
|---|
| 683 | }
|
|---|
| 684 |
|
|---|
| 685 | fPV[0] = fPV[1];
|
|---|
| 686 | fPV[1] = fPV[2];
|
|---|
| 687 |
|
|---|
| 688 | fPV[2].resize(1);
|
|---|
| 689 | fPV[2] = valarray<double>(&avg, 1);
|
|---|
| 690 |
|
|---|
| 691 | // ----- Calculate average currents -----
|
|---|
| 692 |
|
|---|
| 693 | vector<float> A(BIAS::kNumChannels);
|
|---|
| 694 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 695 | A[i] = double(fCurrentsAvg[i]) / fCursorCur;
|
|---|
| 696 |
|
|---|
| 697 | fCurrentsAvg.assign(BIAS::kNumChannels, 0);
|
|---|
| 698 | fCursorCur = 0;
|
|---|
| 699 |
|
|---|
| 700 | // -------- Calculate correction --------
|
|---|
| 701 |
|
|---|
| 702 | // correction = (fSP[0]-fPV[2])*fKi
|
|---|
| 703 | /*
|
|---|
| 704 | const double T21 = 1; // feedback is 1s
|
|---|
| 705 | const double T10 = 1; // feedback is 20s
|
|---|
| 706 |
|
|---|
| 707 | const valarray<double> correction = 1./fGain/1000*
|
|---|
| 708 | (
|
|---|
| 709 | - (fKp+fKd/T21)*(fPV[2] - fPV[1])
|
|---|
| 710 | + fKi*T21*(fSP[0]-fPV[2])
|
|---|
| 711 | + fKd/T10*(fPV[1]-fPV[0])
|
|---|
| 712 | );
|
|---|
| 713 | */
|
|---|
| 714 |
|
|---|
| 715 | // pow of 1.6 comes from the non-linearity of the
|
|---|
| 716 | // amplitude vs bias voltage
|
|---|
| 717 | const valarray<double> correction = 1./fGain/1000*
|
|---|
| 718 | (
|
|---|
| 719 | //fKi*(pow(fSP[0], 1./1.6)-pow(fPV[2], 1./1.6))
|
|---|
| 720 | fKi*(fSP[0]-fPV[2])
|
|---|
| 721 | );
|
|---|
| 722 |
|
|---|
| 723 | Out() << "Correction: " << correction[0] << "V (" << fSP[0] << ")" << endl;
|
|---|
| 724 |
|
|---|
| 725 | const int nch = BIAS::kNumChannels;
|
|---|
| 726 |
|
|---|
| 727 | // FIXME: Sanity check!
|
|---|
| 728 |
|
|---|
| 729 | vector<float> vec;
|
|---|
| 730 | vec.reserve(2*nch+2);
|
|---|
| 731 | vec.insert(vec.begin(), nch, fPV[2][0]-fSP[0]);
|
|---|
| 732 | vec.insert(vec.begin()+nch, nch, correction[0]);
|
|---|
| 733 | vec.push_back(0);
|
|---|
| 734 | vec.push_back(0);
|
|---|
| 735 |
|
|---|
| 736 | fDimDeviation.setQuality(fControlType);
|
|---|
| 737 | fDimDeviation.Update(vec);
|
|---|
| 738 |
|
|---|
| 739 | if (!fOutputEnabled || fStatusBias.second!=BIAS::kVoltageOn)
|
|---|
| 740 | return;
|
|---|
| 741 |
|
|---|
| 742 | Info("Sending new global relative offset to biasctrl.");
|
|---|
| 743 |
|
|---|
| 744 | DimClient::sendCommandNB("BIAS_CONTROL/INCREASE_ALL_CHANNELS_VOLTAGE",
|
|---|
| 745 | vec.data()+BIAS::kNumChannels, BIAS::kNumChannels*sizeof(float));
|
|---|
| 746 | }
|
|---|
| 747 |
|
|---|
| 748 | void infoHandler()
|
|---|
| 749 | {
|
|---|
| 750 | DimInfo *curr = getInfo(); // get current DimInfo address
|
|---|
| 751 | if (!curr)
|
|---|
| 752 | return;
|
|---|
| 753 |
|
|---|
| 754 | if (curr==&fBias)
|
|---|
| 755 | {
|
|---|
| 756 | fStatusBias = GetNewState(fBias);
|
|---|
| 757 | return;
|
|---|
| 758 | }
|
|---|
| 759 |
|
|---|
| 760 | if (curr==&fFAD)
|
|---|
| 761 | {
|
|---|
| 762 | fStatusFAD = GetNewState(fFAD);
|
|---|
| 763 | return;
|
|---|
| 764 | }
|
|---|
| 765 |
|
|---|
| 766 | if (curr==&fFSC)
|
|---|
| 767 | {
|
|---|
| 768 | fStatusFSC = GetNewState(fFSC);
|
|---|
| 769 | return;
|
|---|
| 770 | }
|
|---|
| 771 |
|
|---|
| 772 | if (curr==&fDim)
|
|---|
| 773 | {
|
|---|
| 774 | fStatusDim = GetNewState(fDim);
|
|---|
| 775 | fStatusDim.second = curr->getSize()==4 ? curr->getInt() : 0;
|
|---|
| 776 | return;
|
|---|
| 777 | }
|
|---|
| 778 |
|
|---|
| 779 | if (curr==&fBiasNom)
|
|---|
| 780 | {
|
|---|
| 781 | const float *ptr = reinterpret_cast<float*>(fBiasNom.getData());
|
|---|
| 782 | fVoltGapd.assign(ptr, ptr+416);
|
|---|
| 783 | Info("Nominal bias voltages received.");
|
|---|
| 784 | return;
|
|---|
| 785 | }
|
|---|
| 786 |
|
|---|
| 787 | if (curr==&fCameraTemp && (fControlType==kTemp || fControlType==kCurrents))
|
|---|
| 788 | HandleCameraTemp();
|
|---|
| 789 |
|
|---|
| 790 | if (curr==&fBiasA && fControlType==kTemp && GetCurrentState()==kStateCalibrating)
|
|---|
| 791 | HandleCalibration();
|
|---|
| 792 |
|
|---|
| 793 | if (curr==&fBiasA && (fControlType==kFeedbackGlobal || fControlType==kCurrents))
|
|---|
| 794 | AverageCurrents();
|
|---|
| 795 |
|
|---|
| 796 | if (curr==&fBiasData && fControlType==kFeedback)
|
|---|
| 797 | HandleFeedback();
|
|---|
| 798 |
|
|---|
| 799 | if (curr==&fBiasData && fControlType==kFeedbackGlobal)
|
|---|
| 800 | HandleGlobalFeedback();
|
|---|
| 801 | }
|
|---|
| 802 |
|
|---|
| 803 | bool CheckEventSize(size_t has, const char *name, size_t size)
|
|---|
| 804 | {
|
|---|
| 805 | if (has==size)
|
|---|
| 806 | return true;
|
|---|
| 807 |
|
|---|
| 808 | ostringstream msg;
|
|---|
| 809 | msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
|
|---|
| 810 | Fatal(msg);
|
|---|
| 811 | return false;
|
|---|
| 812 | }
|
|---|
| 813 |
|
|---|
| 814 | void PrintState(const pair<Time,int> &state, const char *server)
|
|---|
| 815 | {
|
|---|
| 816 | const State rc = fNetwork.GetState(server, state.second);
|
|---|
| 817 |
|
|---|
| 818 | Out() << state.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
|
|---|
| 819 | Out() << kBold << server << ": ";
|
|---|
| 820 | Out() << rc.name << "[" << rc.index << "]";
|
|---|
| 821 | Out() << kReset << " - " << kBlue << rc.comment << endl;
|
|---|
| 822 | }
|
|---|
| 823 |
|
|---|
| 824 | int Print()
|
|---|
| 825 | {
|
|---|
| 826 | Out() << fStatusDim.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
|
|---|
| 827 | Out() << kBold << "DIM_DNS: ";
|
|---|
| 828 | if (fStatusDim.second==0)
|
|---|
| 829 | Out() << "Offline" << endl;
|
|---|
| 830 | else
|
|---|
| 831 | Out() << "V" << fStatusDim.second/100 << 'r' << fStatusDim.second%100 << endl;
|
|---|
| 832 |
|
|---|
| 833 | PrintState(fStatusFAD, "FAD_CONTROL");
|
|---|
| 834 | PrintState(fStatusFSC, "FSC_CONTROL");
|
|---|
| 835 | PrintState(fStatusBias, "BIAS_CONTROL");
|
|---|
| 836 |
|
|---|
| 837 | return GetCurrentState();
|
|---|
| 838 | }
|
|---|
| 839 |
|
|---|
| 840 | int PrintCalibration()
|
|---|
| 841 | {
|
|---|
| 842 | if (fCalibration.size()==0)
|
|---|
| 843 | {
|
|---|
| 844 | Out() << "No calibration performed so far." << endl;
|
|---|
| 845 | return GetCurrentState();
|
|---|
| 846 | }
|
|---|
| 847 |
|
|---|
| 848 | const float *avg = fCalibration.data();
|
|---|
| 849 | const float *rms = fCalibration.data()+BIAS::kNumChannels;
|
|---|
| 850 | const float *res = fCalibration.data()+BIAS::kNumChannels*2;
|
|---|
| 851 |
|
|---|
| 852 | Out() << "Average current at " << fCalibrationOffset << "V below G-APD operation voltage:\n";
|
|---|
| 853 |
|
|---|
| 854 | for (int k=0; k<13; k++)
|
|---|
| 855 | for (int j=0; j<8; j++)
|
|---|
| 856 | {
|
|---|
| 857 | Out() << setw(2) << k << "|" << setw(2) << j*4 << "|";
|
|---|
| 858 | for (int i=0; i<4; i++)
|
|---|
| 859 | Out() << Tools::Form(" %6.1f+-%4.1f", avg[k*32+j*4+i], rms[k*32+j*4+i]);
|
|---|
| 860 | Out() << '\n';
|
|---|
| 861 | }
|
|---|
| 862 | Out() << '\n';
|
|---|
| 863 |
|
|---|
| 864 | Out() << "Measured calibration resistor:\n";
|
|---|
| 865 | for (int k=0; k<13; k++)
|
|---|
| 866 | for (int j=0; j<4; j++)
|
|---|
| 867 | {
|
|---|
| 868 | Out() << setw(2) << k << "|" << setw(2) << j*8 << "|";
|
|---|
| 869 | for (int i=0; i<8; i++)
|
|---|
| 870 | Out() << Tools::Form(" %5.0f", res[k*32+j*8+i]);
|
|---|
| 871 | Out() << '\n';
|
|---|
| 872 | }
|
|---|
| 873 |
|
|---|
| 874 | Out() << flush;
|
|---|
| 875 |
|
|---|
| 876 | return GetCurrentState();
|
|---|
| 877 | }
|
|---|
| 878 |
|
|---|
| 879 | void WarnState(bool needfsc, bool needfad)
|
|---|
| 880 | {
|
|---|
| 881 | const bool bias = fStatusBias.second >= BIAS::kConnecting;
|
|---|
| 882 | const bool fsc = fStatusFSC.second >= 2;
|
|---|
| 883 | const bool fad = fStatusFAD.second >= FAD::kConnected;
|
|---|
| 884 |
|
|---|
| 885 | if (!bias)
|
|---|
| 886 | Warn("Bias control not yet ready.");
|
|---|
| 887 | if (needfsc && !fsc)
|
|---|
| 888 | Warn("FSC control not yet ready.");
|
|---|
| 889 | if (needfad && !fad)
|
|---|
| 890 | Warn("FAD control not yet ready.");
|
|---|
| 891 | }
|
|---|
| 892 |
|
|---|
| 893 | int SetConstant(const EventImp &evt, int constant)
|
|---|
| 894 | {
|
|---|
| 895 | if (!CheckEventSize(evt.GetSize(), "SetConstant", 8))
|
|---|
| 896 | return kSM_FatalError;
|
|---|
| 897 |
|
|---|
| 898 | switch (constant)
|
|---|
| 899 | {
|
|---|
| 900 | case 0: fKi = evt.GetDouble(); break;
|
|---|
| 901 | case 1: fKp = evt.GetDouble(); break;
|
|---|
| 902 | case 2: fKd = evt.GetDouble(); break;
|
|---|
| 903 | case 3: fT = evt.GetDouble(); break;
|
|---|
| 904 | case 4: fGain = evt.GetDouble(); break;
|
|---|
| 905 | default:
|
|---|
| 906 | Fatal("SetConstant got an unexpected constant id -- this is a program bug!");
|
|---|
| 907 | return kSM_FatalError;
|
|---|
| 908 | }
|
|---|
| 909 |
|
|---|
| 910 | return GetCurrentState();
|
|---|
| 911 | }
|
|---|
| 912 |
|
|---|
| 913 | int EnableOutput(const EventImp &evt)
|
|---|
| 914 | {
|
|---|
| 915 | if (!CheckEventSize(evt.GetSize(), "EnableOutput", 1))
|
|---|
| 916 | return kSM_FatalError;
|
|---|
| 917 |
|
|---|
| 918 | fOutputEnabled = evt.GetBool();
|
|---|
| 919 |
|
|---|
| 920 | return GetCurrentState();
|
|---|
| 921 | }
|
|---|
| 922 |
|
|---|
| 923 | void ResetData(int16_t n=-1)
|
|---|
| 924 | {
|
|---|
| 925 | fData.assign(n>0 ? n : fData.size(), vector<float>(0));
|
|---|
| 926 |
|
|---|
| 927 | fCursorAmpl = 0;
|
|---|
| 928 | fCursorCur = 0;
|
|---|
| 929 | fCursorTemp = 0;
|
|---|
| 930 |
|
|---|
| 931 | fStartTime = Time();
|
|---|
| 932 |
|
|---|
| 933 | fSP = valarray<double>(0., BIAS::kNumChannels);
|
|---|
| 934 |
|
|---|
| 935 | vector<float> vec(2*BIAS::kNumChannels+2);
|
|---|
| 936 | vec[2*BIAS::kNumChannels+1] = fBiasOffset;
|
|---|
| 937 | fDimDeviation.setQuality(kIdle);
|
|---|
| 938 | fDimDeviation.Update(vec);
|
|---|
| 939 |
|
|---|
| 940 | fPV[0].resize(0);
|
|---|
| 941 | fPV[1].resize(0);
|
|---|
| 942 | fPV[2].resize(0);
|
|---|
| 943 |
|
|---|
| 944 | fCurrentsAvg.assign(BIAS::kNumChannels, 0);
|
|---|
| 945 | fCurrentsRms.assign(BIAS::kNumChannels, 0);
|
|---|
| 946 |
|
|---|
| 947 | if (fKp==0 && fKi==0 && fKd==0)
|
|---|
| 948 | Warn("Control loop parameters are all set to zero.");
|
|---|
| 949 | }
|
|---|
| 950 |
|
|---|
| 951 | int StartFeedback(const EventImp &evt)
|
|---|
| 952 | {
|
|---|
| 953 | if (!CheckEventSize(evt.GetSize(), "StartFeedback", 2))
|
|---|
| 954 | return kSM_FatalError;
|
|---|
| 955 |
|
|---|
| 956 | WarnState(false, true);
|
|---|
| 957 |
|
|---|
| 958 | fBiasOffset = 0;
|
|---|
| 959 | ResetData(evt.GetShort());
|
|---|
| 960 |
|
|---|
| 961 | fControlType = kFeedback;
|
|---|
| 962 |
|
|---|
| 963 | return GetCurrentState();
|
|---|
| 964 | }
|
|---|
| 965 |
|
|---|
| 966 | int StartFeedbackGlobal(const EventImp &evt)
|
|---|
| 967 | {
|
|---|
| 968 | if (!CheckEventSize(evt.GetSize(), "StartFeedbackGlobal", 2))
|
|---|
| 969 | return kSM_FatalError;
|
|---|
| 970 |
|
|---|
| 971 | WarnState(false, true);
|
|---|
| 972 |
|
|---|
| 973 | fBiasOffset = 0;
|
|---|
| 974 | ResetData(evt.GetShort());
|
|---|
| 975 |
|
|---|
| 976 | fControlType = kFeedbackGlobal;
|
|---|
| 977 |
|
|---|
| 978 | return GetCurrentState();
|
|---|
| 979 | }
|
|---|
| 980 |
|
|---|
| 981 | int StartTempCtrl(const EventImp &evt)
|
|---|
| 982 | {
|
|---|
| 983 | if (!CheckEventSize(evt.GetSize(), "StartTempCtrl", 4))
|
|---|
| 984 | return kSM_FatalError;
|
|---|
| 985 |
|
|---|
| 986 | WarnState(true, false);
|
|---|
| 987 |
|
|---|
| 988 | fBiasOffset = evt.GetFloat();
|
|---|
| 989 | fControlType = kTemp;
|
|---|
| 990 |
|
|---|
| 991 | ostringstream out;
|
|---|
| 992 | out << "Starting temperature feedback with an offset of " << fBiasOffset << "V";
|
|---|
| 993 | Message(out);
|
|---|
| 994 |
|
|---|
| 995 | if (fStatusBias.second==BIAS::kVoltageOn)
|
|---|
| 996 | DimClient::sendCommandNB("BIAS_CONTROL/REQUEST_STATUS", NULL, 0);
|
|---|
| 997 |
|
|---|
| 998 | return GetCurrentState();
|
|---|
| 999 | }
|
|---|
| 1000 |
|
|---|
| 1001 | int StartCurrentCtrl(const EventImp &evt)
|
|---|
| 1002 | {
|
|---|
| 1003 | if (!CheckEventSize(evt.GetSize(), "StartCurrentCtrl", 4))
|
|---|
| 1004 | return kSM_FatalError;
|
|---|
| 1005 |
|
|---|
| 1006 | if (fCalibration.size()==0)
|
|---|
| 1007 | {
|
|---|
| 1008 | Warn("Current control needs a bias crate calibration first... command ignored.");
|
|---|
| 1009 | return GetCurrentState();
|
|---|
| 1010 | }
|
|---|
| 1011 |
|
|---|
| 1012 | WarnState(true, false);
|
|---|
| 1013 |
|
|---|
| 1014 | fBiasOffset = evt.GetFloat();
|
|---|
| 1015 | ResetData(0);
|
|---|
| 1016 | fControlType = kCurrents;
|
|---|
| 1017 |
|
|---|
| 1018 | ostringstream out;
|
|---|
| 1019 | out << "Starting current/temp feedback with an offset of " << fBiasOffset << "V";
|
|---|
| 1020 | Message(out);
|
|---|
| 1021 |
|
|---|
| 1022 | return GetCurrentState();
|
|---|
| 1023 | }
|
|---|
| 1024 |
|
|---|
| 1025 | int StopFeedback()
|
|---|
| 1026 | {
|
|---|
| 1027 | fControlType = kIdle;
|
|---|
| 1028 |
|
|---|
| 1029 | return GetCurrentState();
|
|---|
| 1030 | }
|
|---|
| 1031 |
|
|---|
| 1032 | int StoreReference()
|
|---|
| 1033 | {
|
|---|
| 1034 | if (!fPV[0].size() && !fPV[1].size() && !fPV[2].size())
|
|---|
| 1035 | {
|
|---|
| 1036 | Warn("No values in memory. Take enough events first!");
|
|---|
| 1037 | return GetCurrentState();
|
|---|
| 1038 | }
|
|---|
| 1039 |
|
|---|
| 1040 | // FIXME: Check age
|
|---|
| 1041 |
|
|---|
| 1042 | if (!fPV[1].size() && !fPV[2].size())
|
|---|
| 1043 | fSP = fPV[0];
|
|---|
| 1044 |
|
|---|
| 1045 | if (!fPV[2].size())
|
|---|
| 1046 | fSP = fPV[1];
|
|---|
| 1047 | else
|
|---|
| 1048 | fSP = fPV[2];
|
|---|
| 1049 |
|
|---|
| 1050 | vector<float> vec(BIAS::kNumChannels);
|
|---|
| 1051 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 1052 | vec[i] = fSP[i];
|
|---|
| 1053 | fDimReference.Update(vec);
|
|---|
| 1054 |
|
|---|
| 1055 | return GetCurrentState();
|
|---|
| 1056 | }
|
|---|
| 1057 |
|
|---|
| 1058 | int SetReference(const EventImp &evt)
|
|---|
| 1059 | {
|
|---|
| 1060 | if (!CheckEventSize(evt.GetSize(), "SetReference", 4))
|
|---|
| 1061 | return kSM_FatalError;
|
|---|
| 1062 |
|
|---|
| 1063 | const float val = evt.GetFloat();
|
|---|
| 1064 | /*
|
|---|
| 1065 | if (!fPV[0].size() && !fPV[1].size() && !fPV[2].size())
|
|---|
| 1066 | {
|
|---|
| 1067 | Warn("No values in memory. Take enough events first!");
|
|---|
| 1068 | return GetCurrentState();
|
|---|
| 1069 | }*/
|
|---|
| 1070 |
|
|---|
| 1071 | vector<float> vec(BIAS::kNumChannels);
|
|---|
| 1072 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 1073 | vec[i] = fSP[i] = val;
|
|---|
| 1074 | fDimReference.Update(vec);
|
|---|
| 1075 |
|
|---|
| 1076 | Out() << "New global reference value: " << val << "mV" << endl;
|
|---|
| 1077 |
|
|---|
| 1078 | return GetCurrentState();
|
|---|
| 1079 | }
|
|---|
| 1080 |
|
|---|
| 1081 | int CalibrateCurrents()
|
|---|
| 1082 | {
|
|---|
| 1083 | // if (!CheckEventSize(evt.GetSize(), "StartTempCtrl", 4))
|
|---|
| 1084 | // return kSM_FatalError;
|
|---|
| 1085 |
|
|---|
| 1086 | if (fStatusBias.second==BIAS::kRamping)
|
|---|
| 1087 | {
|
|---|
| 1088 | Warn("Calibration cannot be started when biasctrl is in state Ramping.");
|
|---|
| 1089 | return GetCurrentState();
|
|---|
| 1090 | }
|
|---|
| 1091 |
|
|---|
| 1092 | if (fVoltGapd.size()==0)
|
|---|
| 1093 | {
|
|---|
| 1094 | Error("No G-APD reference voltages received yet (BIAS_CONTROL/NOMINAL).");
|
|---|
| 1095 | return GetCurrentState();
|
|---|
| 1096 | }
|
|---|
| 1097 |
|
|---|
| 1098 | WarnState(true, false);
|
|---|
| 1099 |
|
|---|
| 1100 | ostringstream out;
|
|---|
| 1101 | out << "Starting temperature feedback for calibration with an offset of " << fCalibrationOffset << "V";
|
|---|
| 1102 | Message(out);
|
|---|
| 1103 |
|
|---|
| 1104 | fBiasOffset = fCalibrationOffset;
|
|---|
| 1105 | fControlType = kTemp;
|
|---|
| 1106 | fCursorCur = -fNumCalibIgnore;
|
|---|
| 1107 | fCursorTemp = 0;
|
|---|
| 1108 | fCurrentsAvg.assign(BIAS::kNumChannels, 0);
|
|---|
| 1109 | fCurrentsRms.assign(BIAS::kNumChannels, 0);
|
|---|
| 1110 | fCalibration.resize(0);
|
|---|
| 1111 | fStartTime = Time();
|
|---|
| 1112 | fOutputEnabled = true;
|
|---|
| 1113 |
|
|---|
| 1114 | return kStateCalibrating;
|
|---|
| 1115 | }
|
|---|
| 1116 |
|
|---|
| 1117 | int SetCurrentRequestInterval(const EventImp &evt)
|
|---|
| 1118 | {
|
|---|
| 1119 | if (!CheckEventSize(evt.GetSize(), "SetCurrentRequestInterval", 2))
|
|---|
| 1120 | return kSM_FatalError;
|
|---|
| 1121 |
|
|---|
| 1122 | fCurrentRequestInterval = evt.GetUShort();
|
|---|
| 1123 |
|
|---|
| 1124 | Out() << "New current request interval: " << fCurrentRequestInterval << "ms" << endl;
|
|---|
| 1125 |
|
|---|
| 1126 | return GetCurrentState();
|
|---|
| 1127 | }
|
|---|
| 1128 |
|
|---|
| 1129 | int Execute()
|
|---|
| 1130 | {
|
|---|
| 1131 | // Dispatch (execute) at most one handler from the queue. In contrary
|
|---|
| 1132 | // to run_one(), it doesn't wait until a handler is available
|
|---|
| 1133 | // which can be dispatched, so poll_one() might return with 0
|
|---|
| 1134 | // handlers dispatched. The handlers are always dispatched/executed
|
|---|
| 1135 | // synchronously, i.e. within the call to poll_one()
|
|---|
| 1136 | //poll_one();
|
|---|
| 1137 |
|
|---|
| 1138 | if (fStatusDim.second==0)
|
|---|
| 1139 | return kStateDimNetworkNA;
|
|---|
| 1140 |
|
|---|
| 1141 | const bool bias = fStatusBias.second >= BIAS::kConnecting;
|
|---|
| 1142 | const bool fad = fStatusFAD.second >= FAD::kConnected;
|
|---|
| 1143 | const bool fsc = fStatusFSC.second >= 2;
|
|---|
| 1144 |
|
|---|
| 1145 | // All subsystems are not connected
|
|---|
| 1146 | if (!bias && !fad && !fsc)
|
|---|
| 1147 | return kStateDisconnected;
|
|---|
| 1148 |
|
|---|
| 1149 | // At least one subsystem apart from bias is connected
|
|---|
| 1150 | if (bias && !fad && !fsc)
|
|---|
| 1151 | return kStateConnecting;
|
|---|
| 1152 |
|
|---|
| 1153 | /*
|
|---|
| 1154 | // All subsystems are connected
|
|---|
| 1155 | if (GetCurrentStatus()==kStateConfiguringStep1)
|
|---|
| 1156 | {
|
|---|
| 1157 | if (fCursor<1)
|
|---|
| 1158 | return kStateConfiguringStep1;
|
|---|
| 1159 |
|
|---|
| 1160 | if (fCursor==1)
|
|---|
| 1161 | {
|
|---|
| 1162 | fStartTime = Time();
|
|---|
| 1163 | return kStateConfiguringStep2;
|
|---|
| 1164 | }
|
|---|
| 1165 | }
|
|---|
| 1166 | if (GetCurrentStatus()==kStateConfiguringStep2)
|
|---|
| 1167 | {
|
|---|
| 1168 | if (fCursor==1)
|
|---|
| 1169 | {
|
|---|
| 1170 | if ((Time()-fStartTime).total_microseconds()/1000000.<1.5)
|
|---|
| 1171 | return kStateConfiguringStep2;
|
|---|
| 1172 |
|
|---|
| 1173 | Dim::SendCommand("BIAS_CONTROL/REQUEST_STATUS");
|
|---|
| 1174 | }
|
|---|
| 1175 | if (fCursor==2)
|
|---|
| 1176 | {
|
|---|
| 1177 |
|
|---|
| 1178 | int n=0;
|
|---|
| 1179 | double avg = 0;
|
|---|
| 1180 | for (size_t i=0; i<fCurrents.size(); i++)
|
|---|
| 1181 | if (fCurrents[i]>=0)
|
|---|
| 1182 | {
|
|---|
| 1183 | avg += fCurrents[i];
|
|---|
| 1184 | n++;
|
|---|
| 1185 | }
|
|---|
| 1186 |
|
|---|
| 1187 | cout << avg/n << endl;
|
|---|
| 1188 | }
|
|---|
| 1189 | return kStateConnected;
|
|---|
| 1190 | }
|
|---|
| 1191 | */
|
|---|
| 1192 |
|
|---|
| 1193 | // Needs connection of FAD and BIAS
|
|---|
| 1194 | if (bias && fad)
|
|---|
| 1195 | {
|
|---|
| 1196 | if (fControlType==kFeedback || fControlType==kFeedbackGlobal)
|
|---|
| 1197 | return fOutputEnabled ? kStateFeedbackCtrlRunning : kStateFeedbackCtrlIdle;
|
|---|
| 1198 | }
|
|---|
| 1199 |
|
|---|
| 1200 | // Needs connection of FSC and BIAS
|
|---|
| 1201 | if (bias && fsc)
|
|---|
| 1202 | {
|
|---|
| 1203 | if (fControlType==kTemp)
|
|---|
| 1204 | {
|
|---|
| 1205 | if (GetCurrentState()==kStateCalibrating && fCursorCur<fNumCalibRequests)
|
|---|
| 1206 | return GetCurrentState();
|
|---|
| 1207 |
|
|---|
| 1208 | return fOutputEnabled ? kStateTempCtrlRunning : kStateTempCtrlIdle;
|
|---|
| 1209 | }
|
|---|
| 1210 | if (fControlType==kCurrents)
|
|---|
| 1211 | {
|
|---|
| 1212 | static Time past;
|
|---|
| 1213 | if (fCurrentRequestInterval>0 && Time()-past>boost::posix_time::milliseconds(fCurrentRequestInterval))
|
|---|
| 1214 | {
|
|---|
| 1215 | if (fStatusBias.second==BIAS::kVoltageOn)
|
|---|
| 1216 | DimClient::sendCommandNB("BIAS_CONTROL/REQUEST_STATUS", NULL, 0);
|
|---|
| 1217 | past = Time();
|
|---|
| 1218 | }
|
|---|
| 1219 |
|
|---|
| 1220 | return fOutputEnabled ? kStateCurrentCtrlRunning : kStateCurrentCtrlIdle;
|
|---|
| 1221 | }
|
|---|
| 1222 | }
|
|---|
| 1223 |
|
|---|
| 1224 | if (bias && fad && !fsc)
|
|---|
| 1225 | return kStateConnectedFAD;
|
|---|
| 1226 |
|
|---|
| 1227 | if (bias && fsc && !fad)
|
|---|
| 1228 | return kStateConnectedFSC;
|
|---|
| 1229 |
|
|---|
| 1230 | return kStateConnected;
|
|---|
| 1231 | }
|
|---|
| 1232 |
|
|---|
| 1233 | public:
|
|---|
| 1234 | StateMachineFeedback(ostream &out=cout) : StateMachineDim(out, "FEEDBACK"),
|
|---|
| 1235 | fStatusDim(make_pair(Time(), -2)),
|
|---|
| 1236 | fStatusFAD(make_pair(Time(), -2)),
|
|---|
| 1237 | fStatusBias(make_pair(Time(), -2)),
|
|---|
| 1238 | fDim("DIS_DNS/VERSION_NUMBER", (void*)NULL, 0, this),
|
|---|
| 1239 | fFAD("FAD_CONTROL/STATE", (void*)NULL, 0, this),
|
|---|
| 1240 | fFSC("FSC_CONTROL/STATE", (void*)NULL, 0, this),
|
|---|
| 1241 | fBias("BIAS_CONTROL/STATE", (void*)NULL, 0, this),
|
|---|
| 1242 | fBiasA("BIAS_CONTROL/CURRENT", (void*)NULL, 0, this),
|
|---|
| 1243 | fBiasData("FAD_CONTROL/FEEDBACK_DATA", (void*)NULL, 0, this),
|
|---|
| 1244 | fBiasNom("BIAS_CONTROL/NOMINAL", (void*)NULL, 0, this),
|
|---|
| 1245 | fCameraTemp("FSC_CONTROL/TEMPERATURE", (void*)NULL, 0, this),
|
|---|
| 1246 | fDimReference("FEEDBACK/REFERENCE", "F:416",
|
|---|
| 1247 | "Amplitude reference value(s)"
|
|---|
| 1248 | "Vref[mV]:Amplitude reference"),
|
|---|
| 1249 | fDimDeviation("FEEDBACK/DEVIATION", "F:416;F:416;F:1;F:1",
|
|---|
| 1250 | "Control loop information"
|
|---|
| 1251 | "|DeltaAmpl[mV]:Amplitude offset measures"
|
|---|
| 1252 | "|DeltaBias[mV]:Correction value calculated"
|
|---|
| 1253 | "|DeltaTemp[mV]:Correction calculated from temperature"
|
|---|
| 1254 | "|DeltaUser[mV]:Additional offset specified by user"),
|
|---|
| 1255 | fDimCalibration("FEEDBACK/CALIBRATION", "F:416;F:416;F:416",
|
|---|
| 1256 | "Current offsets"
|
|---|
| 1257 | "|Avg[uA]:Average offset"
|
|---|
| 1258 | "|Rms[uA]:Rms of offset"
|
|---|
| 1259 | "|R[Ohm]:Measured calibration resistor"),
|
|---|
| 1260 | fSP(BIAS::kNumChannels),
|
|---|
| 1261 | fKp(0), fKi(0), fKd(0), fT(-1),
|
|---|
| 1262 | fCalibrationOffset(-3),
|
|---|
| 1263 | fCurrentRequestInterval(0),
|
|---|
| 1264 | fNumCalibIgnore(30),
|
|---|
| 1265 | fNumCalibRequests(300),
|
|---|
| 1266 | fOutputEnabled(false)
|
|---|
| 1267 | {
|
|---|
| 1268 | // ba::io_service::work is a kind of keep_alive for the loop.
|
|---|
| 1269 | // It prevents the io_service to go to stopped state, which
|
|---|
| 1270 | // would prevent any consecutive calls to run()
|
|---|
| 1271 | // or poll() to do nothing. reset() could also revoke to the
|
|---|
| 1272 | // previous state but this might introduce some overhead of
|
|---|
| 1273 | // deletion and creation of threads and more.
|
|---|
| 1274 |
|
|---|
| 1275 | // fSP.resize(416);
|
|---|
| 1276 |
|
|---|
| 1277 | // State names
|
|---|
| 1278 | AddStateName(kStateDimNetworkNA, "DimNetworkNotAvailable",
|
|---|
| 1279 | "The Dim DNS is not reachable.");
|
|---|
| 1280 |
|
|---|
| 1281 | AddStateName(kStateDisconnected, "Disconnected",
|
|---|
| 1282 | "The Dim DNS is reachable, but the required subsystems are not available.");
|
|---|
| 1283 |
|
|---|
| 1284 | AddStateName(kStateConnecting, "Connecting",
|
|---|
| 1285 | "Only biasctrl is available and connected with its hardware.");
|
|---|
| 1286 |
|
|---|
| 1287 | AddStateName(kStateConnectedFSC, "ConnectedFSC",
|
|---|
| 1288 | "biasctrl and fscctrl are available and connected with their hardware.");
|
|---|
| 1289 | AddStateName(kStateConnectedFAD, "ConnectedFAD",
|
|---|
| 1290 | "biasctrl and fadctrl are available and connected with their hardware.");
|
|---|
| 1291 | AddStateName(kStateConnected, "Connected",
|
|---|
| 1292 | "biasctrl, fadctrl and fscctrl are available and connected with their hardware.");
|
|---|
| 1293 |
|
|---|
| 1294 | AddStateName(kStateFeedbackCtrlIdle, "FeedbackIdle",
|
|---|
| 1295 | "Feedback control activated, but voltage output disabled.");
|
|---|
| 1296 | AddStateName(kStateTempCtrlIdle, "TempCtrlIdle",
|
|---|
| 1297 | "Temperature control activated, but voltage output disabled.");
|
|---|
| 1298 | AddStateName(kStateCurrentCtrlIdle, "CurrentCtrlIdle",
|
|---|
| 1299 | "Current control activated, but voltage output disabled.");
|
|---|
| 1300 |
|
|---|
| 1301 | AddStateName(kStateFeedbackCtrlRunning, "FeedbackControl",
|
|---|
| 1302 | "Feedback control activated and voltage output enabled.");
|
|---|
| 1303 | AddStateName(kStateTempCtrlRunning, "TempControl",
|
|---|
| 1304 | "Temperature control activated and voltage output enabled.");
|
|---|
| 1305 | AddStateName(kStateCurrentCtrlRunning, "CurrentControl",
|
|---|
| 1306 | "Current/Temp control activated and voltage output enabled.");
|
|---|
| 1307 | AddStateName(kStateCalibrating, "Calibrating",
|
|---|
| 1308 | "Calibrating current offsets.");
|
|---|
| 1309 |
|
|---|
| 1310 | AddEvent("START_FEEDBACK_CONTROL", "S:1", kStateConnectedFAD, kStateConnected)
|
|---|
| 1311 | (bind(&StateMachineFeedback::StartFeedback, this, placeholders::_1))
|
|---|
| 1312 | ("Start the feedback control loop"
|
|---|
| 1313 | "|Num[short]:Number of events 'medianed' to calculate the correction value");
|
|---|
| 1314 |
|
|---|
| 1315 | AddEvent("START_GLOBAL_FEEDBACK", "S:1", kStateConnectedFAD, kStateConnected)
|
|---|
| 1316 | (bind(&StateMachineFeedback::StartFeedbackGlobal, this, placeholders::_1))
|
|---|
| 1317 | ("Start the global feedback control loop"
|
|---|
| 1318 | "Num[short]:Number of events averaged to calculate the correction value");
|
|---|
| 1319 |
|
|---|
| 1320 | AddEvent("START_TEMP_CONTROL", "F:1", kStateConnectedFSC, kStateConnected)
|
|---|
| 1321 | (bind(&StateMachineFeedback::StartTempCtrl, this, placeholders::_1))
|
|---|
| 1322 | ("Start the temperature control loop"
|
|---|
| 1323 | "|offset[V]:Offset from the nominal temperature corrected value in Volts");
|
|---|
| 1324 |
|
|---|
| 1325 | AddEvent("START_CURRENT_CONTROL", "F:1", kStateConnectedFSC, kStateConnected)
|
|---|
| 1326 | (bind(&StateMachineFeedback::StartCurrentCtrl, this, placeholders::_1))
|
|---|
| 1327 | ("Start the current/temperature control loop"
|
|---|
| 1328 | "|offset[V]:Offset from the nominal current/temperature corrected value in Volts");
|
|---|
| 1329 |
|
|---|
| 1330 | // kStateTempCtrlIdle, kStateFeedbackCtrlIdle, kStateTempCtrlRunning, kStateFeedbackCtrlRunning
|
|---|
| 1331 | AddEvent("STOP")
|
|---|
| 1332 | (bind(&StateMachineFeedback::StopFeedback, this))
|
|---|
| 1333 | ("Stop any control loop");
|
|---|
| 1334 |
|
|---|
| 1335 | AddEvent("ENABLE_OUTPUT", "B:1")//, kStateIdle)
|
|---|
| 1336 | (bind(&StateMachineFeedback::EnableOutput, this, placeholders::_1))
|
|---|
| 1337 | ("Enable sending of correction values caluclated by the control loop to the biasctrl");
|
|---|
| 1338 |
|
|---|
| 1339 | AddEvent("STORE_REFERENCE")//, kStateIdle)
|
|---|
| 1340 | (bind(&StateMachineFeedback::StoreReference, this))
|
|---|
| 1341 | ("Store the last (averaged) value as new reference (for debug purpose only)");
|
|---|
| 1342 |
|
|---|
| 1343 | AddEvent("SET_REFERENCE", "F:1")//, kStateIdle)
|
|---|
| 1344 | (bind(&StateMachineFeedback::SetReference, this, placeholders::_1))
|
|---|
| 1345 | ("Set a new global reference value (for debug purpose only)");
|
|---|
| 1346 |
|
|---|
| 1347 | AddEvent("SET_Ki", "D:1")//, kStateIdle)
|
|---|
| 1348 | (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 0))
|
|---|
| 1349 | ("Set integral constant Ki");
|
|---|
| 1350 |
|
|---|
| 1351 | AddEvent("SET_Kp", "D:1")//, kStateIdle)
|
|---|
| 1352 | (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 1))
|
|---|
| 1353 | ("Set proportional constant Kp");
|
|---|
| 1354 |
|
|---|
| 1355 | AddEvent("SET_Kd", "D:1")//, kStateIdle)
|
|---|
| 1356 | (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 2))
|
|---|
| 1357 | ("Set derivative constant Kd");
|
|---|
| 1358 |
|
|---|
| 1359 | AddEvent("SET_T", "D:1")//, kStateIdle)
|
|---|
| 1360 | (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 3))
|
|---|
| 1361 | ("Set time-constant. (-1 to use the cycle time, i.e. the time for the last average cycle, instead)");
|
|---|
| 1362 |
|
|---|
| 1363 | AddEvent("CALIBRATE_CURRENTS", kStateConnectedFSC, kStateConnected)//, kStateIdle)
|
|---|
| 1364 | (bind(&StateMachineFeedback::CalibrateCurrents, this))
|
|---|
| 1365 | ("");
|
|---|
| 1366 |
|
|---|
| 1367 | AddEvent("SET_CURRENT_REQUEST_INTERVAL", kStateConnectedFSC, kStateConnected)//, kStateIdle)
|
|---|
| 1368 | (bind(&StateMachineFeedback::SetCurrentRequestInterval, this, placeholders::_1))
|
|---|
| 1369 | ("|interval[ms]:Interval between two current requests in modes which need that.");
|
|---|
| 1370 |
|
|---|
| 1371 | // Verbosity commands
|
|---|
| 1372 | // AddEvent("SET_VERBOSE", "B:1")
|
|---|
| 1373 | // (bind(&StateMachineMCP::SetVerbosity, this, placeholders::_1))
|
|---|
| 1374 | // ("set verbosity state"
|
|---|
| 1375 | // "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
|
|---|
| 1376 |
|
|---|
| 1377 | AddEvent("PRINT")
|
|---|
| 1378 | (bind(&StateMachineFeedback::Print, this))
|
|---|
| 1379 | ("");
|
|---|
| 1380 |
|
|---|
| 1381 | AddEvent("PRINT_CALIBRATION")
|
|---|
| 1382 | (bind(&StateMachineFeedback::PrintCalibration, this))
|
|---|
| 1383 | ("");
|
|---|
| 1384 | }
|
|---|
| 1385 |
|
|---|
| 1386 | int EvalOptions(Configuration &conf)
|
|---|
| 1387 | {
|
|---|
| 1388 | if (!fMap.Read(conf.Get<string>("pixel-map-file")))
|
|---|
| 1389 | {
|
|---|
| 1390 | Error("Reading mapping table from "+conf.Get<string>("pixel-map-file")+" failed.");
|
|---|
| 1391 | return 1;
|
|---|
| 1392 | }
|
|---|
| 1393 |
|
|---|
| 1394 | fGain = 0.1; // V(Amplitude) / V(Bias)
|
|---|
| 1395 |
|
|---|
| 1396 | // 148 -> 248
|
|---|
| 1397 |
|
|---|
| 1398 | // 33 : 10s < 2%
|
|---|
| 1399 | // 50 : 5s < 2%
|
|---|
| 1400 | // 66 : 3s < 2%
|
|---|
| 1401 | // 85 : 2s < 2%
|
|---|
| 1402 |
|
|---|
| 1403 | fKp = 0;
|
|---|
| 1404 | fKd = 0;
|
|---|
| 1405 | fKi = 0.75;
|
|---|
| 1406 | fT = 1;
|
|---|
| 1407 |
|
|---|
| 1408 | // Is that independent of the aboslute real amplitude of
|
|---|
| 1409 | // the light pulser?
|
|---|
| 1410 |
|
|---|
| 1411 | ostringstream msg;
|
|---|
| 1412 | msg << "Control loop parameters: ";
|
|---|
| 1413 | msg << "Kp=" << fKp << ", Kd=" << fKd << ", Ki=" << fKi << ", ";
|
|---|
| 1414 | if (fT>0)
|
|---|
| 1415 | msg << fT;
|
|---|
| 1416 | else
|
|---|
| 1417 | msg << "<auto>";
|
|---|
| 1418 | msg << ", Gain(DRS/BIAS)=" << fGain << "V/V";
|
|---|
| 1419 |
|
|---|
| 1420 | Message(msg);
|
|---|
| 1421 |
|
|---|
| 1422 | fCurrentRequestInterval = conf.Get<uint16_t>("current-request-interval");
|
|---|
| 1423 | fNumCalibIgnore = conf.Get<uint16_t>("num-calib-ignore");
|
|---|
| 1424 | fNumCalibRequests = conf.Get<uint16_t>("num-calib-average");
|
|---|
| 1425 | fCalibrationOffset = conf.Get<float>("calibration-offset");
|
|---|
| 1426 |
|
|---|
| 1427 | return -1;
|
|---|
| 1428 | }
|
|---|
| 1429 | };
|
|---|
| 1430 |
|
|---|
| 1431 | // ------------------------------------------------------------------------
|
|---|
| 1432 |
|
|---|
| 1433 | #include "Main.h"
|
|---|
| 1434 |
|
|---|
| 1435 | template<class T>
|
|---|
| 1436 | int RunShell(Configuration &conf)
|
|---|
| 1437 | {
|
|---|
| 1438 | return Main::execute<T, StateMachineFeedback>(conf);
|
|---|
| 1439 | }
|
|---|
| 1440 |
|
|---|
| 1441 | void SetupConfiguration(Configuration &conf)
|
|---|
| 1442 | {
|
|---|
| 1443 | po::options_description control("Feedback options");
|
|---|
| 1444 | control.add_options()
|
|---|
| 1445 | ("pixel-map-file", var<string>("FACTmapV5a.txt"), "Pixel mapping file. Used here to get the default reference voltage.")
|
|---|
| 1446 | ("current-request-interval", var<uint16_t>(1000), "Interval between two current requests.")
|
|---|
| 1447 | ("num-calib-ignore", var<uint16_t>(30), "Number of current requests to be ignored before averaging")
|
|---|
| 1448 | ("num-calib-average", var<uint16_t>(300), "Number of current requests to be averaged")
|
|---|
| 1449 | ("calibration-offset", var<float>(-3), "Absolute offset relative to the G-APD operation voltage when calibrating")
|
|---|
| 1450 | ;
|
|---|
| 1451 |
|
|---|
| 1452 | conf.AddOptions(control);
|
|---|
| 1453 | }
|
|---|
| 1454 |
|
|---|
| 1455 | /*
|
|---|
| 1456 | Extract usage clause(s) [if any] for SYNOPSIS.
|
|---|
| 1457 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
|---|
| 1458 | are used to match the usage synopsis in program output. An example from cp
|
|---|
| 1459 | (GNU coreutils) which contains both strings:
|
|---|
| 1460 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
|---|
| 1461 | or: cp [OPTION]... SOURCE... DIRECTORY
|
|---|
| 1462 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
|---|
| 1463 | */
|
|---|
| 1464 | void PrintUsage()
|
|---|
| 1465 | {
|
|---|
| 1466 | cout <<
|
|---|
| 1467 | "The feedback control the BIAS voltages based on the calibration signal.\n"
|
|---|
| 1468 | "\n"
|
|---|
| 1469 | "The default is that the program is started without user intercation. "
|
|---|
| 1470 | "All actions are supposed to arrive as DimCommands. Using the -c "
|
|---|
| 1471 | "option, a local shell can be initialized. With h or help a short "
|
|---|
| 1472 | "help message about the usuage can be brought to the screen.\n"
|
|---|
| 1473 | "\n"
|
|---|
| 1474 | "Usage: feedback [-c type] [OPTIONS]\n"
|
|---|
| 1475 | " or: feedback [OPTIONS]\n";
|
|---|
| 1476 | cout << endl;
|
|---|
| 1477 | }
|
|---|
| 1478 |
|
|---|
| 1479 | void PrintHelp()
|
|---|
| 1480 | {
|
|---|
| 1481 | Main::PrintHelp<StateMachineFeedback>();
|
|---|
| 1482 |
|
|---|
| 1483 | /* Additional help text which is printed after the configuration
|
|---|
| 1484 | options goes here */
|
|---|
| 1485 |
|
|---|
| 1486 | /*
|
|---|
| 1487 | cout << "bla bla bla" << endl << endl;
|
|---|
| 1488 | cout << endl;
|
|---|
| 1489 | cout << "Environment:" << endl;
|
|---|
| 1490 | cout << "environment" << endl;
|
|---|
| 1491 | cout << endl;
|
|---|
| 1492 | cout << "Examples:" << endl;
|
|---|
| 1493 | cout << "test exam" << endl;
|
|---|
| 1494 | cout << endl;
|
|---|
| 1495 | cout << "Files:" << endl;
|
|---|
| 1496 | cout << "files" << endl;
|
|---|
| 1497 | cout << endl;
|
|---|
| 1498 | */
|
|---|
| 1499 | }
|
|---|
| 1500 |
|
|---|
| 1501 | int main(int argc, const char* argv[])
|
|---|
| 1502 | {
|
|---|
| 1503 | Configuration conf(argv[0]);
|
|---|
| 1504 | conf.SetPrintUsage(PrintUsage);
|
|---|
| 1505 | Main::SetupConfiguration(conf);
|
|---|
| 1506 | SetupConfiguration(conf);
|
|---|
| 1507 |
|
|---|
| 1508 | if (!conf.DoParse(argc, argv, PrintHelp))
|
|---|
| 1509 | return -1;
|
|---|
| 1510 |
|
|---|
| 1511 | //try
|
|---|
| 1512 | {
|
|---|
| 1513 | // No console access at all
|
|---|
| 1514 | if (!conf.Has("console"))
|
|---|
| 1515 | {
|
|---|
| 1516 | // if (conf.Get<bool>("no-dim"))
|
|---|
| 1517 | // return RunShell<LocalStream, StateMachine, ConnectionFSC>(conf);
|
|---|
| 1518 | // else
|
|---|
| 1519 | return RunShell<LocalStream>(conf);
|
|---|
| 1520 | }
|
|---|
| 1521 | // Cosole access w/ and w/o Dim
|
|---|
| 1522 | /* if (conf.Get<bool>("no-dim"))
|
|---|
| 1523 | {
|
|---|
| 1524 | if (conf.Get<int>("console")==0)
|
|---|
| 1525 | return RunShell<LocalShell, StateMachine, ConnectionFSC>(conf);
|
|---|
| 1526 | else
|
|---|
| 1527 | return RunShell<LocalConsole, StateMachine, ConnectionFSC>(conf);
|
|---|
| 1528 | }
|
|---|
| 1529 | else
|
|---|
| 1530 | */ {
|
|---|
| 1531 | if (conf.Get<int>("console")==0)
|
|---|
| 1532 | return RunShell<LocalShell>(conf);
|
|---|
| 1533 | else
|
|---|
| 1534 | return RunShell<LocalConsole>(conf);
|
|---|
| 1535 | }
|
|---|
| 1536 | }
|
|---|
| 1537 | /*catch (std::exception& e)
|
|---|
| 1538 | {
|
|---|
| 1539 | cerr << "Exception: " << e.what() << endl;
|
|---|
| 1540 | return -1;
|
|---|
| 1541 | }*/
|
|---|
| 1542 |
|
|---|
| 1543 | return 0;
|
|---|
| 1544 | }
|
|---|