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

Last change on this file since 9872 was 9550, checked in by tbretz, 15 years ago
*** empty log message ***
File size: 12.8 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::CommandARM(TString &str)
275{
276 str = str.Strip(TString::kBoth);
277 if (str.IsNull())
278 {
279 gLog << err << "ERROR - No identifier for ARM command." << endl;
280 return false;
281 }
282 if (str.First(' ')>=0)
283 {
284 gLog << err << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
285 return false;
286 }
287
288 str.ToLower();
289 if (str!="lock" && str!="unlock")
290 {
291 gLog << err << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
292 return false;
293 }
294
295 gLog << all << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
296
297 bool lock = str=="lock";
298
299 if (fQueue)
300 fQueue->PostMsg(WM_ARM, &lock, sizeof(lock));
301 return true;
302}
303
304bool MDriveCom::InterpreteCmd(TString cmd, TString str)
305{
306 if (cmd==(TString)"WAIT" && str.IsNull())
307 {
308 //cout << "MDriveCom - WAIT... start." << endl;
309 gLog << all << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
310 if (fQueue)
311 fQueue->PostMsg(WM_WAIT);
312 //cout << "MDriveCom - WAIT... done." << endl;
313 return true;
314 }
315
316 if (cmd==(TString)"STOP!" && str.IsNull())
317 {
318 //cout << "MDriveCom - STOP!... start." << endl;
319 gLog << all << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
320 if (fQueue)
321 fQueue->PostMsg(WM_STOP);
322 //cout << "MDriveCom - STOP!... done." << endl;
323 return true;
324 }
325
326 if (cmd==(TString)"RADEC")
327 return CommandRADEC(str);
328
329 if (cmd==(TString)"GRB")
330 return CommandGRB(str);
331
332 if (cmd==(TString)"ZDAZ")
333 return CommandZDAZ(str);
334
335 if (cmd==(TString)"CELEST")
336 return CommandCELEST(str);
337
338 if (cmd==(TString)"MOON")
339 return CommandMOON(str);
340
341 if (cmd==(TString)"PREPS")
342 return CommandPREPS(str);
343
344 if (cmd==(TString)"TPOIN")
345 return CommandTPOINT(str);
346
347 if (cmd==(TString)"ARM")
348 return CommandARM(str);
349
350 if (cmd==(TString)"STGMD")
351 return CommandSTGMD(str);
352
353 if (cmd.IsNull() && str.IsNull())
354 {
355 gLog << all << "CC-COMMAND " << MTime(-1) << " Empty command (single '\\n') received." << endl;
356 return false;
357 }
358
359 gLog << err << "CC-COMMAND " << MTime(-1) << " Syntax error: '" << cmd << "':'" << str << "'" << endl;
360 return false;
361}
362
363void MDriveCom::Print(TString &str, Double_t deg) const
364{
365 Char_t sgn;
366 UShort_t d, m, s;
367
368 MAstro::Deg2Dms(deg, sgn, d, m, s);
369
370 str += MString::Format("%c %03d %02d %03d ", sgn, d, m, s);
371}
372
373bool MDriveCom::SendReport(UInt_t stat, RaDec rd, double ha, ZdAz so, ZdAz is, ZdAz er, Bool_t armed, Int_t stargmd)
374{
375 // rd [rad]
376 // so [rad]
377 // is [deg]
378 // er [rad]
379 const MTime t(-1);
380
381 rd *= kRad2Deg;
382 so *= kRad2Deg;
383 er *= kRad2Deg;
384
385 rd.Ra(rd.Ra()/15);
386
387 // Set status flag
388 if (stat&kError)
389 SetStatus(0);
390 if (stat&kStopped)
391 SetStatus(1);
392 if (stat&kStopping || stat&kMoving)
393 SetStatus(3);
394 if (stat&kTracking)
395 SetStatus(4);
396
397 TString str;
398 Print(str, rd.Ra()); // Ra
399 Print(str, rd.Dec()); // Dec
400 Print(str, ha); // HA
401 str += MString::Format("%12.6f ", t.GetMjd()); // mjd
402 Print(str, so.Zd());
403 Print(str, so.Az());
404 Print(str, is.Zd());
405 Print(str, is.Az());
406 str += MString::Format("%08.3f ", er.Zd());
407 str += MString::Format("%08.3f ", er.Az());
408 str += armed ? "1 " : "0 ";
409 str += MString::Format("%d ", stargmd); // Starguider mode: 0=none, 1=starguider, 2=starguider off
410
411 return SendRep("DRIVE-REPORT", str.Data(), kFALSE);
412}
413
414bool MDriveCom::SendStatus(const char *stat)
415{
416 return SendRep("DRIVE-STATUS", stat, kFALSE);
417}
418
419bool 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)
420{
421 // miss [deg]
422 // nompos [deg]
423 const MTime t(-1);
424
425 miss *= 60; // [arcmin]
426
427 // Set status flag
428 if (stat&kError)
429 SetStatus(0);
430 if (stat&kStandby)
431 SetStatus(2);
432 if (stat&kMonitoring)
433 SetStatus(4);
434
435 TString str;
436 str += MString::Format("%05.3f ", miss.Zd()); //[arcmin]
437 str += MString::Format("%05.3f ", miss.Az()); //[arcmin]
438 Print(str, nompos.Zd()); //[deg]
439 Print(str, nompos.Az()); //[deg]
440 str += MString::Format("%05.1f ", center.GetX()); //
441 str += MString::Format("%05.1f ", center.GetY()); //
442 str += MString::Format("%04d ", n); // number of correleated stars
443 str += MString::Format("%03.1f ", bright); // arbitrary sky brightness
444 str += MString::Format("%12.6f ", t.GetMjd()); // mjd
445 str += MString::Format("%d ", numleds); // number of detected leds
446 str += MString::Format("%d ", numrings); // number of detected rings
447 str += MString::Format("%04d ", num); // number of detected stars
448
449 return SendRep("STARG-REPORT", str, kTRUE);
450}
451
452bool MDriveCom::SendTPoint(bool stat, char type, Float_t mag, const char *name, const AltAz &za0, const ZdAz &za1,
453 const TVector2 &xy, Float_t dzd, Float_t daz, const MTime &t,
454 const Ring &center, const Led &star, Int_t numleds, Int_t numrings,
455 Int_t numstars, Int_t numcor, Float_t bright)
456{
457 SetStatus(stat);
458
459 TString str = type;
460 str += MString::Format(" %8.4f ", za0.Az());
461 str += MString::Format("%8.4f ", za0.Alt());
462 str += MString::Format("%8.4f ", fmod(za1.Az()+360, 360));
463 str += MString::Format("%8.4f ", 90-za1.Zd());
464 str += MString::Format("%8.4f ", xy.X());
465 str += MString::Format("%8.4f ", xy.Y());
466 str += MString::Format("%8.4f ", dzd);
467 str += MString::Format("%8.4f ", daz);
468 str += MString::Format("%12.6f ", t.GetMjd());
469 str += MString::Format("%f ", center.GetMag());
470 str += MString::Format("%f ", star.GetMag());
471 str += MString::Format("%f ", center.GetX());
472 str += MString::Format("%f ", center.GetY());
473 str += MString::Format("%f ", star.GetX());
474 str += MString::Format("%f ", star.GetY());
475 str += numleds;
476 str += " ";
477 str += numrings;
478 str += " ";
479 str += numstars;
480 str += " ";
481 str += numcor;
482 str += " ";
483 str += bright;
484 str += " ";
485 str += mag;
486 str += " ";
487 str += name;
488
489 return SendRep("TPOINT-REPORT", str, kTRUE);
490}
Note: See TracBrowser for help on using the repository browser.