source: trunk/FACT++/src/drivectrl.cc@ 15207

Last change on this file since 15207 was 15206, checked in by tbretz, 12 years ago
Make use of the new GetNextSunRise() function of the Time-class
File size: 49.1 KB
Line 
1#include <boost/bind.hpp>
2#include <boost/regex.hpp>
3
4#ifdef HAVE_LIBNOVA
5#include <libnova/solar.h>
6#include <libnova/rise_set.h>
7#endif
8
9#ifdef HAVE_SQL
10#include "Database.h"
11#endif
12
13#include "FACT.h"
14#include "Dim.h"
15#include "Event.h"
16#include "Shell.h"
17#include "StateMachineDim.h"
18#include "Connection.h"
19#include "LocalControl.h"
20#include "Configuration.h"
21#include "Timers.h"
22#include "Console.h"
23
24#include "HeadersDrive.h"
25
26namespace ba = boost::asio;
27namespace bs = boost::system;
28namespace dummy = ba::placeholders;
29
30using namespace std;
31using namespace Drive;
32
33// ------------------------------------------------------------------------
34
35class ConnectionDrive : public Connection
36{
37 int fState;
38
39 bool fIsVerbose;
40
41 // --verbose
42 // --hex-out
43 // --dynamic-out
44 // --load-file
45 // --leds
46 // --trigger-interval
47 // --physcis-coincidence
48 // --calib-coincidence
49 // --physcis-window
50 // --physcis-window
51 // --trigger-delay
52 // --time-marker-delay
53 // --dead-time
54 // --clock-conditioner-r0
55 // --clock-conditioner-r1
56 // --clock-conditioner-r8
57 // --clock-conditioner-r9
58 // --clock-conditioner-r11
59 // --clock-conditioner-r13
60 // --clock-conditioner-r14
61 // --clock-conditioner-r15
62 // ...
63
64 virtual void UpdatePointing(const Time &, const array<double, 2> &)
65 {
66 }
67
68 virtual void UpdateTracking(const Time &, const array<double, 8> &)
69 {
70 }
71
72 virtual void UpdateStatus(const Time &, const array<uint8_t, 3> &)
73 {
74 }
75
76 virtual void UpdateStarguider(const Time &, const DimStarguider &)
77 {
78 }
79
80 virtual void UpdateTPoint(const Time &, const DimTPoint &, const string &)
81 {
82 }
83
84public:
85 virtual void UpdateSource()
86 {
87 }
88 virtual void UpdateSource(const array<double, 6> &, const string& = "")
89 {
90 }
91
92protected:
93 map<uint16_t, int> fCounter;
94
95 ba::streambuf fBuffer;
96
97public:
98 static Time ReadTime(istream &in)
99 {
100 uint16_t y, m, d, hh, mm, ss, ms;
101 in >> y >> m >> d >> hh >> mm >> ss >> ms;
102
103 return Time(y, m, d, hh, mm, ss, ms*1000);
104 }
105
106 static double ReadAngle(istream &in)
107 {
108 char sgn;
109 uint16_t d, m;
110 float s;
111
112 in >> sgn >> d >> m >> s;
113
114 const double ret = ((60.0 * (60.0 * (double)d + (double)m) + s))/3600.;
115 return sgn=='-' ? -ret : ret;
116 }
117
118 double GetDevAbs(double nomzd, double meszd, double devaz)
119 {
120 nomzd *= M_PI/180;
121 meszd *= M_PI/180;
122 devaz *= M_PI/180;
123
124 const double x = sin(meszd) * sin(nomzd) * cos(devaz);
125 const double y = cos(meszd) * cos(nomzd);
126
127 return acos(x + y) * 180/M_PI;
128 }
129
130 uint16_t fDeviationLimit;
131 uint16_t fDeviationCounter;
132 uint16_t fDeviationMax;
133
134 uint64_t fTrackingCounter;
135
136protected:
137 void HandleReceivedReport(const boost::system::error_code& err, size_t bytes_received)
138 {
139 // Do not schedule a new read if the connection failed.
140 if (bytes_received==0 || err)
141 {
142 if (err==ba::error::eof)
143 Warn("Connection closed by remote host (FTM).");
144
145 // 107: Transport endpoint is not connected (bs::error_code(107, bs::system_category))
146 // 125: Operation canceled
147 if (err && err!=ba::error::eof && // Connection closed by remote host
148 err!=ba::error::basic_errors::not_connected && // Connection closed by remote host
149 err!=ba::error::basic_errors::operation_aborted) // Connection closed by us
150 {
151 ostringstream str;
152 str << "Reading from " << URL() << ": " << err.message() << " (" << err << ")";// << endl;
153 Error(str);
154 }
155 PostClose(err!=ba::error::basic_errors::operation_aborted);
156 return;
157 }
158
159 istream is(&fBuffer);
160
161 string line;
162 getline(is, line);
163
164 if (fIsVerbose)
165 Out() << line << endl;
166
167 StartReadReport();
168
169 if (line.substr(0, 13)=="DRIVE-STATUS ")
170 {
171 Message(line.substr(70));
172 return;
173 }
174
175 if (line.substr(0, 13)=="STARG-REPORT ")
176 {
177 istringstream stream(line.substr(16));
178
179 // 0: Error
180 // 1: Standby
181 // 2: Monitoring
182 uint16_t status1;
183 stream >> status1;
184 /*const Time t1 = */ReadTime(stream);
185
186 uint16_t status2;
187 stream >> status2;
188 /*const Time t2 = */ReadTime(stream);
189
190 double misszd, missaz;
191 stream >> misszd >> missaz;
192
193 const double zd = ReadAngle(stream);
194 const double az = ReadAngle(stream);
195
196 double cx, cy;
197 stream >> cx >> cy;
198
199 int ncor;
200 stream >> ncor;
201
202 double bright, mjd;
203 stream >> bright >> mjd;
204
205 int nled, nring, nstars;
206 stream >> nled >> nring >> nstars;
207
208 if (stream.fail())
209 return;
210
211 DimStarguider data;
212
213 data.fMissZd = misszd;
214 data.fMissAz = missaz;
215 data.fNominalZd = zd;
216 data.fNominalAz = az;
217 data.fCenterX = cx;
218 data.fCenterY = cy;
219 data.fNumCorrelated = ncor;
220 data.fBrightness = bright;
221 data.fNumLeds = nled;
222 data.fNumRings = nring;
223 data.fNumStars = nstars;
224
225 UpdateStarguider(Time(mjd), data);
226
227 return;
228
229 }
230
231 if (line.substr(0, 14)=="TPOINT-REPORT ")
232 {
233 istringstream stream(line.substr(17));
234
235 uint16_t status1;
236 stream >> status1;
237 const Time t1 = ReadTime(stream);
238
239 uint16_t status2;
240 stream >> status2;
241 /*const Time t2 =*/ ReadTime(stream);
242
243 char type;
244 stream >> type;
245 if (type != 'T')
246 return;
247
248 double az1, alt1, az2, alt2, ra, dec, dzd, daz;
249 stream >> az1 >> alt1 >> az2 >> alt2 >> ra >> dec >> dzd >> daz;
250
251 // c: center, s:start
252 double mjd, cmag, smag, cx, cy, sx, sy;
253 stream >> mjd >> cmag >> smag >> cx >> cy >> sx >> sy;
254
255 int nled, nring, nstar, ncor;
256 stream >> nled >> nring >> nstar >> ncor;
257
258 double bright, mag;
259 stream >> bright >> mag;
260
261 string name;
262 stream >> name;
263
264 if (stream.fail())
265 return;
266
267 DimTPoint tpoint;
268
269 tpoint.fRa = ra;
270 tpoint.fDec = dec;
271
272 tpoint.fNominalZd = 90-alt1-dzd;
273 tpoint.fNominalAz = 90-az1 +daz;
274
275 tpoint.fPointingZd = 90-alt1;
276 tpoint.fPointingAz = az1;
277
278 tpoint.fFeedbackZd = 90-alt2;
279 tpoint.fFeedbackAz = az2;
280
281 tpoint.fNumLeds = nled;
282 tpoint.fNumRings = nring;
283
284 tpoint.fCenterX = cx;
285 tpoint.fCenterY = cy;
286 tpoint.fCenterMag = cmag;
287
288 tpoint.fStarX = sx;
289 tpoint.fStarY = sy;
290 tpoint.fStarMag = smag;
291
292 tpoint.fRealMag = mag;
293
294 UpdateTPoint(t1, tpoint, name);
295
296 return;
297 }
298
299 if (line.substr(0, 13)=="DRIVE-REPORT ")
300 {
301 // DRIVE-REPORT M1
302 // 01 2011 05 14 11 31 19 038
303 // 02 1858 11 17 00 00 00 000
304 // + 000 00 000 + 000 00 000
305 // + 000 00 000
306 // 55695.480081
307 // + 000 00 000 + 000 00 000
308 // + 000 00 000 + 000 00 000
309 // 0000.000 0000.000
310 // 0 2
311
312 // status
313 // year month day hour minute seconds millisec
314 // year month day hour minute seconds millisec
315 // ra(+ h m s) dec(+ d m s) ha(+ h m s)
316 // mjd
317 // zd(+ d m s) az(+ d m s)
318 // zd(+ d m s) az(+ d m s)
319 // zd_err az_err
320 // armed(0=unlocked, 1=locked)
321 // stgmd(0=none, 1=starguider, 2=starguider off)
322 istringstream stream(line.substr(16));
323
324 uint16_t status1;
325 stream >> status1;
326 const Time t1 = ReadTime(stream);
327
328 uint16_t status2;
329 stream >> status2;
330 /*const Time t2 =*/ ReadTime(stream);
331
332 const double ra = ReadAngle(stream);
333 const double dec = ReadAngle(stream);
334 const double ha = ReadAngle(stream);
335
336 double mjd;
337 stream >> mjd;
338
339 const double zd1 = ReadAngle(stream); // Nominal (zd/az asynchronous, dev synchronous, mjd synchronous with zd)
340 const double az1 = ReadAngle(stream); // Nominal (zd/az asynchronous, dev synchronous, mjd synchronous with z)
341 const double zd2 = ReadAngle(stream); // Masured (zd/az synchronous, dev asynchronous, mjd asynchronous)
342 const double az2 = ReadAngle(stream); // Measurd (zd/az synchronous, dev asynchronous, mjd asynchronous)
343
344 double zd_err, az_err;
345 stream >> zd_err; // Deviation = Nominal - Measured
346 stream >> az_err; // Deviation = Nominal - Measured
347
348 uint16_t armed, stgmd;
349 stream >> armed;
350 stream >> stgmd;
351
352 uint32_t pdo3;
353 stream >> hex >> pdo3;
354
355 if (stream.fail())
356 return;
357
358 // Status 0: Error
359 // Status 1: Stopped
360 // Status 3: Stopping || Moving
361 // Status 4: Tracking
362 if (status1==0)
363 status1 = StateMachineImp::kSM_Error - Drive::State::kNotReady;
364
365 const bool ready = (pdo3&0xef00ef)==0xef00ef;
366 if (!ready)
367 fState = Drive::State::kNotReady;
368 else
369 fState = status1==1 ?
370 Drive::State::kReady+armed :
371 Drive::State::kNotReady+status1;
372
373 // kDisconnected = 1,
374 // kConnected,
375 // kNotReady,
376 // kReady,
377 // kArmed,
378 // kMoving,
379 // kTracking,
380 // kOnTrack,
381
382 // pdo3:
383 // 1 Ab
384 // 2 1
385 // 4 Emergency
386 // 8 OverVolt
387 // 10 Move (Drehen-soll)
388 // 20 Af
389 // 40 1
390 // 80 Power on Az
391 // ------------------
392 // 100 NOT UPS Alarm
393 // 200 UPS on Battery
394 // 400 UPS charging
395
396 // Power cut: 2ef02ef
397 // charging: 4ef04ef
398
399 // Convert to deg
400 zd_err /= 3600;
401 az_err /= 3600;
402
403 // Calculate absolut deviation on the sky
404 const double dev = GetDevAbs(zd1, zd1-zd_err, az_err)*3600;
405
406 // If any other state than tracking or a deviation
407 // larger than 60, reset the counter
408 if (fState!=State::kTracking || dev>fDeviationLimit)
409 fTrackingCounter = 0;
410 else
411 fTrackingCounter++;
412
413 // If in tracking, at least five consecutive reports (5s)
414 // must be below 60arcsec deviation, this is considered OnTrack
415 if (fState==State::kTracking && fTrackingCounter>=fDeviationCounter)
416 fState = State::kOnTrack;
417
418 // Having th state as Tracking will reset the counter
419 if (fState==State::kOnTrack && dev>fDeviationMax)
420 fState = State::kTracking;
421
422 const array<uint8_t, 3> state = {{ uint8_t(pdo3>>16), uint8_t(pdo3), uint8_t(pdo3>>24) }};
423 UpdateStatus(t1, state);
424
425 const array<double, 2> point = {{ zd2, az2 }};
426 UpdatePointing(t1, point);
427
428 const array<double, 8> track =
429 {{
430 ra, dec, ha,
431 zd1, az1,
432 zd_err, az_err,
433 dev
434 }};
435 if (mjd>0)
436 UpdateTracking(Time(mjd), track);
437
438 // ---- DIM ----> t1 as event time
439 // status1
440 // mjd
441 // ra/dec/ha
442 // zd/az (nominal)
443 // zd/az (current)
444 // err(zd/az)
445 // [armed] [stgmd]
446
447 // Maybe:
448 // POINTING_POSITION --> t1, zd/az (current), [armed, stgmd, status1]
449 //
450 // if (mjd>0)
451 // TRACKING_POSITION --> mjd, zd/az (nominal), err(zd/az)
452 // ra/dec, ha(not well defined),
453 // [Nominal + Error == Current]
454
455 // MJD is the time which corresponds to the nominal position
456 // t1 is the time which corresponds to the current position/HA
457
458 return;
459 }
460 }
461
462 void StartReadReport()
463 {
464 boost::asio::async_read_until(*this, fBuffer, '\n',
465 boost::bind(&ConnectionDrive::HandleReceivedReport, this,
466 dummy::error, dummy::bytes_transferred));
467 }
468
469 boost::asio::deadline_timer fKeepAlive;
470
471 void KeepAlive()
472 {
473 PostMessage(string("KEEP_ALIVE"));
474
475 fKeepAlive.expires_from_now(boost::posix_time::seconds(10));
476 fKeepAlive.async_wait(boost::bind(&ConnectionDrive::HandleKeepAlive,
477 this, dummy::error));
478 }
479
480 void HandleKeepAlive(const bs::error_code &error)
481 {
482 // 125: Operation canceled (bs::error_code(125, bs::system_category))
483 if (error && error!=ba::error::basic_errors::operation_aborted)
484 {
485 ostringstream str;
486 str << "Write timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl;
487 Error(str);
488
489 PostClose(false);
490 return;
491 }
492
493 if (!is_open())
494 {
495 // For example: Here we could schedule a new accept if we
496 // would not want to allow two connections at the same time.
497 return;
498 }
499
500 // Check whether the deadline has passed. We compare the deadline
501 // against the current time since a new asynchronous operation
502 // may have moved the deadline before this actor had a chance
503 // to run.
504 if (fKeepAlive.expires_at() > ba::deadline_timer::traits_type::now())
505 return;
506
507 KeepAlive();
508 }
509
510
511private:
512 // This is called when a connection was established
513 void ConnectionEstablished()
514 {
515 StartReadReport();
516 KeepAlive();
517 }
518
519 /*
520 void HandleReadTimeout(const bs::error_code &error)
521 {
522 if (error && error!=ba::error::basic_errors::operation_aborted)
523 {
524 stringstream str;
525 str << "Read timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl;
526 Error(str);
527
528 PostClose();
529 return;
530
531 }
532
533 if (!is_open())
534 {
535 // For example: Here we could schedule a new accept if we
536 // would not want to allow two connections at the same time.
537 return;
538 }
539
540 // Check whether the deadline has passed. We compare the deadline
541 // against the current time since a new asynchronous operation
542 // may have moved the deadline before this actor had a chance
543 // to run.
544 if (fInTimeout.expires_at() > ba::deadline_timer::traits_type::now())
545 return;
546
547 Error("Timeout reading data from "+URL());
548
549 PostClose();
550 }*/
551
552
553public:
554
555 static const uint16_t kMaxAddr;
556
557public:
558 ConnectionDrive(ba::io_service& ioservice, MessageImp &imp) : Connection(ioservice, imp()),
559 fState(-1), fIsVerbose(true), fDeviationLimit(120), fDeviationCounter(5), fDeviationMax(240), fTrackingCounter(0), fKeepAlive(ioservice)
560 {
561 SetLogStream(&imp);
562 }
563
564 void SetVerbose(bool b)
565 {
566 fIsVerbose = b;
567 }
568
569 void SetDeviationCondition(uint16_t limit, uint16_t counter, uint16_t max)
570 {
571 fDeviationLimit = limit;
572 fDeviationCounter = counter;
573 fDeviationMax = max;
574 }
575 int GetState() const
576 {
577 if (!IsConnected())
578 return 1;
579 if (IsConnected() && fState<0)
580 return 2;
581 return fState;
582 }
583};
584
585const uint16_t ConnectionkMaxAddr = 0xfff;
586
587// ------------------------------------------------------------------------
588
589#include "DimDescriptionService.h"
590
591class ConnectionDimDrive : public ConnectionDrive
592{
593private:
594 DimDescribedService fDimPointing;
595 DimDescribedService fDimTracking;
596 DimDescribedService fDimSource;
597 DimDescribedService fDimTPoint;
598 DimDescribedService fDimStatus;
599
600 void UpdatePointing(const Time &t, const array<double, 2> &arr)
601 {
602 fDimPointing.setData(arr);
603 fDimPointing.Update(t);
604 }
605
606 void UpdateTracking(const Time &t,const array<double, 8> &arr)
607 {
608 fDimTracking.setData(arr);
609 fDimTracking.Update(t);
610 }
611
612 void UpdateStatus(const Time &t, const array<uint8_t, 3> &arr)
613 {
614 fDimStatus.setData(arr);
615 fDimStatus.Update(t);
616 }
617
618 void UpdateTPoint(const Time &t, const DimTPoint &data,
619 const string &name)
620 {
621 vector<char> dim(sizeof(data)+name.length()+1);
622 memcpy(dim.data(), &data, sizeof(data));
623 memcpy(dim.data()+sizeof(data), name.c_str(), name.length()+1);
624
625 fDimTPoint.setData(dim);
626 fDimTPoint.Update(t);
627 }
628
629public:
630 ConnectionDimDrive(ba::io_service& ioservice, MessageImp &imp) :
631 ConnectionDrive(ioservice, imp),
632 fDimPointing("DRIVE_CONTROL/POINTING_POSITION", "D:1;D:1",
633 "|Zd[deg]:Zenith distance (encoder readout)"
634 "|Az[deg]:Azimuth angle (encoder readout)"),
635 fDimTracking("DRIVE_CONTROL/TRACKING_POSITION", "D:1;D:1;D:1;D:1;D:1;D:1;D:1;D:1",
636 "|Ra[h]:Command right ascension"
637 "|Dec[deg]:Command declination"
638 "|Ha[h]:Corresponding hour angle"
639 "|Zd[deg]:Nominal zenith distance"
640 "|Az[deg]:Nominal azimuth angle"
641 "|dZd[deg]:Control deviation Zd"
642 "|dAz[deg]:Control deviation Az"
643 "|dev[arcsec]:Absolute control deviation"),
644 fDimSource("DRIVE_CONTROL/SOURCE_POSITION", "D:1;D:1;D:1;D:1;D:1;D:1;C:31",
645 "|Ra_src[h]:Source right ascension"
646 "|Dec_src[deg]:Source declination"
647 "|Ra_cmd[h]:Command right ascension"
648 "|Dec_cmd[deg]:Command declination"
649 "|Offset[deg]:Wobble offset"
650 "|Angle[deg]:Wobble angle"
651 "|Name[string]:Source name if available"),
652 fDimTPoint("DRIVE_CONTROL/TPOINT", "D:1;D:1;D:1;D:1;D:1;D:1;D:1;D:1;S:1;D:1;D:1;D:1;D:1;D:1;D:1;D:1;C",
653 "|Ra[h]:Command right ascension"
654 "|Dec[deg]:Command declination"
655 "|Zd_nom[deg]:Nominal zenith distance"
656 "|Az_nom[deg]:Nominal azimuth angle"
657 "|Zd_cur[deg]:Current zenith distance (calculated from image)"
658 "|Az_cur[deg]:Current azimuth angle (calculated from image)"
659 "|Zd_enc[deg]:Feedback zenith axis (from encoder)"
660 "|Az_enc[deg]:Feedback azimuth angle (from encoder)"
661 "|N_leds[cnt]:Number of detected LEDs"
662 "|N_rings[cnt]:Number of rings used to calculate the camera center"
663 "|Xc[pix]:X position of center in CCD camera frame"
664 "|Yc[pix]:Y position of center in CCD camera frame"
665 "|Ic[au]:Average intensity (LED intensity weighted with their frequency of occurance in the calculation)"
666 "|Xs[pix]:X position of start in CCD camera frame"
667 "|Ys[pix]:Y position of star in CCD camera frame"
668 "|Ms[mag]:Artifical magnitude of star (calculated form image))"
669 "|Mc[mag]:Catalog magnitude of star"),
670 fDimStatus("DRIVE_CONTROL/STATUS", "C:2;C:1", "")
671
672 {
673 }
674
675 void UpdateSource()
676 {
677 const vector<char> empty(6*sizeof(double)+31, 0);
678 fDimSource.setQuality(0);
679 fDimSource.Update(empty);
680 }
681
682 void UpdateSource(const array<double, 6> &arr, const string &name="")
683 {
684 vector<char> dat(6*sizeof(double)+31, 0);
685 memcpy(dat.data(), arr.data(), 6*sizeof(double));
686 strncpy(dat.data()+6*sizeof(double), name.c_str(), 30);
687
688 fDimSource.setQuality(1);
689 fDimSource.Update(dat);
690 }
691
692 // 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
693};
694
695// ------------------------------------------------------------------------
696
697struct Source
698{
699 Source() : ra(0), dec(0), offset(0)
700 {
701 angle[0] = -90;
702 angle[1] = 90;
703 }
704
705 double ra;
706 double dec;
707 double offset;
708 array<double, 2> angle;
709};
710
711
712template <class T, class S>
713class StateMachineDrive : public T, public ba::io_service, public ba::io_service::work
714{
715private:
716 S fDrive;
717
718 string fDatabase;
719
720 typedef map<string, Source> sources;
721 sources fSources;
722
723 string fLastCommand; // Last tracking (RADEC) command
724 int fAutoResume; // 0: disabled, 1: enables, 2: resuming
725
726 // Status 0: Error
727 // Status 1: Unlocked
728 // Status 2: Locked
729 // Status 3: Stopping || Moving
730 // Status 4: Tracking
731
732 bool CheckEventSize(size_t has, const char *name, size_t size)
733 {
734 if (has==size)
735 return true;
736
737 ostringstream msg;
738 msg << name << " - Received event has " << has << " bytes, but expected " << size << ".";
739 T::Fatal(msg);
740 return false;
741 }
742
743 enum Coordinates
744 {
745 kPoint,
746 kTrackSlow,
747 kTrackFast
748 };
749
750 string AngleToStr(double angle)
751 {
752 /* Handle sign */
753 const char sgn = angle<0?'-':'+';
754
755 /* Round interval and express in smallest units required */
756 double a = round(3600. * fabs(angle)); // deg to seconds
757
758 /* Separate into fields */
759 const double ad = trunc(a/3600.);
760 a -= ad * 3600.;
761 const double am = trunc(a/60.);
762 a -= am * 60.;
763 const double as = trunc(a);
764
765 /* Return results */
766 ostringstream str;
767 str << sgn << " " << uint16_t(ad) << " " << uint16_t(am) << " " << as;
768 return str.str();
769 }
770
771 int SendCommand(const string &str, bool upd=true)
772 {
773 // This happens if fLastCommand should be send,
774 // but the last command was not a tracking command
775 if (str.empty())
776 {
777 T::Info("Last command was not a tracking command. RESUME ignored.");
778 return T::GetCurrentState();
779 }
780
781 fLastCommand = str.compare(0, 6, "RADEC ")==0 ? str : "";
782
783 fDrive.PostMessage(str);
784 T::Message("Sending: "+str);
785
786 if (upd)
787 fDrive.UpdateSource();
788
789 return T::GetCurrentState();
790 }
791
792 int Park()
793 {
794 SendCommand("PREPS Park", false);
795
796 // FIXME: Go to locked state only when park position properly reached
797 return Drive::State::kLocked;
798 }
799
800 int Resume()
801 {
802 if (fLastCommand.empty())
803 {
804 T::Info("Last command was not a tracking command. RESUME ignored.");
805 return T::GetCurrentState();
806 }
807
808 return SendCommand(fLastCommand, false);
809 }
810
811 int SendCoordinates(const EventImp &evt, const Coordinates type)
812 {
813 if (!CheckEventSize(evt.GetSize(), "SendCoordinates", 16))
814 return T::kSM_FatalError;
815
816 const double *dat = evt.Ptr<double>();
817
818 string command;
819
820 switch (type)
821 {
822 case kPoint: command += "ZDAZ "; break;
823 case kTrackSlow: command += "RADEC "; break;
824 case kTrackFast: command += "GRB "; break;
825 }
826
827 if (type!=kPoint)
828 {
829 const array<double, 6> dim = {{ dat[0], dat[1], dat[0], dat[1], 0, 0 }};
830 fDrive.UpdateSource(dim);
831 }
832
833 command += AngleToStr(dat[0]) + ' ' + AngleToStr(dat[1]);
834 return SendCommand(command, type==kPoint);
835 }
836
837 int StartWobble(const double &srcra, const double &srcdec,
838 const double &woboff, const double &wobang,
839 const string name="")
840 {
841 const double ra = srcra *M_PI/12;
842 const double dec = srcdec*M_PI/180;
843 const double off = woboff*M_PI/180;
844 const double dir = wobang*M_PI/180;
845
846 const double cosdir = cos(dir);
847 const double sindir = sin(dir);
848 const double cosoff = cos(off);
849 const double sinoff = sin(off);
850 const double cosdec = cos(dec);
851 const double sindec = sin(dec);
852
853 if (off==0)
854 {
855 const array<double, 6> dim = {{ srcra, srcdec, srcra, srcdec, 0, 0 }};
856 fDrive.UpdateSource(dim, name);
857
858 string command = "RADEC ";
859 command += AngleToStr(srcra) + ' ' + AngleToStr(srcdec);
860 return SendCommand(command, false);
861 }
862
863 const double sintheta = sindec*cosoff + cosdec*sinoff*cosdir;
864 if (sintheta >= 1)
865 {
866 T::Error("cos(Zd) > 1");
867 return T::GetCurrentState();
868 }
869
870 const double costheta = sqrt(1 - sintheta*sintheta);
871
872 const double cosdeltara = (cosoff - sindec*sintheta)/(cosdec*costheta);
873 const double sindeltara = sindir*sinoff/costheta;
874
875 const double ndec = asin(sintheta)*180/M_PI;
876 const double nra = (atan2(sindeltara, cosdeltara) + ra)*12/M_PI;
877
878 const array<double, 6> dim = {{ srcra, srcdec, nra, ndec, woboff, wobang }};
879 fDrive.UpdateSource(dim, name);
880
881 string command = "RADEC ";
882 command += AngleToStr(nra) + ' ' + AngleToStr(ndec);
883 return SendCommand(command, false);
884 }
885
886 int Wobble(const EventImp &evt)
887 {
888 if (!CheckEventSize(evt.GetSize(), "Wobble", 32))
889 return T::kSM_FatalError;
890
891 const double *dat = evt.Ptr<double>();
892
893 return StartWobble(dat[0], dat[1], dat[2], dat[3]);
894 }
895
896 const sources::const_iterator GetSourceFromDB(const char *ptr, const char *last)
897 {
898 if (find(ptr, last, '\0')==last)
899 {
900 T::Fatal("TrackWobble - The name transmitted by dim is not null-terminated.");
901 throw uint32_t(T::kSM_FatalError);
902 }
903
904 const string name(ptr);
905
906 const sources::const_iterator it = fSources.find(name);
907 if (it==fSources.end())
908 {
909 T::Error("Source '"+name+"' not found in list.");
910 throw uint32_t(T::GetCurrentState());
911 }
912
913 return it;
914 }
915
916 int TrackWobble(const EventImp &evt)
917 {
918 if (evt.GetSize()<=2)
919 {
920 ostringstream msg;
921 msg << "Track - Received event has " << evt.GetSize() << " bytes, but expected at least 3.";
922 T::Fatal(msg);
923 return T::kSM_FatalError;
924 }
925
926 const uint16_t wobble = evt.GetUShort();
927 if (wobble!=1 && wobble!=2)
928 {
929 ostringstream msg;
930 msg << "TrackWobble - Wobble id " << wobble << " undefined, only 1 and 2 allowed.";
931 T::Error(msg);
932 return T::GetCurrentState();
933 }
934
935 const char *ptr = evt.Ptr<char>(2);
936 const char *last = ptr+evt.GetSize()-2;
937
938 try
939 {
940 const sources::const_iterator it = GetSourceFromDB(ptr, last);
941
942 const string &name = it->first;
943 const Source &src = it->second;
944
945 return StartWobble(src.ra, src.dec, src.offset, src.angle[wobble-1], name);
946 }
947 catch (const uint32_t &e)
948 {
949 return e;
950 }
951 }
952
953 int StartTrackWobble(const char *ptr, size_t size, const double &offset=0, const double &angle=0)
954 {
955 const char *last = ptr+size;
956
957 try
958 {
959 const sources::const_iterator it = GetSourceFromDB(ptr, last);
960
961 const string &name = it->first;
962 const Source &src = it->second;
963
964 return StartWobble(src.ra, src.dec, offset, angle, name);
965 }
966 catch (const uint32_t &e)
967 {
968 return e;
969 }
970
971 }
972
973 int Track(const EventImp &evt)
974 {
975 if (evt.GetSize()<=16)
976 {
977 ostringstream msg;
978 msg << "Track - Received event has " << evt.GetSize() << " bytes, but expected at least 17.";
979 T::Fatal(msg);
980 return T::kSM_FatalError;
981 }
982
983 const double *dat = evt.Ptr<double>();
984 const char *ptr = evt.Ptr<char>(16);
985 const size_t size = evt.GetSize()-16;
986
987 return StartTrackWobble(ptr, size, dat[0], dat[1]);
988 }
989
990 int TrackOn(const EventImp &evt)
991 {
992 if (evt.GetSize()==0)
993 {
994 ostringstream msg;
995 msg << "TrackOn - Received event has " << evt.GetSize() << " bytes, but expected at least 1.";
996 T::Fatal(msg);
997 return T::kSM_FatalError;
998 }
999
1000 return StartTrackWobble(evt.Ptr<char>(), evt.GetSize());
1001 }
1002
1003
1004 int TakeTPoint(const EventImp &evt)
1005 {
1006 if (evt.GetSize()<=4)
1007 {
1008 ostringstream msg;
1009 msg << "TakePoint - Received event has " << evt.GetSize() << " bytes, but expected at least 5.";
1010 T::Fatal(msg);
1011 return T::kSM_FatalError;
1012 }
1013
1014 const float mag = evt.Get<float>();
1015 const char *ptr = evt.Ptr<char>(4);
1016 const size_t size = evt.GetSize()-4;
1017
1018 string src(ptr, size);
1019
1020 while (src.find_first_of(' '))
1021 src.erase(src.find_first_of(' '), 1);
1022
1023 SendCommand("TPOIN "+src+" "+to_string(mag), false);;
1024
1025 return T::GetCurrentState();
1026 }
1027
1028 int SetLedBrightness(const EventImp &evt)
1029 {
1030 if (!CheckEventSize(evt.GetSize(), "SetLedBrightness", 8))
1031 return T::kSM_FatalError;
1032
1033 const uint32_t *led = evt.Ptr<uint32_t>();
1034
1035 ostringstream cmd;
1036 cmd << "LEDS " << led[0] << " " << led[1];
1037 return SendCommand(cmd.str(), false);
1038 }
1039
1040
1041 int SetVerbosity(const EventImp &evt)
1042 {
1043 if (!CheckEventSize(evt.GetSize(), "SetVerbosity", 1))
1044 return T::kSM_FatalError;
1045
1046 fDrive.SetVerbose(evt.GetBool());
1047
1048 return T::GetCurrentState();
1049 }
1050
1051 int SetAutoResume(const EventImp &evt)
1052 {
1053 if (!CheckEventSize(evt.GetSize(), "SetAutoResume", 1))
1054 return T::kSM_FatalError;
1055
1056 fAutoResume = evt.GetBool();
1057
1058 return T::GetCurrentState();
1059 }
1060
1061 int Unlock()
1062 {
1063 return Drive::State::kNotReady;
1064 }
1065
1066 int Print()
1067 {
1068 for (auto it=fSources.begin(); it!=fSources.end(); it++)
1069 {
1070 const string &name = it->first;
1071 const Source &src = it->second;
1072
1073 T::Out() << name << ",";
1074 T::Out() << src.ra << "," << src.dec << "," << src.offset << ",";
1075 T::Out() << src.angle[0] << "," << src.angle[1] << endl;
1076 }
1077 return T::GetCurrentState();
1078 }
1079
1080 int ReloadSources()
1081 {
1082 try
1083 {
1084 ReadDatabase();
1085 }
1086 catch (const exception &e)
1087 {
1088 T::Error("Reading sources from databse failed: "+string(e.what()));
1089 }
1090 return T::GetCurrentState();
1091 }
1092
1093 int Disconnect()
1094 {
1095 // Close all connections
1096 fDrive.PostClose(false);
1097
1098 /*
1099 // Now wait until all connection have been closed and
1100 // all pending handlers have been processed
1101 poll();
1102 */
1103
1104 return T::GetCurrentState();
1105 }
1106
1107 int Reconnect(const EventImp &evt)
1108 {
1109 // Close all connections to supress the warning in SetEndpoint
1110 fDrive.PostClose(false);
1111
1112 // Now wait until all connection have been closed and
1113 // all pending handlers have been processed
1114 poll();
1115
1116 if (evt.GetBool())
1117 fDrive.SetEndpoint(evt.GetString());
1118
1119 // Now we can reopen the connection
1120 fDrive.PostClose(true);
1121
1122 return T::GetCurrentState();
1123 }
1124
1125 Time fSunRise;
1126
1127 int ShiftSunRise()
1128 {
1129 const Time sunrise = fSunRise;
1130
1131 fSunRise = Time().GetNextSunRise();
1132
1133 if (sunrise==fSunRise)
1134 return Drive::State::kLocked;
1135
1136 ostringstream msg;
1137 msg << "Next sun-rise will be at " << fSunRise;
1138 T::Info(msg);
1139
1140 return Drive::State::kLocked;
1141 }
1142
1143 int Execute()
1144 {
1145 // Dispatch (execute) at most one handler from the queue. In contrary
1146 // to run_one(), it doesn't wait until a handler is available
1147 // which can be dispatched, so poll_one() might return with 0
1148 // handlers dispatched. The handlers are always dispatched/executed
1149 // synchronously, i.e. within the call to poll_one()
1150 poll_one();
1151
1152 if (T::GetCurrentState()==Drive::State::kLocked)
1153 return ShiftSunRise();
1154
1155 if (T::GetCurrentState()>Drive::State::kLocked)
1156 {
1157 if (Time()>fSunRise)
1158 return Park();
1159 }
1160
1161 const int state = fDrive.GetState();
1162
1163 if (!fLastCommand.empty())
1164 {
1165 // If auto resume is enabled and the drive is in error,
1166 // resume tracking
1167 if (fAutoResume==1 && state==StateMachineImp::kSM_Error)
1168 {
1169 Resume();
1170 fAutoResume = 2;
1171 }
1172
1173 // If drive got out of the error state,
1174 // enable auto resume again
1175 if (fAutoResume==2 && state!=StateMachineImp::kSM_Error)
1176 fAutoResume = 1;
1177 }
1178
1179 return state;
1180 }
1181
1182
1183public:
1184 StateMachineDrive(ostream &out=cout) :
1185 T(out, "DRIVE_CONTROL"), ba::io_service::work(static_cast<ba::io_service&>(*this)),
1186 fDrive(*this, *this), fAutoResume(false), fSunRise(Time().GetNextSunRise())
1187 {
1188 // State names
1189 T::AddStateName(State::kDisconnected, "Disconnected",
1190 "No connection to cosy");
1191
1192 T::AddStateName(State::kConnected, "Connected",
1193 "Cosy connected, drive stopped");
1194
1195 T::AddStateName(State::kNotReady, "NotReady",
1196 "Drive system not ready for movement");
1197
1198 T::AddStateName(State::kLocked, "Locked",
1199 "Drive system is locked (will not accept commands)");
1200
1201 T::AddStateName(State::kReady, "Ready",
1202 "Drive system ready for movement");
1203
1204 T::AddStateName(State::kArmed, "Armed",
1205 "Cosy armed, drive stopped");
1206
1207 T::AddStateName(State::kMoving, "Moving",
1208 "Telescope moving");
1209
1210 T::AddStateName(State::kTracking, "Tracking",
1211 "Telescope is in tracking mode");
1212
1213 T::AddStateName(State::kOnTrack, "OnTrack",
1214 "Telescope tracking stable");
1215
1216 // State::kIdle
1217 // State::kArmed
1218 // State::kMoving
1219 // State::kTracking
1220
1221 // Init
1222 // -----------
1223 // "ARM lock"
1224 // "STGMD off"
1225
1226 /*
1227 [ ] WAIT -> WM_WAIT
1228 [x] STOP! -> WM_STOP
1229 [x] RADEC ra(+ d m s.f) dec(+ d m s.f)
1230 [x] GRB ra(+ d m s.f) dec(+ d m s.f)
1231 [x] ZDAZ zd(+ d m s.f) az (+ d m s.f)
1232 [ ] CELEST id offset angle
1233 [ ] MOON wobble offset
1234 [ ] PREPS string
1235 [ ] TPOIN star mag
1236 [ ] ARM lock/unlock
1237 [ ] STGMD on/off
1238 */
1239
1240 // Drive Commands
1241 T::AddEvent("MOVE_TO", "D:2", State::kArmed) // ->ZDAZ
1242 (bind(&StateMachineDrive::SendCoordinates, this, placeholders::_1, kPoint))
1243 ("Move the telescope to the given local coordinates"
1244 "|Zd[deg]:Zenith distance"
1245 "|Az[deg]:Azimuth");
1246
1247 T::AddEvent("TRACK", "D:2", State::kArmed, State::kTracking, State::kOnTrack) // ->RADEC/GRB
1248 (bind(&StateMachineDrive::SendCoordinates, this, placeholders::_1, kTrackSlow))
1249 ("Move the telescope to the given sky coordinates and start tracking them"
1250 "|Ra[h]:Right ascension"
1251 "|Dec[deg]:Declination");
1252
1253 T::AddEvent("WOBBLE", "D:4", State::kArmed, State::kTracking, State::kOnTrack) // ->RADEC/GRB
1254 (bind(&StateMachineDrive::Wobble, this, placeholders::_1))
1255 ("Move the telescope to the given wobble position around the given sky coordinates and start tracking them"
1256 "|Ra[h]:Right ascension"
1257 "|Dec[deg]:Declination"
1258 "|Offset[deg]:Wobble offset"
1259 "|Angle[deg]:Wobble angle");
1260
1261 T::AddEvent("TRACK_SOURCE", "D:2;C", State::kArmed, State::kTracking, State::kOnTrack) // ->RADEC/GRB
1262 (bind(&StateMachineDrive::Track, this, placeholders::_1))
1263 ("Move the telescope to the given wobble position around the given source and start tracking"
1264 "|Offset[deg]:Wobble offset"
1265 "|Angle[deg]:Wobble angle"
1266 "|Name[string]:Source name");
1267
1268 T::AddEvent("TRACK_WOBBLE", "S:1;C", State::kArmed, State::kTracking, State::kOnTrack) // ->RADEC/GRB
1269 (bind(&StateMachineDrive::TrackWobble, this, placeholders::_1))
1270 ("Move the telescope to the given wobble position around the given source and start tracking"
1271 "|id:Wobble angle id (1 or 2)"
1272 "|Name[string]:Source name");
1273
1274 T::AddEvent("TRACK_ON", "C", State::kArmed, State::kTracking, State::kOnTrack) // ->RADEC/GRB
1275 (bind(&StateMachineDrive::TrackOn, this, placeholders::_1))
1276 ("Move the telescope to the given position and start tracking"
1277 "|Name[string]:Source name");
1278
1279 T::AddEvent("RESUME", StateMachineImp::kSM_Error)
1280 (bind(&StateMachineDrive::Resume, this))
1281 ("If drive is in Error state, this can b used to resume the last tracking command, if the last command sent to cosy was a tracking command.");
1282
1283 T::AddEvent("MOON", State::kArmed, State::kTracking, State::kOnTrack)
1284 (bind(&StateMachineDrive::SendCommand, this, "MOON 0 0", true))
1285 ("Start tracking the moon");
1286 T::AddEvent("VENUS", State::kArmed, State::kTracking, State::kOnTrack)
1287 (bind(&StateMachineDrive::SendCommand, this, "CELEST 2 0 0", true))
1288 ("Start tracking Venus");
1289 T::AddEvent("MARS", State::kArmed, State::kTracking, State::kOnTrack)
1290 (bind(&StateMachineDrive::SendCommand, this, "CELEST 4 0 0", true))
1291 ("Start tracking Mars");
1292 T::AddEvent("JUPITER", State::kArmed, State::kTracking, State::kOnTrack)
1293 (bind(&StateMachineDrive::SendCommand, this, "CELEST 5 0 0", true))
1294 ("Start tracking Jupiter");
1295 T::AddEvent("SATURN", State::kArmed, State::kTracking, State::kOnTrack)
1296 (bind(&StateMachineDrive::SendCommand, this, "CELEST 6 0 0", true))
1297 ("Start tracking Saturn");
1298
1299 T::AddEvent("PARK", State::kArmed, State::kMoving, State::kTracking, State::kOnTrack, 0x100)
1300 (bind(&StateMachineDrive::Park, this))
1301 ("Park the telescope");
1302
1303 T::AddEvent("TAKE_TPOINT")
1304 (bind(&StateMachineDrive::SendCommand, this, "TPOIN FACT 0", false))
1305 ("Take a TPoint");
1306
1307 T::AddEvent("TPOINT", "F:1;C")
1308 (bind(&StateMachineDrive::TakeTPoint, this, placeholders::_1))
1309 ("Take a TPoint (given values will be written to the TPoint files)"
1310 "|mag[float]:Magnitude of the star"
1311 "|name[string]:Name of the star");
1312
1313 T::AddEvent("SET_LED_BRIGHTNESS", "I:2")
1314 (bind(&StateMachineDrive::SetLedBrightness, this, placeholders::_1))
1315 ("Set the LED brightness of the top and bottom leds"
1316 "|top[au]:Allowed range 0-32767 for top LEDs"
1317 "|bot[au]:Allowed range 0-32767 for bottom LEDs");
1318
1319 T::AddEvent("LEDS_OFF")
1320 (bind(&StateMachineDrive::SendCommand, this, "LEDS 0 0", false))
1321 ("Switch off TPoint LEDs");
1322
1323 T::AddEvent("STOP")
1324 (bind(&StateMachineDrive::SendCommand, this, "STOP!", true))
1325 ("Stop any kind of movement.");
1326
1327// T::AddEvent("ARM", State::kConnected)
1328// (bind(&StateMachineSendCommand, this, "ARM lock"))
1329// ("");
1330
1331 T::AddEvent("UNLOCK", Drive::State::kLocked)
1332 (bind(&StateMachineDrive::Unlock, this))
1333 ("Unlock locked state.");
1334
1335 T::AddEvent("SET_AUTORESUME", "B")
1336 (bind(&StateMachineDrive::SetAutoResume, this, placeholders::_1))
1337 ("Enable/disable auto resume"
1338 "|resume[bool]:if enabled, drive is tracking and goes to error state, the last tracking command is repeated automatically.");
1339
1340 // Verbosity commands
1341 T::AddEvent("SET_VERBOSE", "B")
1342 (bind(&StateMachineDrive::SetVerbosity, this, placeholders::_1))
1343 ("Set verbosity state"
1344 "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
1345
1346 // Conenction commands
1347 T::AddEvent("DISCONNECT", State::kConnected, State::kArmed)
1348 (bind(&StateMachineDrive::Disconnect, this))
1349 ("disconnect from ethernet");
1350
1351 T::AddEvent("RECONNECT", "O", State::kDisconnected, State::kConnected, State::kArmed)
1352 (bind(&StateMachineDrive::Reconnect, this, placeholders::_1))
1353 ("(Re)connect ethernet connection to FTM, a new address can be given"
1354 "|[host][string]:new ethernet address in the form <host:port>");
1355
1356
1357 T::AddEvent("PRINT")
1358 (bind(&StateMachineDrive::Print, this))
1359 ("Print source list.");
1360
1361 T::AddEvent("RELOAD_SOURCES")
1362 (bind(&StateMachineDrive::ReloadSources, this))
1363 ("Reload sources from database after database has changed..");
1364
1365 fDrive.StartConnect();
1366 }
1367
1368 void SetEndpoint(const string &url)
1369 {
1370 fDrive.SetEndpoint(url);
1371 }
1372
1373 bool AddSource(const string &name, const Source &src)
1374 {
1375 const auto it = fSources.find(name);
1376 if (it!=fSources.end())
1377 T::Warn("Source '"+name+"' already in list... overwriting.");
1378
1379 fSources[name] = src;
1380 return it==fSources.end();
1381 }
1382
1383 void ReadDatabase(bool print=true)
1384 {
1385#ifdef HAVE_SQL
1386 Database db(fDatabase);
1387
1388 T::Message("Connected to '"+db.uri()+"'");
1389
1390 const mysqlpp::StoreQueryResult res =
1391 db.query("SELECT fSourceName, fRightAscension, fDeclination, fWobbleOffset, fWobbleAngle0, fWobbleAngle1 FROM source").store();
1392
1393 fSources.clear();
1394 for (vector<mysqlpp::Row>::const_iterator v=res.begin(); v<res.end(); v++)
1395 {
1396 const string name = (*v)[0].c_str();
1397
1398 Source src;
1399 src.ra = (*v)[1];
1400 src.dec = (*v)[2];
1401 src.offset = (*v)[3];
1402 src.angle[0] = (*v)[4];
1403 src.angle[1] = (*v)[5];
1404 AddSource(name, src);
1405
1406 if (!print)
1407 continue;
1408
1409 ostringstream msg;
1410 msg << " " << name << setprecision(8) << ": Ra=" << src.ra << "h Dec=" << src.dec << "deg";
1411 msg << " Wobble=[" << src.offset << "," << src.angle[0] << "," << src.angle[1] << "]";
1412 T::Message(msg);
1413 }
1414#else
1415 T::Warn("MySQL support not compiled into the program.");
1416#endif
1417 }
1418
1419 int EvalOptions(Configuration &conf)
1420 {
1421 if (!fSunRise)
1422 return 1;
1423
1424 fDrive.SetVerbose(!conf.Get<bool>("quiet"));
1425
1426 const vector<string> &vec = conf.Vec<string>("source");
1427
1428 for (vector<string>::const_iterator it=vec.begin(); it!=vec.end(); it++)
1429 {
1430 istringstream stream(*it);
1431
1432 string name;
1433
1434 int i=0;
1435
1436 Source src;
1437
1438 string buffer;
1439 while (getline(stream, buffer, ','))
1440 {
1441 istringstream is(buffer);
1442
1443 switch (i++)
1444 {
1445 case 0: name = buffer; break;
1446 case 1: src.ra = ConnectionDrive::ReadAngle(is); break;
1447 case 2: src.dec = ConnectionDrive::ReadAngle(is); break;
1448 case 3: is >> src.offset; break;
1449 case 4: is >> src.angle[0]; break;
1450 case 5: is >> src.angle[1]; break;
1451 }
1452
1453 if (is.fail())
1454 break;
1455 }
1456
1457 if (i==3 || i==6)
1458 {
1459 AddSource(name, src);
1460 continue;
1461 }
1462
1463 T::Warn("Resource 'source' not correctly formatted: '"+*it+"'");
1464 }
1465
1466 fDrive.SetDeviationCondition(conf.Get<uint16_t>("deviation-limit"),
1467 conf.Get<uint16_t>("deviation-count"),
1468 conf.Get<uint16_t>("deviation-max"));
1469
1470 fAutoResume = conf.Get<bool>("auto-resume");
1471
1472 if (conf.Has("source-database"))
1473 {
1474 fDatabase = conf.Get<string>("source-database");
1475 ReadDatabase();
1476 }
1477
1478 if (fSunRise.IsValid())
1479 {
1480 ostringstream msg;
1481 msg << "Next sun-rise will be at " << fSunRise;
1482 T::Message(msg);
1483 }
1484
1485 // The possibility to connect should be last, so that
1486 // everything else is already initialized.
1487 SetEndpoint(conf.Get<string>("addr"));
1488
1489 return -1;
1490 }
1491};
1492
1493// ------------------------------------------------------------------------
1494
1495#include "Main.h"
1496
1497
1498template<class T, class S, class R>
1499int RunShell(Configuration &conf)
1500{
1501 return Main::execute<T, StateMachineDrive<S, R>>(conf);
1502}
1503
1504void SetupConfiguration(Configuration &conf)
1505{
1506 const string def = "localhost:7404";
1507
1508 po::options_description control("Drive control options");
1509 control.add_options()
1510 ("no-dim,d", po_switch(), "Disable dim services")
1511 ("addr,a", var<string>(def), "Network address of cosy")
1512 ("quiet,q", po_bool(true), "Disable printing contents of all received messages (except dynamic data) in clear text.")
1513 ("source-database", var<string>(), "Database link as in\n\tuser:password@server[:port]/database.")
1514 ("source", vars<string>(), "Additional source entry in the form \"name,hh:mm:ss,dd:mm:ss\"")
1515 ("deviation-limit", var<uint16_t>(90), "Deviation limit in arcsec to get 'OnTrack'")
1516 ("deviation-count", var<uint16_t>(3), "Minimum number of reported deviation below deviation-limit to get 'OnTrack'")
1517 ("deviation-max", var<uint16_t>(180), "Maximum deviation in arcsec allowed to keep status 'OnTrack'")
1518 ("auto-resume", po_bool(false), "Enable auto result during tracking if connection is lost")
1519 ;
1520
1521 conf.AddOptions(control);
1522}
1523
1524/*
1525 Extract usage clause(s) [if any] for SYNOPSIS.
1526 Translators: "Usage" and "or" here are patterns (regular expressions) which
1527 are used to match the usage synopsis in program output. An example from cp
1528 (GNU coreutils) which contains both strings:
1529 Usage: cp [OPTION]... [-T] SOURCE DEST
1530 or: cp [OPTION]... SOURCE... DIRECTORY
1531 or: cp [OPTION]... -t DIRECTORY SOURCE...
1532 */
1533void PrintUsage()
1534{
1535 cout <<
1536 "The drivectrl is an interface to cosy.\n"
1537 "\n"
1538 "The default is that the program is started without user intercation. "
1539 "All actions are supposed to arrive as DimCommands. Using the -c "
1540 "option, a local shell can be initialized. With h or help a short "
1541 "help message about the usuage can be brought to the screen.\n"
1542 "\n"
1543 "Usage: drivectrl [-c type] [OPTIONS]\n"
1544 " or: drivectrl [OPTIONS]\n";
1545 cout << endl;
1546}
1547
1548void PrintHelp()
1549{
1550 Main::PrintHelp<StateMachineDrive<StateMachine,ConnectionDrive>>();
1551
1552 /* Additional help text which is printed after the configuration
1553 options goes here */
1554
1555 /*
1556 cout << "bla bla bla" << endl << endl;
1557 cout << endl;
1558 cout << "Environment:" << endl;
1559 cout << "environment" << endl;
1560 cout << endl;
1561 cout << "Examples:" << endl;
1562 cout << "test exam" << endl;
1563 cout << endl;
1564 cout << "Files:" << endl;
1565 cout << "files" << endl;
1566 cout << endl;
1567 */
1568}
1569
1570int main(int argc, const char* argv[])
1571{
1572 Configuration conf(argv[0]);
1573 conf.SetPrintUsage(PrintUsage);
1574 Main::SetupConfiguration(conf);
1575 SetupConfiguration(conf);
1576
1577 if (!conf.DoParse(argc, argv, PrintHelp))
1578 return 127;
1579
1580 //try
1581 {
1582 // No console access at all
1583 if (!conf.Has("console"))
1584 {
1585 if (conf.Get<bool>("no-dim"))
1586 return RunShell<LocalStream, StateMachine, ConnectionDrive>(conf);
1587 else
1588 return RunShell<LocalStream, StateMachineDim, ConnectionDimDrive>(conf);
1589 }
1590 // Cosole access w/ and w/o Dim
1591 if (conf.Get<bool>("no-dim"))
1592 {
1593 if (conf.Get<int>("console")==0)
1594 return RunShell<LocalShell, StateMachine, ConnectionDrive>(conf);
1595 else
1596 return RunShell<LocalConsole, StateMachine, ConnectionDrive>(conf);
1597 }
1598 else
1599 {
1600 if (conf.Get<int>("console")==0)
1601 return RunShell<LocalShell, StateMachineDim, ConnectionDimDrive>(conf);
1602 else
1603 return RunShell<LocalConsole, StateMachineDim, ConnectionDimDrive>(conf);
1604 }
1605 }
1606 /*catch (std::exception& e)
1607 {
1608 cerr << "Exception: " << e.what() << endl;
1609 return -1;
1610 }*/
1611
1612 return 0;
1613}
Note: See TracBrowser for help on using the repository browser.