source: trunk/Cosy/tcpip/MDriveCom.cc@ 13710

Last change on this file since 13710 was 12599, checked in by tbretz, 13 years ago
Added a command to set the LED brightness in FACT; changed the units of the errors in FACT; make use of the 'real' MJD in the report; added the pdo3 status to the end of the report
File size: 14.0 KB
Line 
1#include "MDriveCom.h"
2
3#include <iostream>
4
5#include <TObjArray.h>
6
7#include "MAstro.h"
8#include "MCosy.h"
9#include "MString.h"
10#include "Ring.h"
11#include "Led.h"
12
13#include "MLog.h"
14#include "MLogManip.h"
15
16using namespace std;
17
18bool MDriveCom::ReadAngle(TString &str, Double_t &ret)
19{
20 Char_t sgn;
21 Int_t d, len;
22 UInt_t m;
23 Float_t s;
24
25 // Skip whitespaces before %c and after %f
26 int n=sscanf(str.Data(), " %c %d %d %f %n", &sgn, &d, &m, &s, &len);
27
28 if (n!=4 || (sgn!='+' && sgn!='-'))
29 return false;
30
31 str.Remove(0, len);
32
33 ret = MAstro::Dms2Deg(d, m, s, sgn);
34 return true;
35}
36
37bool MDriveCom::ReadPosition(TString &str, Double_t &d1, Double_t &d2)
38{
39 if (!ReadAngle(str, d1))
40 return false;
41
42 if (!ReadAngle(str, d2))
43 return false;
44
45 return true;
46}
47
48bool MDriveCom::CommandRADEC(TString &str)
49{
50 Double_t ra, dec;
51 if (!ReadPosition(str, ra, dec))
52 {
53 gLog << err << "ERROR - Reading position from RADEC" << endl;
54 return false;
55 }
56 if (!str.IsNull())
57 {
58 gLog << err << "ERROR - Too many bytes in command RADEC" << endl;
59 return false;
60 }
61
62 gLog << all << "CC-COMMAND " << MTime(-1) << " RADEC " << ra << "h " << dec << "d '" << str << "'" << endl;
63
64 ra *= 15; // h -> deg
65
66 RaDec rd(ra, dec);
67
68 //cout << "MDriveCom - TRACK... start." << endl;
69 if (fQueue)
70 fQueue->PostMsg(WM_TRACK, &rd, sizeof(rd));
71 //cout << "MDriveCom - TRACK... done." << endl;
72 return true;
73}
74
75bool MDriveCom::CommandGRB(TString &str)
76{
77 Double_t ra, dec;
78 if (!ReadPosition(str, ra, dec))
79 {
80 gLog << err << "ERROR - Reading position from GRB" << endl;
81 return false;
82 }
83 if (!str.IsNull())
84 {
85 gLog << err << "ERROR - Too many bytes in command GRB" << endl;
86 return false;
87 }
88
89 gLog << all << "CC-COMMAND " << MTime(-1) << " GRB " << ra << "h " << dec << "d '" << str << "'" << endl;
90
91 ra *= 15; // h -> deg
92
93 RaDec rd[2] = { RaDec(ra, dec), RaDec(ra, dec) };
94
95 //cout << "MDriveCom - TRACK... start." << endl;
96 if (fQueue)
97 fQueue->PostMsg(WM_GRB, &rd, sizeof(rd));
98 //cout << "MDriveCom - TRACK... done." << endl;
99 return true;
100}
101
102bool MDriveCom::CommandZDAZ(TString &str)
103{
104 Double_t zd, az;
105 if (!ReadPosition(str, zd, az))
106 {
107 gLog << err << "ERROR - Reading position from ZDAZ" << endl;
108 return false;
109 }
110
111 if (!str.IsNull())
112 {
113 gLog << err << "ERROR - Too many bytes in command ZDAZ" << endl;
114 return false;
115 }
116
117 gLog << all << "CC-COMMAND " << MTime(-1) << " ZDAZ " << zd << "deg " << az << "deg" << endl;
118
119 ZdAz za(zd, az);
120
121 //cout << "MDriveCom - POSITION... start." << endl;
122 if (fQueue)
123 fQueue->PostMsg(WM_POSITION, &za, sizeof(za));
124 //cout << "MDriveCom - POSITION... done." << endl;
125 return true;
126}
127
128bool MDriveCom::CommandCELEST(TString &str)
129{
130 str = str.Strip(TString::kBoth);
131 if (str.IsNull())
132 {
133 gLog << err << "ERROR - CELEST command empty." << endl;
134 return false;
135 }
136
137 Int_t id, len;
138 Float_t offset, angle;
139 Int_t n=sscanf(str.Data(), "%d %f %f %n", &id, &offset, &angle, &len);
140 if (n!=3)
141 {
142 gLog << warn << "WARNING - Not enough argmuents (CELEST)." << endl;
143 return kCONTINUE;
144 }
145
146 str.Remove(0, len);
147 str = str.Strip(TString::kBoth);
148
149 if (!str.IsNull())
150 {
151 gLog << err << "ERROR - Too many bytes in command CELEST" << endl;
152 return false;
153 }
154
155 gLog << all << "CC-COMMAND " << MTime(-1) << " CELEST ID=" << id << " WobbleOffset=" << offset << "deg WobbleAngle=" << angle << "deg" << endl;
156
157 if (id==0)
158 {
159 gLog << err << "ERROR - Tracking the sun IS STRICLY FORBIDDEN - ignored." << endl;
160 return false;
161 }
162
163 if (id<0 || id>9)
164 {
165 gLog << err << "ERROR - Unknown id " << id << " (must be between 1 and 9)." << endl;
166 return false;
167 }
168
169 Double_t data[3] = { id, offset, angle };
170
171 //cout << "MDriveCom - CELEST... start." << endl;
172 if (fQueue)
173 fQueue->PostMsg(WM_CELEST, data, sizeof(Double_t)*3);
174 //cout << "MDriveCom - CELEST... done." << endl;
175 return true;
176}
177
178bool MDriveCom::CommandMOON(TString &str)
179{
180 str = str.Strip(TString::kBoth);
181 if (str.IsNull())
182 {
183 gLog << err << "ERROR - MOON command empty." << endl;
184 return false;
185 }
186
187 Int_t len;
188 Float_t wobble, offset;
189 Int_t n=sscanf(str.Data(), "%f %f %n", &wobble, &offset, &len);
190 if (n!=2)
191 {
192 gLog << warn << "WARNING - Not enough argmuents (MOON)." << endl;
193 return kCONTINUE;
194 }
195
196 str.Remove(0, len);
197 str = str.Strip(TString::kBoth);
198
199 if (!str.IsNull())
200 {
201 gLog << err << "ERROR - Too many bytes in command MOON" << endl;
202 return false;
203 }
204
205 gLog << all << "CC-COMMAND " << MTime(-1) << " MOON WobbleOffset=" << wobble << "deg ShadowOffset=" << offset << "deg" << endl;
206
207 Double_t data[2] = { wobble, offset };
208
209 //cout << "MDriveCom - MOON... start." << endl;
210 if (fQueue)
211 fQueue->PostMsg(WM_MOON, data, sizeof(Double_t)*2);
212 //cout << "MDriveCom - MOON... done." << endl;
213 return true;
214}
215
216bool MDriveCom::CommandPREPS(TString &str)
217{
218 str = str.Strip(TString::kBoth);
219 if (str.IsNull())
220 {
221 gLog << err << "ERROR - No identifier for preposition (PREPS) given." << endl;
222 return false;
223 }
224 if (str.First(' ')>=0)
225 {
226 gLog << err << "ERROR - PREPS command syntax error (contains whitespaces)." << endl;
227 return false;
228 }
229
230 str.ToLower();
231
232 gLog << all << "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl;
233
234 //cout << "MDriveCom - TRACK... start." << endl;
235 if (fQueue)
236 fQueue->PostMsg(WM_PREPS, (void*)str.Data(), str.Length()+1);
237 //cout << "MDriveCom - TRACK... done." << endl;
238 return true;
239}
240
241bool MDriveCom::CommandTPOINT(TString &str)
242{
243 gLog << all << "CC-COMMAND " << MTime(-1) << " TPOIN " << str << endl;
244
245 TObjArray *arr = str.Tokenize(' ');
246
247 if (arr->GetEntries()!=2)
248 {
249 delete arr;
250 gLog << err << "ERROR - Wrong number of arguments in TPOIN command" << endl;
251 return false;
252 }
253
254 delete arr;
255
256 if (fQueue)
257 fQueue->Proc(WM_STARGTPOINT, (void*)str.Data());
258
259 return true;
260}
261
262bool MDriveCom::CommandSTGMD(TString &str)
263{
264 gLog << all << "CC-COMMAND " << MTime(-1) << " STGMD " << str << endl;
265
266 bool on = str=="ON";
267
268 if (fQueue)
269 fQueue->Proc(WM_STARGMODE, &on);
270
271 return true;
272}
273
274bool MDriveCom::CommandLEDS(TString &str)
275{
276 gLog << all << "CC-COMMAND " << MTime(-1) << " LEDS " << str << endl;
277
278 str = str.Strip(TString::kBoth);
279 if (str.IsNull())
280 {
281 gLog << err << "ERROR - LEDS command empty." << endl;
282 return false;
283 }
284
285 Int_t len;
286 Long_t u[2];
287 Int_t n=sscanf(str.Data(), "%ld %ld %n", &u[0], &u[1], &len);
288 if (n!=2)
289 {
290 gLog << warn << "WARNING - Not enough argmuents (LEDS)." << endl;
291 return kCONTINUE;
292 }
293
294 str.Remove(0, len);
295 str = str.Strip(TString::kBoth);
296
297 if (!str.IsNull())
298 {
299 gLog << err << "ERROR - Too many bytes in command LEDS" << endl;
300 return false;
301 }
302
303
304 if (fQueue)
305 fQueue->Proc(WM_LEDS, u);
306
307 return true;
308}
309
310bool MDriveCom::CommandARM(TString &str)
311{
312 str = str.Strip(TString::kBoth);
313 if (str.IsNull())
314 {
315 gLog << err << "ERROR - No identifier for ARM command." << endl;
316 return false;
317 }
318 if (str.First(' ')>=0)
319 {
320 gLog << err << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
321 return false;
322 }
323
324 str.ToLower();
325 if (str!="lock" && str!="unlock")
326 {
327 gLog << err << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
328 return false;
329 }
330
331 gLog << all << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
332
333 bool lock = str=="lock";
334
335 if (fQueue)
336 fQueue->PostMsg(WM_ARM, &lock, sizeof(lock));
337 return true;
338}
339
340bool MDriveCom::InterpreteCmd(TString cmd, TString str)
341{
342 if (cmd==(TString)"WAIT" && str.IsNull())
343 {
344 //cout << "MDriveCom - WAIT... start." << endl;
345 gLog << all << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
346 if (fQueue)
347 fQueue->PostMsg(WM_WAIT);
348 //cout << "MDriveCom - WAIT... done." << endl;
349 return true;
350 }
351
352 if (cmd==(TString)"STOP!" && str.IsNull())
353 {
354 //cout << "MDriveCom - STOP!... start." << endl;
355 gLog << all << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
356 if (fQueue)
357 fQueue->PostMsg(WM_STOP);
358 //cout << "MDriveCom - STOP!... done." << endl;
359 return true;
360 }
361
362 if (cmd==(TString)"RADEC")
363 return CommandRADEC(str);
364
365 if (cmd==(TString)"GRB")
366 return CommandGRB(str);
367
368 if (cmd==(TString)"ZDAZ")
369 return CommandZDAZ(str);
370
371 if (cmd==(TString)"CELEST")
372 return CommandCELEST(str);
373
374 if (cmd==(TString)"MOON")
375 return CommandMOON(str);
376
377 if (cmd==(TString)"PREPS")
378 return CommandPREPS(str);
379
380 if (cmd==(TString)"TPOIN")
381 return CommandTPOINT(str);
382
383 if (cmd==(TString)"ARM")
384 return CommandARM(str);
385
386 if (cmd==(TString)"STGMD")
387 return CommandSTGMD(str);
388
389 if (cmd==(TString)"LEDS")
390 return CommandLEDS(str);
391
392 if (cmd==(TString)"KEEP_ALIVE")
393 {
394 gLog << dbg << MTime(-1) << " KEEP_ALIVE" << endl;
395 return true;
396 }
397
398 if (cmd.IsNull() && str.IsNull())
399 {
400 gLog << all << "CC-COMMAND " << MTime(-1) << " Empty command (single '\\n') received." << endl;
401 return false;
402 }
403
404 gLog << err << "CC-COMMAND " << MTime(-1) << " Syntax error: '" << cmd << "':'" << str << "'" << endl;
405 return false;
406}
407
408void MDriveCom::Print(TString &str, Double_t deg) const
409{
410 Char_t sgn;
411 UShort_t d, m, s;
412
413 MAstro::Deg2Dms(deg, sgn, d, m, s);
414
415 str += MString::Format("%c %03d %02d %03d ", sgn, d, m, s);
416}
417
418bool MDriveCom::SendReport(UInt_t stat, Double_t mjd, RaDec rd, double ha, ZdAz so, ZdAz is, ZdAz er, Bool_t armed, Int_t stargmd, UInt_t pdo3)
419{
420 // rd [rad]
421 // so [rad]
422 // is [deg]
423 // er [rad]
424 rd *= kRad2Deg;
425 so *= kRad2Deg;
426#ifdef FACT
427 er *= kRad2Deg*3600;
428#else
429 er *= kRad2Deg;
430#endif
431
432 rd.Ra(rd.Ra()/15);
433
434 // Set status flag
435 if (stat&kError)
436 SetStatus(0);
437 if (stat&kStopped)
438 SetStatus(1);
439 if (stat&kStopping || stat&kMoving)
440 SetStatus(3);
441 if (stat&kTracking)
442 SetStatus(4);
443
444 TString str;
445 Print(str, rd.Ra()); // Ra
446 Print(str, rd.Dec()); // Dec
447 Print(str, ha); // HA
448 //str += MString::Format("%12.6f ", mjd==0 ? MTime(-1).GetMjd() : mjd); // mjd
449 str += MString::Format("%12.6f ", mjd); // mjd
450 Print(str, so.Zd());
451 Print(str, so.Az());
452 Print(str, is.Zd());
453 Print(str, is.Az());
454 str += MString::Format("%08.3f ", er.Zd());
455 str += MString::Format("%08.3f ", er.Az());
456 str += armed ? "1 " : "0 ";
457 str += MString::Format("%d ", stargmd); // Starguider mode: 0=none, 1=starguider, 2=starguider off
458 str += MString::Format("%x ", pdo3);
459
460 return SendRep("DRIVE-REPORT", str.Data(), kFALSE);
461}
462
463bool MDriveCom::SendStatus(const char *stat)
464{
465 return SendRep("DRIVE-STATUS", stat, kFALSE);
466}
467
468bool MDriveCom::SendStargReport(UInt_t stat, ZdAz miss, ZdAz nompos, Ring center, Int_t num, Int_t n, Double_t bright, Double_t mjd, Int_t numleds, Int_t numrings)
469{
470 // miss [deg]
471 // nompos [deg]
472 const MTime t(-1);
473
474 miss *= 60; // [arcmin]
475
476 // Set status flag
477 if (stat&kError)
478 SetStatus(0);
479 if (stat&kStandby)
480 SetStatus(2);
481 if (stat&kMonitoring)
482 SetStatus(4);
483
484 TString str;
485 str += MString::Format("%05.3f ", miss.Zd()); //[arcmin]
486 str += MString::Format("%05.3f ", miss.Az()); //[arcmin]
487 Print(str, nompos.Zd()); //[deg]
488 Print(str, nompos.Az()); //[deg]
489 str += MString::Format("%05.1f ", center.GetX()); //
490 str += MString::Format("%05.1f ", center.GetY()); //
491 str += MString::Format("%04d ", n); // number of correleated stars
492 str += MString::Format("%03.1f ", bright); // arbitrary sky brightness
493 str += MString::Format("%12.6f ", t.GetMjd()); // mjd
494 str += MString::Format("%d ", numleds); // number of detected leds
495 str += MString::Format("%d ", numrings); // number of detected rings
496 str += MString::Format("%04d ", num); // number of detected stars
497
498 return SendRep("STARG-REPORT", str, kTRUE);
499}
500
501bool MDriveCom::SendTPoint(bool stat, char type, Float_t mag, const char *name, const AltAz &za0, const ZdAz &za1,
502 const TVector2 &xy, Float_t dzd, Float_t daz, const MTime &t,
503 const Ring &center, const Led &star, Int_t numleds, Int_t numrings,
504 Int_t numstars, Int_t numcor, Float_t bright)
505{
506 SetStatus(stat);
507
508 TString str = type;
509 str += MString::Format(" %8.4f ", za0.Az());
510 str += MString::Format("%8.4f ", za0.Alt());
511 str += MString::Format("%8.4f ", fmod(za1.Az()+360, 360));
512 str += MString::Format("%8.4f ", 90-za1.Zd());
513 str += MString::Format("%8.4f ", xy.X());
514 str += MString::Format("%8.4f ", xy.Y());
515 str += MString::Format("%8.4f ", dzd);
516 str += MString::Format("%8.4f ", daz);
517 str += MString::Format("%12.6f ", t.GetMjd());
518 str += MString::Format("%f ", center.GetMag());
519 str += MString::Format("%f ", star.GetMag());
520 str += MString::Format("%f ", center.GetX());
521 str += MString::Format("%f ", center.GetY());
522 str += MString::Format("%f ", star.GetX());
523 str += MString::Format("%f ", star.GetY());
524 str += numleds;
525 str += " ";
526 str += numrings;
527 str += " ";
528 str += numstars;
529 str += " ";
530 str += numcor;
531 str += " ";
532 str += bright;
533 str += " ";
534 str += mag;
535 str += " ";
536 str += name;
537
538 return SendRep("TPOINT-REPORT", str, kTRUE);
539}
Note: See TracBrowser for help on using the repository browser.