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

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