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

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