source: branches/testFACT++branch/src/biasctrl.cc@ 18656

Last change on this file since 18656 was 18149, checked in by tbretz, 10 years ago
Initiate an emergency shutdown also when an overcurrent is detected; print messages only once; there is still a little chance that between the initiation of an emergency shutdown and the transfer to the Locked state another ramp up command is processed.
File size: 72.9 KB
Line 
1#include <functional>
2
3#include <boost/bind.hpp>
4
5#include "Dim.h"
6#include "Event.h"
7#include "Shell.h"
8#include "StateMachineDim.h"
9#include "StateMachineAsio.h"
10#include "ConnectionUSB.h"
11#include "Configuration.h"
12#include "Console.h"
13#include "externals/PixelMap.h"
14
15#include "tools.h"
16
17#include "LocalControl.h"
18#include "HeadersBIAS.h"
19
20namespace ba = boost::asio;
21namespace bs = boost::system;
22namespace dummy = ba::placeholders;
23
24using namespace std::placeholders;
25using namespace std;
26
27// We can do that because we do not include other headers than HeadersBIAS
28using namespace BIAS;
29
30// ------------------------------------------------------------------------
31
32class ConnectionBias : public ConnectionUSB
33{
34 boost::asio::deadline_timer fSyncTimer;
35 boost::asio::deadline_timer fRampTimer;
36 boost::asio::deadline_timer fUpdateTimer;
37
38 vector<uint8_t> fBuffer;
39 vector<uint8_t> fBufferRamp;
40 vector<uint8_t> fBufferUpdate;
41
42 bool fIsVerbose;
43 bool fIsDummyMode;
44
45 vector<bool> fPresent;
46
47 int64_t fWrapCounter;
48 int64_t fSendCounter;
49
50 int16_t fGlobalDacCmd; // Command value to be reached
51
52 int16_t fRampStep;
53 int16_t fRampTime;
54
55 uint32_t fUpdateTime;
56 uint16_t fSyncTime;
57 uint32_t fReconnectDelay;
58
59 int fIsInitializing;
60 bool fIsRamping;
61 int fWaitingForAnswer;
62
63 vector<uint64_t> fCounter;
64
65 Time fLastConnect;
66
67 int32_t fEmergencyLimit;
68 bool fEmergencyShutdown;
69
70protected:
71
72 vector<int16_t> fCurrent; // Current in ADC units (12bit = 5mA)
73
74 virtual void UpdateV(const Time = Time())
75 {
76 }
77
78 virtual void UpdateVgapd()
79 {
80 }
81
82public:
83 virtual void UpdateVA()
84 {
85 }
86
87 // ====================================================
88
89protected:
90 vector<float> fOperationVoltage; // Operation voltage of GAPDs
91 //vector<float> fChannelOffset; // User defined channel offset
92
93 vector<float> fCalibrationOffset; // Bias crate channel offset
94 vector<float> fCalibrationSlope; // Bias crate channel slope
95
96 float fVoltageMaxAbs; // Maximum voltage
97 float fVoltageMaxRel; // Maximum voltgage above (what?)
98
99 vector<uint16_t> fDacTarget; // Target values
100 vector<uint16_t> fDacCommand; // Last sent command value
101 vector<uint16_t> fDacActual; // Actual value
102
103 // ====================================================
104
105private:
106 vector<char> GetCmd(uint16_t board, uint16_t channel, Command_t cmd, uint16_t dac=0)
107 {
108 vector<char> data(3);
109
110 /*
111 if (board>kNumBoards)
112 return;
113 if (channel>kNumChannelsPerBoard)
114 return;
115 if (dac>0xfff)
116 return;
117 */
118
119 data[0] = (cmd<<5) | (board<<1) | (((channel&16)>>4) & 1);
120 data[1] = (channel<<4) | (dac>>8);
121 data[2] = dac&0xff;
122
123 return data;
124 }
125
126 vector<char> GetCmd(Command_t cmd, uint16_t id=0, uint16_t dac=0)
127 {
128 const unsigned int board = id/kNumChannelsPerBoard;
129 const unsigned int channel = id%kNumChannelsPerBoard;
130
131 return GetCmd(board, channel, cmd, dac);
132 }
133
134 bool CheckMessageLength(int received, int expected, const string &msg)
135 {
136 if (received==expected)
137 return true;
138
139 ostringstream str;
140 str << msg << ": Expected " << expected << " bytes in answer, but got " << received << endl;
141 Error(str);
142
143 return false;
144 }
145
146 bool EvalAnswer(const uint8_t *answer, uint16_t id, int command)
147 {
148 answer += id*3;
149
150 const uint16_t status = (answer[0]>>7)&1;
151 const uint16_t wrap = (answer[0]>>4)&7;
152 const uint16_t ddd = ((uint16_t(answer[0])&0xf)<<8) | answer[1];
153 const uint16_t error = (answer[2]>>4)&0xf;
154 const uint16_t board = answer[2]&0xf;
155
156 // 0x10 00 7f
157 // status = 0
158 // wrap = 1
159 // ddd = 0
160 // error = not present
161 // board = 15
162
163 /*
164 Out() << dec << setw(2) << board << '|' << wrap << " ";
165 if (id%8==7)
166 Out() << endl;
167 */
168
169 if (fWrapCounter>=0)
170 {
171 if ((fWrapCounter+1)%8 != wrap)
172 {
173 ostringstream msg;
174 msg << "Corrupted answer (id=" << id << "): received wrap counter " << wrap << " doesn't match last one " << fWrapCounter << " ";
175 msg << " (fSendCounter=" << fSendCounter << ")";
176 Error(msg);
177 return false;
178 }
179 }
180
181 fWrapCounter = wrap;
182
183 if (command==kSynchronize)
184 {
185 ostringstream msg;
186 msg << hex << setfill('0');
187 msg << "Initial answer received: 0x";
188 msg << setw(2) << (int)answer[2];
189 msg << setw(2) << (int)answer[1];
190 msg << setw(2) << (int)answer[0];
191 Message(msg);
192
193 if (status!=0 || ddd!=0 || error!=0 || board!=0)
194 {
195 Warn("Initial answer doesn't seem to be a reset as naively expected.");
196
197 //ostringstream msg;
198 //msg << hex << setfill('0');
199 //msg << "S=" << status << " D=" << ddd << " E=" << error << " B=" << board;
200 //Message(msg);
201 }
202
203 fSendCounter = wrap;
204
205 msg.str("");
206 msg << "Setting fSendCounter to " << wrap;
207 Info(msg);
208
209 return true;
210 }
211
212 if (error==0x8) // No device
213 {
214 Message("Reset button on crate pressed!");
215 RampAllDacs(0);
216 return true;
217 }
218
219 if (command==kCmdReset)
220 {
221 if (status==0 && ddd==0 && error==0 && board==0)
222 {
223 Message("Reset successfully executed.");
224 return true;
225 }
226
227 Warn("Answer to 'reset' command contains unexpected data.");
228 return false;
229 }
230
231 if (command==kCmdGlobalSet)
232 {
233 if (status==0 && ddd==0 && error==0 && board==0)
234 {
235 for (int i=0; i<kNumChannels; i++)
236 fDacActual[i] = fGlobalDacCmd;
237
238 fGlobalDacCmd = -1;
239
240 return true;
241 }
242
243 Warn("Answer to 'global set' command contains unexpected data.");
244 return false;
245 }
246
247 if ((command&0xff)==kExpertChannelSet)
248 id = command>>8;
249
250 const int cmd = command&3;
251
252 if (cmd==kCmdRead || cmd==kCmdChannelSet)
253 {
254 if (board!=id/kNumChannelsPerBoard)
255 {
256 ostringstream out;
257 out << "Talked to board " << id/kNumChannelsPerBoard << ", but got answer from board " << board << " (fSendCounter=" << fSendCounter << ")";
258 Error(out);
259 return false;
260 }
261
262 // Not present
263 if (error==0x7 || error==0xf)
264 {
265 fPresent[board] = false;
266 fCurrent[id] = 0x8000;
267 return true;
268 }
269
270 // There is no -0 therefore we make a trick and replace it by -1.
271 // This is not harmfull, because typical zero currents are in the
272 // order of one to three bits anyway and they are never stable.
273 fCurrent[id] = status ? -(ddd==0?1:ddd) : ddd;
274 fPresent[board] = true;
275
276 if (!fEmergencyShutdown)
277 {
278 if (fCurrent[id]<0)
279 {
280 Warn("OverCurrent detected.");
281 fEmergencyShutdown = true;
282 }
283
284 if (fEmergencyLimit>0 && fCurrent[id]>fEmergencyLimit && !fEmergencyShutdown)
285 {
286 Warn("Emergency limit exceeded.");
287 fEmergencyShutdown = true;
288 }
289
290 if (fEmergencyShutdown)
291 {
292 Error("Emergency ramp down initiated.");
293 Dim::SendCommandNB("MCP/STOP");
294 RampAllDacs(0);
295 }
296 }
297 }
298
299 if (cmd==kCmdChannelSet)
300 fDacActual[id] = fDacCommand[id];
301
302 return true;
303
304 }
305
306private:
307 void DelayedReconnect()
308 {
309 const Time now;
310
311 // If we have been connected without a diconnect for at least 60s
312 // we can reset the delay.
313 if (now-fLastConnect>boost::posix_time::seconds(60))
314 fReconnectDelay = 1;
315
316 ostringstream msg;
317 msg << "Automatic reconnect in " << fReconnectDelay << "s after being connected for ";
318 msg << (now-fLastConnect).seconds() << "s";
319 Info(msg);
320
321 CloseImp(fReconnectDelay);
322 fReconnectDelay *= 2;
323 }
324
325 void HandleReceivedData(const vector<uint8_t> &buf, size_t bytes_received, int command, int send_counter)
326 {
327#ifdef DEBUG
328 ofstream fout("received.txt", ios::app);
329 fout << Time() << ": ";
330 for (unsigned int i=0; i<bytes_received; i++)
331 fout << hex << setfill('0') << setw(2) << (uint16_t)buf[i];
332 fout << endl;
333#endif
334
335 // Now print the received message if requested by the user
336 if (fIsVerbose/* && command!=kUpdate*/)
337 {
338 Out() << endl << kBold << dec << "Data received (size=" << bytes_received << "):" << endl;
339 Out() << " Command=" << command << " fWrapCounter=" << fWrapCounter << " fSendCounter=" << fSendCounter << " fIsInitializing=" << fIsInitializing << " fIsRamping=" << fIsRamping;
340 Out() << hex << setfill('0');
341
342 for (size_t i=0; i<bytes_received/3; i++)
343 {
344 if (i%8==0)
345 Out() << '\n' << setw(2) << bytes_received/24 << "| ";
346
347 Out() << setw(2) << uint16_t(buf[i*3+2]);
348 Out() << setw(2) << uint16_t(buf[i*3+1]);
349 Out() << setw(2) << uint16_t(buf[i*3+0]) << " ";
350 }
351 Out() << endl;
352 }
353
354 const int cmd = command&0xf;
355
356 // Check the number of received_byted according to the answer expected
357 if ((cmd==kSynchronize && !CheckMessageLength(bytes_received, 3, "Synchronization")) ||
358 (cmd==kCmdReset && !CheckMessageLength(bytes_received, 3, "CmdReset")) ||
359 (cmd==kCmdRead && !CheckMessageLength(bytes_received, 3*kNumChannels, "CmdRead")) ||
360 (cmd==kCmdChannelSet && !CheckMessageLength(bytes_received, 3*kNumChannels, "CmdChannelSet")) ||
361 (cmd==kExpertChannelSet && !CheckMessageLength(bytes_received, 3, "CmdExpertChannelSet")))
362 {
363 CloseImp(-1);
364 return;
365 }
366
367 // Now evaluate the whole bunch of messages
368 for (size_t i=0; i<bytes_received/3; i++)
369 {
370 if (!EvalAnswer(buf.data(), i, command))
371 {
372 DelayedReconnect();
373 return;
374 }
375 }
376
377 if (command==kSynchronize)
378 {
379 Message("Stream successfully synchronized.");
380 fIsInitializing = 2;
381
382 // Cancel sending of the next 0
383 fSyncTimer.cancel();
384 fCounter[0]++;
385
386 // Start continous reading of all channels
387 ScheduleUpdate(100);
388 return;
389 }
390
391 if (send_counter%8 != fWrapCounter)
392 {
393 ostringstream msg;
394 msg << "Corrupted answer: received wrap counter " << fWrapCounter << " is not send counter " << send_counter << "%8.";
395 Error(msg);
396
397 DelayedReconnect();
398 }
399
400
401 // Check if new values have been received
402 if (cmd==kCmdRead || cmd==kCmdChannelSet || cmd==kExpertChannelSet)
403 UpdateVA();
404
405 // ----- Take action depending on what is going on -----
406
407 if (command==kCmdReset)
408 {
409 Message("Reset command successfully answered...");
410
411 fCounter[1]++;
412
413 // Re-start cyclic reading of values after a short time
414 // to allow the currents to become stable. This ensures that
415 // we get an update soon but wait long enough to get reasonable
416 // values
417 fUpdateTimer.cancel();
418
419 if (fUpdateTime==0)
420 ReadAllChannels(true);
421 else
422 {
423 Message("...restarting automatic readout.");
424 ScheduleUpdate(100);
425 }
426 }
427
428 if (command==kResetChannels)
429 {
430 ExpertReset(false);
431 fCounter[5]++;
432 }
433
434 if (command==kUpdate)
435 {
436 ScheduleUpdate(fUpdateTime);
437 fCounter[2]++;
438 }
439
440 // If we are ramping, schedule a new ramp step
441 if (command==kCmdChannelSet && fIsRamping)
442 {
443 bool oc = false;
444 for (int ch=0; ch<kNumChannels; ch++)
445 if (fPresent[ch/kNumChannelsPerBoard] && fCurrent[ch]<0)
446 oc = true;
447
448 if (oc)
449 {
450 if (!fEmergencyShutdown)
451 {
452 Warn("OverCurrent detected - emergency ramp down initiated.");
453 Dim::SendCommandNB("MCP/STOP");
454 RampAllDacs(0);
455 fEmergencyShutdown = true;
456 }
457 }
458 else
459 ScheduleRampStep();
460
461 fCounter[3]++;
462 }
463
464 if (command==kCmdRead)
465 fCounter[4]++;
466
467 if ((command&0xff)==kExpertChannelSet)
468 fCounter[6]++;
469
470 if (command==kCmdGlobalSet)
471 fCounter[7]++;
472 }
473
474 void HandleReceivedData(const bs::error_code& err, size_t bytes_received, int command, int send_counter)
475 {
476 // Do not schedule a new read if the connection failed.
477 if (bytes_received==0 || err)
478 {
479 if (err==ba::error::eof)
480 {
481 ostringstream msg;
482 msg << "Connection closed by remote host (BIAS, fSendCounter=" << fSendCounter << ")";
483 Warn(msg);
484 }
485
486 // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
487 // 125: Operation canceled
488 if (err && err!=ba::error::eof && // Connection closed by remote host
489 err!=ba::error::basic_errors::not_connected && // Connection closed by remote host
490 err!=ba::error::basic_errors::operation_aborted) // Connection closed by us
491 {
492 ostringstream str;
493 str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
494 Error(str);
495 }
496 CloseImp(-1);//err!=ba::error::basic_errors::operation_aborted);
497 return;
498 }
499
500 // Check if the number of received bytes is correctly dividable by 3
501 // This check should never fail - just for sanity
502 if (bytes_received%3)
503 {
504 Error("Number of received bytes not a multiple of 3, can't read data.");
505 CloseImp(-1);
506 return;
507 }
508
509 // We have three different parallel streams:
510 // 1) The setting of voltages due to ramping
511 // 2) The cynclic request of the currents
512 // 3) Answers to commands
513 // For each of these three streams an own buffer is needed, otherwise
514 // a buffer which is filled in the background might overwrite
515 // a buffer which is currently evaluated. In all other programs
516 // this is no problem because the boards don't answer and if
517 // they do the answer identifies itself. Consequently,
518 // there is always only one async_read in progress. Here we have
519 // three streams which need to be connected somehow to the
520 // commands.
521
522 // Maybe a better possibility would be to setup a command
523 // queue (each command will be queued in a buffer)
524 // and whenever an answer has been received, a new async_read is
525 // scheduled.
526 // Build a command queue<pair<command, vector<char>>>
527 /// This replaces the send counter and the command argument
528 // in handleReceivedData
529
530 switch (command&0xff)
531 {
532 case kSynchronize:
533 case kCmdReset:
534 case kExpertChannelSet:
535 case kCmdGlobalSet:
536 case kResetChannels:
537 case kCmdRead:
538 HandleReceivedData(fBuffer, bytes_received, command, send_counter);
539 fWaitingForAnswer = -1;
540 return;
541
542 case kCmdChannelSet:
543 HandleReceivedData(fBufferRamp, bytes_received, command, send_counter);
544 return;
545
546 case kUpdate:
547 HandleReceivedData(fBufferUpdate, bytes_received, command, send_counter);
548 return;
549 }
550 }
551
552 // --------------------------------------------------------------------
553
554 void HandleSyncTimer(int counter, const bs::error_code &error)
555 {
556 if (error==ba::error::basic_errors::operation_aborted)
557 {
558 if (fIsInitializing==1)
559 Warn("Synchronization aborted...");
560 // case 0 and 2 should not happen
561 return;
562 }
563
564 if (error)
565 {
566 ostringstream str;
567 str << "Synchronization timer: " << error.message() << " (" << error << ")";// << endl;
568 Error(str);
569
570 CloseImp(-1);
571 return;
572 }
573
574 if (!is_open())
575 {
576 Warn("Synchronization in progress, but disconnected.");
577 return;
578 }
579
580 ostringstream msg;
581 msg << "Synchronization time expired (" << counter << ")";
582 Info(msg);
583
584 if (fIsInitializing)
585 {
586 PostMessage("\0", 1);
587
588 if (counter==2)
589 {
590 Error("Synchronization attempt timed out.");
591 CloseImp(-1);
592 return;
593 }
594
595 ScheduleSync(counter+1);
596 return;
597 }
598
599 Info("Synchronisation successfull.");
600 }
601
602 void ScheduleSync(int counter=0)
603 {
604 fSyncTimer.expires_from_now(boost::posix_time::milliseconds(fSyncTime));
605 fSyncTimer.async_wait(boost::bind(&ConnectionBias::HandleSyncTimer, this, counter, dummy::error));
606 }
607
608 // This is called when a connection was established
609 void ConnectionEstablished()
610 {
611 // We connect for the first time or haven't received
612 // a valid warp counter yet... this procedure also sets
613 // our volatges to 0 if we have connected but never received
614 // any answer.
615 if (fWrapCounter<0)
616 {
617 fDacTarget.assign(kNumChannels, 0);
618 fDacCommand.assign(kNumChannels, 0);
619 fDacActual.assign(kNumChannels, 0);
620 }
621
622 // Reset everything....
623 fSendCounter = -1;
624 fWrapCounter = -1;
625 fGlobalDacCmd = -1;
626 fIsInitializing = 1;
627 fIsRamping = false;
628
629 fLastConnect = Time();
630
631 // Send a single 0 (and possible two consecutive 0's
632 // to make sure we are in sync with the device)
633 PostMessage("\0", 1);
634 AsyncRead(ba::buffer(fBuffer, 3), kSynchronize, 0);//++fSendCounter);
635 fWaitingForAnswer = kSynchronize;
636
637 // Wait for some time before sending the next 0
638 ScheduleSync();
639 }
640
641 // --------------------------------------------------------------------
642
643 void HandleUpdateTimer(const bs::error_code &error)
644 {
645 if (error==ba::error::basic_errors::operation_aborted)
646 {
647 Warn("Update timer aborted...");
648 fIsRamping = false;
649 return;
650 }
651
652 if (error)
653 {
654 ostringstream str;
655 str << "Update timer: " << error.message() << " (" << error << ")";// << endl;
656 Error(str);
657
658 CloseImp(-1);
659 return;
660 }
661
662 if (!is_open())
663 return;
664
665 if (fUpdateTime==0 && fIsInitializing!=2)
666 return;
667
668 if (fIsRamping)
669 ScheduleUpdate(fUpdateTime);
670 else
671 ReadAllChannels(true);
672
673 fIsInitializing = 0;
674 }
675
676 void ScheduleUpdate(int millisec)
677 {
678 fUpdateTimer.expires_from_now(boost::posix_time::milliseconds(millisec));
679 fUpdateTimer.async_wait(boost::bind(&ConnectionBias::HandleUpdateTimer, this, dummy::error));
680 }
681
682 // --------------------------------------------------------------------
683
684 void PrintLineCmdDac(int b, int ch, const vector<uint16_t> &dac)
685 {
686 Out() << setw(2) << b << "|";
687
688 for (int c=ch; c<ch+4; c++)
689 {
690 const int id = c+kNumChannelsPerBoard*b;
691 Out() << " " << setw(4) << int32_t(dac[id])<<"/"<<fDacActual[id] << ":" << setw(5) << ConvertDacToVolt(id, fDacTarget[id]);
692 }
693 Out() << endl;
694 }
695
696 void PrintCommandDac(const vector<uint16_t> &dac)
697 {
698 Out() << dec << setprecision(2) << fixed << setfill(' ');
699 for (int b=0; b<kNumBoards; b++)
700 {
701 if (!fPresent[b])
702 {
703 Out() << setw(2) << b << "-" << endl;
704 continue;
705 }
706
707 PrintLineCmdDac(b, 0, dac);
708 PrintLineCmdDac(b, 4, dac);
709 PrintLineCmdDac(b, 8, dac);
710 PrintLineCmdDac(b, 12, dac);
711 PrintLineCmdDac(b, 16, dac);
712 PrintLineCmdDac(b, 20, dac);
713 PrintLineCmdDac(b, 24, dac);
714 PrintLineCmdDac(b, 28, dac);
715 }
716 }
717
718 void SetAllChannels(const vector<uint16_t> &dac, bool special=false)
719 {
720 if (fIsDummyMode)
721 {
722 PrintCommandDac(dac);
723 return;
724 }
725
726 vector<char> data;
727 data.reserve(kNumChannels*3);
728
729 for (int ch=0; ch<kNumChannels; ch++)
730 {
731 // FIXME: dac[ch] += calib_offset
732 const vector<char> cmd = GetCmd(kCmdChannelSet, ch, dac[ch]);
733 data.insert(data.end(), cmd.begin(), cmd.end());
734
735 fDacCommand[ch] = dac[ch];
736 }
737
738 fSendCounter += kNumChannels;
739
740 PostMessage(data);
741 AsyncRead(ba::buffer(special ? fBuffer : fBufferRamp, kNumChannels*3),
742 special ? kResetChannels : kCmdChannelSet, fSendCounter);
743
744 if (special)
745 fWaitingForAnswer = kResetChannels;
746 }
747
748 uint16_t RampOneStep(uint16_t ch)
749 {
750 if (fDacTarget[ch]>fDacActual[ch])
751 return fDacActual[ch]+fRampStep>fDacTarget[ch] ? fDacTarget[ch] : fDacActual[ch]+fRampStep;
752
753 if (fDacTarget[ch]<fDacActual[ch])
754 return fDacActual[ch]-fRampStep<fDacTarget[ch] ? fDacTarget[ch] : fDacActual[ch]-fRampStep;
755
756 return fDacActual[ch];
757 }
758
759 bool RampOneStep()
760 {
761 if (fRampTime<0)
762 {
763 Warn("Ramping step time not yet set... ramping not started.");
764 return false;
765 }
766 if (fRampStep<0)
767 {
768 Warn("Ramping step not yet set... ramping not started.");
769 return false;
770 }
771
772 vector<uint16_t> dac(kNumChannels);
773
774 bool identical = true;
775 for (int ch=0; ch<kNumChannels; ch++)
776 {
777 dac[ch] = RampOneStep(ch);
778 if (dac[ch]!=fDacActual[ch] && fPresent[ch/kNumChannelsPerBoard])
779 identical = false;
780 }
781
782 if (identical)
783 {
784 Info("Ramping: target values reached.");
785 return false;
786 }
787
788 if (fWaitingForAnswer<0)
789 {
790 SetAllChannels(dac);
791 return true;
792 }
793
794 ostringstream msg;
795 msg << "RampOneStep while waiting for answer to last command (id=" << fWaitingForAnswer << ")... ramp step delayed.";
796 Warn(msg);
797
798 // Delay ramping
799 ScheduleRampStep();
800 return true;
801 }
802
803 void HandleRampTimer(const bs::error_code &error)
804 {
805 if (error==ba::error::basic_errors::operation_aborted)
806 {
807 Warn("Ramping aborted...");
808 fIsRamping = false;
809 return;
810 }
811
812 if (error)
813 {
814 ostringstream str;
815 str << "Ramping timer: " << error.message() << " (" << error << ")";// << endl;
816 Error(str);
817
818 fIsRamping = false;
819 CloseImp(-1);
820 return;
821 }
822
823 if (!is_open())
824 {
825 Warn("Ramping in progress, but disconnected.");
826 fIsRamping = false;
827 return;
828 }
829
830 if (!fIsRamping)
831 {
832 Error("Ramp handler called although no ramping in progress.");
833 return;
834 }
835
836 // Check whether the deadline has passed. We compare the deadline
837 // against the current time since a new asynchronous operation
838 // may have moved the deadline before this actor had a chance
839 // to run.
840 if (fRampTimer.expires_at() > ba::deadline_timer::traits_type::now())
841 return;
842
843 fIsRamping = RampOneStep();
844 }
845
846 void ScheduleRampStep()
847 {
848 fRampTimer.expires_from_now(boost::posix_time::milliseconds(fRampTime));
849 fRampTimer.async_wait(boost::bind(&ConnectionBias::HandleRampTimer, this, dummy::error));
850 }
851
852public:
853 ConnectionBias(ba::io_service& ioservice, MessageImp &imp) : ConnectionUSB(ioservice, imp()),
854 fSyncTimer(ioservice),
855 fRampTimer(ioservice),
856 fUpdateTimer(ioservice),
857 fBuffer(3*kNumChannels),
858 fBufferRamp(3*kNumChannels),
859 fBufferUpdate(3*kNumChannels),
860 fIsVerbose(false),
861 fIsDummyMode(false),
862 fPresent(kNumBoards),
863 fWrapCounter(-1),
864 fRampStep(-1),
865 fRampTime(-1),
866 fUpdateTime(3000),
867 fSyncTime(333),
868 fReconnectDelay(1),
869 fIsRamping(false),
870 fWaitingForAnswer(-1),
871 fCounter(8),
872 fEmergencyLimit(0),
873 fEmergencyShutdown(false),
874 fCurrent(kNumChannels),
875 fOperationVoltage(kNumChannels, 0),
876 //fChannelOffset(kNumChannels),
877 fCalibrationOffset(kNumChannels),
878 fCalibrationSlope(kNumChannels, 90000),
879 fVoltageMaxAbs(75),
880 fVoltageMaxRel(2),
881 fDacTarget(kNumChannels),
882 fDacCommand(kNumChannels),
883 fDacActual(kNumChannels)
884 {
885 SetLogStream(&imp);
886 }
887
888 // --------------------------------------------------------------------
889
890 bool CheckDac(uint16_t dac)
891 {
892 if (dac<4096)
893 return true;
894
895 ostringstream msg;
896 msg << "CheckDac - Dac value of " << dac << " exceeds maximum of 4095.";
897 Error(msg);
898 return false;
899 }
900
901 bool CheckChannel(uint16_t ch)
902 {
903 if (ch<kNumChannels)
904 return true;
905
906 ostringstream msg;
907 msg << "CheckChannel - Channel " << ch << " out of range [0;" << kNumChannels-1 << "].";
908 Error(msg);
909 return false;
910 }
911
912 bool CheckChannelVoltage(uint16_t ch, float volt)
913 {
914 if (volt>fVoltageMaxAbs)
915 {
916 ostringstream msg;
917 msg << "CheckChannelVoltage - Set voltage " << volt << "V of channel " << ch << " exceeds absolute limit of " << fVoltageMaxAbs << "V.";
918 Warn(msg);
919 return false;
920 }
921
922 if (fOperationVoltage[ch]<=0)
923 return true;
924
925 if (volt>fOperationVoltage[ch]+fVoltageMaxRel) // FIXME: fVoltageMaxRel!!!
926 {
927 ostringstream msg;
928 msg << "CheckChannelVoltage - Set voltage " << volt << "V of channel " << ch << " exceeds limit of " << fVoltageMaxRel << "V above operation voltage " << fOperationVoltage[ch] << "V + limit " << fVoltageMaxRel << "V.";
929 Error(msg);
930 return false;
931 }
932
933 return true;
934 }
935
936 // --------------------------------------------------------------------
937
938 bool RampSingleChannelDac(uint16_t ch, uint16_t dac)
939 {
940 if (!CheckChannel(ch))
941 return false;
942
943 if (!CheckDac(dac))
944 return false;
945
946 fDacTarget[ch] = dac;
947 UpdateV();
948
949 if (!fIsRamping)
950 fIsRamping = RampOneStep();
951
952 return true;
953 }
954
955 bool RampAllChannelsDac(const vector<uint16_t> &dac)
956 {
957 for (int ch=0; ch<kNumChannels; ch++)
958 if (!CheckDac(dac[ch]))
959 return false;
960
961 fDacTarget = dac;
962 UpdateV();
963
964 if (!fIsRamping)
965 fIsRamping = RampOneStep();
966
967 return true;
968 }
969
970 bool RampAllDacs(uint16_t dac)
971 {
972 return RampAllChannelsDac(vector<uint16_t>(kNumChannels, dac));
973 }
974
975 // --------------------------------------------------------------------
976
977 uint16_t ConvertVoltToDac(uint16_t ch, double volt)
978 {
979 if (fCalibrationSlope[ch]<=0)
980 return 0;
981
982 const double current = (volt-fCalibrationOffset[ch])/fCalibrationSlope[ch];
983 return current<0 ? 0 : nearbyint(current*4096000); // Current [A] to dac [ /= 1mA/4096]
984 }
985
986 double ConvertDacToVolt(uint16_t ch, uint16_t dac)
987 {
988 if (fCalibrationSlope[ch]<=0)
989 return 0;
990
991 const double current = dac/4096000.; // Convert dac to current [A] [ *= 1mA/4096]
992 return current*fCalibrationSlope[ch] + fCalibrationOffset[ch];
993 }
994
995 // --------------------------------------------------------------------
996
997 bool RampSingleChannelVoltage(uint16_t ch, float volt)
998 {
999 if (!CheckChannel(ch))
1000 return false;
1001
1002 if (!CheckChannelVoltage(ch, volt))
1003 return false;
1004
1005 const uint16_t dac = ConvertVoltToDac(ch, volt);
1006 return RampSingleChannelDac(ch, dac);
1007 }
1008
1009 bool RampAllChannelsVoltage(const vector<float> &volt)
1010 {
1011 vector<uint16_t> dac(kNumChannels);
1012 for (size_t ch=0; ch<kNumChannels; ch++)
1013 {
1014 if (!CheckChannelVoltage(ch, volt[ch]))
1015 return false;
1016
1017 dac[ch] = ConvertVoltToDac(ch, volt[ch]);
1018 }
1019
1020 return RampAllChannelsDac(dac);
1021 }
1022
1023 bool RampAllVoltages(float volt)
1024 {
1025 return RampAllChannelsVoltage(vector<float>(kNumChannels, volt));
1026 }
1027
1028 // --------------------------------------------------------------------
1029
1030 /*
1031 bool RampSingleChannelOffset(uint16_t ch, float offset, bool relative)
1032 {
1033 if (!CheckChannel(ch))
1034 return false;
1035
1036// if (relative)
1037// offset += fDacActual[ch]*90./4096 - fBreakdownVoltage[ch];
1038
1039 const float volt = fBreakdownVoltage[ch]>0 ? fBreakdownVoltage[ch] + offset : 0;
1040
1041 if (!RampSingleChannelVoltage(ch, volt))
1042 return false;
1043
1044 fChannelOffset[ch] = offset;
1045
1046 return true;
1047 }
1048
1049 bool RampAllChannelsOffset(vector<float> offset, bool relative)
1050 {
1051 vector<float> volt(kNumChannels);
1052
1053// if (relative)
1054// for (size_t ch=0; ch<kNumChannels; ch++)
1055// offset[ch] += fDacActual[ch]*90./4096 - fBreakdownVoltage[ch];
1056
1057 for (size_t ch=0; ch<kNumChannels; ch++)
1058 volt[ch] = fBreakdownVoltage[ch]>0 ? fBreakdownVoltage[ch] + offset[ch] : 0;
1059
1060 if (!RampAllChannelsVoltage(volt))
1061 return false;
1062
1063 fChannelOffset = offset;
1064
1065 return true;
1066 }
1067
1068 bool RampAllOffsets(float offset, bool relative)
1069 {
1070 return RampAllChannelsOffset(vector<float>(kNumChannels, offset), relative);
1071 }
1072 */
1073
1074 /*
1075 bool RampSingleChannelOvervoltage(float offset)
1076 {
1077 return RampAllChannelsOvervoltage(vector<float>(kNumChannels, offset));
1078 }
1079 bool RampAllOvervoltages(const vector<float> &overvoltage)
1080 {
1081 vector<float> volt(kNumChannels);
1082
1083 for (size_t ch=0; ch<kNumChannels; ch++)
1084 volt[ch] = fBreakdownVoltage[ch] + fOvervoltage[ch] + fChannelOffset[ch];
1085
1086#warning What about empty channels?
1087
1088 if (!RampAllChannelsVoltage(volt))
1089 return false;
1090
1091 for (size_t ch=0; ch<kNumChannels; ch++)
1092 fOvervoltage[ch] = overvoltage[ch];
1093
1094 return true;
1095 }*/
1096
1097 // --------------------------------------------------------------------
1098
1099 void OverCurrentReset()
1100 {
1101 if (fWaitingForAnswer>=0)
1102 {
1103 ostringstream msg;
1104 msg << "OverCurrentReset - Answer on last command (id=" << fWaitingForAnswer << ") not yet received.";
1105 Error(msg);
1106 return;
1107 }
1108
1109 if (fIsRamping)
1110 {
1111 Warn("OverCurrentReset - Ramping in progres.");
1112 RampStop();
1113 }
1114
1115 vector<uint16_t> dac(fDacActual);
1116
1117 for (int ch=0; ch<kNumChannels; ch++)
1118 if (fCurrent[ch]<0)
1119 dac[ch] = 0;
1120
1121 SetAllChannels(dac, true);
1122 }
1123
1124 void ReadAllChannels(bool special = false)
1125 {
1126 if (!special && fWaitingForAnswer>=0)
1127 {
1128 ostringstream msg;
1129 msg << "ReadAllChannels - Answer on last command (id=" << fWaitingForAnswer << ") not yet received.";
1130 Error(msg);
1131 return;
1132 }
1133
1134 vector<char> data;
1135 data.reserve(kNumChannels*3);
1136
1137 for (int ch=0; ch<kNumChannels; ch++)
1138 {
1139 const vector<char> cmd = GetCmd(kCmdRead, ch);
1140 data.insert(data.end(), cmd.begin(), cmd.end());
1141 }
1142
1143 fSendCounter += kNumChannels;
1144
1145 PostMessage(data);
1146 AsyncRead(ba::buffer(special ? fBufferUpdate : fBuffer, kNumChannels*3),
1147 special ? kUpdate : kCmdRead, fSendCounter);
1148
1149 if (!special)
1150 fWaitingForAnswer = kCmdRead;
1151 }
1152
1153 bool SetReferences(const vector<float> &volt, const vector<float> &offset, const vector<float> &slope)
1154 {
1155 if (volt.size()!=kNumChannels)
1156 {
1157 ostringstream out;
1158 out << "SetReferences - Given vector has " << volt.size() << " elements - expected " << kNumChannels << endl;
1159 Error(out);
1160 return false;
1161 }
1162 if (offset.size()!=kNumChannels)
1163 {
1164 ostringstream out;
1165 out << "SetReferences - Given vector has " << offset.size() << " elements - expected " << kNumChannels << endl;
1166 Error(out);
1167 return false;
1168 }
1169 if (slope.size()!=kNumChannels)
1170 {
1171 ostringstream out;
1172 out << "SetReferences - Given vector has " << slope.size() << " elements - expected " << kNumChannels << endl;
1173 Error(out);
1174 return false;
1175 }
1176
1177 fOperationVoltage = volt;
1178 fCalibrationOffset = offset;
1179 fCalibrationSlope = slope;
1180
1181 UpdateVgapd();
1182
1183 return true;
1184 }
1185
1186 // --------------------------------------------------------------------
1187
1188 void RampStop()
1189 {
1190 fRampTimer.cancel();
1191 fIsRamping = false;
1192
1193 Message("Ramping stopped.");
1194 }
1195
1196 void RampStart()
1197 {
1198 if (fIsRamping)
1199 {
1200 Warn("RampStart - Ramping already in progress... ignored.");
1201 return;
1202 }
1203
1204 fIsRamping = RampOneStep();
1205 }
1206
1207 void SetRampTime(uint16_t val)
1208 {
1209 fRampTime = val;
1210 }
1211
1212 void SetRampStep(uint16_t val)
1213 {
1214 fRampStep = val;
1215 }
1216
1217 uint16_t GetRampStepVolt() const
1218 {
1219 return fRampStep*90./4096;
1220 }
1221
1222 bool IsRamping() const { return fIsRamping; }
1223
1224 // -------------------------------------------------------------------
1225
1226 void ExpertReset(bool expert_mode=true)
1227 {
1228 if (expert_mode && fWaitingForAnswer>=0)
1229 {
1230 ostringstream msg;
1231 msg << "ExpertReset - Answer on last command (id=" << fWaitingForAnswer << ") not yet received.";
1232 Error(msg);
1233 return;
1234 }
1235
1236 if (expert_mode)
1237 Warn("EXPERT MODE: Sending reset.");
1238
1239 PostMessage(GetCmd(kCmdReset));
1240 AsyncRead(ba::buffer(fBuffer, 3), kCmdReset, ++fSendCounter);
1241 fWaitingForAnswer = kCmdReset;
1242 }
1243
1244
1245 bool ExpertChannelSetDac(uint16_t ch, uint16_t dac)
1246 {
1247 if (fWaitingForAnswer>=0)
1248 {
1249 ostringstream msg;
1250 msg << "ExpertChannelSetDac - Answer on last command (id=" << fWaitingForAnswer << ") not yet received.";
1251 Error(msg);
1252 return false;
1253 }
1254
1255 if (!CheckDac(dac))
1256 return false;
1257
1258 fDacCommand[ch] = dac;
1259
1260 ostringstream msg;
1261 msg << "EXPERT MODE: Sending 'ChannelSet' (set ch " << ch << " to DAC=" << dac << ")";
1262 Warn(msg);
1263
1264 // FIXME: dac += calib_offset
1265 PostMessage(GetCmd(kCmdChannelSet, ch, dac));
1266 AsyncRead(ba::buffer(fBuffer, 3), kExpertChannelSet|(ch<<8), ++fSendCounter);
1267 fWaitingForAnswer = kExpertChannelSet|(ch<<8);
1268
1269 return true;
1270 }
1271
1272 bool ExpertChannelSetVolt(uint16_t ch, double volt)
1273 {
1274 return ExpertChannelSetDac(ch, volt*4096/90.);
1275 }
1276
1277 bool ExpertGlobalSetDac(uint16_t dac)
1278 {
1279 if (fWaitingForAnswer>=0)
1280 {
1281 ostringstream msg;
1282 msg << "ExpertGlobalSetDac - Answer on last command (id=" << fWaitingForAnswer << ") not yet received.";
1283 Error(msg);
1284 return false;
1285 }
1286
1287 if (!CheckDac(dac))
1288 return false;
1289
1290 if (fGlobalDacCmd>=0)
1291 {
1292 Error("ExpertGlobalSetDac - Still waiting for previous answer to 'GlobalSet'");
1293 return false;
1294 }
1295
1296 fGlobalDacCmd = dac;
1297
1298 ostringstream msg;
1299 msg << "EXPERT MODE: Sending 'GlobalSet' (DAC=" << dac << ")";
1300 Warn(msg);
1301
1302 PostMessage(GetCmd(kCmdGlobalSet, 0, dac));
1303 AsyncRead(ba::buffer(fBuffer, 3), kCmdGlobalSet, ++fSendCounter);
1304 fWaitingForAnswer = kCmdGlobalSet;
1305
1306 return true;
1307 }
1308
1309 bool ExpertGlobalSetVolt(float volt)
1310 {
1311 return ExpertGlobalSetDac(volt*4096/90);
1312 }
1313
1314 // --------------------------------------------------------------------
1315
1316 void SetVerbose(bool b)
1317 {
1318 fIsVerbose = b;
1319 }
1320
1321 void SetDummyMode(bool b)
1322 {
1323 fIsDummyMode = b;
1324 }
1325
1326 void PrintInfo()
1327 {
1328 Out() << endl << kBold << dec << '\n';
1329 Out() << "fWrapCounter = " << fWrapCounter << '\n';
1330 Out() << "fSendCounter = " << fSendCounter%8 << " (" << fSendCounter << ")" << '\n';
1331 Out() << "fIsInitializing = " << fIsInitializing << '\n';
1332 Out() << "fIsRamping = " << fIsRamping << '\n';
1333 Out() << "Answer counter:" << '\n';
1334 Out() << " - Synchronization: " << fCounter[0] << '\n';
1335 Out() << " - Reset: " << fCounter[1] << '\n';
1336 Out() << " - Request update: " << fCounter[2] << '\n';
1337 Out() << " - Ramp step: " << fCounter[3] << '\n';
1338 Out() << " - Read: " << fCounter[4] << '\n';
1339 Out() << " - Reset channels: " << fCounter[5] << '\n';
1340 Out() << " - Global set: " << fCounter[7] << '\n';
1341 Out() << " - Channel set: " << fCounter[6] << '\n' << endl;
1342 }
1343
1344 void PrintLineA(int b, int ch)
1345 {
1346 Out() << setw(2) << b << "|";
1347
1348 for (int c=ch; c<ch+8; c++)
1349 {
1350 const int id = c+kNumChannelsPerBoard*b;
1351 Out() << (fCurrent[id]<0?kRed:kGreen);
1352 Out() << " " << setw(7) << abs(fCurrent[id])*5000/4096.;
1353 }
1354 Out() << endl;
1355
1356 }
1357
1358 void PrintA()
1359 {
1360 Out() << dec << setprecision(2) << fixed << setfill(' ');
1361 for (int b=0; b<kNumBoards; b++)
1362 {
1363 if (!fPresent[b])
1364 {
1365 Out() << setw(2) << b << "-" << endl;
1366 continue;
1367 }
1368
1369 PrintLineA(b, 0);
1370 PrintLineA(b, 8);
1371 PrintLineA(b, 16);
1372 PrintLineA(b, 24);
1373 }
1374 }
1375
1376 void PrintLineV(int b, int ch)
1377 {
1378 Out() << setw(2) << b << "|";
1379
1380 for (int c=ch; c<ch+4; c++)
1381 {
1382 const int id = c+kNumChannelsPerBoard*b;
1383 Out() << " ";
1384 Out() << (fDacActual[id]==fDacTarget[id]?kGreen:kRed);
1385 //Out() << setw(5) << fDacActual[id]*90/4096. << '/';
1386 //Out() << setw(5) << fDacTarget[id]*90/4096.;
1387
1388 Out() << setw(5) << ConvertDacToVolt(id, fDacActual[id]) << '/';
1389 Out() << setw(5) << ConvertDacToVolt(id, fDacTarget[id]);
1390 }
1391 Out() << endl;
1392 }
1393
1394 void PrintV()
1395 {
1396 Out() << dec << setprecision(2) << fixed << setfill(' ');
1397 for (int b=0; b<kNumBoards; b++)
1398 {
1399 if (!fPresent[b])
1400 {
1401 Out() << setw(2) << b << "-" << endl;
1402 continue;
1403 }
1404
1405 PrintLineV(b, 0);
1406 PrintLineV(b, 4);
1407 PrintLineV(b, 8);
1408 PrintLineV(b, 12);
1409 PrintLineV(b, 16);
1410 PrintLineV(b, 20);
1411 PrintLineV(b, 24);
1412 PrintLineV(b, 28);
1413 }
1414 }
1415
1416 void PrintLineGapd(int b, int ch)
1417 {
1418 Out() << setw(2) << b << "|";
1419
1420 for (int c=ch; c<ch+8; c++)
1421 {
1422 const int id = c+kNumChannelsPerBoard*b;
1423 Out() << " " << setw(5) << fOperationVoltage[id];
1424 }
1425 Out() << endl;
1426 }
1427
1428 void PrintReferenceVoltage()
1429 {
1430 Out() << dec << setprecision(2) << fixed << setfill(' ');
1431 for (int b=0; b<kNumBoards; b++)
1432 {
1433 if (!fPresent[b])
1434 {
1435 Out() << setw(2) << b << "-" << endl;
1436 continue;
1437 }
1438
1439 PrintLineGapd(b, 0);
1440 PrintLineGapd(b, 8);
1441 PrintLineGapd(b, 16);
1442 PrintLineGapd(b, 24);
1443 }
1444 }
1445
1446 // -------------------------------------------------------------------
1447
1448 void SetUpdateInterval(uint32_t val)
1449 {
1450 fUpdateTime = val;
1451
1452 if (!IsConnected() || fIsInitializing)
1453 return;
1454
1455 fUpdateTimer.cancel();
1456
1457 if (fUpdateTime>0)
1458 ScheduleUpdate(fUpdateTime);
1459 }
1460
1461 void SetSyncDelay(uint16_t val)
1462 {
1463 fSyncTime = val;
1464 }
1465
1466 void SetVoltMaxAbs(float max)
1467 {
1468 if (max>90)
1469 max = 90;
1470 if (max<0)
1471 max = 0;
1472
1473 fVoltageMaxAbs = max;
1474 }
1475
1476 void SetVoltMaxRel(float max)
1477 {
1478 if (max>90)
1479 max = 90;
1480 if (max<0)
1481 max = 0;
1482
1483 fVoltageMaxRel = max;
1484 }
1485
1486 uint16_t GetVoltMaxAbs() const
1487 {
1488 return fVoltageMaxAbs;
1489 }
1490
1491 uint16_t GetVoltMaxRel() const
1492 {
1493 return fVoltageMaxRel;
1494 }
1495
1496 State::states_t GetStatus()
1497 {
1498 if (!IsConnected())
1499 return State::kDisconnected;
1500
1501 if (IsConnecting())
1502 return State::kConnecting;
1503
1504 if (fIsInitializing)
1505 return State::kInitializing;
1506
1507 if (fIsRamping)
1508 return State::kRamping;
1509
1510 for (int ch=0; ch<kNumChannels; ch++)
1511 if (fPresent[ch/kNumChannelsPerBoard] && fCurrent[ch]<0)
1512 return State::kOverCurrent;
1513
1514 bool isoff = true;
1515 for (int ch=0; ch<kNumChannels; ch++)
1516 if (fPresent[ch/kNumChannelsPerBoard] && fDacActual[ch]!=0)
1517 isoff = false;
1518 if (isoff)
1519 return State::kVoltageOff;
1520
1521 for (int ch=0; ch<kNumChannels; ch++)
1522 if (fPresent[ch/kNumChannelsPerBoard] && fDacActual[ch]!=fDacTarget[ch])
1523 return State::kNotReferenced;
1524
1525 return State::kVoltageOn;
1526 }
1527
1528 void SetReconnectDelay(uint32_t delay=1)
1529 {
1530 fReconnectDelay = delay;
1531 }
1532
1533 void SetEmergencyLimit(int32_t limit=0)
1534 {
1535 fEmergencyLimit = limit;
1536 }
1537
1538 void ResetEmergencyShutdown()
1539 {
1540 fEmergencyShutdown = false;
1541 }
1542
1543 bool IsEmergencyShutdown() const
1544 {
1545 return fEmergencyShutdown;
1546 }
1547};
1548
1549// ------------------------------------------------------------------------
1550
1551#include "DimDescriptionService.h"
1552
1553class ConnectionDimBias : public ConnectionBias
1554{
1555private:
1556
1557 DimDescribedService fDimCurrent;
1558 DimDescribedService fDimDac;
1559 DimDescribedService fDimVolt;
1560 DimDescribedService fDimGapd;
1561
1562public:
1563 void UpdateVA()
1564 {
1565 const Time now;
1566
1567 UpdateV(now);
1568
1569 fDimCurrent.setTime(now);
1570 fDimCurrent.Update(fCurrent);
1571 }
1572
1573private:
1574 void UpdateV(const Time now=Time())
1575 {
1576 const bool rc = !memcmp(fDacActual.data(), fDacTarget.data(), kNumChannels*2);
1577
1578 vector<uint16_t> val(2*kNumChannels);
1579 memcpy(val.data(), fDacActual.data(), kNumChannels*2);
1580 memcpy(val.data()+kNumChannels, fDacTarget.data(), kNumChannels*2);
1581 fDimDac.setTime(now);
1582 fDimDac.setQuality(rc);
1583 fDimDac.Update(val);
1584
1585 vector<float> volt(kNumChannels);
1586 for (float ch=0; ch<kNumChannels; ch++)
1587 volt[ch] = ConvertDacToVolt(ch, fDacActual[ch]);
1588 fDimVolt.setTime(now);
1589 fDimVolt.setQuality(rc);
1590 fDimVolt.Update(volt);
1591 }
1592
1593 void UpdateVgapd()
1594 {
1595 vector<float> volt;
1596 volt.reserve(3*kNumChannels);
1597 volt.insert(volt.end(), fOperationVoltage.begin(), fOperationVoltage.end());
1598 volt.insert(volt.end(), fCalibrationOffset.begin(), fCalibrationOffset.end());
1599 volt.insert(volt.end(), fCalibrationSlope.begin(), fCalibrationSlope.end());
1600 fDimGapd.Update(volt);
1601 }
1602
1603public:
1604 ConnectionDimBias(ba::io_service& ioservice, MessageImp &imp) :
1605 ConnectionBias(ioservice, imp),
1606 fDimCurrent("BIAS_CONTROL/CURRENT", "S:416",
1607 "|I[dac]:Bias current (conversion: 5000uA/4096dac)"),
1608 fDimDac("BIAS_CONTROL/DAC", "S:416;S:416",
1609 "|U[dac]:Current dac setting"
1610 "|Uref[dac]:Reference dac setting"),
1611 fDimVolt("BIAS_CONTROL/VOLTAGE", "F:416",
1612 "|Uout[V]:Output voltage"),
1613 fDimGapd("BIAS_CONTROL/NOMINAL", "F:416;F:416;F:416",
1614 "|Uop[V]:Nominal operation voltage at 25deg C"
1615 "|Uoff[V]:Bias crate channel calibration offsets"
1616 "|Rcal[Ohm]:Bias crate channel calibration slope")
1617 {
1618 }
1619
1620 // A B [C] [D] E [F] G H [I] J K [L] M N O P Q R [S] T U V W [X] Y Z
1621};
1622
1623// ------------------------------------------------------------------------
1624
1625template <class T, class S>
1626class StateMachineBias : public StateMachineAsio<T>
1627{
1628 int Wrap(boost::function<void()> f)
1629 {
1630 f();
1631 return T::GetCurrentState();
1632 }
1633
1634 function<int(const EventImp &)> Wrapper(function<void()> func)
1635 {
1636 return bind(&StateMachineBias::Wrap, this, func);
1637 }
1638
1639 bool CheckEventSize(size_t has, const char *name, size_t size)
1640 {
1641 if (has==size)
1642 return true;
1643
1644 ostringstream msg;
1645 msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
1646 T::Fatal(msg);
1647 return false;
1648 }
1649
1650private:
1651 S fBias;
1652
1653 bool fExpertMode;
1654
1655 Time fSunRise;
1656
1657 // --------------------------------------------------------------------
1658
1659 // SET_GLOBAL_DAC_VALUE
1660 int SetGlobalDac(const EventImp &evt)
1661 {
1662 if (!CheckEventSize(evt.GetSize(), "SetGlobalDac", 2))
1663 return false;
1664
1665 fBias.RampAllDacs(evt.GetUShort());
1666
1667 return T::GetCurrentState();
1668 }
1669
1670 // SET_CHANNEL_DAC_VALUE
1671 int SetChannelDac(const EventImp &evt)
1672 {
1673 if (!CheckEventSize(evt.GetSize(), "SetChannelDac", 4))
1674 return false;
1675
1676 fBias.RampSingleChannelDac(evt.Get<uint16_t>(), evt.Get<uint16_t>(2));
1677
1678 return T::GetCurrentState();
1679 }
1680
1681 // --------------------------------------------------------------------
1682
1683 // SET_CHANNEL_VOLTAGE
1684 int SetChannelVolt(const EventImp &evt)
1685 {
1686 if (!CheckEventSize(evt.GetSize(), "SetChannelVolt", 6))
1687 return false;
1688
1689 fBias.RampSingleChannelVoltage(evt.GetUShort(), evt.Get<float>(2));
1690
1691 return T::GetCurrentState();
1692 }
1693
1694 // SET_GLOBAL_VOLTAGE
1695 int SetGlobalVolt(const EventImp &evt)
1696 {
1697 if (!CheckEventSize(evt.GetSize(), "SetGlobalVolt", 4))
1698 return false;
1699
1700 fBias.RampAllVoltages(evt.GetFloat());
1701
1702 return T::GetCurrentState();
1703 }
1704
1705 // SET_ALL_CHANNELS_VOLTAGES
1706 int SetAllChannelsVolt(const EventImp &evt)
1707 {
1708 if (!CheckEventSize(evt.GetSize(), "SetAllChannelsVolt", 4*kNumChannels))
1709 return false;
1710
1711 const float *ptr = evt.Ptr<float>();
1712 fBias.RampAllChannelsVoltage(vector<float>(ptr, ptr+kNumChannels));
1713
1714 return T::GetCurrentState();
1715 }
1716
1717 // --------------------------------------------------------------------
1718
1719/* // INCREASE_GLOBAL_VOLTAGE
1720 int IncGlobalVolt(const EventImp &evt)
1721 {
1722 if (!CheckEventSize(evt.GetSize(), "IncGlobalVolt", 4))
1723 return false;
1724
1725 fBias.RampAllOffsets(evt.GetFloat(), true);
1726
1727 return T::GetCurrentState();
1728 }
1729
1730 // INCREASE_CHANNEL_VOLTAGE
1731 int IncChannelVolt(const EventImp &evt)
1732 {
1733 if (!CheckEventSize(evt.GetSize(), "IncChannelVolt", 6))
1734 return false;
1735
1736 fBias.RampSingleChannelOffset(evt.Get<uint16_t>(), evt.Get<float>(2), true);
1737
1738 return T::GetCurrentState();
1739 }
1740
1741 // INCREASE_ALL_CHANNELS_VOLTAGES
1742 int IncAllChannelsVolt(const EventImp &evt)
1743 {
1744 if (!CheckEventSize(evt.GetSize(), "IncAllChannelsVolt", 4*kNumChannels))
1745 return false;
1746
1747 const float *ptr = evt.Ptr<float>();
1748 fBias.RampAllChannelsOffset(vector<float>(ptr, ptr+416), true);
1749
1750 return T::GetCurrentState();
1751 }
1752*/
1753 // --------------------------------------------------------------------
1754
1755 int ExpertSetGlobalVolt(const EventImp &evt)
1756 {
1757 if (!CheckEventSize(evt.GetSize(), "ExpertSetGlobalVolt", 4))
1758 return false;
1759
1760 fBias.ExpertGlobalSetVolt(evt.GetFloat());
1761
1762 return T::GetCurrentState();
1763 }
1764
1765 int ExpertSetGlobalDac(const EventImp &evt)
1766 {
1767 if (!CheckEventSize(evt.GetSize(), "ExpertSetGlobalDac", 2))
1768 return false;
1769
1770 fBias.ExpertGlobalSetDac(evt.GetUShort());
1771
1772 return T::GetCurrentState();
1773 }
1774
1775 int ExpertSetChannelVolt(const EventImp &evt)
1776 {
1777 if (!CheckEventSize(evt.GetSize(), "ExpertSetChannelVolt", 6))
1778 return false;
1779
1780 fBias.ExpertChannelSetVolt(evt.GetUShort(), evt.Get<float>(2));
1781
1782 return T::GetCurrentState();
1783 }
1784
1785 int ExpertSetChannelDac(const EventImp &evt)
1786 {
1787 if (!CheckEventSize(evt.GetSize(), "ExpertSetChannelDac", 4))
1788 return false;
1789
1790 fBias.ExpertChannelSetDac(evt.Get<uint16_t>(), evt.Get<uint16_t>(2));
1791
1792 return T::GetCurrentState();
1793 }
1794
1795 int ExpertLoadMapFile(const EventImp &evt)
1796 {
1797 if (evt.GetSize()==0)
1798 {
1799 T::Warn("ExpertLoadMapFile - No file name given.");
1800 return T::GetCurrentState();
1801 }
1802
1803 if (fBias.GetStatus()!=State::kVoltageOff)
1804 {
1805 T::Warn("ExpertLoadMapFile - Voltage must have been turned off.");
1806 return T::GetCurrentState();
1807 }
1808
1809 BiasMap map;
1810
1811 try
1812 {
1813 map.Read(evt.GetText());
1814 }
1815 catch (const runtime_error &e)
1816 {
1817 T::Warn("Getting reference voltages failed: "+string(e.what()));
1818 return T::GetCurrentState();
1819 }
1820
1821 if (!fBias.SetReferences(map.Vgapd(), map.Voffset(), map.Vslope()))
1822 {
1823 T::Warn("Setting reference voltages failed.");
1824 return T::GetCurrentState();
1825 }
1826
1827 fBias.UpdateVA();
1828
1829 T::Info("Successfully loaded new mapping '"+evt.GetString()+"'");
1830
1831 return T::GetCurrentState();
1832 }
1833
1834 // --------------------------------------------------------------------
1835
1836 int SetUpdateInterval(const EventImp &evt)
1837 {
1838 if (!CheckEventSize(evt.GetSize(), "SetUpdateInterval", 4))
1839 return false;
1840
1841 fBias.SetUpdateInterval(evt.Get<int32_t>()<0 ? 0 : evt.Get<uint32_t>());
1842
1843 return T::GetCurrentState();
1844 }
1845
1846 int Disconnect()
1847 {
1848 // Close all connections
1849 fBias.PostClose(-1);
1850
1851 /*
1852 // Now wait until all connection have been closed and
1853 // all pending handlers have been processed
1854 poll();
1855 */
1856
1857 return T::GetCurrentState();
1858 }
1859
1860 int Reconnect(const EventImp &evt)
1861 {
1862 // Close all connections to supress the warning in SetEndpoint
1863 fBias.PostClose(-1);
1864
1865 // Now wait until all connection have been closed and
1866 // all pending handlers have been processed
1867 ba::io_service::poll();
1868
1869 if (evt.GetBool())
1870 fBias.SetEndpoint(evt.GetString());
1871
1872 // Now we can reopen the connection
1873 fBias.SetReconnectDelay();
1874 fBias.PostClose(0);
1875
1876 return T::GetCurrentState();
1877 }
1878
1879 int SetVerbosity(const EventImp &evt)
1880 {
1881 if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
1882 return T::kSM_FatalError;
1883
1884 fBias.SetVerbose(evt.GetBool());
1885
1886 return T::GetCurrentState();
1887 }
1888
1889 int SetDummyMode(const EventImp &evt)
1890 {
1891 if (!CheckEventSize(evt.GetSize(), "SetDummyMode", 1))
1892 return T::kSM_FatalError;
1893
1894 fBias.SetDummyMode(evt.GetBool());
1895
1896 return T::GetCurrentState();
1897 }
1898
1899 int SetExpertMode(const EventImp &evt)
1900 {
1901 if (!CheckEventSize(evt.GetSize(), "SetExpertMode", 1))
1902 return T::kSM_FatalError;
1903
1904 fExpertMode = evt.GetBool();
1905
1906 if (fExpertMode)
1907 T::Warn("Expert commands enabled -- please ensure that you EXACTLY know what you do. These commands can destroy the system.");
1908
1909 return T::GetCurrentState();
1910 }
1911
1912 int Shutdown(const string &reason)
1913 {
1914 fBias.RampAllDacs(0);
1915 T::Info("Emergency shutdown initiated ["+reason+"].");
1916 return State::kLocked;
1917 }
1918
1919 int Unlock()
1920 {
1921 fBias.ResetEmergencyShutdown();
1922 return fBias.GetStatus();
1923 }
1924
1925 int Execute()
1926 {
1927 const int state = fBias.GetStatus();
1928
1929 if (fBias.IsEmergencyShutdown()/* && state>State::kInitializing && state<State::kExpertMode*/)
1930 {
1931 // This needs to be repeated for the case that in between a different command was processed
1932 fBias.RampAllDacs(0);
1933 return State::kLocked;
1934 }
1935
1936 const Time now;
1937 if (now>fSunRise)
1938 {
1939 const bool shutdown =
1940 state==State::kRamping ||
1941 state==State::kVoltageOn ||
1942 state==State::kNotReferenced ||
1943 state==State::kOverCurrent;
1944
1945 if (shutdown)
1946 Shutdown("beginning of civil twilight");
1947
1948 fSunRise = now.GetNextSunRise(-6);
1949
1950 ostringstream msg;
1951 msg << "During next sun-rise nautical twilight will end at " << fSunRise;
1952 T::Info(msg);
1953
1954 if (shutdown)
1955 return State::kLocked;
1956 }
1957
1958 if (T::GetCurrentState()==State::kLocked)
1959 return T::GetCurrentState();
1960
1961 if (fExpertMode && state>=State::kConnected)
1962 return State::kExpertMode;
1963
1964 return state;
1965 }
1966
1967public:
1968 StateMachineBias(ostream &out=cout) :
1969 StateMachineAsio<T>(out, "BIAS_CONTROL"), fBias(*this, *this),
1970 fExpertMode(false), fSunRise(Time().GetNextSunRise(-6))
1971 {
1972 // State names
1973 T::AddStateName(State::kDisconnected, "Disconnected",
1974 "Bias-power supply not connected via USB.");
1975
1976 T::AddStateName(State::kConnecting, "Connecting",
1977 "Trying to establish USB connection to bias-power supply.");
1978
1979 T::AddStateName(State::kInitializing, "Initializing",
1980 "USB connection to bias-power supply established, synchronizing USB stream.");
1981
1982 T::AddStateName(State::kConnected, "Connected",
1983 "USB connection to bias-power supply established.");
1984
1985 T::AddStateName(State::kNotReferenced, "NotReferenced",
1986 "Internal reference voltage does not match last sent voltage.");
1987
1988 T::AddStateName(State::kVoltageOff, "VoltageOff",
1989 "All voltages are supposed to be switched off.");
1990
1991 T::AddStateName(State::kVoltageOn, "VoltageOn",
1992 "At least one voltage is switched on and all are at reference.");
1993
1994 T::AddStateName(State::kOverCurrent, "OverCurrent",
1995 "At least one channel is in over current state.");
1996
1997 T::AddStateName(State::kExpertMode, "ExpertMode",
1998 "Special (risky!) mode to directly send command to the bias-power supply.");
1999
2000 T::AddStateName(State::kRamping, "Ramping",
2001 "Voltage ramping in progress.");
2002
2003 T::AddStateName(State::kLocked, "Locked",
2004 "Locked due to emergency shutdown, no commands accepted except UNLOCK.");
2005
2006 // Verbosity commands
2007 T::AddEvent("SET_VERBOSE", "B:1")
2008 (bind(&StateMachineBias::SetVerbosity, this, placeholders::_1))
2009 ("set verbosity state"
2010 "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
2011
2012 T::AddEvent("ENABLE_DUMMY_MODE", "B:1")
2013 (bind(&StateMachineBias::SetDummyMode, this, placeholders::_1))
2014 ("Enable dummy mode. In this mode SetAllChannels prints informations instead of sending anything to the bias crate."
2015 "|enable[bool]:disable or enable dummy mode");
2016
2017 // Conenction commands
2018 T::AddEvent("DISCONNECT", State::kConnected, State::kVoltageOff)
2019 (bind(&StateMachineBias::Disconnect, this))
2020 ("disconnect from USB");
2021 T::AddEvent("RECONNECT", "O", State::kDisconnected, State::kConnected, State::kVoltageOff)
2022 (bind(&StateMachineBias::Reconnect, this, placeholders::_1))
2023 ("(Re)connect USB connection to Bias power supply, a new address can be given"
2024 "|tty[string]:new USB address");
2025
2026
2027 T::AddEvent("SET_UPDATE_INTERVAL", "I:1")
2028 (bind(&StateMachineBias::SetUpdateInterval, this, placeholders::_1))
2029 ("Set the updat einterval how often the currents are requested"
2030 "|interval[ms]:Update interval in milliseconds");
2031
2032
2033
2034 T::AddEvent("REQUEST_STATUS", State::kConnected, State::kVoltageOn, State::kVoltageOff, State::kNotReferenced, State::kOverCurrent)
2035 (Wrapper(bind(&ConnectionBias::ReadAllChannels, &fBias, false)))
2036 ("Asynchronously request the status (current) of all channels.");
2037
2038 T::AddEvent("RESET_OVER_CURRENT_STATUS", State::kOverCurrent)
2039 (Wrapper(bind(&ConnectionBias::OverCurrentReset, &fBias)))
2040 ("Set all channels in over current state to 0V and send a system reset to reset the over current flags.");
2041
2042
2043 T::AddEvent("SET_CHANNEL_DAC", "S:1;S:1")(State::kConnected)(State::kVoltageOff)(State::kVoltageOn)(State::kNotReferenced)(State::kOverCurrent)(State::kRamping)
2044 (bind(&StateMachineBias::SetChannelDac, this, placeholders::_1))
2045 ("Set a new target value in DAC counts for a single channel. Starts ramping if necessary."
2046 "|channel[short]:Channel for which to set the target voltage [0-415]"
2047 "|voltage[dac]:Target voltage in DAC units for the given channel");
2048 T::AddEvent("SET_GLOBAL_DAC", "S:1")(State::kConnected)(State::kVoltageOff)(State::kVoltageOn)(State::kNotReferenced)(State::kOverCurrent)(State::kRamping)
2049 (bind(&StateMachineBias::SetGlobalDac, this, placeholders::_1))
2050 ("Set a new target value for all channels in DAC counts. Starts ramping if necessary. (This command is not realized with the GLOBAL SET command.)"
2051 "|voltage[dac]:Global target voltage in DAC counts.");
2052
2053
2054 T::AddEvent("SET_CHANNEL_VOLTAGE", "S:1;F:1")(State::kConnected)(State::kVoltageOff)(State::kVoltageOn)(State::kNotReferenced)(State::kOverCurrent)(State::kRamping)
2055 (bind(&StateMachineBias::SetChannelVolt, this, placeholders::_1))
2056 ("Set a new target voltage for a single channel. Starts ramping if necessary."
2057 "|channel[short]:Channel for which to set the target voltage [0-415]"
2058 "|voltage[V]:Target voltage in volts for the given channel (will be converted to DAC units)");
2059 T::AddEvent("SET_GLOBAL_VOLTAGE", "F:1")(State::kConnected)(State::kVoltageOff)(State::kVoltageOn)(State::kNotReferenced)(State::kOverCurrent)(State::kRamping)
2060 (bind(&StateMachineBias::SetGlobalVolt, this, placeholders::_1))
2061 ("Set a new target voltage for all channels. Starts ramping if necessary. (This command is not realized with the GLOBAL SET command.)"
2062 "|voltage[V]:Global target voltage in volts (will be converted to DAC units)");
2063 T::AddEvent("SET_ALL_CHANNELS_VOLTAGE", "F:416")(State::kConnected)(State::kVoltageOff)(State::kVoltageOn)(State::kNotReferenced)(State::kOverCurrent)(State::kRamping)
2064 (bind(&StateMachineBias::SetAllChannelsVolt, this, placeholders::_1))
2065 ("Set all channels to the given new reference voltage. Starts ramping if necessary."
2066 "|voltage[V]:New reference voltage for all channels");
2067
2068/*
2069 T::AddEvent("INCREASE_CHANNEL_VOLTAGE", "S:1;F:1", State::kConnected, State::kVoltageOff, State::kVoltageOn, State::kNotReferenced, State::kOverCurrent)
2070 (bind(&StateMachineBias::IncChannelVolt, this, placeholders::_1))
2071 ("Increases the voltage of all channels by the given offset. Starts ramping if necessary. (This command is not realized with the GLOBAL SET command.)"
2072 "|channel[short]:Channel for which to adapt the voltage [0-415]"
2073 "|offset[V]:Offset to be added to all channels (will be converted to DAC counts)");
2074 T::AddEvent("INCREASE_GLOBAL_VOLTAGE", "F:1", State::kConnected, State::kVoltageOff, State::kVoltageOn, State::kNotReferenced, State::kOverCurrent)
2075 (bind(&StateMachineBias::IncGlobalVolt, this, placeholders::_1))
2076 ("Increases the voltage of all channels by the given offset. Starts ramping if necessary. (This command is not realized with the GLOBAL SET command.)"
2077 "|offset[V]:Offset to be added to all channels (will be converted to DAC counts)");
2078 T::AddEvent("INCREASE_ALL_CHANNELS_VOLTAGE", "F:416", State::kConnected, State::kVoltageOff, State::kVoltageOn, State::kNotReferenced, State::kOverCurrent)
2079 (bind(&StateMachineBias::IncAllChannelsVolt, this, placeholders::_1))
2080 ("Add the given voltages to the current reference voltages. Starts ramping if necessary."
2081 "offset[V]:Offsets to be added to the reference voltage of all channels in volts");
2082*/
2083
2084
2085
2086 T::AddEvent("SET_ZERO_VOLTAGE")(State::kConnected)(State::kVoltageOff)(State::kVoltageOn)(State::kNotReferenced)(State::kOverCurrent)(State::kRamping)
2087 (Wrapper(bind(&ConnectionBias::RampAllDacs, &fBias, 0)))
2088 ("Set all channels to a zero reference voltage. Starts ramping if necessary.");
2089 T::AddEvent("SHUTDOWN")(State::kConnected)(State::kVoltageOff)(State::kVoltageOn)(State::kNotReferenced)(State::kOverCurrent)(State::kRamping)
2090 (bind(&StateMachineBias::Shutdown, this, "user request"))
2091 ("Same as SET_ZERO_VOLTAGE; but goes to locked state afterwards.");
2092
2093 T::AddEvent("UNLOCK", State::kLocked)
2094 (bind(&StateMachineBias::Unlock, this))
2095 ("Unlock if in locked state.");
2096
2097
2098
2099
2100
2101 T::AddEvent("STOP", State::kConnected, State::kRamping)
2102 (Wrapper(bind(&ConnectionBias::RampStop, &fBias)))
2103 ("Stop an on-going ramping");
2104
2105 T::AddEvent("START", State::kConnected, State::kNotReferenced)
2106 (Wrapper(bind(&ConnectionBias::RampStart, &fBias)))
2107 ("Start a ramping if no ramping is in progress and if reference values differ from current voltages");
2108
2109
2110
2111 T::AddEvent("PRINT_INFO")
2112 (Wrapper(bind(&ConnectionBias::PrintInfo, &fBias)))
2113 ("Print a table with all current read back with the last request operation");
2114 T::AddEvent("PRINT_CURRENTS")
2115 (Wrapper(bind(&ConnectionBias::PrintA, &fBias)))
2116 ("Print a table with all current read back with the last request operation");
2117 T::AddEvent("PRINT_VOLTAGES")
2118 (Wrapper(bind(&ConnectionBias::PrintV, &fBias)))
2119 ("Print a table with all voltages (current and reference voltages as currently in memory)");
2120 T::AddEvent("PRINT_GAPD_REFERENCE_VOLTAGES")
2121 (Wrapper(bind(&ConnectionBias::PrintReferenceVoltage, &fBias)))
2122 ("Print the G-APD reference values (breakdown voltage + overvoltage) obtained from file");
2123
2124
2125 T::AddEvent("EXPERT_MODE", "B:1")
2126 (bind(&StateMachineBias::SetExpertMode, this, placeholders::_1))
2127 ("Enable usage of expert commands (note that for safty reasons the are exclusive with the standard commands)");
2128
2129 T::AddEvent("EXPERT_RESET", State::kExpertMode)
2130 (Wrapper(bind(&ConnectionBias::ExpertReset, &fBias, true)))
2131 ("Send the RESET command (note that this is possibly harmfull command)");
2132
2133 T::AddEvent("EXPERT_SET_GLOBAL_VOLTAGE", "F:1", State::kExpertMode)
2134 (bind(&StateMachineBias::ExpertSetGlobalVolt, this, placeholders::_1))
2135 ("Send the global set command. The given voltage is converted to DAC counts.");
2136
2137 T::AddEvent("EXPERT_SET_GLOBAL_DAC", "S:1", State::kExpertMode)
2138 (bind(&StateMachineBias::ExpertSetGlobalDac, this, placeholders::_1))
2139 ("Send the global set command.");
2140
2141 T::AddEvent("EXPERT_SET_CHANNEL_VOLTAGE", "S:1;F:1", State::kExpertMode)
2142 (bind(&StateMachineBias::ExpertSetChannelVolt, this, placeholders::_1))
2143 ("Send a single channel set command. The given voltage is converted to DAC commands.");
2144
2145 T::AddEvent("EXPERT_SET_CHANNEL_DAC", "S:1;S:1", State::kExpertMode)
2146 (bind(&StateMachineBias::ExpertSetChannelDac, this, placeholders::_1))
2147 ("Send a single channel set command.");
2148
2149 T::AddEvent("EXPERT_LOAD_MAP_FILE", "C", State::kExpertMode)
2150 (bind(&StateMachineBias::ExpertLoadMapFile, this, placeholders::_1))
2151 ("Load a new mapping file.");
2152 }
2153
2154 ~StateMachineBias() { T::Warn("TODO: Implement rampming at shutdown!"); }
2155
2156 int EvalOptions(Configuration &conf)
2157 {
2158 // FIXME: Read calib_offset
2159 // FIXME: Check calib offset being smaller than +/-0.25V
2160
2161 fBias.SetVerbose(!conf.Get<bool>("quiet"));
2162 fBias.SetDummyMode(conf.Get<bool>("dummy-mode"));
2163
2164 if (conf.Has("dev"))
2165 {
2166 fBias.SetEndpoint(conf.Get<string>("dev"));
2167 T::Message("Setting device to "+fBias.URL());
2168 }
2169
2170 const uint16_t step = conf.Get<uint16_t>("ramp-step");
2171 const uint16_t time = conf.Get<uint16_t>("ramp-delay");
2172
2173 if (step>230) // 5V
2174 {
2175 T::Error("ramp-step exceeds allowed range.");
2176 return 1;
2177 }
2178
2179 fBias.SetRampStep(step);
2180 fBias.SetRampTime(time);
2181 fBias.SetUpdateInterval(conf.Get<uint32_t>("update-interval"));
2182 fBias.SetEmergencyLimit(conf.Get<uint16_t>("emergency-limit"));
2183 fBias.SetSyncDelay(conf.Get<uint16_t>("sync-delay"));
2184
2185 ostringstream str1, str2;
2186 str1 << "Ramping in effective steps of " << fBias.GetRampStepVolt() << "V";
2187 str2 << "Ramping with a delay per step of " << time << "ms";
2188 T::Message(str1);
2189 T::Message(str2);
2190
2191 // --------------------------------------------------------------------------
2192
2193 const float maxabsv = conf.Get<float>("volt-max-abs");
2194 const float maxrelv = conf.Get<float>("volt-max-rel");
2195 if (maxabsv>90)
2196 {
2197 T::Error("volt-max exceeds 90V.");
2198 return 2;
2199 }
2200 if (maxabsv>75)
2201 T::Warn("volt-max exceeds 75V.");
2202 if (maxabsv<70)
2203 T::Warn("volt-max below 70V.");
2204 if (maxabsv<0)
2205 {
2206 T::Error("volt-max negative.");
2207 return 3;
2208 }
2209
2210 fBias.SetVoltMaxAbs(maxabsv);
2211 fBias.SetVoltMaxRel(maxrelv);
2212
2213 ostringstream str3, str4;
2214 str3 << "Effective maximum allowed absolute voltage: " << fBias.GetVoltMaxAbs() << "V";
2215 str4 << "Effective maximum difference w.r.t to G-APD reference: " << fBias.GetVoltMaxRel() << "V";
2216 T::Message(str3);
2217 T::Message(str4);
2218
2219 // --------------------------------------------------------------------------
2220
2221 BiasMap map;
2222
2223 if (!conf.Has("bias-map-file") && !conf.Has("bias-database"))
2224 {
2225 T::Error("Neither bias-map-file not bias-database specified.");
2226 return 5;
2227 }
2228
2229 try
2230 {
2231 if (conf.Has("bias-map-file"))
2232 map.Read(conf.Get<string>("bias-map-file"));
2233
2234 //if (conf.Has("bias-database"))
2235 // map.Retrieve(conf.Get<string>("bias-database"));
2236 }
2237 catch (const runtime_error &e)
2238 {
2239 T::Error("Getting reference voltages failed: "+string(e.what()));
2240 return 7;
2241 }
2242
2243 if (!fBias.SetReferences(map.Vgapd(), map.Voffset(), map.Vslope()))
2244 {
2245 T::Error("Setting reference voltages failed.");
2246 return 8;
2247 }
2248
2249 // --------------------------------------------------------------------------
2250
2251 if (conf.Has("dev"))
2252 fBias.Connect();
2253
2254 return -1;
2255 }
2256};
2257
2258// ------------------------------------------------------------------------
2259
2260#include "Main.h"
2261
2262template<class T, class S, class R>
2263int RunShell(Configuration &conf)
2264{
2265 return Main::execute<T, StateMachineBias<S, R>>(conf);
2266}
2267
2268void SetupConfiguration(Configuration &conf)
2269{
2270 po::options_description control("BIAS control options");
2271 control.add_options()
2272 ("no-dim,d", po_bool(), "Disable dim services")
2273 ("dev", var<string>(), "Device address of USB port to bias-power supply")
2274 ("quiet,q", po_bool(true), "Disable printing contents of all received messages (except dynamic data) in clear text.")
2275 ("dummy-mode", po_bool(), "Dummy mode - SetAllChannels prints info instead of sending new values.")
2276 ("ramp-delay", var<uint16_t>(15), "Delay between the answer of one ramping step and sending the next ramp command to all channels in milliseconds.")
2277 ("ramp-step", var<uint16_t>(46), "Maximum step in DAC counts during ramping (Volt = DAC*90/4096)")
2278 ("update-interval", var<uint32_t>(3000), "Interval between two current requests in milliseconds")
2279 ("sync-delay", var<uint16_t>(500), "Delay between sending the inital 0's after a newly established connection to synchronize the output stream in milliseconds")
2280 ("volt-max-abs", var<float>(75), "Absolte upper limit for the voltage (in Volts)")
2281 ("volt-max-rel", var<float>(3.5), "Relative upper limit for the voltage w.r.t. the G-APD reference voltage (in Volts)")
2282 ("bias-map-file", var<string>(), "File with nominal and offset voltages for each channel.")
2283 ("bias-database", var<string>(), "")
2284 ("emergency-limit", var<uint16_t>(2200), "A current limit in ADC counts which, if exceeded, will initiate an emergency shutdown (0=off)")
2285 ;
2286
2287 conf.AddOptions(control);
2288}
2289
2290/*
2291 Extract usage clause(s) [if any] for SYNOPSIS.
2292 Translators: "Usage" and "or" here are patterns (regular expressions) which
2293 are used to match the usage synopsis in program output. An example from cp
2294 (GNU coreutils) which contains both strings:
2295 Usage: cp [OPTION]... [-T] SOURCE DEST
2296 or: cp [OPTION]... SOURCE... DIRECTORY
2297 or: cp [OPTION]... -t DIRECTORY SOURCE...
2298 */
2299void PrintUsage()
2300{
2301 cout <<
2302 "The biasctrl program controls the bias-power supply boards.\n"
2303 "\n"
2304 "Note: At default the program is started without a command line (user) "
2305 "interface. In this case Actions/Commands are available via Dim "
2306 "exclusively.\n"
2307 "Use the -c option to start the program with a command line interface.\n"
2308 "\n"
2309 "In the running application:\n"
2310 "Use h or help to print a short help message about its usage.\n"
2311 "\n"
2312 "Usage: biasctrl [-c type] [OPTIONS]\n"
2313 " or: biasctrl [OPTIONS]\n";
2314 cout << endl;
2315}
2316
2317void PrintHelp()
2318{
2319 Main::PrintHelp<StateMachineBias<StateMachine,ConnectionBias>>();
2320
2321 /* Additional help text which is printed after the configuration
2322 options goes here */
2323
2324 /*
2325 cout << "bla bla bla" << endl << endl;
2326 cout << endl;
2327 cout << "Environment:" << endl;
2328 cout << "environment" << endl;
2329 cout << endl;
2330 cout << "Examples:" << endl;
2331 cout << "test exam" << endl;
2332 cout << endl;
2333 cout << "Files:" << endl;
2334 cout << "files" << endl;
2335 cout << endl;
2336 */
2337}
2338
2339int main(int argc, const char* argv[])
2340{
2341 Configuration conf(argv[0]);
2342 conf.SetPrintUsage(PrintUsage);
2343 Main::SetupConfiguration(conf);
2344 SetupConfiguration(conf);
2345
2346 if (!conf.DoParse(argc, argv, PrintHelp))
2347 return 127;
2348
2349 //try
2350 {
2351 // No console access at all
2352 if (!conf.Has("console"))
2353 {
2354 if (conf.Get<bool>("no-dim"))
2355 return RunShell<LocalStream, StateMachine, ConnectionBias>(conf);
2356 else
2357 return RunShell<LocalStream, StateMachineDim, ConnectionDimBias>(conf);
2358 }
2359 // Cosole access w/ and w/o Dim
2360 if (conf.Get<bool>("no-dim"))
2361 {
2362 if (conf.Get<int>("console")==0)
2363 return RunShell<LocalShell, StateMachine, ConnectionBias>(conf);
2364 else
2365 return RunShell<LocalConsole, StateMachine, ConnectionBias>(conf);
2366 }
2367 else
2368 {
2369 if (conf.Get<int>("console")==0)
2370 return RunShell<LocalShell, StateMachineDim, ConnectionDimBias>(conf);
2371 else
2372 return RunShell<LocalConsole, StateMachineDim, ConnectionDimBias>(conf);
2373 }
2374 }
2375 /*catch (std::exception& e)
2376 {
2377 cerr << "Exception: " << e.what() << endl;
2378 return -1;
2379 }*/
2380
2381 return 0;
2382}
Note: See TracBrowser for help on using the repository browser.