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

Last change on this file since 9262 was 8998, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 9.0 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 return true;
151}
152
153bool MDriveCom::CommandARM(TString &str)
154{
155 str = str.Strip(TString::kBoth);
156 if (str.IsNull())
157 {
158 cout << "ERROR - No identifier for ARM command." << endl;
159 return false;
160 }
161 if (str.First(' ')>=0)
162 {
163 cout << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
164 return false;
165 }
166
167 str.ToLower();
168 if (str!="lock" && str!="unlock")
169 {
170 cout << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
171 return false;
172 }
173
174 cout << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
175
176 bool lock = str=="lock";
177
178 if (fQueue)
179 fQueue->PostMsg(WM_ARM, &lock, sizeof(lock));
180 return true;
181}
182
183bool MDriveCom::InterpreteCmd(TString cmd, TString str)
184{
185 if (cmd==(TString)"WAIT" && str.IsNull())
186 {
187 //cout << "MDriveCom - WAIT... start." << endl;
188 cout << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
189 if (fQueue)
190 fQueue->PostMsg(WM_WAIT);
191 //cout << "MDriveCom - WAIT... done." << endl;
192 return true;
193 }
194
195 if (cmd==(TString)"STOP!" && str.IsNull())
196 {
197 //cout << "MDriveCom - STOP!... start." << endl;
198 cout << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
199 if (fQueue)
200 fQueue->PostMsg(WM_STOP);
201 //cout << "MDriveCom - STOP!... done." << endl;
202 return true;
203 }
204
205 if (cmd==(TString)"RADEC")
206 return CommandRADEC(str);
207
208 if (cmd==(TString)"GRB")
209 return CommandGRB(str);
210
211 if (cmd==(TString)"ZDAZ")
212 return CommandZDAZ(str);
213
214 if (cmd==(TString)"PREPS")
215 return CommandPREPS(str);
216
217 if (cmd==(TString)"TPOIN")
218 return CommandTPOINT(str);
219
220 if (cmd==(TString)"ARM")
221 return CommandARM(str);
222
223 if (cmd.IsNull() && str.IsNull())
224 {
225 cout << "CC-COMMAND " << MTime(-1) << " Empty command (single '\\n') received." << endl;
226 return false;
227 }
228
229 cout << "CC-COMMAND " << MTime(-1) << " Syntax error: '" << cmd << "':'" << str << "'" << endl;
230 return false;
231}
232
233void MDriveCom::Print(TString &str, Double_t deg) const
234{
235 Char_t sgn;
236 UShort_t d, m, s;
237
238 MAstro::Deg2Dms(deg, sgn, d, m, s);
239
240 str += MString::Format("%c %03d %02d %03d ", sgn, d, m, s);
241}
242
243bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed)
244{
245 // rd [rad]
246 // so [rad]
247 // is [deg]
248 // er [rad]
249 const MTime t(-1);
250
251 rd *= kRad2Deg;
252 so *= kRad2Deg;
253 er *= kRad2Deg;
254
255 rd.Ra(rd.Ra()/15);
256
257 // Set status flag
258 if (stat&kError)
259 SetStatus(0);
260 if (stat&kStopped)
261 SetStatus(1);
262 if (stat&kStopping || stat&kMoving)
263 SetStatus(3);
264 if (stat&kTracking)
265 SetStatus(4);
266
267 TString str;
268 Print(str, rd.Ra()); // Ra
269 Print(str, rd.Dec()); // Dec
270 Print(str, 0); // HA
271 str += MString::Format("%12.6f ", t.GetMjd()); // mjd
272 Print(str, so.Zd());
273 Print(str, so.Az());
274 Print(str, is.Zd());
275 Print(str, is.Az());
276 str += MString::Format("%08.3f ", er.Zd());
277 str += MString::Format("%08.3f ", er.Az());
278 str += armed ? "1 " : "0 ";
279
280 return SendRep("DRIVE-REPORT", str, kFALSE);
281}
282
283bool MDriveCom::SendStatus(const char *stat)
284{
285 return SendRep("DRIVE-STATUS", stat, kFALSE);
286}
287
288bool MDriveCom::SendStargReport(UInt_t stat, ZdAz miss, ZdAz nompos, Ring center, Int_t num, Int_t n, Double_t bright, Double_t mjd, Double_t x, Double_t y)
289{
290 // miss [deg]
291 // nompos [deg]
292 const MTime t(-1);
293
294 miss *= 60; // [arcmin]
295
296 // Set status flag
297 if (stat&kError)
298 SetStatus(0);
299 if (stat&kStandby)
300 SetStatus(2);
301 if (stat&kMonitoring)
302 SetStatus(4);
303
304 TString str;
305 str += MString::Format("%05.3f ", miss.Zd()); //[arcmin]
306 str += MString::Format("%05.3f ", miss.Az()); //[arcmin]
307 Print(str, nompos.Zd()); //[deg]
308 Print(str, nompos.Az()); //[deg]
309 str += MString::Format("%05.1f ", center.GetX()); //number
310 str += MString::Format("%05.1f ", center.GetY()); //number
311 str += MString::Format("%04d ", n); //number of correleated stars
312 str += MString::Format("%03.1f ", bright);
313 str += MString::Format("%12.6f ", t.GetMjd()); // mjd
314 str += MString::Format("%.1f ", x);
315 str += MString::Format("%.1f ", y);
316 str += MString::Format("%04d ", num); //number of detected stars
317
318 return SendRep("STARG-REPORT", str, kTRUE);
319}
320
321bool MDriveCom::SendTPoint(UInt_t stat)
322{
323 /*
324 // miss [deg]
325 // nompos [deg]
326 const MTime t(-1);
327
328 miss *= 60; // [arcmin]
329
330 // Set status flag
331 if (stat&kError)
332 SetStatus(0);
333 if (stat&kStandby)
334 SetStatus(2);
335 if (stat&kMonitoring)
336 SetStatus(4);
337
338 MString txt;
339
340 TString str;
341 str += txt.Print("%05.3f ", miss.Zd()); //[arcmin]
342 str += txt.Print("%05.3f ", miss.Az()); //[arcmin]
343 Print(str, nompos.Zd()); //[deg]
344 Print(str, nompos.Az()); //[deg]
345 str += txt.Print("%05.1f ", center.GetX()); //number
346 str += txt.Print("%05.1f ", center.GetY()); //number
347 str += txt.Print("%04d ", n); //number of correleated stars
348 str += txt.Print("%03.1f ", bright);
349 str += txt.Print("%12.6f ", t.GetMjd()); // mjd
350 str += txt.Print("%.1f ", x);
351 str += txt.Print("%.1f ", y);
352 str += txt.Print("%04d ", num); //number of detected stars
353 */
354
355 return SendRep("TPOINT-REPORT", "", kTRUE);
356}
Note: See TracBrowser for help on using the repository browser.