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

Last change on this file since 9478 was 9445, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 10.4 KB
Line 
1#include "MDriveCom.h"
2
3#include <iostream>
4
5#include "MAstro.h"
6#include "MCosy.h"
7#include "MString.h"
8#include "Ring.h"
9#include "Led.h"
10
11#include "MLog.h"
12#include "MLogManip.h"
13
14using namespace std;
15
16bool MDriveCom::ReadAngle(TString &str, Double_t &ret)
17{
18 Char_t sgn;
19 Int_t d, len;
20 UInt_t m;
21 Float_t s;
22
23 // Skip whitespaces before %c and after %f
24 int n=sscanf(str.Data(), " %c %d %d %f %n", &sgn, &d, &m, &s, &len);
25
26 if (n!=4 || (sgn!='+' && sgn!='-'))
27 return false;
28
29 str.Remove(0, len);
30
31 ret = MAstro::Dms2Deg(d, m, s, sgn);
32 return true;
33}
34
35bool MDriveCom::ReadPosition(TString &str, Double_t &d1, Double_t &d2)
36{
37 if (!ReadAngle(str, d1))
38 return false;
39
40 if (!ReadAngle(str, d2))
41 return false;
42
43 return true;
44}
45
46bool MDriveCom::CommandRADEC(TString &str)
47{
48 Double_t ra, dec;
49 if (!ReadPosition(str, ra, dec))
50 {
51 gLog << err << "ERROR - Reading position from RADEC" << endl;
52 return false;
53 }
54 if (!str.IsNull())
55 {
56 gLog << err << "ERROR - Too many bytes in command RADEC" << endl;
57 return false;
58 }
59
60 gLog << all << "CC-COMMAND " << MTime(-1) << " RADEC " << ra << "h " << dec << "d '" << str << "'" << endl;
61
62 ra *= 15; // h -> deg
63
64 RaDec rd[2] = { RaDec(ra, dec), RaDec(ra, dec) };
65
66 //cout << "MDriveCom - TRACK... start." << endl;
67 if (fQueue)
68 fQueue->PostMsg(WM_TRACK, &rd, sizeof(rd));
69 //cout << "MDriveCom - TRACK... done." << endl;
70 return true;
71}
72
73bool MDriveCom::CommandGRB(TString &str)
74{
75 Double_t ra, dec;
76 if (!ReadPosition(str, ra, dec))
77 {
78 gLog << err << "ERROR - Reading position from GRB" << endl;
79 return false;
80 }
81 if (!str.IsNull())
82 {
83 gLog << err << "ERROR - Too many bytes in command GRB" << endl;
84 return false;
85 }
86
87 gLog << all << "CC-COMMAND " << MTime(-1) << " GRB " << ra << "h " << dec << "d '" << str << "'" << endl;
88
89 ra *= 15; // h -> deg
90
91 RaDec rd[2] = { RaDec(ra, dec), RaDec(ra, dec) };
92
93 //cout << "MDriveCom - TRACK... start." << endl;
94 if (fQueue)
95 fQueue->PostMsg(WM_GRB, &rd, sizeof(rd));
96 //cout << "MDriveCom - TRACK... done." << endl;
97 return true;
98}
99
100bool MDriveCom::CommandZDAZ(TString &str)
101{
102 Double_t zd, az;
103 if (!ReadPosition(str, zd, az))
104 {
105 gLog << err << "ERROR - Reading position from ZDAZ" << endl;
106 return false;
107 }
108
109 if (!str.IsNull())
110 {
111 gLog << err << "ERROR - Too many bytes in command ZDAZ" << endl;
112 return false;
113 }
114
115 gLog << all << "CC-COMMAND " << MTime(-1) << " ZDAZ " << zd << "deg " << az << "deg" << endl;
116
117 ZdAz za(zd, az);
118
119 //cout << "MDriveCom - POSITION... start." << endl;
120 if (fQueue)
121 fQueue->PostMsg(WM_POSITION, &za, sizeof(za));
122 //cout << "MDriveCom - POSITION... done." << endl;
123 return true;
124}
125
126bool MDriveCom::CommandPREPS(TString &str)
127{
128 str = str.Strip(TString::kBoth);
129 if (str.IsNull())
130 {
131 gLog << err << "ERROR - No identifier for preposition (PREPS) given." << endl;
132 return false;
133 }
134 if (str.First(' ')>=0)
135 {
136 gLog << err << "ERROR - PREPS command syntax error (contains whitespaces)." << endl;
137 return false;
138 }
139
140 str.ToLower();
141
142 gLog << all << "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl;
143
144 //cout << "MDriveCom - TRACK... start." << endl;
145 if (fQueue)
146 fQueue->PostMsg(WM_PREPS, (void*)str.Data(), str.Length()+1);
147 //cout << "MDriveCom - TRACK... done." << endl;
148 return true;
149}
150
151bool MDriveCom::CommandTPOINT(TString &str)
152{
153 gLog << all << "CC-COMMAND " << MTime(-1) << " TPOIN " << str << endl;
154
155 TObjArray *arr = str.Tokenize(' ');
156
157 if (arr->GetEntries()!=2)
158 {
159 delete arr;
160 gLog << err << "ERROR - Wrong number of arguments in TPOIN command" << endl;
161 return false;
162 }
163
164 delete arr;
165
166 if (fQueue)
167 fQueue->Proc(WM_STARGTPOINT, (void*)str.Data());
168
169 return true;
170}
171
172bool MDriveCom::CommandSTGMD(TString &str)
173{
174 gLog << all << "CC-COMMAND " << MTime(-1) << " STGMD " << str << endl;
175
176 bool on = str=="ON";
177
178 if (fQueue)
179 fQueue->Proc(WM_STARGMODE, &on);
180
181 return true;
182}
183
184bool MDriveCom::CommandARM(TString &str)
185{
186 str = str.Strip(TString::kBoth);
187 if (str.IsNull())
188 {
189 gLog << err << "ERROR - No identifier for ARM command." << endl;
190 return false;
191 }
192 if (str.First(' ')>=0)
193 {
194 gLog << err << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
195 return false;
196 }
197
198 str.ToLower();
199 if (str!="lock" && str!="unlock")
200 {
201 gLog << err << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
202 return false;
203 }
204
205 gLog << all << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
206
207 bool lock = str=="lock";
208
209 if (fQueue)
210 fQueue->PostMsg(WM_ARM, &lock, sizeof(lock));
211 return true;
212}
213
214bool MDriveCom::InterpreteCmd(TString cmd, TString str)
215{
216 if (cmd==(TString)"WAIT" && str.IsNull())
217 {
218 //cout << "MDriveCom - WAIT... start." << endl;
219 gLog << all << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
220 if (fQueue)
221 fQueue->PostMsg(WM_WAIT);
222 //cout << "MDriveCom - WAIT... done." << endl;
223 return true;
224 }
225
226 if (cmd==(TString)"STOP!" && str.IsNull())
227 {
228 //cout << "MDriveCom - STOP!... start." << endl;
229 gLog << all << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
230 if (fQueue)
231 fQueue->PostMsg(WM_STOP);
232 //cout << "MDriveCom - STOP!... done." << endl;
233 return true;
234 }
235
236 if (cmd==(TString)"RADEC")
237 return CommandRADEC(str);
238
239 if (cmd==(TString)"GRB")
240 return CommandGRB(str);
241
242 if (cmd==(TString)"ZDAZ")
243 return CommandZDAZ(str);
244
245 if (cmd==(TString)"PREPS")
246 return CommandPREPS(str);
247
248 if (cmd==(TString)"TPOIN")
249 return CommandTPOINT(str);
250
251 if (cmd==(TString)"ARM")
252 return CommandARM(str);
253
254 if (cmd==(TString)"STGMD")
255 return CommandSTGMD(str);
256
257 if (cmd.IsNull() && str.IsNull())
258 {
259 gLog << all << "CC-COMMAND " << MTime(-1) << " Empty command (single '\\n') received." << endl;
260 return false;
261 }
262
263 gLog << err << "CC-COMMAND " << MTime(-1) << " Syntax error: '" << cmd << "':'" << str << "'" << endl;
264 return false;
265}
266
267void MDriveCom::Print(TString &str, Double_t deg) const
268{
269 Char_t sgn;
270 UShort_t d, m, s;
271
272 MAstro::Deg2Dms(deg, sgn, d, m, s);
273
274 str += MString::Format("%c %03d %02d %03d ", sgn, d, m, s);
275}
276
277bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed, Int_t stargmd)
278{
279 // rd [rad]
280 // so [rad]
281 // is [deg]
282 // er [rad]
283 const MTime t(-1);
284
285 rd *= kRad2Deg;
286 so *= kRad2Deg;
287 er *= kRad2Deg;
288
289 rd.Ra(rd.Ra()/15);
290
291 // Set status flag
292 if (stat&kError)
293 SetStatus(0);
294 if (stat&kStopped)
295 SetStatus(1);
296 if (stat&kStopping || stat&kMoving)
297 SetStatus(3);
298 if (stat&kTracking)
299 SetStatus(4);
300
301 TString str;
302 Print(str, rd.Ra()); // Ra
303 Print(str, rd.Dec()); // Dec
304 Print(str, 0); // HA
305 str += MString::Format("%12.6f ", t.GetMjd()); // mjd
306 Print(str, so.Zd());
307 Print(str, so.Az());
308 Print(str, is.Zd());
309 Print(str, is.Az());
310 str += MString::Format("%08.3f ", er.Zd());
311 str += MString::Format("%08.3f ", er.Az());
312 str += armed ? "1 " : "0 ";
313 str += MString::Format("%d ", stargmd); // Starguider mode: 0=none, 1=starguider, 2=starguider off
314
315 return SendRep("DRIVE-REPORT", str.Data(), kFALSE);
316}
317
318bool MDriveCom::SendStatus(const char *stat)
319{
320 return SendRep("DRIVE-STATUS", stat, kFALSE);
321}
322
323bool 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)
324{
325 // miss [deg]
326 // nompos [deg]
327 const MTime t(-1);
328
329 miss *= 60; // [arcmin]
330
331 // Set status flag
332 if (stat&kError)
333 SetStatus(0);
334 if (stat&kStandby)
335 SetStatus(2);
336 if (stat&kMonitoring)
337 SetStatus(4);
338
339 TString str;
340 str += MString::Format("%05.3f ", miss.Zd()); //[arcmin]
341 str += MString::Format("%05.3f ", miss.Az()); //[arcmin]
342 Print(str, nompos.Zd()); //[deg]
343 Print(str, nompos.Az()); //[deg]
344 str += MString::Format("%05.1f ", center.GetX()); //
345 str += MString::Format("%05.1f ", center.GetY()); //
346 str += MString::Format("%04d ", n); // number of correleated stars
347 str += MString::Format("%03.1f ", bright); // arbitrary sky brightness
348 str += MString::Format("%12.6f ", t.GetMjd()); // mjd
349 str += MString::Format("%d ", numleds); // number of detected leds
350 str += MString::Format("%d ", numrings); // number of detected rings
351 str += MString::Format("%04d ", num); // number of detected stars
352
353 return SendRep("STARG-REPORT", str, kTRUE);
354}
355
356bool MDriveCom::SendTPoint(bool stat, char type, Float_t mag, const char *name, const AltAz &za0, const ZdAz &za1,
357 const TVector2 &xy, Float_t dzd, Float_t daz, const MTime &t,
358 const Ring &center, const Led &star, Int_t numleds, Int_t numrings,
359 Int_t numstars, Int_t numcor, Float_t bright)
360{
361 SetStatus(stat);
362
363 TString str = type;
364 str += MString::Format(" %8.4f ", za0.Az());
365 str += MString::Format("%8.4f ", za0.Alt());
366 str += MString::Format("%8.4f ", fmod(za1.Az()+360, 360));
367 str += MString::Format("%8.4f ", 90-za1.Zd());
368 str += MString::Format("%8.4f ", xy.X());
369 str += MString::Format("%8.4f ", xy.Y());
370 str += MString::Format("%8.4f ", dzd);
371 str += MString::Format("%8.4f ", daz);
372 str += MString::Format("%12.6f ", t.GetMjd());
373 str += MString::Format("%f ", center.GetMag());
374 str += MString::Format("%f ", star.GetMag());
375 str += MString::Format("%f ", center.GetX());
376 str += MString::Format("%f ", center.GetY());
377 str += MString::Format("%f ", star.GetX());
378 str += MString::Format("%f ", star.GetY());
379 str += numleds;
380 str += " ";
381 str += numrings;
382 str += " ";
383 str += numstars;
384 str += " ";
385 str += numcor;
386 str += " ";
387 str += bright;
388 str += " ";
389 str += mag;
390 str += " ";
391 str += name;
392
393 return SendRep("TPOINT-REPORT", str, kTRUE);
394}
Note: See TracBrowser for help on using the repository browser.