source: trunk/Cosy/main/MCosy.cc@ 9844

Last change on this file since 9844 was 9550, checked in by tbretz, 15 years ago
*** empty log message ***
File size: 28.6 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(Int_t id, Double_t offset, Double_t angle)
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.SetWobble(offset, angle);
363 track.TrackPlanet((ePlanets_t)id);
364}
365
366void MCosy::TrackMoon(Double_t wobble, Double_t offset)
367{
368 MTracking track(this);
369 track.SetOut(fOutRep);
370
371 track.SetPointAcc(0.03, 0.01);
372 track.SetPointVelocity(0.3);
373 track.SetTrackAcc(0.01, 0.01);
374
375 track.TrackMoon(wobble, offset);
376}
377
378void MCosy::TrackPosition(const RaDec &dst) // ra, dec [rad]
379{
380 MTracking track(this);
381 track.SetOut(fOutRep);
382
383 track.SetPointAcc(0.03, 0.01);
384 track.SetPointVelocity(0.3);
385 track.SetTrackAcc(0.01, 0.01);
386
387 //track.SetWobbleOffset(offset, angle);
388 track.TrackPosition(dst);
389}
390
391void MCosy::TrackPositionGRB(const RaDec &dst) // ra, dec [rad]
392{
393 TrackPosition(dst);
394 return;
395
396 MTracking track(this);
397 track.SetOut(fOutRep);
398 track.SetPointAcc(0.09, 0.03);
399 track.SetPointVelocity(1.0);
400 track.SetTrackAcc(0.01, 0.01);
401
402 //track.SetWobbleOffset(offset, angle);
403 track.TrackPosition(dst);
404}
405
406// --------------------------------------------------------------------------
407//
408// Stops the movement of both motors.
409//
410// Sets the status to stopping. Sets the deceleration to 50% of the maximum.
411// stops. Quits the revolution mode and wait for the end of the movement.
412//
413void MCosy::StopMovement()
414{
415 //
416 // Set status to Stopping
417 //
418 SetStatus(MDriveCom::kStopping);
419
420 //
421 // set deceleration to 50%
422 //
423 gLog << inf2 << "Stopping movement..." << endl;
424 if (fMac1 && fMac2)
425 {
426 // FIXME: Makes sense?
427 fMac1->SetAcceleration(TMath::Nint(0.03*1000000000));
428 fMac2->SetAcceleration(TMath::Nint(0.09*1000000000));
429
430 fMac1->SetRpmMode(FALSE);
431 fMac2->SetRpmMode(FALSE);
432 }
433
434 //
435 // Wait for the movement to really be finished.
436 //
437#ifdef EXPERT
438 cout << "Waiting for end of movement..." << endl;
439#endif
440 WaitForEndMovement();
441
442 //
443 // Check whether everything works fine.
444 //
445 CheckForError();
446#ifdef EXPERT
447 cout << "Movement stopped." << endl;
448#endif
449}
450
451bool MCosy::CheckNetwork()
452{
453 if (!HasConnection())
454 {
455 gLog << warn << "- No connection to network." << endl;
456 return false;
457 }
458
459 CheckForError();
460
461 if (HasZombie())
462 {
463 gLog << warn << "- Found Zombies in Network..." << endl;
464 if (!RebootZombies())
465 return false;
466 }
467
468 /*
469 FIXME HANDLING ERROR
470 */
471 if (HasError())
472 {
473 fMac1->HandleError();
474 fMac2->HandleError();
475 if (HasError() || HasZombie())
476 return false;
477 }
478
479 CheckForError();
480
481 return fMac1->IsOperative() && fMac2->IsOperative();
482}
483
484Int_t MCosy::Proc(int msg, void *mp)
485{
486 switch (msg)
487 {
488 case WM_WAIT:
489 gLog << inf2 << "Wait for execution of Proc(WM_*, ): done." << endl;
490 return 0;
491
492 case WM_STOP:
493 //cout << "MCosy::Proc: Stop." << endl;
494 if (!CheckNetwork())
495 return 0xebb0;
496 StopMovement();
497 return 0;
498
499 case WM_TPOINT:
500 {
501 //cout << "WM_TPoint: start." << endl;
502 SlaStars sla(fObservatory);
503 sla.Now();
504
505 RaDec rd = *((RaDec*)mp);
506 cout << "TPoint Star: " << rd.Ra()/15 << "h " << rd.Dec() << "°" << endl;
507
508 AltAz za=sla.CalcAltAz(rd*TMath::DegToRad())*TMath::RadToDeg();
509
510 if (!fOutTp)
511 {
512 //
513 // open tpoint file
514 //
515 const TString name = GetFileName("tpoint", "old-tpoint", "txt");
516 cout << "TPoint-Cosy File ********* " << name << " ********** " << endl;
517
518 fOutTp = new ofstream(name);
519 *fOutTp << "Magic Model TPOINT data file" << endl;
520 *fOutTp << ": ALTAZ" << endl;
521 *fOutTp << "49 48 0 ";
522 *fOutTp << sla.GetTime().Year() << " " << sla.GetTime().Month() << " " << sla.GetTime().Day() << " ";
523 *fOutTp << /*"20 1013.25 300 0.5 0.55 0.0065" <<*/ endl;
524 // temp(°C) pressure(mB) height(m) humidity(1) wavelength(microm) troplapserate(K/m)
525 }
526
527 cout << " Alt/Az: " << za.Alt() << "° " << za.Az() << "°" << endl;
528 *fOutTp << setprecision(7) << za.Az() << " " << za.Alt() << " ";
529
530 ZdAz sepos = GetSePos()*TMath::TwoPi();
531 za.Set(TMath::Pi()/2-sepos.Zd(), sepos.Az());
532 za *= TMath::RadToDeg();
533
534 cout << " SE-Pos: " << za.Alt() << "° " << za.Az() << "°" << endl;
535 *fOutTp << fmod(za.Az()+360, 360) << " " << za.Alt() << " ";
536
537 if (fStarguider)
538 {
539 XY tp = fStarguider->GetCoordinates();
540 *fOutTp << 90-tp.X() << " " << tp.Y() << " ";
541 }
542
543 *fOutTp << rd.Ra()/15 << " " << rd.Dec() << " " << setprecision(11) << sla.GetMjd() << endl;
544
545 //cout << "WM_TPoint: done. (return 0xaffe)" << endl;
546 }
547 break;
548
549 case WM_STARGTPOINT:
550 if (fStarguider)
551 fStarguider->StartTPoint((char*)mp);
552 break;
553
554 case WM_STARGMODE:
555 if (fStarguider)
556 fStarguider->StartStarguider(*((bool*)mp));
557 break;
558
559 case WM_TRACKPOS:
560 //cout << "WM_TrackPosition: start." << endl;
561 {
562 if (!CheckNetwork())
563 return 0xebb0;
564
565 ZdAz dest = *((ZdAz*)mp) * TMath::DegToRad();
566 //if (!SetPosition(dest, kTRUE))
567 // return 0x1234;
568
569 SlaStars sla(fObservatory);
570 sla.Now();
571
572 RaDec rd = sla.CalcRaDec(dest);
573 TrackPosition(rd);
574 }
575 //cout << "WM_TrackPosition: done. (return 0xabcd)" << endl;
576 break;
577
578 case WM_ARM:
579 //cout << "WM_Position: start." << endl;
580 {
581 if (!CheckNetwork())
582 return 0xebb0;
583
584 const bool arm = mp ? *((bool*)mp) : true;
585 if (arm)
586 {
587 fMac1->Arm();
588 fMac2->Arm();
589 }
590 else
591 {
592 fMac1->Disarm();
593 fMac2->Disarm();
594 }
595 }
596 //cout << "WM_Position: done. (return 0x7777)" << endl;
597 break;
598
599 case WM_POSITION:
600 //cout << "WM_Position: start." << endl;
601 {
602 if (!CheckNetwork())
603 return 0xebb0;
604
605 ZdAz dest = *((ZdAz*)mp);
606 SetPosition(dest*TMath::DegToRad());
607 }
608 //cout << "WM_Position: done. (return 0x7777)" << endl;
609 break;
610
611 case WM_POSITION1:
612 //cout << "WM_Position1: start." << endl;
613 {
614 if (!CheckNetwork())
615 return 0xebb0;
616
617 ZdAz dest = *((ZdAz*)mp);
618 SetPosition(dest*TMath::DegToRad(), kTRUE);
619 }
620 //cout << "WM_Position: done. (return 0x7777)" << endl;
621 break;
622
623 case WM_PREPS:
624 //cout << "WM_Track: START" << endl;
625 {
626 if (!CheckNetwork())
627 return 0xebb0;
628
629 const char *preps = (const char*)mp;
630 cout << "Preposition command to " << preps << " received." << endl;
631
632 ifstream fin(fFilePrepos);
633 if (!fin)
634 {
635 cout << "ERROR: cannot open " << fFilePrepos << endl;
636 return 0xebb1;
637 }
638
639 while (1)
640 {
641 Double_t zd, az;
642 fin >> zd >> az;
643
644 TString str;
645 str.ReadLine(fin);
646 if (!fin)
647 break;
648
649 str.ToLower();
650
651 if (str.Strip(TString::kBoth)==preps)
652 {
653 ZdAz dest(zd, az);
654 SetPosition(dest*TMath::DegToRad());
655 return 0x7979;
656 }
657 cout << "ERROR - Requested preposition not found in file..." << endl;
658 }
659 }
660 //cout << "WM_Track: done. (return 0x8888)" << endl;
661 break;
662/*
663 case WM_TESTSE:
664 //cout << "WM_TestSe: start." << endl;
665 fBackground = mp ? kBgdSeTest : kBgdNone;
666 //cout << "WM_TestSe: done. (return 0x1e51)" << endl;
667 return 0x1e51;
668
669 case WM_GEAR:
670 //cout << "WM_Gear: start." << endl;
671 fBackground = mp ? kBgdGear : kBgdNone;
672 //cout << "WM_Gear: done. (return 0xfeaf)" << endl;
673 return 0xfeaf;
674
675 case WM_DISPLAY:
676 //cout << "WM_Display: start." << endl;
677 fTriggerDisplay = kTRUE;
678 //cout << "WM_Disply: done. (return 0xd1e1)" << endl;
679 return 0xd1e1;
680 */
681 case WM_TRACK:
682 case WM_GRB:
683 //cout << "WM_Track/GRB: START" << endl;
684 {
685 RaDec dest = ((RaDec*)mp)[0];
686 if (!CheckNetwork())
687 return 0xebb0;
688
689 if (msg==WM_TRACK)
690 TrackPosition(dest*TMath::DegToRad());
691 else
692 TrackPositionGRB(dest*TMath::DegToRad());
693 }
694 //cout << "WM_Track/GRB: done. (return 0x8888)" << endl;
695 break;
696
697 case WM_CELEST:
698 //cout << "WM_PLANET: START" << endl;
699 {
700 if (!CheckNetwork())
701 return 0xebb1;
702
703 const Double_t *d = (Double_t*)mp;
704
705 const Int_t id = TMath::Nint(d[0]);
706 const Double_t offset = d[1];
707 const Double_t angle = d[2];
708
709 TrackPlanet(id, offset, angle);
710 }
711 //cout << "WM_PLANET: done. (return 0x8889)" << endl;
712 break;
713
714 case WM_MOON:
715 //cout << "WM_PLANET: START" << endl;
716 {
717 if (!CheckNetwork())
718 return 0xebb1;
719
720 const Double_t *d = (Double_t*)mp;
721
722 const Double_t wobble = d[0];
723 const Double_t offset = d[1];
724
725 TrackMoon(wobble, offset);
726 }
727 //cout << "WM_PLANET: done. (return 0x8889)" << endl;
728 break;
729/*
730 case WM_NEWTRACK:
731 //cout << "WM_NewTrack: START" << endl;
732 fRaDec = *((RaDec*)mp);
733 //cout << "WM_NewTrack: done. (return 0x9999)" << endl;
734 return 0x9999;
735*/
736 case WM_LOADBENDING:
737 //cout << "WM_LoadBending: START" << endl;
738 fBending.Load("bending.txt");
739 //cout << "WM_LoadBending: done. (return 0xbe0d)" << endl;
740 break;
741
742 case WM_RESETBENDING:
743 //cout << "WM_ResetBending: START" << endl;
744 fBending.Reset();
745 //cout << "WM_ResetBending: done. (return 0xbe0e)" << endl;
746 break;
747
748 case WM_CALCALTAZ:
749 {
750 cout << endl;
751
752 SlaStars sla(fObservatory);
753 sla.Now();
754
755 XY xy = *((XY*)mp);
756 RaDec rd(xy.X()*15., xy.Y()); // [deg]
757
758 ZdAz a1 = sla.CalcZdAz(rd*TMath::DegToRad()); // [rad]
759
760 cout << "Ra/Dec source: " << xy.X() << "h " << xy.Y() << "°" << endl;
761 cout << "Zd/Az target: " << a1.Zd()*TMath::RadToDeg() << "° " << a1.Az()*TMath::RadToDeg() << "°" << endl;
762
763 if (fMac1 && fMac2)
764 a1 = AlignTrackingPos(a1);
765
766 a1 = fBending(a1);
767 CheckRange(a1);
768 a1 *= TMath::RadToDeg();
769
770 const ZdAz a2 = a1/360;
771
772 cout << "Zd/Az bended: " << a1.Zd() << "° " << a1.Az() << "°" << endl;
773 cout << "SE bended: " << a2.Zd() << " " << a2.Az() << endl;
774 }
775 break;
776
777 case WM_ENDSWITCH:
778 {
779 ZdAz pos = GetSePos()*TMath::TwoPi();
780 pos = fBending.SubtractOffsets(pos)*TMath::RadToDeg();
781
782 cout << "Endswitch Position: Zd=" << pos.Zd() << "° Az=";
783 cout << pos.Az() << "°" << endl;
784 }
785 break;
786
787 case WM_QUIT:
788 cout << "WM_Quit: now." << endl;
789 if (!CheckNetwork())
790 {
791 gLog << err << "ERROR: Cannot shutdown network." << endl;
792 gLog << " Please shutdown the drive system manually" << endl;
793 }
794 TerminateApp();
795 cout << "WM_Quit: done." << endl;
796 break;
797
798 default:
799 cout << "MCosy::Proc: Unknown message 0x" << msg << endl;
800 return 0xffffffff;
801 }
802
803 return msg;
804}
805
806void MCosy::ReadConfig(MEnv &env)
807{
808 gLog << inf2 << "Reading telescope range..." << flush;
809 const Double_t amin = env.GetValue("Az_Min[deg]", -95.0);
810 const Double_t zmin = env.GetValue("Zd_Min[deg]", -75.0);
811 fMin.Set(zmin, amin);
812
813 const Double_t amax = env.GetValue("Az_Max[deg]", 305.0);
814 const Double_t zmax = env.GetValue("Zd_Max[deg]", 98.25);
815 fMax.Set(zmax, amax);
816 gLog << "done." << endl;
817
818 gLog << all << flush;
819
820 gLog << " * Min: " << zmin << "deg " << amin << "deg" << endl;
821 gLog << " * Max: " << zmax << "deg " << amax << "deg" << endl;
822
823 fMin = fBending.AddOffsets(fMin*TMath::DegToRad());
824 fMax = fBending.AddOffsets(fMax*TMath::DegToRad());
825
826 gLog << " * Min': " << fMin.Zd()*TMath::RadToDeg() << "deg " << fMin.Az()*TMath::RadToDeg() << "deg" << endl;
827 gLog << " * Max': " << fMax.Zd()*TMath::RadToDeg() << "deg " << fMax.Az()*TMath::RadToDeg() << "deg" << endl;
828}
829
830ZdAz MCosy::GetPointingPos() const
831{
832 if (fMac1->IsZombieNode() || fMac2->IsZombieNode())
833 return ZdAz(0, 0);
834
835 // GetPointingPos [deg]
836 const ZdAz seist = GetSePos()*TMath::TwoPi(); // [rad]
837 return fBending.CorrectBack(seist)*TMath::RadToDeg();
838}
839
840Bool_t MCosy::HandleTimer(TTimer *t)
841{
842 const Int_t rc = fMutexGui.TryLock();
843 if (rc==13)
844 gLog << warn << "MCosy::HandleTimer - mutex is already locked by this thread" << endl;
845
846 if (rc)
847 {
848 gLog << warn << "* GUI update skipped due to locked mutex." << endl;
849 return kTRUE;
850 }
851
852 //
853 // Update Gui, foremer MTGui.
854 //
855 if (fMac1)
856 fMac1->DisplayVal();
857 if (fMac2)
858 fMac2->DisplayVal();
859
860 /*
861 Byte_t avail = 0;
862
863 avail |= (fMac1 && !fMac1->IsZombieNode()) ? 0x01 : 0;
864 avail |= (fMac2 && !fMac2->IsZombieNode()) ? 0x02 : 0;
865// avail |= (!(fStatus&MDriveCom::kError) && 1 ? 0x40 : 0;
866 */
867 Bool_t armed = kTRUE;
868
869 armed &= fMac1 && fMac1->IsArmed();
870 armed &= fMac2 && fMac2->IsArmed();
871
872 if (HasError())
873 SetStatus(MDriveCom::kError);
874
875 const TString stataz = fMac1 ? fMac1->GetStatusDKC() : "";
876 const TString statzd = fMac2 ? fMac2->GetStatusDKC() : "";
877
878 const UInt_t stat1 = fMac1 ? fMac1->GetStatusPdo3() : 0;
879 const UInt_t stat2 = fMac2 ? fMac2->GetStatusPdo3() : 0;
880
881 ZdAz bendist = GetPointingPos();
882
883 //cout << (fStatus&MDriveCom::kTracking?"TRA: ":"POS: ") << bendist.Zd() << " " << bendist.Az() << endl;
884
885 static MTimeout tout(0);
886 if (tout.HasTimedOut())
887 {
888 tout.Start(999);
889 fCom->SendReport(fStatus, fRaDec, fHourAngle, fZdAzSoll, bendist, fTrackingError, armed,
890 fStarguider ? fStarguider->GetStarguiderMode() : 0);
891 }
892
893 fWin->UpdateWeather(*fCom);
894 fWin->Update(bendist, fTrackingError, /*fVelocity, fOffset,*/
895 fRaDec, fZdAzSoll, fStatus, (stat1<<8)|stat2, HasConnection(), armed, statzd, stataz);
896
897 gLog.UpdateGui();
898
899 if (fMutexGui.UnLock()==13)
900 gLog << warn << "MCosy::HandleTimer - tried to unlock mutex locked by other thread." << endl;
901
902 return kTRUE;
903}
904
905// --------------------------------------------------------------------------
906//
907// Start the work of the application:
908//
909// Start the Can-Network.
910// Start the MCosy::TalkThread thread.
911// turn on the gui update
912//
913void MCosy::Start(MEnv &env)
914{
915 // Don't call this function twice!
916 Network::Start();
917
918 CheckForError();
919
920 ReadConfig(env);
921
922 gLog << inf << "- Starting GUI update." << endl;
923 fUpdateGui->TurnOn();
924}
925
926// --------------------------------------------------------------------------
927//
928// Start the work of the application:
929//
930// Turn of the gui update
931// stop the MCosy::TalkThread thread.
932// Stop the network
933//
934void MCosy::Stop()
935{
936 gLog << inf << "- Stopping GUI update." << endl;
937 fUpdateGui->TurnOff();
938 gLog << inf << "- GUI Update stopped." << endl;
939
940 gLog << inf << "- Stopping CAN network." << endl;
941 Network::Stop();
942 gLog << inf << "- CAN network stopped." << endl;
943
944 gLog << inf << "- Stopping message queue." << endl;
945 CancelThread();
946 gLog << inf << "- Message queue stopped." << endl;
947}
948
949TString MCosy::GetFileName(const char *path, const char *name, const char *ext)
950{
951 // FIXME: Timeout missing
952
953 while (1)
954 {
955 MTime time(-1);
956
957 // This is the full qualified date which is part of the name
958 const TString clock = time.GetStringFmt("%Y%m%d_%H%M%S");
959
960 // This gives the night in which the date belongs to
961 time.SetMjd(TMath::Nint(time.GetMjd()));
962
963 const TString night = time.GetStringFmt("%Y_%m_%d");
964
965 const TString dir = Form("%s/%s", path, night.Data());
966 const TString fname = Form("%s_%s.%s", name, clock.Data(), ext);
967
968 const TString full = Form("%s/%s", dir.Data(), fname.Data());
969
970 gSystem->mkdir(dir, kTRUE);
971
972 if (gSystem->AccessPathName(full, kFileExists))
973 return full;
974
975 break;// !!!!!!!!!!!!!!!!!!!!!!!
976
977 usleep(1000);
978 }
979 return "";
980}
981
982MCosy::MCosy(MEnv &env, MDriveCom *com)
983: Network(), fObservatory(MObservatory::kMagic1), fStarguider(NULL),
984fMac1(0), fMac2(0), fStatus(MDriveCom::kStopped), fOutTp(0), fOutRep(0)
985{
986 const Int_t id1 = env.GetValue("Az_Id", 1);
987 const Int_t id2 = env.GetValue("Zd_Id", 3);
988
989 fFilePrepos = env.GetValue("FilePredefinedPositions", "prepos.txt");
990
991 TString name = GetFileName("rep", "cosy", "rep");
992 gLog << inf << "Open Repfile: " << name << endl;
993 fOutRep = new MLog(name, kTRUE);
994 *fOutRep << all << "[Drive Report File]" << endl;
995 *fOutRep << "Version <cvs>" << endl;
996 *fOutRep << "Date " << MTime(-1) << endl;
997 *fOutRep << "[Reports]" << endl;
998
999 const TString pointing = env.GetValue("PointingModel", "bending.txt");
1000
1001 gLog << all << "Reading pointing model from " << pointing << "..." << endl;
1002 if (fBending.Load(pointing))
1003 gLog << all << "Reading pointing model from " << pointing << " successfull." << endl;
1004 else
1005 gLog << err << "ERROR - Reading pointing model from " << pointing << endl;
1006
1007 //
1008 // Create Nodes
1009 //
1010 gLog << inf << "- Setting up network." << endl;
1011
1012 fMac1=new Dkc(id1, "DKC/Az");
1013 fMac2=new Dkc(id2, "DKC/Zd");
1014
1015 fMac1->SetReport(fOutRep);
1016 fMac2->SetReport(fOutRep);
1017
1018 gLog << inf << "- Connecting devices to network." << endl;
1019
1020 //
1021 // Connect the devices to the network
1022 //
1023 SetNode(fMac1);
1024 SetNode(fMac2);
1025
1026 //
1027 // Create Gui Event timer and Gui
1028 //
1029 gLog << inf << "- Initializing GUI Timer." << endl;
1030 fUpdateGui = new TTimer(this, 100); // 100ms
1031
1032 gLog << all << "- Starting GUI." << endl;
1033 fWin=new MGCosy(fObservatory, fFilePrepos, this);
1034
1035 gLog.SetOutputGui(fWin->GetLog(), kTRUE);
1036
1037 fMac2->SetDisplay(fWin->GetLabel2());
1038 fMac1->SetDisplay(fWin->GetLabel1());
1039
1040 fCom = com;//new MDriveCom(this, addr, tx, rx, fOutRep);
1041 fCom->SetOutRep(fOutRep);
1042 // fCom->Start();
1043}
1044
1045void MCosy::TerminateApp()
1046{
1047 gLog << inf2 << "MCosy::TerminateApp()" << endl;
1048/*
1049 Int_t rc;
1050 TGMessageBox msg(this, gClient->GetRoot(),
1051 "Information",
1052 "Cosy is shutting down the system - this may take wa while!",
1053 kMBIconExclamation,
1054 kMBOK, //kMBClose
1055 &rc, 0);
1056*/
1057
1058 gLog.DisableOutputDevice(MLog::eGui);
1059 // FIXME: WHY DOES THIS CRASH THE APPLICATIOn WHILE TRAKING?
1060 // gLog.SetOutputGui(NULL, kFALSE);
1061
1062 gApplication->Terminate(0);
1063}
1064
1065MCosy::~MCosy()
1066{
1067 if(fCom)
1068 {
1069 fCom->SetMsgQueue(NULL);
1070 fCom->SetOutRep(NULL);
1071 }
1072
1073 gLog << inf2 << "Deleting GUI timer." << endl;
1074 // FIXME: Wait until last Update was finished!!!
1075 delete fUpdateGui;
1076
1077 //fMutexGui.Lock();
1078
1079 // Now the files can safely be closed
1080 gLog << inf2 << "Closing output files." << endl;
1081 if (fOutTp)
1082 {
1083 *fOutTp << "END" << endl;
1084 delete fOutTp;
1085 }
1086
1087 delete fOutRep;
1088
1089 //gLog << inf2 << "Deleting CC communication." << endl;
1090 //delete fCom;
1091
1092 gLog << inf2 << "Deleting Nodes." << endl;
1093 fMac1->SetReport(0);
1094 fMac2->SetReport(0);
1095
1096 delete fMac1;
1097 delete fMac2;
1098
1099 gLog << inf2 << "Deleting MGCosy." << endl;
1100
1101 gLog.DisableOutputDevice(MLog::eGui);
1102
1103 delete fWin;
1104
1105 gLog << inf2 << "MGCosy destructed." << endl;
1106}
Note: See TracBrowser for help on using the repository browser.