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

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