source: trunk/MagicSoft/Cosy/main/MCosy.cc@ 9516

Last change on this file since 9516 was 9516, checked in by tbretz, 15 years ago
*** empty log message ***
File size: 27.7 KB
Line 
1#include "MCosy.h"
2#include "MCosy.h"
3
4#include <iomanip>
5#include <fstream>
6
7#include <TEnv.h>
8#include <TTimer.h>
9#include <TApplication.h>
10
11//#include "MLog.h"
12#include "MLogManip.h"
13
14#include "MEnv.h"
15#include "MTime.h"
16#include "MPointing.h"
17
18#include "MGCosy.h"
19#include "MDriveCom.h"
20#include "MStarguider.h"
21#include "SlaStars.h"
22#include "MTracking.h"
23
24#include "dkc.h"
25
26ClassImp(MCosy);
27
28using namespace std;
29
30typedef struct tm tm_t;
31
32//#define EXPERT
33#undef EXPERT
34
35/*
36ZdAz MCosy::CorrectTarget(const ZdAz &src, const ZdAz &dst)
37{
38 // CorrectTarget [se]
39
40 // src [se]
41 // dst [rad]
42
43 // fAltMax = 70
44 // fAltMin = -105/110
45 // fAzMin = -355
46 // fAzMax = 355
47
48 ZdAz source = src * 360.0/16384.0;
49 ZdAz dest = dst * TMath::RadToDeg();
50
51 if (dest.Zd()>-3 && dest.Zd()<3)
52 dest.Zd(dest.Zd()<0?-3:3);
53
54 if (dest.Zd()>-1e-6 && dest.Zd()<1e-6)
55 return dst*(16384.0/k2Pi);
56
57 const float fZdMin = -67;
58 const float fZdMax = 67;
59 const float fAzMin = -29;
60 const float fAzMax = 423;
61
62 //
63 // This corrects to target for the shortest distance, not for the fastest move!
64 //
65 ZdAz s = source-dest;
66
67 float min = s.Sqr();
68
69 //
70 // Is it enought to search inside one revolution?
71 //
72 ZdAz ret = dest;
73
74 for (int i=-5; i<5+1; i++)
75 {
76 const ZdAz p(i%2 ? -dest.Zd() : dest.Zd(), dest.Az() - i*180);
77
78 //
79 // Range Check
80 //
81 if (p.Zd()<fZdMin || p.Zd()>fZdMax)
82 continue;
83
84 if (p.Az()<fAzMin || p.Az()>fAzMax)
85 continue;
86
87 //
88 // Calculate distance
89 //
90 s = source-p;
91
92 const float dist = s.Sqr();
93
94 if (dist > min)
95 continue;
96
97 //
98 // New shortest distance
99 //
100 ret = p;
101 min = dist;
102 }
103 return ret*(16384.0/360.0);
104}
105*/
106// --------------------------------------------------------------------------
107//
108// GetSePos, reads the Shaftencoder positions from the Can-drivers
109// for the shaftencoders. The two shaft encoders at the elevation axis
110// are avaraged. The values are returned as a ZdAz object.
111//
112// If one of the two shaftencoders on the elevation axis is missing
113// the other one's position is returned.
114//
115// The positions are alway up-to-date because the shaftencoders are
116// sending all changes immediatly.
117//
118ZdAz MCosy::GetSePos() const
119{
120 const Double_t pa = fMac1 ? (Double_t)fMac1->GetPdoPos2()/fMac1->GetPosRes() : 0;
121 const Double_t p1 = fMac2 ? (Double_t)fMac2->GetPdoPos2()/fMac2->GetPosRes() : 0;
122
123 return ZdAz(p1, pa);
124}
125
126// --------------------------------------------------------------------------
127//
128// check for a break-signal (from the msgqueue) and errors.
129//
130int MCosy::StopWaitingForSDO() const
131{
132 return 0/*Break() || HasError()*/;
133}
134
135// --------------------------------------------------------------------------
136//
137// Waits for a movement to become finished.
138//
139// First waits for all peding Sdos, then waits until both motors are stopped
140// or waiting for SDOs was stopped (either by an error or by Break)
141//
142void MCosy::WaitForEndMovement()
143{
144 // FIXME, what when waiting times out (Zombie)
145 if (!fMac1 || !fMac2)
146 return;
147
148 while ((fMac1->IsPositioning() || fMac2->IsPositioning()) &&
149 !(Break() || HasError() || HasZombie()))
150 usleep(1);
151
152 if (!Break() && !HasError() && !HasZombie())
153 return;
154
155 MTime t(-1);
156 gLog << inf << t << " - MCosy::WaitForEndMovement aborted...";
157 if (Break())
158 gLog << " Break signal...";
159 if (HasError())
160 gLog << " Network has error...";
161 if (HasZombie())
162 gLog << " Network has zombie...";
163 gLog << endl;
164}
165
166// --------------------------------------------------------------------------
167//
168// Check for an error...
169//
170// This is ment for usage after the Action: All Motors Stop.
171//
172void MCosy::CheckForError()
173{
174 //
175 // Check all Can-Nodes for an Error. If there is no error the motor
176 // status is set to stopped.
177 //
178 if (HasError() || HasZombie())
179 {
180 SetStatus(MDriveCom::kError);
181 return;
182 }
183
184 if (fMac1->IsPositioning() || fMac2->IsPositioning())
185 SetStatus(MDriveCom::kMoving);
186 else
187 SetStatus(MDriveCom::kStopped);
188
189 //
190 // If there is an error, the error status is set to Error.
191 //
192
193 /*
194 FIXME: HANDLINGE ERROR
195
196 //
197 // Now try to handle the error.
198 //
199 fMac1->HandleError();
200 fMac2->HandleError();
201
202 //
203 // If the error couldn't get solved return
204 //
205 if (HasError())
206 return;
207
208 //
209 // Set motor status to stopped
210 //
211 SetStatus(MDriveCom::kStopped);
212 */
213}
214
215Bool_t MCosy::CheckRange(const ZdAz &d) const
216{
217 // d [rad]
218
219 if (d.Zd()<fMin.Zd())
220 {
221 gLog << err << "ERROR: Requested Zenith Angle " << d.Zd()*TMath::RadToDeg() << " below negative endswitch " << fMin.Zd()*TMath::RadToDeg() << endl;
222 return kFALSE;
223 }
224
225 if (d.Zd()>fMax.Zd())
226 {
227 gLog << err << "ERROR: Requested Zenith Angle " << d.Zd()*TMath::RadToDeg() << " behind positive endswitch " << fMax.Zd()*TMath::RadToDeg() << endl;
228 return kFALSE;
229 }
230
231 if (d.Az()<fMin.Az())
232 {
233 gLog << err << "ERROR: Requested Azimuth Angle " << d.Az()*TMath::RadToDeg() << " below negative endswitch " << fMin.Az()*TMath::RadToDeg() << endl;
234 if (TMath::Abs(d.Zd())<1)
235 gLog << " Remember that there is a small inaccessible region around zenith!" << endl;
236
237 return kFALSE;
238 }
239
240 if (d.Az()>fMax.Az())
241 {
242 gLog << err << "ERROR: Requested Azimuth Angle " << d.Az()*TMath::RadToDeg() << " behind positive endswitch " << fMax.Az()*TMath::RadToDeg() << endl;
243 if (TMath::Abs(d.Zd())<1)
244 gLog << " Remember that there is a small inaccessible region around zenith!" << endl;
245 return kFALSE;
246 }
247
248
249 return kTRUE;
250}
251
252ZdAz MCosy::AlignTrackingPos(ZdAz pointing) const
253{
254 // pointing [rad]
255 // AlignTrackingPos [deg]
256
257 pointing *= TMath::RadToDeg();
258
259 if (pointing.Zd()<0)
260 {
261 pointing.Zd(-pointing.Zd());
262 pointing.Az(pointing.Az()+180);
263 //gLog << "ZD=-ZD Az+=180" << endl;
264 }
265
266 const ZdAz se = GetSePos()*TMath::TwoPi(); // [rad]
267 const ZdAz unbendedse = fBending.CorrectBack(se)*TMath::RadToDeg(); // ist pointing
268
269 //gLog << "Unbended: " << unbendedse.Zd() << " " << unbendedse.Az() << endl;
270
271 do
272 {
273 const Double_t d = unbendedse.Az() - pointing.Az();
274 if (d>-180 && d<=180)
275 break;
276
277 //gLog << "AZ += " << TMath::Sign(360., d) << endl;
278
279 pointing.Az(pointing.Az()+TMath::Sign(360., d));
280 } while (1);
281
282 return pointing/TMath::RadToDeg();
283/*
284 const Bool_t rc = CheckRange(pointing);
285 za = pointing/TMath::RadToDeg(); // [rad]
286
287 if (!rc)
288 gLog << "Error: Aligned position out of Range." << endl;
289
290 return rc;*/
291}
292
293/*
294Double_t MCosy::Starguider(Double_t mjd, ZdAz &dest) const
295{
296 ifstream fin("pointingpos.txt");
297 if (!fin)
298 return -1;
299
300 Double_t mjd0, zd, az;
301 fin >> mjd0 >> zd >> az;
302
303 mjd0 += 52000;
304
305 if (mjd0+1./24/60 <mjd)
306 return -1;
307
308 ZdAz point=AlignTrackingPos(ZdAz(zd, az)/TMath::RadToDeg());
309
310 const ZdAz diff = (dest-point)*TMath::RadToDeg();
311
312 if (diff.Zd()>5 || diff.Az()>5)
313 {
314 cout << "Starguider deviation too large... dZd=" << diff.Zd() <<" dAz="<<diff.Az() << endl;
315 return -1;
316 }
317
318 dest -= point;
319 dest *= -kGearTot/TMath::TwoPi(); // [re]
320
321 cout << "Using Starguider... dZd=" << dest.Zd() << " dAz=" << dest.Az() << endl;
322
323 return (mjd-mjd0) * (24*60*60); // [s]
324}
325*/
326
327// --------------------------------------------------------------------------
328//
329// Move the telescope to the given position. The position must be given in
330// a ZdAz object in rad.
331//
332// The first positioning is done absolutely. If we didn't reach the
333// correct psotion we try to correct for this by 10 relative position
334// maneuvers. If this doesn't help positioning failed.
335//
336// As a reference the shaftencoder values are used.
337//
338int MCosy::SetPosition(const ZdAz &dst, Bool_t track) // [rad]
339{
340 MSlewing point(this);
341
342 // Default: point.SetPointAcc(0.03, 0.01);
343 // Default: point.SetPointVelocity(0.3);
344 point.SetPointAcc(0.03, 0.01);
345 point.SetPointVelocity(0.3);
346
347 //point.SetPointAcc(0.09, 0.03);
348 //point.SetPointVelocity(1.0);
349
350 return point.SetPosition(dst, track);
351}
352
353void MCosy::TrackPlanet(const Int_t &p) // ra, dec [rad]
354{
355 MTracking track(this);
356 track.SetOut(fOutRep);
357
358 track.SetPointAcc(0.03, 0.01);
359 track.SetPointVelocity(0.3);
360 track.SetTrackAcc(0.01, 0.01);
361
362 track.TrackPlanet((ePlanets_t)p);
363}
364
365void MCosy::TrackPosition(const RaDec &dst) // ra, dec [rad]
366{
367 MTracking track(this);
368 track.SetOut(fOutRep);
369
370 track.SetPointAcc(0.03, 0.01);
371 track.SetPointVelocity(0.3);
372 track.SetTrackAcc(0.01, 0.01);
373
374 track.TrackPosition(dst);
375}
376
377void MCosy::TrackPositionGRB(const RaDec &dst) // ra, dec [rad]
378{
379 TrackPosition(dst);
380 return;
381
382 MTracking track(this);
383 track.SetOut(fOutRep);
384 track.SetPointAcc(0.09, 0.03);
385 track.SetPointVelocity(1.0);
386 track.SetTrackAcc(0.01, 0.01);
387
388 track.TrackPosition(dst);
389}
390
391// --------------------------------------------------------------------------
392//
393// Stops the movement of both motors.
394//
395// Sets the status to stopping. Sets the deceleration to 50% of the maximum.
396// stops. Quits the revolution mode and wait for the end of the movement.
397//
398void MCosy::StopMovement()
399{
400 //
401 // Set status to Stopping
402 //
403 SetStatus(MDriveCom::kStopping);
404
405 //
406 // set deceleration to 50%
407 //
408 gLog << inf2 << "Stopping movement..." << endl;
409 if (fMac1 && fMac2)
410 {
411 // FIXME: Makes sense?
412 fMac1->SetAcceleration(TMath::Nint(0.03*1000000000));
413 fMac2->SetAcceleration(TMath::Nint(0.09*1000000000));
414
415 fMac1->SetRpmMode(FALSE);
416 fMac2->SetRpmMode(FALSE);
417 }
418
419 //
420 // Wait for the movement to really be finished.
421 //
422#ifdef EXPERT
423 cout << "Waiting for end of movement..." << endl;
424#endif
425 WaitForEndMovement();
426
427 //
428 // Check whether everything works fine.
429 //
430 CheckForError();
431#ifdef EXPERT
432 cout << "Movement stopped." << endl;
433#endif
434}
435
436bool MCosy::CheckNetwork()
437{
438 if (!HasConnection())
439 {
440 gLog << warn << "- No connection to network." << endl;
441 return false;
442 }
443
444 CheckForError();
445
446 if (HasZombie())
447 {
448 gLog << warn << "- Found Zombies in Network..." << endl;
449 if (!RebootZombies())
450 return false;
451 }
452
453 /*
454 FIXME HANDLING ERROR
455 */
456 if (HasError())
457 {
458 fMac1->HandleError();
459 fMac2->HandleError();
460 if (HasError() || HasZombie())
461 return false;
462 }
463
464 CheckForError();
465
466 return fMac1->IsOperative() && fMac2->IsOperative();
467}
468
469Int_t MCosy::Proc(int msg, void *mp)
470{
471 switch (msg)
472 {
473 case WM_WAIT:
474 gLog << inf2 << "Wait for execution of Proc(WM_*, ): done." << endl;
475 return 0;
476
477 case WM_STOP:
478 //cout << "MCosy::Proc: Stop." << endl;
479 if (!CheckNetwork())
480 return 0xebb0;
481 StopMovement();
482 return 0;
483
484 case WM_TPOINT:
485 {
486 //cout << "WM_TPoint: start." << endl;
487 SlaStars sla(fObservatory);
488 sla.Now();
489
490 RaDec rd = *((RaDec*)mp);
491 cout << "TPoint Star: " << rd.Ra()/15 << "h " << rd.Dec() << "°" << endl;
492
493 AltAz za=sla.CalcAltAz(rd*TMath::DegToRad())*TMath::RadToDeg();
494
495 if (!fOutTp)
496 {
497 //
498 // open tpoint file
499 //
500 const TString name = GetFileName("tpoint", "old-tpoint", "txt");
501 cout << "TPoint-Cosy File ********* " << name << " ********** " << endl;
502
503 fOutTp = new ofstream(name);
504 *fOutTp << "Magic Model TPOINT data file" << endl;
505 *fOutTp << ": ALTAZ" << endl;
506 *fOutTp << "49 48 0 ";
507 *fOutTp << sla.GetTime().Year() << " " << sla.GetTime().Month() << " " << sla.GetTime().Day() << " ";
508 *fOutTp << /*"20 1013.25 300 0.5 0.55 0.0065" <<*/ endl;
509 // temp(°C) pressure(mB) height(m) humidity(1) wavelength(microm) troplapserate(K/m)
510 }
511
512 cout << " Alt/Az: " << za.Alt() << "° " << za.Az() << "°" << endl;
513 *fOutTp << setprecision(7) << za.Az() << " " << za.Alt() << " ";
514
515 ZdAz sepos = GetSePos()*TMath::TwoPi();
516 za.Set(TMath::Pi()/2-sepos.Zd(), sepos.Az());
517 za *= TMath::RadToDeg();
518
519 cout << " SE-Pos: " << za.Alt() << "° " << za.Az() << "°" << endl;
520 *fOutTp << fmod(za.Az()+360, 360) << " " << za.Alt() << " ";
521
522 if (fStarguider)
523 {
524 XY tp = fStarguider->GetCoordinates();
525 *fOutTp << 90-tp.X() << " " << tp.Y() << " ";
526 }
527
528 *fOutTp << rd.Ra()/15 << " " << rd.Dec() << " " << setprecision(11) << sla.GetMjd() << endl;
529
530 //cout << "WM_TPoint: done. (return 0xaffe)" << endl;
531 }
532 return 0xca1b;
533
534 case WM_STARGTPOINT:
535 if (fStarguider)
536 fStarguider->StartTPoint((char*)mp);
537 return 0xca1c;
538
539 case WM_STARGMODE:
540 if (fStarguider)
541 fStarguider->StartStarguider(*((bool*)mp));
542 return 0xca1c;
543
544 case WM_TRACKPOS:
545 //cout << "WM_TrackPosition: start." << endl;
546 {
547 if (!CheckNetwork())
548 return 0xebb0;
549
550 ZdAz dest = *((ZdAz*)mp) * TMath::DegToRad();
551 //if (!SetPosition(dest, kTRUE))
552 // return 0x1234;
553
554 SlaStars sla(fObservatory);
555 sla.Now();
556
557 RaDec rd = sla.CalcRaDec(dest);
558 TrackPosition(rd);
559 }
560 //cout << "WM_TrackPosition: done. (return 0xabcd)" << endl;
561 return 0xabcd;
562
563 case WM_ARM:
564 //cout << "WM_Position: start." << endl;
565 {
566 if (!CheckNetwork())
567 return 0xebb0;
568
569 const bool arm = mp ? *((bool*)mp) : true;
570 if (arm)
571 {
572 fMac1->Arm();
573 fMac2->Arm();
574 }
575 else
576 {
577 fMac1->Disarm();
578 fMac2->Disarm();
579 }
580 }
581 //cout << "WM_Position: done. (return 0x7777)" << endl;
582 return 0x9999;
583
584 case WM_POSITION:
585 //cout << "WM_Position: start." << endl;
586 {
587 if (!CheckNetwork())
588 return 0xebb0;
589
590 ZdAz dest = *((ZdAz*)mp);
591 SetPosition(dest*TMath::DegToRad());
592 }
593 //cout << "WM_Position: done. (return 0x7777)" << endl;
594 return 0x7777;
595
596 case WM_POSITION1:
597 //cout << "WM_Position1: start." << endl;
598 {
599 if (!CheckNetwork())
600 return 0xebb0;
601
602 ZdAz dest = *((ZdAz*)mp);
603 SetPosition(dest*TMath::DegToRad(), kTRUE);
604 }
605 //cout << "WM_Position: done. (return 0x7777)" << endl;
606 return 0x7777;
607
608 case WM_PREPS:
609 //cout << "WM_Track: START" << endl;
610 {
611 if (!CheckNetwork())
612 return 0xebb0;
613
614 const char *preps = (const char*)mp;
615 cout << "Preposition command to " << preps << " received." << endl;
616
617 ifstream fin(fFilePrepos);
618 if (!fin)
619 {
620 cout << "ERROR: cannot open " << fFilePrepos << endl;
621 return 0xebb1;
622 }
623
624 while (1)
625 {
626 Double_t zd, az;
627 fin >> zd >> az;
628
629 TString str;
630 str.ReadLine(fin);
631 if (!fin)
632 break;
633
634 str.ToLower();
635
636 if (str.Strip(TString::kBoth)==preps)
637 {
638 ZdAz dest(zd, az);
639 SetPosition(dest*TMath::DegToRad());
640 return 0x7979;
641 }
642 cout << "ERROR - Requested preposition not found in file..." << endl;
643 }
644 }
645 //cout << "WM_Track: done. (return 0x8888)" << endl;
646 return 0x7878;
647/*
648 case WM_TESTSE:
649 //cout << "WM_TestSe: start." << endl;
650 fBackground = mp ? kBgdSeTest : kBgdNone;
651 //cout << "WM_TestSe: done. (return 0x1e51)" << endl;
652 return 0x1e51;
653
654 case WM_GEAR:
655 //cout << "WM_Gear: start." << endl;
656 fBackground = mp ? kBgdGear : kBgdNone;
657 //cout << "WM_Gear: done. (return 0xfeaf)" << endl;
658 return 0xfeaf;
659
660 case WM_DISPLAY:
661 //cout << "WM_Display: start." << endl;
662 fTriggerDisplay = kTRUE;
663 //cout << "WM_Disply: done. (return 0xd1e1)" << endl;
664 return 0xd1e1;
665 */
666 case WM_TRACK:
667 case WM_GRB:
668 //cout << "WM_Track/GRB: START" << endl;
669 {
670 RaDec dest = ((RaDec*)mp)[0];
671 if (!CheckNetwork())
672 return 0xebb0;
673
674 if (msg==WM_TRACK)
675 TrackPosition(dest*TMath::DegToRad());
676 else
677 TrackPositionGRB(dest*TMath::DegToRad());
678 }
679 //cout << "WM_Track/GRB: done. (return 0x8888)" << endl;
680 return 0x8888;
681
682 case WM_PLANET:
683 //cout << "WM_PLANET: START" << endl;
684 {
685 const Int_t p = ((Int_t*)mp)[0];
686 if (!CheckNetwork())
687 return 0xebb1;
688
689 TrackPlanet(p);
690 }
691 //cout << "WM_PLANET: done. (return 0x8889)" << endl;
692 return 0x8889;
693/*
694 case WM_NEWTRACK:
695 //cout << "WM_NewTrack: START" << endl;
696 fRaDec = *((RaDec*)mp);
697 //cout << "WM_NewTrack: done. (return 0x9999)" << endl;
698 return 0x9999;
699*/
700 case WM_LOADBENDING:
701 //cout << "WM_LoadBending: START" << endl;
702 fBending.Load("bending.txt");
703 //cout << "WM_LoadBending: done. (return 0xbe0d)" << endl;
704 return 0xbe0d;
705
706 case WM_RESETBENDING:
707 //cout << "WM_ResetBending: START" << endl;
708 fBending.Reset();
709 //cout << "WM_ResetBending: done. (return 0xbe0e)" << endl;
710 return 0xbe0e;
711
712 case WM_CALCALTAZ:
713 {
714 cout << endl;
715
716 SlaStars sla(fObservatory);
717 sla.Now();
718
719 XY xy = *((XY*)mp);
720 RaDec rd(xy.X()*15., xy.Y()); // [deg]
721
722 ZdAz a1 = sla.CalcZdAz(rd*TMath::DegToRad()); // [rad]
723
724 cout << "Ra/Dec source: " << xy.X() << "h " << xy.Y() << "°" << endl;
725 cout << "Zd/Az target: " << a1.Zd()*TMath::RadToDeg() << "° " << a1.Az()*TMath::RadToDeg() << "°" << endl;
726
727 if (fMac1 && fMac2)
728 a1 = AlignTrackingPos(a1);
729
730 a1 = fBending(a1);
731 CheckRange(a1);
732 a1 *= TMath::RadToDeg();
733
734 const ZdAz a2 = a1/360;
735
736 cout << "Zd/Az bended: " << a1.Zd() << "° " << a1.Az() << "°" << endl;
737 cout << "SE bended: " << a2.Zd() << " " << a2.Az() << endl;
738 }
739 return 0xa17a;
740
741 case WM_ENDSWITCH:
742 {
743 ZdAz pos = GetSePos()*TMath::TwoPi();
744 pos = fBending.SubtractOffsets(pos)*TMath::RadToDeg();
745
746 cout << "Endswitch Position: Zd=" << pos.Zd() << "° Az=";
747 cout << pos.Az() << "°" << endl;
748 }
749
750 return 0x1010;
751
752 case WM_QUIT:
753 cout << "WM_Quit: now." << endl;
754 if (!CheckNetwork())
755 {
756 gLog << err << "ERROR: Cannot shutdown network." << endl;
757 gLog << " Please shutdown the drive system manually" << endl;
758 }
759 TerminateApp();
760 cout << "WM_Quit: done." << endl;
761 return 0xaaaa;
762 }
763 cout << "MCosy::Proc: Unknown message 0x" << msg << endl;
764 return 0xffffffff;
765}
766
767void MCosy::ReadConfig(MEnv &env)
768{
769 gLog << inf2 << "Reading telescope range..." << flush;
770 const Double_t amin = env.GetValue("Az_Min[deg]", -95.0);
771 const Double_t zmin = env.GetValue("Zd_Min[deg]", -75.0);
772 fMin.Set(zmin, amin);
773
774 const Double_t amax = env.GetValue("Az_Max[deg]", 305.0);
775 const Double_t zmax = env.GetValue("Zd_Max[deg]", 98.25);
776 fMax.Set(zmax, amax);
777 gLog << "done." << endl;
778
779 gLog << all << flush;
780
781 gLog << " * Min: " << zmin << "deg " << amin << "deg" << endl;
782 gLog << " * Max: " << zmax << "deg " << amax << "deg" << endl;
783
784 fMin = fBending.AddOffsets(fMin*TMath::DegToRad());
785 fMax = fBending.AddOffsets(fMax*TMath::DegToRad());
786
787 gLog << " * Min': " << fMin.Zd()*TMath::RadToDeg() << "deg " << fMin.Az()*TMath::RadToDeg() << "deg" << endl;
788 gLog << " * Max': " << fMax.Zd()*TMath::RadToDeg() << "deg " << fMax.Az()*TMath::RadToDeg() << "deg" << endl;
789}
790
791ZdAz MCosy::GetPointingPos() const
792{
793 if (fMac1->IsZombieNode() || fMac2->IsZombieNode())
794 return ZdAz(0, 0);
795
796 // GetPointingPos [deg]
797 const ZdAz seist = GetSePos()*TMath::TwoPi(); // [rad]
798 return fBending.CorrectBack(seist)*TMath::RadToDeg();
799}
800
801Bool_t MCosy::HandleTimer(TTimer *t)
802{
803 const Int_t rc = fMutexGui.TryLock();
804 if (rc==13)
805 gLog << warn << "MCosy::HandleTimer - mutex is already locked by this thread" << endl;
806
807 if (rc)
808 {
809 gLog << warn << "* GUI update skipped due to locked mutex." << endl;
810 return kTRUE;
811 }
812
813 //
814 // Update Gui, foremer MTGui.
815 //
816 if (fMac1)
817 fMac1->DisplayVal();
818 if (fMac2)
819 fMac2->DisplayVal();
820
821 /*
822 Byte_t avail = 0;
823
824 avail |= (fMac1 && !fMac1->IsZombieNode()) ? 0x01 : 0;
825 avail |= (fMac2 && !fMac2->IsZombieNode()) ? 0x02 : 0;
826// avail |= (!(fStatus&MDriveCom::kError) && 1 ? 0x40 : 0;
827 */
828 Bool_t armed = kTRUE;
829
830 armed &= fMac1 && fMac1->IsArmed();
831 armed &= fMac2 && fMac2->IsArmed();
832
833 if (HasError())
834 SetStatus(MDriveCom::kError);
835
836 const TString stataz = fMac1 ? fMac1->GetStatusDKC() : "";
837 const TString statzd = fMac2 ? fMac2->GetStatusDKC() : "";
838
839 const UInt_t stat1 = fMac1 ? fMac1->GetStatusPdo3() : 0;
840 const UInt_t stat2 = fMac2 ? fMac2->GetStatusPdo3() : 0;
841
842 ZdAz bendist = GetPointingPos();
843
844 //cout << (fStatus&MDriveCom::kTracking?"TRA: ":"POS: ") << bendist.Zd() << " " << bendist.Az() << endl;
845
846 static MTimeout tout(0);
847 if (tout.HasTimedOut())
848 {
849 tout.Start(999);
850 fCom->SendReport(fStatus, fRaDec, fHourAngle, fZdAzSoll, bendist, fTrackingError, armed,
851 fStarguider ? fStarguider->GetStarguiderMode() : 0);
852 }
853
854 fWin->UpdateWeather(*fCom);
855 fWin->Update(bendist, fTrackingError, /*fVelocity, fOffset,*/
856 fRaDec, fZdAzSoll, fStatus, (stat1<<8)|stat2, HasConnection(), armed, statzd, stataz);
857
858 gLog.UpdateGui();
859
860 if (fMutexGui.UnLock()==13)
861 gLog << warn << "MCosy::HandleTimer - tried to unlock mutex locked by other thread." << endl;
862
863 return kTRUE;
864}
865
866// --------------------------------------------------------------------------
867//
868// Start the work of the application:
869//
870// Start the Can-Network.
871// Start the MCosy::TalkThread thread.
872// turn on the gui update
873//
874void MCosy::Start(MEnv &env)
875{
876 // Don't call this function twice!
877 Network::Start();
878
879 CheckForError();
880
881 ReadConfig(env);
882
883 gLog << inf << "- Starting GUI update." << endl;
884 fUpdateGui->TurnOn();
885}
886
887// --------------------------------------------------------------------------
888//
889// Start the work of the application:
890//
891// Turn of the gui update
892// stop the MCosy::TalkThread thread.
893// Stop the network
894//
895void MCosy::Stop()
896{
897 gLog << inf << "- Stopping GUI update." << endl;
898 fUpdateGui->TurnOff();
899 gLog << inf << "- GUI Update stopped." << endl;
900
901 gLog << inf << "- Stopping CAN network." << endl;
902 Network::Stop();
903 gLog << inf << "- CAN network stopped." << endl;
904
905 gLog << inf << "- Stopping message queue." << endl;
906 CancelThread();
907 gLog << inf << "- Message queue stopped." << endl;
908}
909
910TString MCosy::GetFileName(const char *path, const char *name, const char *ext)
911{
912 // FIXME: Timeout missing
913
914 while (1)
915 {
916 MTime time(-1);
917
918 // This is the full qualified date which is part of the name
919 const TString clock = time.GetStringFmt("%Y%m%d_%H%M%S");
920
921 // This gives the night in which the date belongs to
922 time.SetMjd(TMath::Nint(time.GetMjd()));
923
924 const TString night = time.GetStringFmt("%Y_%m_%d");
925
926 const TString dir = Form("%s/%s", path, night.Data());
927 const TString fname = Form("%s_%s.%s", name, clock.Data(), ext);
928
929 const TString full = Form("%s/%s", dir.Data(), fname.Data());
930
931 gSystem->mkdir(dir, kTRUE);
932
933 if (gSystem->AccessPathName(full, kFileExists))
934 return full;
935
936 break;// !!!!!!!!!!!!!!!!!!!!!!!
937
938 usleep(1000);
939 }
940 return "";
941}
942
943MCosy::MCosy(MEnv &env, MDriveCom *com)
944: Network(), fObservatory(MObservatory::kMagic1), fStarguider(NULL),
945fMac1(0), fMac2(0), fStatus(MDriveCom::kStopped), fOutTp(0), fOutRep(0)
946{
947 const Int_t id1 = env.GetValue("Az_Id", 1);
948 const Int_t id2 = env.GetValue("Zd_Id", 3);
949
950 fFilePrepos = env.GetValue("FilePredefinedPositions", "prepos.txt");
951
952 TString name = GetFileName("rep", "cosy", "rep");
953 gLog << inf << "Open Repfile: " << name << endl;
954 fOutRep = new MLog(name, kTRUE);
955 *fOutRep << all << "[Drive Report File]" << endl;
956 *fOutRep << "Version <cvs>" << endl;
957 *fOutRep << "Date " << MTime(-1) << endl;
958 *fOutRep << "[Reports]" << endl;
959
960 const TString pointing = env.GetValue("PointingModel", "bending.txt");
961
962 gLog << all << "Reading pointing model from " << pointing << "..." << endl;
963 if (fBending.Load(pointing))
964 gLog << all << "Reading pointing model from " << pointing << " successfull." << endl;
965 else
966 gLog << err << "ERROR - Reading pointing model from " << pointing << endl;
967
968 //
969 // Create Nodes
970 //
971 gLog << inf << "- Setting up network." << endl;
972
973 fMac1=new Dkc(id1, "DKC/Az");
974 fMac2=new Dkc(id2, "DKC/Zd");
975
976 fMac1->SetReport(fOutRep);
977 fMac2->SetReport(fOutRep);
978
979 gLog << inf << "- Connecting devices to network." << endl;
980
981 //
982 // Connect the devices to the network
983 //
984 SetNode(fMac1);
985 SetNode(fMac2);
986
987 //
988 // Create Gui Event timer and Gui
989 //
990 gLog << inf << "- Initializing GUI Timer." << endl;
991 fUpdateGui = new TTimer(this, 100); // 100ms
992
993 gLog << all << "- Starting GUI." << endl;
994 fWin=new MGCosy(fObservatory, fFilePrepos, this);
995
996 gLog.SetOutputGui(fWin->GetLog(), kTRUE);
997
998 fMac2->SetDisplay(fWin->GetLabel2());
999 fMac1->SetDisplay(fWin->GetLabel1());
1000
1001 fCom = com;//new MDriveCom(this, addr, tx, rx, fOutRep);
1002 fCom->SetOutRep(fOutRep);
1003 // fCom->Start();
1004}
1005
1006void MCosy::TerminateApp()
1007{
1008 gLog << inf2 << "MCosy::TerminateApp()" << endl;
1009/*
1010 Int_t rc;
1011 TGMessageBox msg(this, gClient->GetRoot(),
1012 "Information",
1013 "Cosy is shutting down the system - this may take wa while!",
1014 kMBIconExclamation,
1015 kMBOK, //kMBClose
1016 &rc, 0);
1017*/
1018
1019 gLog.DisableOutputDevice(MLog::eGui);
1020 // FIXME: WHY DOES THIS CRASH THE APPLICATIOn WHILE TRAKING?
1021 // gLog.SetOutputGui(NULL, kFALSE);
1022
1023 gApplication->Terminate(0);
1024}
1025
1026MCosy::~MCosy()
1027{
1028 if(fCom)
1029 {
1030 fCom->SetMsgQueue(NULL);
1031 fCom->SetOutRep(NULL);
1032 }
1033
1034 gLog << inf2 << "Deleting GUI timer." << endl;
1035 // FIXME: Wait until last Update was finished!!!
1036 delete fUpdateGui;
1037
1038 //fMutexGui.Lock();
1039
1040 // Now the files can safely be closed
1041 gLog << inf2 << "Closing output files." << endl;
1042 if (fOutTp)
1043 {
1044 *fOutTp << "END" << endl;
1045 delete fOutTp;
1046 }
1047
1048 delete fOutRep;
1049
1050 //gLog << inf2 << "Deleting CC communication." << endl;
1051 //delete fCom;
1052
1053 gLog << inf2 << "Deleting Nodes." << endl;
1054 fMac1->SetReport(0);
1055 fMac2->SetReport(0);
1056
1057 delete fMac1;
1058 delete fMac2;
1059
1060 gLog << inf2 << "Deleting MGCosy." << endl;
1061
1062 gLog.DisableOutputDevice(MLog::eGui);
1063
1064 delete fWin;
1065
1066 gLog << inf2 << "MGCosy destructed." << endl;
1067}
Note: See TracBrowser for help on using the repository browser.