| 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 | kStateConnected,
|
|---|
| 54 | kStateTempCtrlIdle,
|
|---|
| 55 | kStateFeedbackCtrlIdle,
|
|---|
| 56 | kStateTempCtrlRunning,
|
|---|
| 57 | kStateFeedbackCtrlRunning,
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | PixelMap fMap;
|
|---|
| 61 |
|
|---|
| 62 | DimServiceInfoList fNetwork;
|
|---|
| 63 |
|
|---|
| 64 | pair<Time, int> fStatusDim;
|
|---|
| 65 | pair<Time, int> fStatusFAD;
|
|---|
| 66 | pair<Time, int> fStatusFSC;
|
|---|
| 67 | pair<Time, int> fStatusBias;
|
|---|
| 68 |
|
|---|
| 69 | DimStampedInfo fDim;
|
|---|
| 70 | DimStampedInfo fFAD;
|
|---|
| 71 | DimStampedInfo fFSC;
|
|---|
| 72 | DimStampedInfo fBias;
|
|---|
| 73 |
|
|---|
| 74 | DimStampedInfo *fBiasData;
|
|---|
| 75 | DimStampedInfo *fCameraTemp;
|
|---|
| 76 |
|
|---|
| 77 | DimDescribedService fDimReference;
|
|---|
| 78 | DimDescribedService fDimDeviation;
|
|---|
| 79 |
|
|---|
| 80 | vector<vector<float>> fData;
|
|---|
| 81 |
|
|---|
| 82 | uint64_t fCursor;
|
|---|
| 83 |
|
|---|
| 84 | Time fBiasLast;
|
|---|
| 85 | Time fStartTime;
|
|---|
| 86 |
|
|---|
| 87 | valarray<double> fPV[3]; // Process variable (intgerated/averaged amplitudes)
|
|---|
| 88 | valarray<double> fSP; // Set point (target amplitudes)
|
|---|
| 89 |
|
|---|
| 90 | double fKp; // Proportional constant
|
|---|
| 91 | double fKi; // Integral constant
|
|---|
| 92 | double fKd; // Derivative constant
|
|---|
| 93 | double fT; // Time constant (cycle time)
|
|---|
| 94 | double fGain; // Gain (conversion from a DRS voltage deviation into a BIAS voltage change at G-APD reference voltage)
|
|---|
| 95 |
|
|---|
| 96 | double fT21;
|
|---|
| 97 |
|
|---|
| 98 | bool fOutputEnabled;
|
|---|
| 99 |
|
|---|
| 100 | pair<Time, int> GetNewState(DimStampedInfo &info) const
|
|---|
| 101 | {
|
|---|
| 102 | const bool disconnected = info.getSize()==0;
|
|---|
| 103 |
|
|---|
| 104 | // Make sure getTimestamp is called _before_ getTimestampMillisecs
|
|---|
| 105 | const int tsec = info.getTimestamp();
|
|---|
| 106 | const int tms = info.getTimestampMillisecs();
|
|---|
| 107 |
|
|---|
| 108 | return make_pair(Time(tsec, tms*1000),
|
|---|
| 109 | disconnected ? -2 : info.getQuality());
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | void ResetData()
|
|---|
| 113 | {
|
|---|
| 114 | fData.clear();
|
|---|
| 115 | fData.resize(500);
|
|---|
| 116 | fCursor = 0;
|
|---|
| 117 | fStartTime = Time();
|
|---|
| 118 |
|
|---|
| 119 | fSP.resize(416);
|
|---|
| 120 | fSP = valarray<double>(0., 416);
|
|---|
| 121 |
|
|---|
| 122 | vector<float> vec(2*BIAS::kNumChannels);
|
|---|
| 123 | fDimDeviation.Update(vec);
|
|---|
| 124 |
|
|---|
| 125 | fPV[0].resize(0);
|
|---|
| 126 | fPV[1].resize(0);
|
|---|
| 127 | fPV[2].resize(0);
|
|---|
| 128 |
|
|---|
| 129 | if (fKp==0 && fKi==0 && fKd==0)
|
|---|
| 130 | Warn("Control loop parameters are all set to zero.");
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | void infoHandler()
|
|---|
| 134 | {
|
|---|
| 135 | DimInfo *curr = getInfo(); // get current DimInfo address
|
|---|
| 136 | if (!curr)
|
|---|
| 137 | return;
|
|---|
| 138 |
|
|---|
| 139 | if (curr==&fBias)
|
|---|
| 140 | {
|
|---|
| 141 | fStatusBias = GetNewState(fBias);
|
|---|
| 142 | return;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | if (curr==&fFAD)
|
|---|
| 146 | {
|
|---|
| 147 | fStatusFAD = GetNewState(fFAD);
|
|---|
| 148 | return;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | if (curr==&fFSC)
|
|---|
| 152 | {
|
|---|
| 153 | fStatusFSC = GetNewState(fFSC);
|
|---|
| 154 | return;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | if (curr==&fDim)
|
|---|
| 158 | {
|
|---|
| 159 | fStatusDim = GetNewState(fDim);
|
|---|
| 160 | fStatusDim.second = curr->getSize()==4 ? curr->getInt() : 0;
|
|---|
| 161 | return;
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | if (curr==fCameraTemp)
|
|---|
| 165 | {
|
|---|
| 166 | if (curr->getSize()==0)
|
|---|
| 167 | return;
|
|---|
| 168 | if (curr->getSize()!=60*sizeof(float))
|
|---|
| 169 | return;
|
|---|
| 170 |
|
|---|
| 171 | const float *ptr = (float*)curr->getData();
|
|---|
| 172 |
|
|---|
| 173 | double avg = 0;
|
|---|
| 174 | int num = 0;
|
|---|
| 175 | for (int i=1; i<32; i++)
|
|---|
| 176 | if (ptr[i]!=0)
|
|---|
| 177 | {
|
|---|
| 178 | avg += ptr[i];
|
|---|
| 179 | num++;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | if (num==0)
|
|---|
| 183 | return;
|
|---|
| 184 |
|
|---|
| 185 | const double diff = (25-avg)*0.057;
|
|---|
| 186 |
|
|---|
| 187 | vector<float> vec(2*BIAS::kNumChannels);
|
|---|
| 188 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 189 | vec[i+416] = diff;
|
|---|
| 190 |
|
|---|
| 191 | fDimDeviation.Update(vec);
|
|---|
| 192 |
|
|---|
| 193 | if (fOutputEnabled)
|
|---|
| 194 | {
|
|---|
| 195 | Info("Sending correction to feedback.");
|
|---|
| 196 |
|
|---|
| 197 | DimClient::sendCommandNB((char*)"BIAS_CONTROL/SET_GAPD_REFERENCE_OFFSET",
|
|---|
| 198 | (void*)&diff, sizeof(float));
|
|---|
| 199 | }
|
|---|
| 200 | return;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | if (curr==fBiasData)
|
|---|
| 204 | {
|
|---|
| 205 | if (curr->getSize()==0)
|
|---|
| 206 | return;
|
|---|
| 207 | if (curr->getSize()!=1440*sizeof(float))
|
|---|
| 208 | return;
|
|---|
| 209 |
|
|---|
| 210 | // -------- Check age of last stored event --------
|
|---|
| 211 |
|
|---|
| 212 | // Must be called in this order
|
|---|
| 213 | const int tsec = curr->getTimestamp();
|
|---|
| 214 | const int tms = curr->getTimestampMillisecs();
|
|---|
| 215 |
|
|---|
| 216 | const Time tm(tsec, tms*1000);
|
|---|
| 217 |
|
|---|
| 218 | if (Time()-fBiasLast>boost::posix_time::seconds(30))
|
|---|
| 219 | {
|
|---|
| 220 | Warn("Last received event data older than 30s... resetting average calculation.");
|
|---|
| 221 | ResetData();
|
|---|
| 222 | }
|
|---|
| 223 | fBiasLast = tm;
|
|---|
| 224 |
|
|---|
| 225 | // -------- Store new event --------
|
|---|
| 226 |
|
|---|
| 227 | fData[fCursor%fData.size()].assign(reinterpret_cast<float*>(curr->getData()),
|
|---|
| 228 | reinterpret_cast<float*>(curr->getData())+1440);
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 | fCursor++;
|
|---|
| 232 |
|
|---|
| 233 | if (fCursor<fData.size())
|
|---|
| 234 | return;
|
|---|
| 235 |
|
|---|
| 236 | // -------- Calculate statistics --------
|
|---|
| 237 |
|
|---|
| 238 | valarray<double> med(1440);
|
|---|
| 239 |
|
|---|
| 240 | for (int ch=0; ch<1440; ch++)
|
|---|
| 241 | {
|
|---|
| 242 | vector<float> arr(fData.size());
|
|---|
| 243 | for (size_t i=0; i<fData.size(); i++)
|
|---|
| 244 | arr[i] = fData[i][ch];
|
|---|
| 245 |
|
|---|
| 246 | sort(arr.begin(), arr.end());
|
|---|
| 247 |
|
|---|
| 248 | med[ch] = arr[arr.size()/2];
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /*
|
|---|
| 252 | vector<float> med(1440);
|
|---|
| 253 | vector<float> rms(1440);
|
|---|
| 254 | for (size_t i=0; i<fData.size(); i++)
|
|---|
| 255 | {
|
|---|
| 256 | if (fData[i].size()==0)
|
|---|
| 257 | return;
|
|---|
| 258 |
|
|---|
| 259 | for (int j=0; j<1440; j++)
|
|---|
| 260 | {
|
|---|
| 261 | med[j] += fData[i][j];
|
|---|
| 262 | rms[j] += fData[i][j]*fData[i][j];
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 | */
|
|---|
| 266 |
|
|---|
| 267 | vector<double> avg(BIAS::kNumChannels);
|
|---|
| 268 | vector<int> num(BIAS::kNumChannels);
|
|---|
| 269 | for (int i=0; i<1440; i++)
|
|---|
| 270 | {
|
|---|
| 271 | const PixelMapEntry &ch = fMap.hw(i);
|
|---|
| 272 |
|
|---|
| 273 | // FIXME: Add a consistency check if the median makes sense...
|
|---|
| 274 | // FIXME: Add a consistency check to remove pixels with bright stars (median?)
|
|---|
| 275 |
|
|---|
| 276 | avg[ch.hv()] += med[i];
|
|---|
| 277 | num[ch.hv()]++;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 281 | {
|
|---|
| 282 | if (num[i])
|
|---|
| 283 | avg[i] /= num[i];
|
|---|
| 284 |
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | // -------- Calculate correction --------
|
|---|
| 288 |
|
|---|
| 289 | // http://bestune.50megs.com/typeABC.htm
|
|---|
| 290 |
|
|---|
| 291 | // CO: Controller output
|
|---|
| 292 | // PV: Process variable
|
|---|
| 293 | // SP: Set point
|
|---|
| 294 | // T: Sampling period (loop update period)
|
|---|
| 295 | // e = SP - PV
|
|---|
| 296 | //
|
|---|
| 297 | // Kp : No units
|
|---|
| 298 | // Ki : per seconds
|
|---|
| 299 | // Kd : seconds
|
|---|
| 300 |
|
|---|
| 301 | // 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) ]
|
|---|
| 302 |
|
|---|
| 303 | if (fCursor%fData.size()==0)
|
|---|
| 304 | {
|
|---|
| 305 | // FIXME: Take out broken / dead boards.
|
|---|
| 306 |
|
|---|
| 307 | const Time tm0 = Time();
|
|---|
| 308 |
|
|---|
| 309 | const double T21 = fT>0 ? fT : (tm0-fStartTime).total_microseconds()/1000000.;
|
|---|
| 310 | const double T10 = fT21;
|
|---|
| 311 | fT21 = T21;
|
|---|
| 312 |
|
|---|
| 313 | fStartTime = tm0;
|
|---|
| 314 |
|
|---|
| 315 | ostringstream out;
|
|---|
| 316 | out << "New " << fData.size() << " event received: " << fCursor << " / " << setprecision(3) << T21 << "s";
|
|---|
| 317 | Info(out);
|
|---|
| 318 |
|
|---|
| 319 | if (fPV[0].size()==0)
|
|---|
| 320 | {
|
|---|
| 321 | fPV[0].resize(avg.size());
|
|---|
| 322 | fPV[0] = valarray<double>(avg.data(), avg.size());
|
|---|
| 323 | }
|
|---|
| 324 | else
|
|---|
| 325 | if (fPV[1].size()==0)
|
|---|
| 326 | {
|
|---|
| 327 | fPV[1].resize(avg.size());
|
|---|
| 328 | fPV[1] = valarray<double>(avg.data(), avg.size());
|
|---|
| 329 | }
|
|---|
| 330 | else
|
|---|
| 331 | if (fPV[2].size()==0)
|
|---|
| 332 | {
|
|---|
| 333 | fPV[2].resize(avg.size());
|
|---|
| 334 | fPV[2] = valarray<double>(avg.data(), avg.size());
|
|---|
| 335 | }
|
|---|
| 336 | else
|
|---|
| 337 | {
|
|---|
| 338 | fPV[0] = fPV[1];
|
|---|
| 339 | fPV[1] = fPV[2];
|
|---|
| 340 |
|
|---|
| 341 | fPV[2].resize(avg.size());
|
|---|
| 342 | fPV[2] = valarray<double>(avg.data(), avg.size());
|
|---|
| 343 |
|
|---|
| 344 | if (T10<=0 || T21<=0)
|
|---|
| 345 | return;
|
|---|
| 346 |
|
|---|
| 347 | cout << "Calculating (" << fCursor << ":" << T21 << ")... " << endl;
|
|---|
| 348 |
|
|---|
| 349 | // fKi[j] = response[j]*gain;
|
|---|
| 350 | // Kp = 0;
|
|---|
| 351 | // Kd = 0;
|
|---|
| 352 |
|
|---|
| 353 | // -110 / -110 (-23 DAC / -0.51V)
|
|---|
| 354 | // Reference voltage: -238 / -203
|
|---|
| 355 | // -360 / -343 ( 23 DAC / 0.51V)
|
|---|
| 356 |
|
|---|
| 357 | // 0.005 A/V
|
|---|
| 358 | // 220 Amplitude / 1V
|
|---|
| 359 |
|
|---|
| 360 | // Gain = 1V / 200 = 0.005
|
|---|
| 361 |
|
|---|
| 362 | // => Kp = 0.01 * gain = 0.00005
|
|---|
| 363 | // => Ki = 0.8 * gain/20s = 0.00025
|
|---|
| 364 | // => Kd = 0.1 * gain/20s = 0.00003
|
|---|
| 365 |
|
|---|
| 366 | //valarray<double> correction = - Kp*(PV[2] - PV[1]) + Ki * dT * (SP-PV[2]) - Kd/dT * (PV[2] - 2*PV[1] + PV[0]);
|
|---|
| 367 | //valarray<double> correction =
|
|---|
| 368 | // - Kp*(PV[2] - PV[1]) + Ki * T21 * (SP-PV[2]) - Kd*(PV[2]-PV[1])/T21 - Kd*(PV[0]-PV[1])/T01;
|
|---|
| 369 | const valarray<double> correction = fGain/1000*
|
|---|
| 370 | (
|
|---|
| 371 | - (fKp+fKd/T21)*(fPV[2] - fPV[1])
|
|---|
| 372 | + fKi*T21*(fSP-fPV[2])
|
|---|
| 373 | + fKd/T10*(fPV[1]-fPV[0])
|
|---|
| 374 | );
|
|---|
| 375 |
|
|---|
| 376 | vector<float> vec(2*BIAS::kNumChannels);
|
|---|
| 377 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 378 | vec[i] = fPV[2][i]-fSP[i];
|
|---|
| 379 |
|
|---|
| 380 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 381 | vec[i+416] = correction[i];
|
|---|
| 382 |
|
|---|
| 383 | fDimDeviation.Update(vec);
|
|---|
| 384 |
|
|---|
| 385 | if (fOutputEnabled)
|
|---|
| 386 | {
|
|---|
| 387 | Info("Sending correction to feedback.");
|
|---|
| 388 |
|
|---|
| 389 | DimClient::sendCommandNB((char*)"BIAS_CONTROL/ADD_REFERENCE_VOLTAGES",
|
|---|
| 390 | (void*)(vec.data()+416), 416*sizeof(float));
|
|---|
| 391 |
|
|---|
| 392 | /*
|
|---|
| 393 | if (!Dim::SendCommand("BIAS_CONTROL/ADD_REFERENCE_VOLTAGES",
|
|---|
| 394 | (const void*)(vec.data()+416), 416*sizeof(float)))
|
|---|
| 395 | {
|
|---|
| 396 | Error("Sending correction to bias control failed... switching off.");
|
|---|
| 397 | fOutputEnabled=false;
|
|---|
| 398 | }
|
|---|
| 399 | else
|
|---|
| 400 | Info("Success!");
|
|---|
| 401 | */
|
|---|
| 402 | }
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | /*
|
|---|
| 408 | vector<float> correction(416);
|
|---|
| 409 | vector<float> response(416);
|
|---|
| 410 | vector<float> dev(416);
|
|---|
| 411 |
|
|---|
| 412 | double gain = 0;
|
|---|
| 413 |
|
|---|
| 414 | for (int j=0; j<416; j++)
|
|---|
| 415 | {
|
|---|
| 416 | //avg[j] /= fData.size();
|
|---|
| 417 | //rms[j] /= sqrt((rms[j]*fData.size() - avg[j]*avg[j]))/fData.size();
|
|---|
| 418 |
|
|---|
| 419 | dev[j] = avg[j] - target[j];
|
|---|
| 420 |
|
|---|
| 421 | // Determine correction from response maxtrix and change voltages
|
|---|
| 422 | correction[j] = -dev[j]*response[j]*gain;
|
|---|
| 423 |
|
|---|
| 424 | if (correction[j]==0)
|
|---|
| 425 | continue;
|
|---|
| 426 |
|
|---|
| 427 | // Limit voltage steps // Limit changes to 100 mV
|
|---|
| 428 | // if (fabs(correction[j]) > 0.1)
|
|---|
| 429 | // correction[j] = 0.1*fabs(correction[j])/correction[j];
|
|---|
| 430 |
|
|---|
| 431 | // Add voltage change command if not too noisy
|
|---|
| 432 | // if (fabs(Average[i]) > 2*Sigma[i])
|
|---|
| 433 | // Cmd << fIDTable[i] << " " << std::showpos << Correction/Multiplicity[i] << " ";
|
|---|
| 434 |
|
|---|
| 435 | }
|
|---|
| 436 | */
|
|---|
| 437 | return;
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | bool CheckEventSize(size_t has, const char *name, size_t size)
|
|---|
| 443 | {
|
|---|
| 444 | if (has==size)
|
|---|
| 445 | return true;
|
|---|
| 446 |
|
|---|
| 447 | ostringstream msg;
|
|---|
| 448 | msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
|
|---|
| 449 | Fatal(msg);
|
|---|
| 450 | return false;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | void PrintState(const pair<Time,int> &state, const char *server)
|
|---|
| 454 | {
|
|---|
| 455 | const State rc = fNetwork.GetState(server, state.second);
|
|---|
| 456 |
|
|---|
| 457 | Out() << state.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
|
|---|
| 458 | Out() << kBold << server << ": ";
|
|---|
| 459 | Out() << rc.name << "[" << rc.index << "]";
|
|---|
| 460 | Out() << kReset << " - " << kBlue << rc.comment << endl;
|
|---|
| 461 | }
|
|---|
| 462 |
|
|---|
| 463 | int Print()
|
|---|
| 464 | {
|
|---|
| 465 | Out() << fStatusDim.first.GetAsStr("%H:%M:%S.%f").substr(0, 12) << " - ";
|
|---|
| 466 | Out() << kBold << "DIM_DNS: ";
|
|---|
| 467 | if (fStatusDim.second==0)
|
|---|
| 468 | Out() << "Offline" << endl;
|
|---|
| 469 | else
|
|---|
| 470 | Out() << "V" << fStatusDim.second/100 << 'r' << fStatusDim.second%100 << endl;
|
|---|
| 471 |
|
|---|
| 472 | PrintState(fStatusFAD, "FAD_CONTROL");
|
|---|
| 473 | PrintState(fStatusBias, "BIAS_CONTROL");
|
|---|
| 474 |
|
|---|
| 475 | return GetCurrentState();
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | int SetConstant(const EventImp &evt, int constant)
|
|---|
| 479 | {
|
|---|
| 480 | if (!CheckEventSize(evt.GetSize(), "SetConstant", 8))
|
|---|
| 481 | return kSM_FatalError;
|
|---|
| 482 |
|
|---|
| 483 | switch (constant)
|
|---|
| 484 | {
|
|---|
| 485 | case 0: fKi = evt.GetDouble(); break;
|
|---|
| 486 | case 1: fKp = evt.GetDouble(); break;
|
|---|
| 487 | case 2: fKd = evt.GetDouble(); break;
|
|---|
| 488 | case 3: fT = evt.GetDouble(); break;
|
|---|
| 489 | case 4: fGain = evt.GetDouble(); break;
|
|---|
| 490 | default:
|
|---|
| 491 | Fatal("SetConstant got an unexpected constant id -- this is a program bug!");
|
|---|
| 492 | return kSM_FatalError;
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | return GetCurrentState();
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | int EnableOutput(const EventImp &evt)
|
|---|
| 499 | {
|
|---|
| 500 | if (!CheckEventSize(evt.GetSize(), "EnableOutput", 1))
|
|---|
| 501 | return kSM_FatalError;
|
|---|
| 502 |
|
|---|
| 503 | fOutputEnabled = evt.GetBool();
|
|---|
| 504 |
|
|---|
| 505 | return GetCurrentState();
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | int StartFeedback()
|
|---|
| 509 | {
|
|---|
| 510 | fBiasData = new DimStampedInfo("FAD_CONTROL/FEEDBACK_DATA", (void*)NULL, 0, this);
|
|---|
| 511 |
|
|---|
| 512 | ResetData();
|
|---|
| 513 |
|
|---|
| 514 | return GetCurrentState();
|
|---|
| 515 | }
|
|---|
| 516 |
|
|---|
| 517 | int StartTempCtrl()
|
|---|
| 518 | {
|
|---|
| 519 | fCameraTemp = new DimStampedInfo("FSC_CONTROL/TEMPERATURE", (void*)NULL, 0, this);
|
|---|
| 520 |
|
|---|
| 521 | return GetCurrentState();
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | int StopFeedback()
|
|---|
| 525 | {
|
|---|
| 526 | if (fBiasData)
|
|---|
| 527 | {
|
|---|
| 528 | delete fBiasData;
|
|---|
| 529 | fBiasData = 0;
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | if (fCameraTemp)
|
|---|
| 533 | {
|
|---|
| 534 | delete fCameraTemp;
|
|---|
| 535 | fCameraTemp = 0;
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | return GetCurrentState();
|
|---|
| 539 | }
|
|---|
| 540 |
|
|---|
| 541 | int StoreReference()
|
|---|
| 542 | {
|
|---|
| 543 | if (!fPV[0].size() && !fPV[1].size() && !fPV[2].size())
|
|---|
| 544 | {
|
|---|
| 545 | Warn("No values in memory. Take enough events first!");
|
|---|
| 546 | return GetCurrentState();
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | // FIXME: Check age
|
|---|
| 550 |
|
|---|
| 551 | if (!fPV[1].size() && !fPV[2].size())
|
|---|
| 552 | fSP = fPV[0];
|
|---|
| 553 |
|
|---|
| 554 | if (!fPV[2].size())
|
|---|
| 555 | fSP = fPV[1];
|
|---|
| 556 | else
|
|---|
| 557 | fSP = fPV[2];
|
|---|
| 558 |
|
|---|
| 559 | vector<float> vec(BIAS::kNumChannels);
|
|---|
| 560 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 561 | vec[i] = fSP[i];
|
|---|
| 562 | fDimReference.Update(vec);
|
|---|
| 563 |
|
|---|
| 564 | return GetCurrentState();
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | int SetReference(const EventImp &evt)
|
|---|
| 568 | {
|
|---|
| 569 | if (!CheckEventSize(evt.GetSize(), "SetReference", 4))
|
|---|
| 570 | return kSM_FatalError;
|
|---|
| 571 |
|
|---|
| 572 | const float val = evt.GetFloat();
|
|---|
| 573 | if (!fPV[0].size() && !fPV[1].size() && !fPV[2].size())
|
|---|
| 574 | {
|
|---|
| 575 | Warn("No values in memory. Take enough events first!");
|
|---|
| 576 | return GetCurrentState();
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | vector<float> vec(BIAS::kNumChannels);
|
|---|
| 580 | for (int i=0; i<BIAS::kNumChannels; i++)
|
|---|
| 581 | vec[i] = fSP[i] = val;
|
|---|
| 582 | fDimReference.Update(vec);
|
|---|
| 583 |
|
|---|
| 584 | return GetCurrentState();
|
|---|
| 585 | }
|
|---|
| 586 |
|
|---|
| 587 |
|
|---|
| 588 | int Execute()
|
|---|
| 589 | {
|
|---|
| 590 | // Dispatch (execute) at most one handler from the queue. In contrary
|
|---|
| 591 | // to run_one(), it doesn't wait until a handler is available
|
|---|
| 592 | // which can be dispatched, so poll_one() might return with 0
|
|---|
| 593 | // handlers dispatched. The handlers are always dispatched/executed
|
|---|
| 594 | // synchronously, i.e. within the call to poll_one()
|
|---|
| 595 | //poll_one();
|
|---|
| 596 |
|
|---|
| 597 | if (fStatusDim.second==0)
|
|---|
| 598 | return kStateDimNetworkNA;
|
|---|
| 599 |
|
|---|
| 600 | // All subsystems are not connected
|
|---|
| 601 | if (fStatusFAD.second<FAD::kConnected &&
|
|---|
| 602 | fStatusBias.second<BIAS::kConnecting &&
|
|---|
| 603 | fStatusFSC.second<2)
|
|---|
| 604 | return kStateDisconnected;
|
|---|
| 605 |
|
|---|
| 606 | // At least one subsystem is not connected
|
|---|
| 607 | if (fStatusFAD.second<FAD::kConnected ||
|
|---|
| 608 | fStatusBias.second<BIAS::kConnected ||
|
|---|
| 609 | fStatusFSC.second<2)
|
|---|
| 610 | return kStateConnecting;
|
|---|
| 611 |
|
|---|
| 612 | // All subsystems are connected
|
|---|
| 613 |
|
|---|
| 614 | if (fBiasData)
|
|---|
| 615 | return fOutputEnabled ? kStateFeedbackCtrlRunning : kStateFeedbackCtrlIdle;
|
|---|
| 616 |
|
|---|
| 617 | if (fCameraTemp)
|
|---|
| 618 | return fOutputEnabled ? kStateTempCtrlRunning : kStateTempCtrlIdle;
|
|---|
| 619 |
|
|---|
| 620 | return kStateConnected;
|
|---|
| 621 | }
|
|---|
| 622 |
|
|---|
| 623 | public:
|
|---|
| 624 | StateMachineFeedback(ostream &out=cout) : StateMachineDim(out, "FEEDBACK"),
|
|---|
| 625 | fStatusDim(make_pair(Time(), -2)),
|
|---|
| 626 | fStatusFAD(make_pair(Time(), -2)),
|
|---|
| 627 | fStatusBias(make_pair(Time(), -2)),
|
|---|
| 628 | fDim("DIS_DNS/VERSION_NUMBER", (void*)NULL, 0, this),
|
|---|
| 629 | fFAD("FAD_CONTROL/STATE", (void*)NULL, 0, this),
|
|---|
| 630 | fFSC("FSC_CONTROL/STATE", (void*)NULL, 0, this),
|
|---|
| 631 | fBias("BIAS_CONTROL/STATE", (void*)NULL, 0, this),
|
|---|
| 632 | fBiasData(0), fCameraTemp(0),
|
|---|
| 633 | fDimReference("FEEDBACK/REFERENCE", "F:416", ""),
|
|---|
| 634 | fDimDeviation("FEEDBACK/DEVIATION", "F:416;F:416", ""),
|
|---|
| 635 | fKp(0), fKi(0), fKd(0), fT(-1), fOutputEnabled(false)
|
|---|
| 636 | {
|
|---|
| 637 | // ba::io_service::work is a kind of keep_alive for the loop.
|
|---|
| 638 | // It prevents the io_service to go to stopped state, which
|
|---|
| 639 | // would prevent any consecutive calls to run()
|
|---|
| 640 | // or poll() to do nothing. reset() could also revoke to the
|
|---|
| 641 | // previous state but this might introduce some overhead of
|
|---|
| 642 | // deletion and creation of threads and more.
|
|---|
| 643 |
|
|---|
| 644 | // State names
|
|---|
| 645 | AddStateName(kStateDimNetworkNA, "DimNetworkNotAvailable",
|
|---|
| 646 | "The Dim DNS is not reachable.");
|
|---|
| 647 |
|
|---|
| 648 | AddStateName(kStateDisconnected, "Disconnected",
|
|---|
| 649 | "The Dim DNS is reachable, but the required subsystems are not available.");
|
|---|
| 650 |
|
|---|
| 651 | AddStateName(kStateConnecting, "Connecting",
|
|---|
| 652 | "Not all subsystems (FAD, FSC, Bias) are connected to their hardware.");
|
|---|
| 653 |
|
|---|
| 654 | AddStateName(kStateConnected, "Connected",
|
|---|
| 655 | "All needed subsystems are connected to their hardware, no action is performed.");
|
|---|
| 656 |
|
|---|
| 657 | AddStateName(kStateFeedbackCtrlIdle, "FeedbackIdle",
|
|---|
| 658 | "Feedback control activated, but voltage output disabled.");
|
|---|
| 659 |
|
|---|
| 660 | AddStateName(kStateTempCtrlIdle, "FeedbackIdle",
|
|---|
| 661 | "Temperature control activated, but voltage output disabled.");
|
|---|
| 662 |
|
|---|
| 663 | AddStateName(kStateFeedbackCtrlRunning, "FeedbackControl",
|
|---|
| 664 | "Feedback control activated and voltage output enabled.");
|
|---|
| 665 |
|
|---|
| 666 | AddStateName(kStateTempCtrlRunning, "TempControl",
|
|---|
| 667 | "Temperature control activated and voltage output enabled.");
|
|---|
| 668 |
|
|---|
| 669 | AddEvent("START_FEEDBACK_CONTROL", kStateConnected)
|
|---|
| 670 | (bind(&StateMachineFeedback::StartFeedback, this))
|
|---|
| 671 | ("Start the feedback control loop");
|
|---|
| 672 |
|
|---|
| 673 | AddEvent("START_TEMP_CONTROL", kStateConnected)
|
|---|
| 674 | (bind(&StateMachineFeedback::StartTempCtrl, this))
|
|---|
| 675 | ("Start the temperature control loop");
|
|---|
| 676 |
|
|---|
| 677 | AddEvent("STOP", kStateTempCtrlIdle, kStateFeedbackCtrlIdle, kStateTempCtrlRunning, kStateFeedbackCtrlRunning)
|
|---|
| 678 | (bind(&StateMachineFeedback::StopFeedback, this))
|
|---|
| 679 | ("Stop any control loop");
|
|---|
| 680 |
|
|---|
| 681 | AddEvent("ENABLE_OUTPUT", "B:1")//, kStateIdle)
|
|---|
| 682 | (bind(&StateMachineFeedback::EnableOutput, this, placeholders::_1))
|
|---|
| 683 | ("Enable sending of correction values caluclated by the control loop to the biasctrl");
|
|---|
| 684 |
|
|---|
| 685 | AddEvent("STORE_REFERENCE")//, kStateIdle)
|
|---|
| 686 | (bind(&StateMachineFeedback::StoreReference, this))
|
|---|
| 687 | ("Store the last (averaged) value as new reference (for debug purpose only)");
|
|---|
| 688 |
|
|---|
| 689 | AddEvent("SET_REFERENCE", "F:1")//, kStateIdle)
|
|---|
| 690 | (bind(&StateMachineFeedback::SetReference, this, placeholders::_1))
|
|---|
| 691 | ("Set a new global reference value (for debug purpose only)");
|
|---|
| 692 |
|
|---|
| 693 | AddEvent("SET_Ki", "D:1")//, kStateIdle)
|
|---|
| 694 | (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 0))
|
|---|
| 695 | ("Set integral constant Ki");
|
|---|
| 696 |
|
|---|
| 697 | AddEvent("SET_Kp", "D:1")//, kStateIdle)
|
|---|
| 698 | (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 1))
|
|---|
| 699 | ("Set proportional constant Kp");
|
|---|
| 700 |
|
|---|
| 701 | AddEvent("SET_Kd", "D:1")//, kStateIdle)
|
|---|
| 702 | (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 2))
|
|---|
| 703 | ("Set derivative constant Kd");
|
|---|
| 704 |
|
|---|
| 705 | AddEvent("SET_T", "D:1")//, kStateIdle)
|
|---|
| 706 | (bind(&StateMachineFeedback::SetConstant, this, placeholders::_1, 3))
|
|---|
| 707 | ("Set time-constant. (-1 to use the cycle time, i.e. the time for the last average cycle, instead)");
|
|---|
| 708 |
|
|---|
| 709 | // Verbosity commands
|
|---|
| 710 | // AddEvent("SET_VERBOSE", "B:1")
|
|---|
| 711 | // (bind(&StateMachineMCP::SetVerbosity, this, placeholders::_1))
|
|---|
| 712 | // ("set verbosity state"
|
|---|
| 713 | // "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
|
|---|
| 714 |
|
|---|
| 715 | AddEvent("PRINT")
|
|---|
| 716 | (bind(&StateMachineFeedback::Print, this))
|
|---|
| 717 | ("");
|
|---|
| 718 | }
|
|---|
| 719 |
|
|---|
| 720 | int EvalOptions(Configuration &conf)
|
|---|
| 721 | {
|
|---|
| 722 | if (!fMap.Read(conf.Get<string>("pixel-map-file")))
|
|---|
| 723 | {
|
|---|
| 724 | Error("Reading mapping table from "+conf.Get<string>("pixel-map-file")+" failed.");
|
|---|
| 725 | return 1;
|
|---|
| 726 | }
|
|---|
| 727 |
|
|---|
| 728 | fGain = 5; // (BIAS)V / (DRS)V ( 1V / 0.22V )
|
|---|
| 729 |
|
|---|
| 730 | fKp = 0;
|
|---|
| 731 | fKd = 0;
|
|---|
| 732 | fKi = 0.12;
|
|---|
| 733 | fT = 1;
|
|---|
| 734 |
|
|---|
| 735 | ostringstream msg;
|
|---|
| 736 | msg << "Control loop parameters: ";
|
|---|
| 737 | msg << "Kp=" << fKp << ", Kd=" << fKd << ", Ki=" << fKi << ", ";
|
|---|
| 738 | if (fT>0)
|
|---|
| 739 | msg << fT;
|
|---|
| 740 | else
|
|---|
| 741 | msg << "<auto>";
|
|---|
| 742 | msg << ", Gain(BIAS/DRS)=" << fGain << "V/V";
|
|---|
| 743 |
|
|---|
| 744 | Message(msg);
|
|---|
| 745 |
|
|---|
| 746 | return -1;
|
|---|
| 747 | }
|
|---|
| 748 | };
|
|---|
| 749 |
|
|---|
| 750 | // ------------------------------------------------------------------------
|
|---|
| 751 |
|
|---|
| 752 | #include "Main.h"
|
|---|
| 753 |
|
|---|
| 754 | template<class T>
|
|---|
| 755 | int RunShell(Configuration &conf)
|
|---|
| 756 | {
|
|---|
| 757 | return Main::execute<T, StateMachineFeedback>(conf);
|
|---|
| 758 | }
|
|---|
| 759 |
|
|---|
| 760 | void SetupConfiguration(Configuration &conf)
|
|---|
| 761 | {
|
|---|
| 762 | po::options_description control("Feedback options");
|
|---|
| 763 | control.add_options()
|
|---|
| 764 | ("pixel-map-file", var<string>("FACTmapV5a.txt"), "Pixel mapping file. Used here to get the default reference voltage.")
|
|---|
| 765 | ;
|
|---|
| 766 |
|
|---|
| 767 | conf.AddOptions(control);
|
|---|
| 768 | }
|
|---|
| 769 |
|
|---|
| 770 | /*
|
|---|
| 771 | Extract usage clause(s) [if any] for SYNOPSIS.
|
|---|
| 772 | Translators: "Usage" and "or" here are patterns (regular expressions) which
|
|---|
| 773 | are used to match the usage synopsis in program output. An example from cp
|
|---|
| 774 | (GNU coreutils) which contains both strings:
|
|---|
| 775 | Usage: cp [OPTION]... [-T] SOURCE DEST
|
|---|
| 776 | or: cp [OPTION]... SOURCE... DIRECTORY
|
|---|
| 777 | or: cp [OPTION]... -t DIRECTORY SOURCE...
|
|---|
| 778 | */
|
|---|
| 779 | void PrintUsage()
|
|---|
| 780 | {
|
|---|
| 781 | cout <<
|
|---|
| 782 | "The feedback control the BIAS voltages based on the calibration signal.\n"
|
|---|
| 783 | "\n"
|
|---|
| 784 | "The default is that the program is started without user intercation. "
|
|---|
| 785 | "All actions are supposed to arrive as DimCommands. Using the -c "
|
|---|
| 786 | "option, a local shell can be initialized. With h or help a short "
|
|---|
| 787 | "help message about the usuage can be brought to the screen.\n"
|
|---|
| 788 | "\n"
|
|---|
| 789 | "Usage: feedback [-c type] [OPTIONS]\n"
|
|---|
| 790 | " or: feedback [OPTIONS]\n";
|
|---|
| 791 | cout << endl;
|
|---|
| 792 | }
|
|---|
| 793 |
|
|---|
| 794 | void PrintHelp()
|
|---|
| 795 | {
|
|---|
| 796 | /* Additional help text which is printed after the configuration
|
|---|
| 797 | options goes here */
|
|---|
| 798 |
|
|---|
| 799 | /*
|
|---|
| 800 | cout << "bla bla bla" << endl << endl;
|
|---|
| 801 | cout << endl;
|
|---|
| 802 | cout << "Environment:" << endl;
|
|---|
| 803 | cout << "environment" << endl;
|
|---|
| 804 | cout << endl;
|
|---|
| 805 | cout << "Examples:" << endl;
|
|---|
| 806 | cout << "test exam" << endl;
|
|---|
| 807 | cout << endl;
|
|---|
| 808 | cout << "Files:" << endl;
|
|---|
| 809 | cout << "files" << endl;
|
|---|
| 810 | cout << endl;
|
|---|
| 811 | */
|
|---|
| 812 | }
|
|---|
| 813 |
|
|---|
| 814 | int main(int argc, const char* argv[])
|
|---|
| 815 | {
|
|---|
| 816 | Configuration conf(argv[0]);
|
|---|
| 817 | conf.SetPrintUsage(PrintUsage);
|
|---|
| 818 | Main::SetupConfiguration(conf);
|
|---|
| 819 | SetupConfiguration(conf);
|
|---|
| 820 |
|
|---|
| 821 | if (!conf.DoParse(argc, argv, PrintHelp))
|
|---|
| 822 | return -1;
|
|---|
| 823 |
|
|---|
| 824 | //try
|
|---|
| 825 | {
|
|---|
| 826 | // No console access at all
|
|---|
| 827 | if (!conf.Has("console"))
|
|---|
| 828 | {
|
|---|
| 829 | // if (conf.Get<bool>("no-dim"))
|
|---|
| 830 | // return RunShell<LocalStream, StateMachine, ConnectionFSC>(conf);
|
|---|
| 831 | // else
|
|---|
| 832 | return RunShell<LocalStream>(conf);
|
|---|
| 833 | }
|
|---|
| 834 | // Cosole access w/ and w/o Dim
|
|---|
| 835 | /* if (conf.Get<bool>("no-dim"))
|
|---|
| 836 | {
|
|---|
| 837 | if (conf.Get<int>("console")==0)
|
|---|
| 838 | return RunShell<LocalShell, StateMachine, ConnectionFSC>(conf);
|
|---|
| 839 | else
|
|---|
| 840 | return RunShell<LocalConsole, StateMachine, ConnectionFSC>(conf);
|
|---|
| 841 | }
|
|---|
| 842 | else
|
|---|
| 843 | */ {
|
|---|
| 844 | if (conf.Get<int>("console")==0)
|
|---|
| 845 | return RunShell<LocalShell>(conf);
|
|---|
| 846 | else
|
|---|
| 847 | return RunShell<LocalConsole>(conf);
|
|---|
| 848 | }
|
|---|
| 849 | }
|
|---|
| 850 | /*catch (std::exception& e)
|
|---|
| 851 | {
|
|---|
| 852 | cerr << "Exception: " << e.what() << endl;
|
|---|
| 853 | return -1;
|
|---|
| 854 | }*/
|
|---|
| 855 |
|
|---|
| 856 | return 0;
|
|---|
| 857 | }
|
|---|