source: trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc@ 9516

Last change on this file since 9516 was 9516, checked in by tbretz, 15 years ago
*** empty log message ***
File size: 11.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; // Starguider switched on or not
138 Float_t dra, ddec;
139 Int_t n=sscanf(str.Data(), "%d %f %f %n", &id, &dra, &ddec, &len);
140 if (n!=1)
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 << " dRa=" << dra << "deg dDec=" << ddec << "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 // Moon;
170 id = 3;
171
172 gLog << warn << "WARNING - Tracking the moon's center forced." << endl;
173
174 //cout << "MDriveCom - POSITION... start." << endl;
175 if (fQueue)
176 fQueue->PostMsg(WM_PLANET, &id, sizeof(id));
177 //cout << "MDriveCom - POSITION... done." << endl;
178 return true;
179}
180
181bool MDriveCom::CommandPREPS(TString &str)
182{
183 str = str.Strip(TString::kBoth);
184 if (str.IsNull())
185 {
186 gLog << err << "ERROR - No identifier for preposition (PREPS) given." << endl;
187 return false;
188 }
189 if (str.First(' ')>=0)
190 {
191 gLog << err << "ERROR - PREPS command syntax error (contains whitespaces)." << endl;
192 return false;
193 }
194
195 str.ToLower();
196
197 gLog << all << "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl;
198
199 //cout << "MDriveCom - TRACK... start." << endl;
200 if (fQueue)
201 fQueue->PostMsg(WM_PREPS, (void*)str.Data(), str.Length()+1);
202 //cout << "MDriveCom - TRACK... done." << endl;
203 return true;
204}
205
206bool MDriveCom::CommandTPOINT(TString &str)
207{
208 gLog << all << "CC-COMMAND " << MTime(-1) << " TPOIN " << str << endl;
209
210 TObjArray *arr = str.Tokenize(' ');
211
212 if (arr->GetEntries()!=2)
213 {
214 delete arr;
215 gLog << err << "ERROR - Wrong number of arguments in TPOIN command" << endl;
216 return false;
217 }
218
219 delete arr;
220
221 if (fQueue)
222 fQueue->Proc(WM_STARGTPOINT, (void*)str.Data());
223
224 return true;
225}
226
227bool MDriveCom::CommandSTGMD(TString &str)
228{
229 gLog << all << "CC-COMMAND " << MTime(-1) << " STGMD " << str << endl;
230
231 bool on = str=="ON";
232
233 if (fQueue)
234 fQueue->Proc(WM_STARGMODE, &on);
235
236 return true;
237}
238
239bool MDriveCom::CommandARM(TString &str)
240{
241 str = str.Strip(TString::kBoth);
242 if (str.IsNull())
243 {
244 gLog << err << "ERROR - No identifier for ARM command." << endl;
245 return false;
246 }
247 if (str.First(' ')>=0)
248 {
249 gLog << err << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
250 return false;
251 }
252
253 str.ToLower();
254 if (str!="lock" && str!="unlock")
255 {
256 gLog << err << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
257 return false;
258 }
259
260 gLog << all << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
261
262 bool lock = str=="lock";
263
264 if (fQueue)
265 fQueue->PostMsg(WM_ARM, &lock, sizeof(lock));
266 return true;
267}
268
269bool MDriveCom::InterpreteCmd(TString cmd, TString str)
270{
271 if (cmd==(TString)"WAIT" && str.IsNull())
272 {
273 //cout << "MDriveCom - WAIT... start." << endl;
274 gLog << all << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
275 if (fQueue)
276 fQueue->PostMsg(WM_WAIT);
277 //cout << "MDriveCom - WAIT... done." << endl;
278 return true;
279 }
280
281 if (cmd==(TString)"STOP!" && str.IsNull())
282 {
283 //cout << "MDriveCom - STOP!... start." << endl;
284 gLog << all << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
285 if (fQueue)
286 fQueue->PostMsg(WM_STOP);
287 //cout << "MDriveCom - STOP!... done." << endl;
288 return true;
289 }
290
291 if (cmd==(TString)"RADEC")
292 return CommandRADEC(str);
293
294 if (cmd==(TString)"GRB")
295 return CommandGRB(str);
296
297 if (cmd==(TString)"ZDAZ")
298 return CommandZDAZ(str);
299
300 if (cmd==(TString)"CELEST")
301 return CommandCELEST(str);
302
303 if (cmd==(TString)"PREPS")
304 return CommandPREPS(str);
305
306 if (cmd==(TString)"TPOIN")
307 return CommandTPOINT(str);
308
309 if (cmd==(TString)"ARM")
310 return CommandARM(str);
311
312 if (cmd==(TString)"STGMD")
313 return CommandSTGMD(str);
314
315 if (cmd.IsNull() && str.IsNull())
316 {
317 gLog << all << "CC-COMMAND " << MTime(-1) << " Empty command (single '\\n') received." << endl;
318 return false;
319 }
320
321 gLog << err << "CC-COMMAND " << MTime(-1) << " Syntax error: '" << cmd << "':'" << str << "'" << endl;
322 return false;
323}
324
325void MDriveCom::Print(TString &str, Double_t deg) const
326{
327 Char_t sgn;
328 UShort_t d, m, s;
329
330 MAstro::Deg2Dms(deg, sgn, d, m, s);
331
332 str += MString::Format("%c %03d %02d %03d ", sgn, d, m, s);
333}
334
335bool MDriveCom::SendReport(UInt_t stat, RaDec rd, double ha, ZdAz so, ZdAz is, ZdAz er, Bool_t armed, Int_t stargmd)
336{
337 // rd [rad]
338 // so [rad]
339 // is [deg]
340 // er [rad]
341 const MTime t(-1);
342
343 rd *= kRad2Deg;
344 so *= kRad2Deg;
345 er *= kRad2Deg;
346
347 rd.Ra(rd.Ra()/15);
348
349 // Set status flag
350 if (stat&kError)
351 SetStatus(0);
352 if (stat&kStopped)
353 SetStatus(1);
354 if (stat&kStopping || stat&kMoving)
355 SetStatus(3);
356 if (stat&kTracking)
357 SetStatus(4);
358
359 TString str;
360 Print(str, rd.Ra()); // Ra
361 Print(str, rd.Dec()); // Dec
362 Print(str, ha); // HA
363 str += MString::Format("%12.6f ", t.GetMjd()); // mjd
364 Print(str, so.Zd());
365 Print(str, so.Az());
366 Print(str, is.Zd());
367 Print(str, is.Az());
368 str += MString::Format("%08.3f ", er.Zd());
369 str += MString::Format("%08.3f ", er.Az());
370 str += armed ? "1 " : "0 ";
371 str += MString::Format("%d ", stargmd); // Starguider mode: 0=none, 1=starguider, 2=starguider off
372
373 return SendRep("DRIVE-REPORT", str.Data(), kFALSE);
374}
375
376bool MDriveCom::SendStatus(const char *stat)
377{
378 return SendRep("DRIVE-STATUS", stat, kFALSE);
379}
380
381bool 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)
382{
383 // miss [deg]
384 // nompos [deg]
385 const MTime t(-1);
386
387 miss *= 60; // [arcmin]
388
389 // Set status flag
390 if (stat&kError)
391 SetStatus(0);
392 if (stat&kStandby)
393 SetStatus(2);
394 if (stat&kMonitoring)
395 SetStatus(4);
396
397 TString str;
398 str += MString::Format("%05.3f ", miss.Zd()); //[arcmin]
399 str += MString::Format("%05.3f ", miss.Az()); //[arcmin]
400 Print(str, nompos.Zd()); //[deg]
401 Print(str, nompos.Az()); //[deg]
402 str += MString::Format("%05.1f ", center.GetX()); //
403 str += MString::Format("%05.1f ", center.GetY()); //
404 str += MString::Format("%04d ", n); // number of correleated stars
405 str += MString::Format("%03.1f ", bright); // arbitrary sky brightness
406 str += MString::Format("%12.6f ", t.GetMjd()); // mjd
407 str += MString::Format("%d ", numleds); // number of detected leds
408 str += MString::Format("%d ", numrings); // number of detected rings
409 str += MString::Format("%04d ", num); // number of detected stars
410
411 return SendRep("STARG-REPORT", str, kTRUE);
412}
413
414bool MDriveCom::SendTPoint(bool stat, char type, Float_t mag, const char *name, const AltAz &za0, const ZdAz &za1,
415 const TVector2 &xy, Float_t dzd, Float_t daz, const MTime &t,
416 const Ring &center, const Led &star, Int_t numleds, Int_t numrings,
417 Int_t numstars, Int_t numcor, Float_t bright)
418{
419 SetStatus(stat);
420
421 TString str = type;
422 str += MString::Format(" %8.4f ", za0.Az());
423 str += MString::Format("%8.4f ", za0.Alt());
424 str += MString::Format("%8.4f ", fmod(za1.Az()+360, 360));
425 str += MString::Format("%8.4f ", 90-za1.Zd());
426 str += MString::Format("%8.4f ", xy.X());
427 str += MString::Format("%8.4f ", xy.Y());
428 str += MString::Format("%8.4f ", dzd);
429 str += MString::Format("%8.4f ", daz);
430 str += MString::Format("%12.6f ", t.GetMjd());
431 str += MString::Format("%f ", center.GetMag());
432 str += MString::Format("%f ", star.GetMag());
433 str += MString::Format("%f ", center.GetX());
434 str += MString::Format("%f ", center.GetY());
435 str += MString::Format("%f ", star.GetX());
436 str += MString::Format("%f ", star.GetY());
437 str += numleds;
438 str += " ";
439 str += numrings;
440 str += " ";
441 str += numstars;
442 str += " ";
443 str += numcor;
444 str += " ";
445 str += bright;
446 str += " ";
447 str += mag;
448 str += " ";
449 str += name;
450
451 return SendRep("TPOINT-REPORT", str, kTRUE);
452}
Note: See TracBrowser for help on using the repository browser.