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

Last change on this file since 8864 was 8864, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 44.5 KB
Line 
1#include "MCosy.h"
2
3#include <iomanip>
4#include <fstream>
5
6#include <TEnv.h>
7#include <TTimer.h>
8#include <TApplication.h>
9
10#include <TH2.h>
11#include <TH3.h>
12#include <TProfile.h>
13#include <TCanvas.h>
14
15//#include "MLog.h"
16#include "MLogManip.h"
17
18#include "MEnv.h"
19#include "MTime.h"
20#include "MPointing.h"
21
22#include "MGCosy.h"
23#include "MDriveCom.h"
24#include "MStarguider.h"
25#include "SlaStars.h"
26#include "MTracking.h"
27
28#include "macs.h"
29#include "shaftencoder.h"
30
31ClassImp(MCosy);
32
33using namespace std;
34
35typedef struct tm tm_t;
36
37/* +===================================+
38 FIXME: What if fMac3 (Sync) died?
39 +===================================+
40*/
41
42//#define EXPERT
43#undef EXPERT
44
45/*
46ZdAz MCosy::CorrectTarget(const ZdAz &src, const ZdAz &dst)
47{
48 // CorrectTarget [se]
49
50 // src [se]
51 // dst [rad]
52
53 // fAltMax = 70
54 // fAltMin = -105/110
55 // fAzMin = -355
56 // fAzMax = 355
57
58 ZdAz source = src * 360.0/16384.0;
59 ZdAz dest = dst * TMath::RadToDeg();
60
61 if (dest.Zd()>-3 && dest.Zd()<3)
62 dest.Zd(dest.Zd()<0?-3:3);
63
64 if (dest.Zd()>-1e-6 && dest.Zd()<1e-6)
65 return dst*(16384.0/k2Pi);
66
67 const float fZdMin = -67;
68 const float fZdMax = 67;
69 const float fAzMin = -29;
70 const float fAzMax = 423;
71
72 //
73 // This corrects to target for the shortest distance, not for the fastest move!
74 //
75 ZdAz s = source-dest;
76
77 float min = s.Sqr();
78
79 //
80 // Is it enought to search inside one revolution?
81 //
82 ZdAz ret = dest;
83
84 for (int i=-5; i<5+1; i++)
85 {
86 const ZdAz p(i%2 ? -dest.Zd() : dest.Zd(), dest.Az() - i*180);
87
88 //
89 // Range Check
90 //
91 if (p.Zd()<fZdMin || p.Zd()>fZdMax)
92 continue;
93
94 if (p.Az()<fAzMin || p.Az()>fAzMax)
95 continue;
96
97 //
98 // Calculate distance
99 //
100 s = source-p;
101
102 const float dist = s.Sqr();
103
104 if (dist > min)
105 continue;
106
107 //
108 // New shortest distance
109 //
110 ret = p;
111 min = dist;
112 }
113 return ret*(16384.0/360.0);
114}
115*/
116
117MTTalk::~MTTalk()
118{
119 gLog << inf2 << "~MTTalk::CancelThread" << std::endl;
120 CancelThread();
121 gLog << inf2 << "~MTTalk::MTTalk" << std::endl;
122}
123
124// --------------------------------------------------------------------------
125//
126// GetSePos, reads the Shaftencoder positions from the Can-drivers
127// for the shaftencoders. The two shaft encoders at the elevation axis
128// are avaraged. The values are returned as a ZdAz object.
129//
130// If one of the two shaftencoders on the elevation axis is missing
131// the other one's position is returned.
132//
133// The positions are alway up-to-date because the shaftencoders are
134// sending all changes immediatly.
135//
136ZdAz MCosy::GetSePos() const
137{
138 const int pa = fAz->GetPos();
139 if (fZd1->IsZombieNode() && fZd2->IsZombieNode())
140 return ZdAz(0, pa);
141
142 //
143 // Get the values (FIXME!)
144 //
145 //int p1 = (fZd1->GetPos()+fZd1->GetPhysRes()/2)%fZd1->GetPhysRes();
146 //int p2 = -(fZd2->GetPos()+fZd2->GetPhysRes()/2)%fZd2->GetPhysRes();
147
148 int p1 = fZd1->GetPos();//+fZd1->GetPhysRes()/2)%fZd1->GetPhysRes();
149 int p2 = -fZd2->GetPos();//+fZd2->GetPhysRes()/2)%fZd2->GetPhysRes();
150
151 if (fZd1->IsZombieNode())
152 return ZdAz(p2, pa);
153 if (fZd2->IsZombieNode())
154 return ZdAz(p1, pa);
155
156 //
157 // interpolate shaft encoder positions
158 //
159 float p = (float)(p1+p2)/2;
160
161 return ZdAz(p, pa);
162}
163
164// --------------------------------------------------------------------------
165//
166// reads the Rotary encoder positions from the last request of the Macs.
167//
168// The positions are returned as a ZdAz object. Use RequestRePos to request
169// the current positions first.
170//
171ZdAz MCosy::GetRePos()
172{
173 return ZdAz(fMac2->GetPos(), fMac1->GetPos());
174}
175
176// --------------------------------------------------------------------------
177//
178// reads the Rotary encoder positions from the Macs.
179//
180// The positions are returned as a ZdAz object. The positions are the ones
181// which are send as PDOs to the computer. This is done at a given
182// frequency. Which means, that this positions are not ought to be
183// up-to-date.
184//
185ZdAz MCosy::GetRePosPdo()
186{
187 return ZdAz(fMac2->GetPdoPos(), fMac1->GetPdoPos());
188}
189
190// --------------------------------------------------------------------------
191//
192// check for a break-signal (from the msgqueue) and errors.
193//
194int MCosy::StopWaitingForSDO() const
195{
196 return 0/*Break() || HasError()*/;
197}
198
199// --------------------------------------------------------------------------
200//
201// Waits for a movement to become finished.
202//
203// First waits for all peding Sdos, then waits until both motors are stopped
204// or waiting for SDOs was stopped (either by an error or by Break)
205//
206void MCosy::WaitForEndMovement()
207{
208 // FIXME, what when waiting times out (Zombie)
209 if (!fMac1 || !fMac2)
210 return;
211
212 while ((fMac1->IsPositioning() || fMac2->IsPositioning()) &&
213 !(Break() || HasError() || HasZombie()))
214 usleep(1);
215
216 if (!Break() && !HasError() && !HasZombie())
217 return;
218
219 MTime t(-1);
220 gLog << inf << t << " - MCosy::WaitForEndMovement aborted...";
221 if (Break())
222 gLog << " Break signal...";
223 if (HasError())
224 gLog << " Network has error...";
225 if (HasZombie())
226 gLog << " Network has zombie...";
227 gLog << endl;
228}
229
230// --------------------------------------------------------------------------
231//
232// Check for an error...
233//
234// This is ment for usage after the Action: All Motors Stop.
235//
236void MCosy::CheckForError()
237{
238 //
239 // Check all Can-Nodes for an Error. If there is no error the motor
240 // status is set to stopped.
241 //
242 if (HasError() || HasZombie())
243 {
244 SetStatus(MDriveCom::kError);
245 return;
246 }
247
248 if (fMac1->IsPositioning() || fMac2->IsPositioning())
249 SetStatus(MDriveCom::kMoving);
250 else
251 SetStatus(MDriveCom::kStopped);
252
253 //
254 // If there is an error, the error status is set to Error.
255 //
256
257 /*
258 FIXME: HANDLINGE ERROR
259
260 //
261 // Now try to handle the error.
262 //
263 fMac1->HandleError();
264 fMac2->HandleError();
265
266 //
267 // If the error couldn't get solved return
268 //
269 if (HasError())
270 return;
271
272 //
273 // Set motor status to stopped
274 //
275 SetStatus(MDriveCom::kStopped);
276 */
277}
278
279Bool_t MCosy::CheckRange(const ZdAz &d) const
280{
281 // d [rad]
282
283 if (d.Zd()<fMin.Zd())
284 {
285 gLog << err << "ERROR: Requested Zenith Angle below negative endswitch." << endl;
286 return kFALSE;
287 }
288
289 if (d.Zd()>fMax.Zd())
290 {
291 gLog << err << "ERROR: Requested Zenith Angle behind positive endswitch." << endl;
292 return kFALSE;
293 }
294
295 if (d.Az()<fMin.Az())
296 {
297 gLog << err << "ERROR: Requested Azimuth Angle below negative endswitch." << endl;
298 return kFALSE;
299 }
300
301 if (d.Az()>fMax.Az())
302 {
303 gLog << err << "ERROR: Requested Azimuth Angle behind positive endswitch." << endl;
304 return kFALSE;
305 }
306
307
308 return kTRUE;
309}
310
311ZdAz MCosy::AlignTrackingPos(ZdAz pointing) const
312{
313 // pointing [rad]
314 // AlignTrackingPos [deg]
315
316 pointing *= TMath::RadToDeg();
317
318 if (pointing.Zd()<0)
319 {
320 pointing.Zd(-pointing.Zd());
321 pointing.Az(pointing.Az()+180);
322 //gLog << "ZD=-ZD Az+=180" << endl;
323 }
324
325 const ZdAz se = GetSePos()*TMath::TwoPi()/kResSE; // [rad]
326 const ZdAz unbendedse = fBending.CorrectBack(se)*TMath::RadToDeg(); // ist pointing
327
328 //gLog << "Unbended: " << unbendedse.Zd() << " " << unbendedse.Az() << endl;
329
330 do
331 {
332 const Double_t d = unbendedse.Az() - pointing.Az();
333 if (d>-180 && d<=180)
334 break;
335
336 //gLog << "AZ += " << TMath::Sign(360., d) << endl;
337
338 pointing.Az(pointing.Az()+TMath::Sign(360., d));
339 } while (1);
340
341 return pointing/TMath::RadToDeg();
342/*
343 const Bool_t rc = CheckRange(pointing);
344 za = pointing/TMath::RadToDeg(); // [rad]
345
346 if (!rc)
347 gLog << "Error: Aligned position out of Range." << endl;
348
349 return rc;*/
350}
351
352Double_t MCosy::Starguider(Double_t mjd, ZdAz &dest) const
353{
354 ifstream fin("pointingpos.txt");
355 if (!fin)
356 return -1;
357
358 Double_t mjd0, zd, az;
359 fin >> mjd0 >> zd >> az;
360
361 mjd0 += 52000;
362
363 if (mjd0+1./24/60 <mjd)
364 return -1;
365
366 ZdAz point=AlignTrackingPos(ZdAz(zd, az)/TMath::RadToDeg());
367 /*
368 if (!AlignTrackingPos(ZdAz(zd, az), point))
369 {
370 cout << "Starguider position couldn't be aligned..." << endl;
371 return -1;
372 }*/
373
374 // FIXME: Check Range missing!
375
376 const ZdAz diff = (dest-point)*TMath::RadToDeg();
377
378 if (diff.Zd()>5 || diff.Az()>5)
379 {
380 cout << "Starguider deviation too large... dZd=" << diff.Zd() <<" dAz="<<diff.Az() << endl;
381 return -1;
382 }
383
384 dest -= point;
385 dest *= -kGearTot/TMath::TwoPi(); // [re]
386
387 cout << "Using Starguider... dZd=" << dest.Zd() << " dAz=" << dest.Az() << endl;
388
389 return (mjd-mjd0) * (24*60*60); // [s]
390}
391
392// --------------------------------------------------------------------------
393//
394// Move the telescope to the given position. The position must be given in
395// a ZdAz object in rad.
396//
397// The first positioning is done absolutely. If we didn't reach the
398// correct psotion we try to correct for this by 10 relative position
399// maneuvers. If this doesn't help positioning failed.
400//
401// As a reference the shaftencoder values are used.
402//
403int MCosy::SetPosition(const ZdAz &dst, Bool_t track) // [rad]
404{
405 MSlewing point(this);
406
407//#ifdef EXPERT
408// point.SetAccDec(0.4, 0.4);
409// point.SetVelocity(0.2); // fast: 0.6, slow: 0.2
410//#else
411
412// original settings
413//
414 point.SetPointAccDec(0.2, 0.1);
415
416 point.SetPointVelocity(0.1);
417
418
419//#endif
420
421// point.SetPointAccDec(0.4, 0.4);
422// point.SetPointVelocity(0.4);
423
424
425// original
426 return point.SetPosition(dst, track);
427
428// test
429// return point.SetPosition(dst, kTRUE);
430
431
432
433}
434
435void MCosy::SetTrackingPosRE(ZdAz za)
436{
437 za /= kGearTot; // [U_tel]
438 za *= TMath::TwoPi(); // [rad]
439 //cout << "RE1: " << za.Zd()*180/3.1415 << " " << za.Az()*180/3.1415 << endl;
440 fTrackingPosRaw = za*TMath::RadToDeg();
441 fTrackingPos = fBending.CorrectBack(za)*TMath::RadToDeg();
442 //cout << "RE2: " << fTrackingPos.Zd() << " " << fTrackingPos.Az() << endl;
443}
444
445void MCosy::TrackPosition(const RaDec &dst) // ra, dec [rad]
446{
447 MTracking track(this);
448 track.SetOut(fOutRep);
449//#ifdef EXPERT
450// track.SetPointAccDec(0.4, 0.4);
451// track.SetPointVelocity(0.2); // fast: 0.6, slow: 0.2
452//#else
453
454
455
456 track.SetPointAccDec(0.2, 0.1);
457 track.SetPointVelocity(0.1);
458
459 // track.SetPointAccDec(0.4, 0.4);
460 // track.SetPointVelocity(0.4);
461
462
463
464//#endif
465 track.SetTrackAccDec(0.1, 0.1);
466
467 track.TrackPosition(dst);
468}
469
470void MCosy::TrackPositionGRB(const RaDec &dst) // ra, dec [rad]
471{
472 MTracking track(this);
473 track.SetOut(fOutRep);
474//#ifdef EXPERT
475// track.SetPointAccDec(0.4, 0.4);
476// track.SetPointVelocity(0.2); // fast: 0.6, slow: 0.2
477//#else
478 track.SetPointAccDec(0.4, 0.4);
479 track.SetPointVelocity(0.3);
480//#endif
481 track.SetTrackAccDec(0.1, 0.1);
482
483 track.TrackPosition(dst);
484}
485
486// --------------------------------------------------------------------------
487//
488// Stops the movement of both motors.
489//
490// Sets the status to stopping. Sets the deceleration to 50% of the maximum.
491// stops. Quits the revolution mode and wait for the end of the movement.
492//
493void MCosy::StopMovement()
494{
495 //
496 // Set status to Stopping
497 //
498 SetStatus(MDriveCom::kStopping);
499
500 //
501 // set deceleration to 50%
502 //
503 gLog << inf2 << "Stopping movement (dec=30%)..." << endl;
504 if (fMac1 && fMac2)
505 {
506#ifdef EXPERT
507 fMac1->SetDeceleration(TMath::Nint(0.5*fMac1->GetVelRes()));
508 fMac2->SetDeceleration(TMath::Nint(0.5*fMac2->GetVelRes()));
509#else
510 fMac1->SetDeceleration(TMath::Nint(0.3*fMac1->GetVelRes()));
511 fMac2->SetDeceleration(TMath::Nint(0.3*fMac2->GetVelRes()));
512#endif
513 fMac1->SetRpmMode(FALSE);
514 fMac2->SetRpmMode(FALSE);
515 }
516
517/*
518 fMac1->SetDeceleration(0.3*fMac1->GetVelRes());
519 fMac2->SetDeceleration(0.3*fMac2->GetVelRes());
520
521 fMac2->SendSDO(0x3000, Macs::string('s','t','o','p'));
522 fMac1->SendSDO(0x3000, Macs::string('s','t','o','p'));
523 fMac2->WaitForSdo(0x3000, 0);
524 fMac1->WaitForSdo(0x3000, 0);
525 fMac1->SetRpmMode(FALSE);
526 fMac2->SetRpmMode(FALSE);
527 */
528
529 //
530 // Wait for the movement to really be finished.
531 //
532#ifdef EXPERT
533 cout << "Waiting for end of movement..." << endl;
534#endif
535 WaitForEndMovement();
536
537 //
538 // Check whether everything works fine.
539 //
540 CheckForError();
541#ifdef EXPERT
542 cout << "Movement stopped." << endl;
543#endif
544}
545
546bool MCosy::CheckNetwork()
547{
548 //return kTRUE;
549 //CheckConnections();
550
551 CheckForError();
552
553 if (HasZombie())
554 {
555 gLog << warn << "- Found Zombies in Network..." << endl;
556 if (!RebootZombies())
557 return false;
558 }
559
560 /*
561 FIXME HANDLING ERROR
562 */
563 if (HasError())
564 {
565 fMac1->HandleError();
566 fMac2->HandleError();
567 if (fMac3)
568 fMac3->HandleError();
569 if (HasError() || HasZombie())
570 return false;
571 }
572
573 CheckForError();
574 return true;
575}
576
577Int_t MCosy::Proc(int msg, void *mp)
578{
579 switch (msg)
580 {
581 case WM_WAIT:
582 gLog << inf2 << "Wait for execution of Proc(WM_*, ): done." << endl;
583 return 0;
584
585 case WM_STOP:
586 //cout << "MCosy::Proc: Stop." << endl;
587 if (!CheckNetwork())
588 return 0xebb0;
589 StopMovement();
590 return 0;
591/*
592 case WM_PRESET:
593 cout << "WM_Preset: start." << endl;
594 if (!CheckNetwork())
595 return (void*)0xebb0;
596 fZd1->SetPreset();
597 fZd2->SetPreset();
598 fAz->SetPreset();
599 cout << "WM_Preset: done. (return 0xaffe)" << endl;
600 return (void*)0xaffe;
601*/
602 /*
603 case WM_CALIB:
604 {
605 cout << "WM_Calib: start." << endl;
606 if (!CheckNetwork())
607 return (void*)0xebb0;
608
609 SlaStars sla(fObservatory);
610 sla.Now();
611
612 RaDec rd = *((RaDec*)mp);
613
614 //RaDec rd(37.94, 89.2644); // POLARIS
615 //RaDec rd(213.915417, 19.1825); // ARCTURUS
616
617 cout << "Calibrating to: " << rd.Ra()*24/360 << "h " << rd.Dec() << "°" << endl;
618
619 ZdAz za=sla.CalcZdAz(rd*TMath::DegToRad())*16384.0/k2Pi;
620
621 cout << "Calc Zd: " << za.Zd() << " Az: " << za.Az() << endl;
622
623 ZdAz sepos = GetSePos();
624 cout << "Got Zd: " << sepos.Zd() << " Az: " << sepos.Az() << endl;
625
626 fZd1->SetPreset(za.Zd());
627 fZd2->SetPreset(-za.Zd());
628 fAz->SetPreset(za.Az());
629
630 cout << "WM_Calib: done. (return 0xaffe)" << endl;
631 }
632 return (void*)0xaffe;
633 */
634 case WM_TPOINT:
635 {
636 //cout << "WM_TPoint: start." << endl;
637 SlaStars sla(fObservatory);
638 sla.Now();
639
640 RaDec rd = *((RaDec*)mp);
641 cout << "TPoint Star: " << rd.Ra()/15 << "h " << rd.Dec() << "°" << endl;
642
643 AltAz za=sla.CalcAltAz(rd*TMath::DegToRad())*TMath::RadToDeg();
644
645 if (!fOutTp)
646 {
647 //
648 // open tpoint file
649 //
650 const TString name = GetFileName("tpoint", "old-tpoint", "txt");
651 cout << "TPoint-Cosy File ********* " << name << " ********** " << endl;
652
653 fOutTp = new ofstream(name);
654 *fOutTp << "Magic Model TPOINT data file" << endl;
655 *fOutTp << ": ALTAZ" << endl;
656 *fOutTp << "49 48 0 ";
657 *fOutTp << sla.GetTime().Year() << " " << sla.GetTime().Month() << " " << sla.GetTime().Day() << " ";
658 *fOutTp << /*"20 1013.25 300 0.5 0.55 0.0065" <<*/ endl;
659 // temp(°C) pressure(mB) height(m) humidity(1) wavelength(microm) troplapserate(K/m)
660 }
661
662 cout << " Alt/Az: " << za.Alt() << "° " << za.Az() << "°" << endl;
663 *fOutTp << setprecision(7) << za.Az() << " " << za.Alt() << " ";
664
665 ZdAz sepos = GetSePos()*TMath::TwoPi()/kResSE;
666 za.Set(TMath::Pi()/2-sepos.Zd(), sepos.Az());
667 za *= TMath::RadToDeg();
668
669 cout << " SE-Pos: " << za.Alt() << "° " << za.Az() << "°" << endl;
670 *fOutTp << fmod(za.Az()+360, 360) << " " << za.Alt() << " ";
671
672 if (fStarguider)
673 {
674 XY tp = fStarguider->GetCoordinates();
675 *fOutTp << 90-tp.X() << " " << tp.Y() << " ";
676 }
677
678 *fOutTp << rd.Ra()/15 << " " << rd.Dec() << " " << setprecision(11) << sla.GetMjd() << endl;
679
680 //cout << "WM_TPoint: done. (return 0xaffe)" << endl;
681 }
682 return 0xca1b;
683
684 case WM_TRACKPOS:
685 //cout << "WM_TrackPosition: start." << endl;
686 {
687 if (!CheckNetwork())
688 return 0xebb0;
689
690 ZdAz dest = *((ZdAz*)mp) * TMath::DegToRad();
691 if (!SetPosition(dest, kTRUE))
692 return 0x1234;
693
694 SlaStars sla(fObservatory);
695 sla.Now();
696
697 RaDec rd = sla.CalcRaDec(dest);
698 TrackPosition(rd);
699 }
700 //cout << "WM_TrackPosition: done. (return 0xabcd)" << endl;
701 return 0xabcd;
702/*
703 case WM_ARM:
704 //cout << "WM_Position: start." << endl;
705 {
706 if (!CheckNetwork())
707 return (void*)0xebb0;
708
709 const bool arm = *((bool*)mp);
710 if (arm)
711 {
712 fMac1->Arm();
713 fMac2->Arm();
714 cout << "ARMED" << endl;
715 }
716 else
717 {
718 fMac1->Disarm();
719 fMac2->Disarm();
720 cout << "DISARMED" << endl;
721 }
722 }
723 //cout << "WM_Position: done. (return 0x7777)" << endl;
724 return (void*)0x9999;
725 */
726 case WM_POSITION:
727 //cout << "WM_Position: start." << endl;
728 {
729 if (!CheckNetwork())
730 return 0xebb0;
731
732 ZdAz dest = *((ZdAz*)mp);
733 SetPosition(dest*TMath::DegToRad());
734 }
735 //cout << "WM_Position: done. (return 0x7777)" << endl;
736 return 0x7777;
737
738 case WM_POSITION1:
739 //cout << "WM_Position1: start." << endl;
740 {
741 if (!CheckNetwork())
742 return 0xebb0;
743
744 ZdAz dest = *((ZdAz*)mp);
745 SetPosition(dest*TMath::DegToRad(), kTRUE);
746 }
747 //cout << "WM_Position: done. (return 0x7777)" << endl;
748 return 0x7777;
749
750 case WM_PREPS:
751 //cout << "WM_Track: START" << endl;
752 {
753 if (!CheckNetwork())
754 return 0xebb0;
755
756 const char *preps = (const char*)mp;
757 cout << "Preposition command to " << preps << " received." << endl;
758
759 ifstream fin("prepos.txt");
760 if (!fin)
761 {
762 cout << "ERROR: cannot open prepos.txt." << endl;
763 return 0xebb1;
764 }
765
766 while (1)
767 {
768 Double_t zd, az;
769 fin >> zd >> az;
770
771 TString str;
772 str.ReadLine(fin);
773 if (!fin)
774 break;
775
776 str.ToLower();
777
778 if (str.Strip(TString::kBoth)==preps)
779 {
780 ZdAz dest(zd, az);
781 SetPosition(dest*TMath::DegToRad());
782 return 0x7979;
783 }
784 cout << "ERROR - Requested preposition not found in file..." << endl;
785 }
786 }
787 //cout << "WM_Track: done. (return 0x8888)" << endl;
788 return 0x7878;
789
790 case WM_TESTSE:
791 //cout << "WM_TestSe: start." << endl;
792 fBackground = mp ? kBgdSeTest : kBgdNone;
793 //cout << "WM_TestSe: done. (return 0x1e51)" << endl;
794 return 0x1e51;
795
796 case WM_GEAR:
797 //cout << "WM_Gear: start." << endl;
798 fBackground = mp ? kBgdGear : kBgdNone;
799 //cout << "WM_Gear: done. (return 0xfeaf)" << endl;
800 return 0xfeaf;
801
802 case WM_DISPLAY:
803 //cout << "WM_Display: start." << endl;
804 fTriggerDisplay = kTRUE;
805 //cout << "WM_Disply: done. (return 0xd1e1)" << endl;
806 return 0xd1e1;
807
808 case WM_TRACK:
809 case WM_GRB:
810 //cout << "WM_Track/GRB: START" << endl;
811 {
812 RaDec dest = ((RaDec*)mp)[0];
813 if (fStarguider)
814 fStarguider->SetPointingPosition(((RaDec*)mp)[1]);
815 if (!CheckNetwork())
816 return 0xebb0;
817
818 if (msg==WM_TRACK)
819 TrackPosition(dest*TMath::DegToRad());
820 else
821 TrackPositionGRB(dest*TMath::DegToRad());
822 }
823 //cout << "WM_Track/GRB: done. (return 0x8888)" << endl;
824 return 0x8888;
825
826 case WM_NEWTRACK:
827 //cout << "WM_NewTrack: START" << endl;
828 fRaDec = *((RaDec*)mp);
829 //cout << "WM_NewTrack: done. (return 0x9999)" << endl;
830 return 0x9999;
831
832 case WM_LOADBENDING:
833 //cout << "WM_LoadBending: START" << endl;
834 fBending.Load("bending.txt");
835 //cout << "WM_LoadBending: done. (return 0xbe0d)" << endl;
836 return 0xbe0d;
837
838 case WM_RESETBENDING:
839 //cout << "WM_ResetBending: START" << endl;
840 fBending.Reset();
841 //cout << "WM_ResetBending: done. (return 0xbe0e)" << endl;
842 return 0xbe0e;
843
844 case WM_HOME:
845 //cout << "WM_Home: START" << endl;
846 if (!CheckNetwork())
847 return 0xebb0;
848 else
849 {
850 cout << "HOME NOT ALLOWED... for Magic." << endl;
851 /*
852 cout << "Going Home..." << endl;
853 TEnv env(".cosyrc");
854
855 SetStatus(MDriveCom::kMoving);
856
857 fMac1->SetHome(250000, env.GetValue("Az_MaxTime2ReachHome[s]", 100));
858 fMac2->SetHome(250000, env.GetValue("Zd_MaxTime2ReachHome[s]", 100));
859
860 gLog << "SETHOME DONE" << endl;
861
862 SetStatus(HasError() ? MDriveCom::kError : MDriveCom::kStopped);
863
864 fAz->SetPreset();
865 fZd1->SetPreset();
866 fZd2->SetPreset();
867
868 fMac1->ReqPos();
869 fMac2->ReqPos();
870 fMac3->StopMotor();
871 */
872 }
873 //cout << "WM_Home: done. (return 0x403e)" << endl;
874 return 0x403e;
875
876 case WM_CALCALTAZ:
877 {
878 cout << endl;
879
880 SlaStars sla(fObservatory);
881 sla.Now();
882
883 XY xy = *((XY*)mp);
884 RaDec rd(xy.X()*15., xy.Y()); // [deg]
885
886 ZdAz a1 = sla.CalcZdAz(rd*TMath::DegToRad()); // [rad]
887
888 cout << "Ra/Dec source: " << xy.X() << "h " << xy.Y() << "°" << endl;
889 cout << "Zd/Az target: " << a1.Zd()*TMath::RadToDeg() << "° " << a1.Az()*TMath::RadToDeg() << "°" << endl;
890
891 if (fZd1 && fZd2 && fAz)
892 a1 = AlignTrackingPos(a1);
893
894 a1 = fBending(a1);
895 CheckRange(a1);
896 a1 *= TMath::RadToDeg();
897
898 const ZdAz a2 = a1*kResSE/360;
899
900 cout << "Zd/Az bended: " << a1.Zd() << "° " << a1.Az() << "°" << endl;
901 cout << "SE bended: " << a2.Zd() << " " << a2.Az() << endl;
902 }
903 return 0xa17a;
904
905 case WM_ENDSWITCH:
906 {
907 ZdAz pos = GetSePos()*TMath::TwoPi()/kResSE;
908 pos = fBending.SubtractOffsets(pos)*TMath::RadToDeg();
909
910 cout << "Endswitch Position: Zd=" << pos.Zd() << "° Az=";
911 cout << pos.Az() << "°" << endl;
912 }
913
914 return 0x1010;
915
916 case WM_QUIT:
917 cout << "WM_Quit: now." << endl;
918 if (!CheckNetwork())
919 {
920 gLog << err << "ERROR: Cannot shutdown CANbus network." << endl;
921 return 0xebb0;
922 }
923 TerminateApp();
924 cout << "WM_Quit: done." << endl;
925 return 0xaaaa;
926 }
927 cout << "MCosy::Proc: Unknown message 0x" << msg << endl;
928 return 0xffffffff;
929}
930
931Int_t MTTalk::Thread()
932{
933 fCosy->TalkThread();
934 return 0;
935}
936
937void MCosy::ReadConfig(MEnv &env)
938{
939 gLog << inf2 << "Reading telescope range..." << flush;
940 const Double_t amin = env.GetValue("Az_Min[deg]", -95.0);
941 const Double_t zmin = env.GetValue("Zd_Min[deg]", -75.0);
942 fMin.Set(zmin, amin);
943
944 const Double_t amax = env.GetValue("Az_Max[deg]", 305.0);
945 const Double_t zmax = env.GetValue("Zd_Max[deg]", 98.25);
946 fMax.Set(zmax, amax);
947 gLog << "done." << endl;
948
949 gLog << all << flush;
950
951 gLog << " * Min: " << zmin << "deg " << amin << "deg" << endl;
952 gLog << " * Max: " << zmax << "deg " << amax << "deg" << endl;
953
954 fMin = fBending.AddOffsets(fMin*TMath::DegToRad());
955 fMax = fBending.AddOffsets(fMax*TMath::DegToRad());
956
957 gLog << " * Min': " << fMin.Zd()*TMath::RadToDeg() << "deg " << fMin.Az()*TMath::RadToDeg() << "deg" << endl;
958 gLog << " * Max': " << fMax.Zd()*TMath::RadToDeg() << "deg " << fMax.Az()*TMath::RadToDeg() << "deg" << endl;
959
960 kGear.Set(env.GetValue("Zd_GearRatio[U_mot/U_tel]", 1000.0),
961 env.GetValue("Az_GearRatio[U_mot/U_tel]", 1000.0));
962
963 Bool_t mac1 = fMac1 && !fMac1->IsZombieNode();
964 Bool_t mac2 = fMac2 && !fMac2->IsZombieNode();
965 Bool_t mac3 = fMac3 && !fMac3->IsZombieNode();
966
967 Bool_t zd1 = fZd1 && !fZd1->IsZombieNode();
968 Bool_t zd2 = fZd2 && !fZd2->IsZombieNode();
969 Bool_t az = fAz && !fAz->IsZombieNode();
970
971 Double_t x = 0;
972 Double_t y = 0;
973 y = mac1 ? fMac1->GetRes() : (mac3 ? fMac3->GetRes() : env.GetValue("Az_ResRE[re/U_mot]", 1500));
974 x = mac2 ? fMac2->GetRes() : env.GetValue("Zd_ResRE[re/U_mot]", 1500);
975 kResRE.Set(x,y);
976
977 gLog << " * Az RE resolution: " << x << " re/U_mot" << endl;
978 gLog << " * Zd RE resolution: " << y << " re/U_mot" << endl;
979
980 x = zd1 ? fZd1->GetPhysRes() : (zd2 ? fZd2->GetPhysRes() : env.GetValue("Zd_ResSE[se/U_tel]", 16384));
981 y = az ? fAz->GetPhysRes() : env.GetValue("Az_ResSE[se/U_tel]", 16384);
982 kResSE.Set(x,y);
983
984 gLog << " * Az SE resolution: " << x << " se/U_tel" << endl;
985 gLog << " * Zd SE resolution: " << y << " se/U_tel" << endl;
986
987 /*
988 kResRE.Y(0);
989 if (fMac1 && !fMac1->IsZombieNode())
990 kResRE.Y(fMac1->GetRes());
991 else
992 y = fMac3 && !fMac3->IsZombieNode() ? fMac3->GetRes() : env.GetValue("Az_ResRE[re/U_mot]", 1500);
993 if (fMac3 && !fMac3->IsZombieNode())
994 kResRE.Y(fMac3->GetRes());
995 else
996 kResRE.Y(env.GetValue("Az_ResRE[re/U_mot]", 1500));
997
998 kResRE.X(0);
999 if (fMac2 && !fMac2->IsZombieNode())
1000 kResRE.X(fMac2->GetRes());
1001 else
1002 kResRE.X(env.GetValue("Zd_ResRE[re/U_mot]", 1500));
1003
1004 kResSE.X(0);
1005 if (fZd1 && !fZd1->IsZombieNode())
1006 kResSE.X(fZd1->GetPhysRes());
1007 else
1008 if (fZd2 && !fZd2->IsZombieNode())
1009 kResSE.X(fZd2->GetPhysRes());
1010 else
1011 kResSE.X(env.GetValue("Zd_ResSE[se/U_mot]", 16384));
1012
1013 kResSE.Y(0);
1014 if (fAz && !fAz->IsZombieNode())
1015 kResSE.Y(fAz->GetPhysRes());
1016 else
1017 kResSE.Y(env.GetValue("Az_ResSE[se/U_mot]", 16384));
1018 */
1019
1020 // believing the Macs manual '*4' shouldn't be necessary, but it is.
1021 // Because the a RE is 4 quad counts.
1022 // Calculating speeds we have to convert back to qc
1023 kResRE *= 4;
1024 kGearTot = Mul(kResRE, kGear);
1025// kGearTot = kResRE*kGear;
1026
1027// gLog << all;
1028// gLog << " * Setting Gear Ratios:" << endl;
1029 // gLog << " --------------------" << endl;
1030 gLog << " * X: " << kGear.X() << "*" << kResRE.X()/4 << "/" << kResSE.X() << "=4*" << kGearTot.X() << "/" << kResSE.X() << endl;
1031 gLog << " * Y: " << kGear.Y() << "*" << kResRE.Y()/4 << "/" << kResSE.Y() << "=4*" << kGearTot.Y() << "/" << kResSE.Y() << endl;
1032}
1033/*
1034void MCosy::InitSync()
1035{
1036 if (!fMac3)
1037 {
1038 gLog << "Unable to Init Sync! Mac3 not available." << endl;
1039 return;
1040 }
1041
1042 const int res = fMac3->GetVelRes();
1043
1044 fMac3->SetVelocity(0.3*res);
1045 fMac3->SetAcceleration(0.2*res);
1046 fMac3->SetDeceleration(0.2*res);
1047 fMac3->StartPosSync();
1048}
1049*/
1050void MCosy::TalkThreadSeTest()
1051{
1052// if (fZd1->IsZombieNode() || fZd2->IsZombieNode())
1053 // return;
1054
1055 if (fHist)
1056 {
1057 gLog << err << "You are much too fast... try again." << endl;
1058 return;
1059 }
1060
1061 fHist = new TH2F("Diff", "Difference of SE values",
1062 201, fMin.Zd(), fMax.Zd(), 41, -10.5, 10.5);
1063 fHist->SetXTitle("ZA [\\circ]");
1064 fHist->SetYTitle("\\Delta SE");
1065
1066 Double_t offset = 0;
1067
1068 int cnt = 0;
1069
1070 gLog << inf2 << "Starting Shaftencoder Test..." << endl;
1071
1072 gLog << err << "Build in values!!!" << endl;
1073
1074 while (fBackground==kBgdSeTest)
1075 {
1076 fZd1->ResetPosHasChanged();
1077 fZd2->ResetPosHasChanged();
1078
1079 while (!fZd1->PosHasChanged() && !fZd2->PosHasChanged() &&
1080 fBackground==kBgdSeTest)
1081 {
1082 usleep(1);
1083 TThread::CancelPoint();
1084 }
1085
1086 const Double_t pos[3] = {
1087 (fZd1->GetPos()+8192)%16384,
1088 (fZd2->GetPos()+8192)%16384,
1089 fAz->GetPos() };
1090
1091 //
1092 // Estimate Offset from the first ten positions
1093 //
1094 if (cnt++<10)
1095 {
1096 offset += pos[0]+pos[1];
1097 continue;
1098 }
1099 if (cnt==11)
1100 {
1101 offset /= 10;
1102 cnt++;
1103 }
1104
1105 Double_t apos = (pos[0]-pos[1])/2 * TMath::TwoPi() / kResSE.X();
1106
1107 ZdAz bend = fBending.CorrectBack(ZdAz(apos, pos[2]))*TMath::RadToDeg();
1108 fHist->Fill(bend.Zd(), pos[0]+pos[1]-offset);
1109 }
1110
1111 gLog << inf2 << "Shaftencoder Test Stopped... displaying Histogram." << endl;
1112
1113 fBackground=kBgdSeTestDispl;
1114}
1115
1116void MCosy::TalkThreadGear()
1117{
1118// if (fZd1->IsZombieNode() || fZd2->IsZombieNode())
1119 // return;
1120
1121 if (fHist)
1122 {
1123 gLog << err << "You are much too fast... try again." << endl;
1124 return;
1125 }
1126
1127 fHist = new TH3F("Gear", "Gear Ratio Re/Se",
1128 (int)((fMax.Zd()-fMin.Zd())/2.5+1), fMin.Zd(), fMax.Zd(),
1129 (int)((fMax.Az()-fMin.Az())/2.5+1), fMin.Az(), fMax.Az(),
1130 61, 349.5, 500.5);
1131
1132 fHist->SetXTitle("Zd [\\circ]");
1133 fHist->SetYTitle("Az [\\circ]");
1134 fHist->SetZTitle("Re/Se");
1135
1136 gLog << inf2 << "Starting Gear determination..." << endl;
1137
1138 ZdAz se0 = GetSePos();
1139 ZdAz re0 = GetRePosPdo();
1140
1141 while (fBackground==kBgdGear)
1142 {
1143 fZd1->ResetPosHasChanged();
1144 fZd2->ResetPosHasChanged();
1145 fAz->ResetPosHasChanged();
1146
1147 while (!fZd1->PosHasChanged() && !fZd2->PosHasChanged() &&
1148 !fAz->PosHasChanged() && fBackground==kBgdGear)
1149 {
1150 usleep(1);
1151 TThread::CancelPoint();
1152 }
1153
1154 ZdAz se = GetSePos();
1155 ZdAz re = GetRePosPdo();
1156
1157 ZdAz dse = se-se0;
1158 ZdAz dre = re-re0;
1159
1160 if (fabs(dse.Zd())*144>kResSE.X()) // Each 2.5deg (144)
1161 {
1162 se0.Zd(se.Zd());
1163 re0.Zd(re.Zd());
1164
1165 se -= dse/2;
1166
1167 ZdAz bend = fBending.CorrectBack(se*TMath::TwoPi()/kResSE)*TMath::RadToDeg();
1168 ((TH3*)fHist)->Fill(bend.Zd(), bend.Az(), dre.Zd()/dse.Zd());
1169 }
1170
1171 if (fabs(dse.Az())*144>kResSE.Y()) // Each 2.5deg (144)
1172 {
1173 se0.Az(se.Az());
1174 re0.Az(re.Az());
1175
1176 se -= dse/2;
1177
1178 ZdAz bend = fBending.CorrectBack(se*TMath::TwoPi()/kResSE)*TMath::RadToDeg();
1179 ((TH3*)fHist)->Fill(bend.Az(), bend.Az(), dre.Az()/dse.Az());
1180 }
1181 }
1182 gLog << inf2 << "Gear Test Stopped... displaying Histogram." << endl;
1183
1184 fBackground=kBgdGearDispl;
1185}
1186
1187void MCosy::TalkThread()
1188{
1189 /* ========== FIXME? =============
1190 if (fMac1->IsZombieNode() || fMac2->IsZombieNode())
1191 return;
1192 */
1193
1194 if (fMac1 && fMac2)
1195 {
1196 fMac1->ReqPos();
1197 fMac2->ReqPos();
1198 }
1199
1200 //InitSync();
1201
1202 /*** FOR DEMO MODE ***/
1203 if (!fZd1 || !fZd2 || !fAz)
1204 return;
1205 /*** FOR DEMO MODE ***/
1206
1207 //
1208 // Start the Network
1209 //
1210 while (1)
1211 {
1212 //
1213 // wait until a tracking session is started
1214 //
1215 while (fBackground==kBgdNone)
1216 {
1217 usleep(1);
1218 TThread::CancelPoint();
1219 }
1220
1221 switch (fBackground)
1222 {
1223 case kBgdNone:
1224 continue;
1225/*#ifndef NEWALGO
1226 case kBgdTracking:
1227 TalkThreadTracking();
1228 continue;
1229#endif*/
1230 case kBgdSeTest:
1231 TalkThreadSeTest();
1232 continue;
1233
1234 case kBgdGear:
1235 TalkThreadGear();
1236 continue;
1237
1238 default:
1239 continue;
1240 }
1241 }
1242}
1243
1244ZdAz MCosy::GetPointingPos() const
1245{
1246 if (fZd1->IsZombieNode() || fZd2->IsZombieNode() || fAz->IsZombieNode())
1247 return ZdAz(0, 0);
1248
1249 // GetPointingPos [deg]
1250 const ZdAz seist = GetSePos()*TMath::TwoPi()/kResSE; // [rad]
1251 return fBending.CorrectBack(seist)*TMath::RadToDeg();
1252}
1253
1254Bool_t MCosy::HandleTimer(TTimer *t)
1255{
1256 const Int_t rc = fMutexGui.TryLock();
1257 if (rc==13)
1258 gLog << warn << "MCosy::HandleTimer - mutex is already locked by this thread" << endl;
1259
1260 if (rc)
1261 {
1262 gLog << warn << "* GUI update skipped due to locked mutex." << endl;
1263 return kTRUE;
1264 }
1265
1266 //
1267 // Update Gui, foremer MTGui.
1268 //
1269 if (fZd1)
1270 fZd1->DisplayVal();
1271 if (fZd2)
1272 fZd2->DisplayVal();
1273 if (fAz)
1274 fAz->DisplayVal();
1275
1276 Byte_t avail = 0;
1277
1278 avail |= (fMac1 && !fMac1->IsZombieNode()) ? 0x01 : 0;
1279 avail |= (fMac2 && !fMac2->IsZombieNode()) ? 0x02 : 0;
1280 avail |= (fMac3 && !fMac3->IsZombieNode()) ? 0x04 : 0;
1281 avail |= (fZd1 && !fZd1->IsZombieNode()) ? 0x08 : 0;
1282 avail |= (fZd2 && !fZd2->IsZombieNode()) ? 0x10 : 0;
1283 avail |= (fAz && !fAz->IsZombieNode()) ? 0x20 : 0;
1284// avail |= (!(fStatus&MDriveCom::kError) && 1 ? 0x40 : 0;
1285
1286 if (HasError())
1287 SetStatus(MDriveCom::kError);
1288
1289
1290 ZdAz bendist = fStatus&MDriveCom::kTracking ? fTrackingPos : GetPointingPos();
1291
1292 //cout << (fStatus&MDriveCom::kTracking?"TRA: ":"POS: ") << bendist.Zd() << " " << bendist.Az() << endl;
1293
1294 fCom->SendReport(fStatus, fRaDec, fZdAzSoll, bendist, fTrackingError);
1295
1296 fWin->UpdateWeather(*fCom);
1297 fWin->Update(bendist, fTrackingError, fVelocity, /*fOffset,*/
1298 fRaDec, fZdAzSoll, fStatus, avail);
1299
1300 gLog.UpdateGui();
1301
1302 const Bool_t trigger = fTriggerDisplay;
1303 fTriggerDisplay = kFALSE;
1304
1305 if (fBackground==kBgdSeTestDispl || (trigger&&fBackground==kBgdSeTest))
1306 DisplayHistTestSe(!trigger);
1307
1308 if (fBackground==kBgdGearDispl || (trigger&&fBackground==kBgdGear))
1309 DisplayHistGear(!trigger);
1310
1311 if (fMutexGui.UnLock()==13)
1312 gLog << warn << "MCosy::HandleTimer - tried to unlock mutex locked by other thread." << endl;
1313
1314 return kTRUE;
1315}
1316
1317void MCosy::DisplayHistTestSe(Bool_t del)
1318{
1319 gLog << inf2 << "Displaying histogram..." << endl;
1320
1321 TH2F &hist = *(TH2F*)fHist;
1322
1323 if (del)
1324 {
1325 fHist = NULL;
1326 fBackground = kBgdNone;
1327 }
1328
1329 TCanvas *c=new TCanvas("c1", "", 1000, 1000);
1330 c->Divide(1,2);
1331
1332 c->cd(1);
1333 TH2 *h=(TH2*)hist.DrawCopy();
1334
1335 TProfile *p = h->ProfileX("_pfx", -1, 9999, "s");
1336 p->SetLineColor(kBlue);
1337 p->Draw("same");
1338 p->SetBit(kCanDelete);
1339
1340 c->cd(2);
1341
1342 TH1F p2("spread", "Spread of the differences", hist.GetNbinsX(), hist.GetBinLowEdge(1),
1343 hist.GetBinLowEdge(hist.GetNbinsX()+1));
1344 p2.SetXTitle("Zd [\\circ]");
1345 for (int i=0; i<hist.GetNbinsX(); i++)
1346 p2.SetBinError(i, p->GetBinError(i));
1347 p2.SetLineColor(kRed);
1348 p2.SetStats(0);
1349 p2.DrawCopy();
1350
1351 if (del)
1352 delete &hist;
1353}
1354
1355void MCosy::DisplayHistGear(Bool_t del)
1356{
1357 gLog << inf2 << "Displaying histogram..." << endl;
1358
1359 TH3F &hist = *(TH3F*)fHist;
1360
1361 if (del)
1362 {
1363 fHist = NULL;
1364 fBackground = kBgdNone;
1365 }
1366
1367 TCanvas *c=new TCanvas("c1", "", 1000, 1000);
1368 c->Divide(2,2);
1369
1370 // ----------
1371
1372 c->cd(1);
1373 TH2D &h1=*(TH2D*)hist.Project3D("zx"); // Zd
1374 h1.SetTitle(" Gear Ratio Zenith Distance [re/se] ");
1375 h1.SetXTitle("Zd [\\circ]");
1376 h1.Draw();
1377 h1.SetBit(kCanDelete);
1378
1379 TProfile *p1 = h1.ProfileX("_pfx", -1, 9999, "s");
1380 p1->SetLineColor(kBlue);
1381 p1->Draw("same");
1382 p1->SetBit(kCanDelete);
1383
1384 // ----------
1385
1386 c->cd(2);
1387 TH2D &h2=*(TH2D*)hist.Project3D("zy"); // Az
1388 h2.SetTitle(" Gear Ratio Azimuth [re/se] ");
1389 h2.SetXTitle("Zd [\\circ]");
1390 h2.Draw();
1391 h2.SetBit(kCanDelete);
1392
1393 TProfile *p2 = h2.ProfileX("_pfx", -1, 9999, "s");
1394 p2->SetLineColor(kBlue);
1395 p2->Draw("same");
1396 p2->SetBit(kCanDelete);
1397
1398 // ----------
1399
1400 c->cd(3);
1401
1402 TAxis &axe1 = *h1.GetXaxis();
1403
1404 TH1F f1("spreadzd", " Spread Zenith Distance ",
1405 axe1.GetNbins(), axe1.GetXmin(), axe1.GetXmax());
1406 f1.SetXTitle("Zd [\\circ]");
1407 for (int i=0; i<axe1.GetNbins(); i++)
1408 f1.SetBinError(i, p1->GetBinError(i));
1409 f1.SetLineColor(kRed);
1410 f1.SetStats(0);
1411 f1.DrawCopy();
1412
1413 c->cd(4);
1414
1415 // ----------
1416
1417 TAxis &axe2 = *h2.GetXaxis();
1418
1419 TH1F f2("spreadaz", " Spread Azimuth ",
1420 axe2.GetNbins(), axe2.GetXmin(), axe2.GetXmax());
1421 f2.SetXTitle("Az [\\circ]");
1422 for (int i=0; i<axe2.GetNbins(); i++)
1423 f2.SetBinError(i, p2->GetBinError(i));
1424 f2.SetLineColor(kRed);
1425 f2.SetStats(0);
1426 f2.DrawCopy();
1427
1428 // ----------
1429
1430 if (del)
1431 delete &hist;
1432}
1433
1434// --------------------------------------------------------------------------
1435//
1436// Start the work of the application:
1437//
1438// Start the Can-Network.
1439// Start the MCosy::TalkThread thread.
1440// turn on the gui update
1441//
1442void MCosy::Start(MEnv &env)
1443{
1444 // Don't call this function twice!
1445 Network::Start();
1446
1447 CheckForError();
1448
1449 ReadConfig(env);
1450
1451 gLog << inf << "- Starting TX Thread." << endl;
1452 fTTalk = new MTTalk(this);
1453
1454 gLog << inf << "- Starting GUI update." << endl;
1455 fUpdateGui->TurnOn();
1456}
1457
1458// --------------------------------------------------------------------------
1459//
1460// Start the work of the application:
1461//
1462// Turn of the gui update
1463// stop the MCosy::TalkThread thread.
1464// Stop the network
1465//
1466void MCosy::Stop()
1467{
1468 gLog << inf << "- Stopping GUI update." << endl;
1469 fUpdateGui->TurnOff();
1470 gLog << inf << "- GUI Update stopped." << endl;
1471
1472 gLog << inf << "- Stopping TX Thread." << endl;
1473 delete fTTalk;
1474 gLog << inf << "- TX Thread stopped." << endl;
1475
1476 gLog << inf << "- Stopping CAN network." << endl;
1477 Network::Stop();
1478 gLog << inf << "- CAN network stopped." << endl;
1479
1480 gLog << inf << "- Stopping message queue." << endl;
1481 CancelThread();
1482 gLog << inf << "- Message queue stopped." << endl;
1483}
1484
1485// --------------------------------------------------------------------------
1486//
1487// Disable the synchronization by using a negative CAN Id for id2.
1488//
1489void MCosy::Constructor(Int_t id1, Int_t id2, Int_t id3,
1490 Int_t id4, Int_t id5, Int_t id6)
1491{
1492 //
1493 // Create Nodes
1494 //
1495 gLog << inf << "- Setting up network." << endl;
1496
1497 fMac1=new Macs(id1, "Mac/Az");
1498 fMac2=new Macs(id3, "Mac/Zd");
1499 if (id2>=0)
1500 fMac3=new Macs(id2, "Mac/Az-Sync");
1501
1502 fZd1=new ShaftEncoder(id4, "SE/Zd1");
1503 fZd2=new ShaftEncoder(id5, "SE/Zd2");
1504 fAz =new ShaftEncoder(id6, "SE/Az");
1505
1506 fZd1->SetReport(fOutRep);
1507 fZd2->SetReport(fOutRep);
1508 fAz->SetReport(fOutRep);
1509
1510 fAz->SetMotor(fMac1);
1511 fZd1->SetMotor(fMac2);
1512 fZd2->SetMotor(fMac2);
1513
1514 gLog << inf << "- Connecting devices to network." << endl;
1515
1516 //
1517 // Connect the devices to the network
1518 //
1519 SetNode(fMac1);
1520 SetNode(fMac2);
1521 if (id2>=0)
1522 SetNode(fMac3);
1523 SetNode(fZd1);
1524 SetNode(fZd2);
1525 SetNode(fAz);
1526
1527 //
1528 // Create Gui Event timer and Gui
1529 //
1530 gLog << inf << "- Initializing GUI Timer." << endl;
1531 fUpdateGui = new TTimer(this, 100); // 100ms
1532
1533 gLog << all << "- Starting GUI." << endl;
1534 fWin=new MGCosy(fObservatory, this, gClient->GetRoot(), 1, 1);
1535}
1536/*
1537void MCosy::ConstructorSE(Int_t id4, Int_t id5, Int_t id6)
1538{
1539 //
1540 // Create Nodes
1541 //
1542 gLog << "- Setting up network." << endl;
1543
1544 fZd1=new ShaftEncoder(id4, "SE/Zd1", gLog);
1545 fZd2=new ShaftEncoder(id5, "SE/Zd2", gLog);
1546 fAz =new ShaftEncoder(id6, "SE/Az", gLog);
1547
1548 gLog << "- Connecting devices to network." << endl;
1549
1550 //
1551 // Connect the devices to the network
1552 //
1553 SetNode(fZd1);
1554 SetNode(fZd2);
1555 SetNode(fAz);
1556
1557 //
1558 // Create Gui Event timer and Gui
1559 //
1560 gLog << "- Initializing GUI Timer." << endl;
1561 fUpdateGui = new TTimer(this, 100); // 100ms
1562
1563 gLog << "- Starting GUI." << endl;
1564 fWin=new MGCosy(fObservatory, this, gClient->GetRoot(), 1, 1);
1565}
1566
1567void MCosy::ConstructorDemo()
1568{
1569 //
1570 // Create Nodes
1571 //
1572 gLog << "- Setting up network." << endl;
1573
1574 //
1575 // Create Gui Event timer and Gui
1576 //
1577 gLog << "- Initializing GUI Timer." << endl;
1578 fUpdateGui = new TTimer(this, 100); // 100ms
1579
1580 gLog << "- Starting GUI." << endl;
1581 fWin=new MGCosy(fObservatory, this, gClient->GetRoot(), 1, 1);
1582}
1583*/
1584
1585TString MCosy::GetFileName(const char *path, const char *name, const char *ext)
1586{
1587 // FIXME: Timeout missing
1588
1589 while (1)
1590 {
1591 MTime time(-1);
1592
1593 // This is the full qualified date which is part of the name
1594 const TString clock = time.GetStringFmt("%Y%m%d_%H%M%S");
1595
1596 // This gives the night in which the date belongs to
1597 time.SetMjd(TMath::Nint(time.GetMjd()));
1598
1599 const TString night = time.GetStringFmt("%Y_%m_%d");
1600
1601 const TString dir = Form("%s/%s", path, night.Data());
1602 const TString fname = Form("%s_%s.%s", name, clock.Data(), ext);
1603
1604 const TString full = Form("%s/%s", dir.Data(), fname.Data());
1605
1606 gSystem->mkdir(dir, kTRUE);
1607
1608 if (gSystem->AccessPathName(full, kFileExists))
1609 return full;
1610
1611 break;// !!!!!!!!!!!!!!!!!!!!!!!
1612
1613 usleep(1000);
1614 }
1615 return "";
1616}
1617
1618MCosy::MCosy(MEnv &env, const char *addr, const char *pointing)
1619: Network(), fObservatory(MObservatory::kMagic1), fStarguider(NULL),
1620fZd1(0), fZd2(0), fAz(0), fMac1(0), fMac2(0), fMac3(0),
1621fBackground(kBgdNone), fTriggerDisplay(kFALSE), fStatus(MDriveCom::kStopped),
1622fOutTp(0), fOutRep(0)
1623{
1624 const Int_t id1 = env.GetValue("Az_Id-MAC1", 1); //1
1625 const Int_t id2 = env.GetValue("Az_Id-MAC2", 2); //2
1626 const Int_t id3 = env.GetValue("Zd_Id-MAC", 3); //3
1627 const Int_t id4 = env.GetValue("Zd_Id-SE1", 4); //4
1628 const Int_t id5 = env.GetValue("Zd_Id-SE2", 5); //5
1629 const Int_t id6 = env.GetValue("Az_Id-SE", 6); //6
1630
1631 TString name = GetFileName("rep", "cosy", "rep");
1632 gLog << inf << "Open Repfile: " << name << endl;
1633 fOutRep = new MLog(name, kTRUE);
1634 *fOutRep << all << "[Drive Report File]" << endl;
1635 *fOutRep << "Version <cvs>" << endl;
1636 *fOutRep << "Date " << MTime(-1) << endl;
1637 *fOutRep << "[Reports]" << endl;
1638
1639/*
1640 gLog << "- Program in ";
1641 switch (mode)
1642 {
1643 case 0:
1644 gLog << "<<Standard mode>>" << endl;*/
1645 gLog << all << "Reading pointing model from " << pointing << "..." << endl;
1646 if (fBending.Load(pointing))
1647 gLog << all << "Reading pointing model from " << pointing << " successfull." << endl;
1648 else
1649 gLog << err << "ERROR - Reading pointing model from " << pointing << endl;
1650 Constructor(id1, id2, id3, id4, id5, id6);/*
1651 break;
1652 case 1:
1653 gLog << "<<SE mode>>" << endl;
1654 fBending.Load("bending.txt");
1655 ConstructorSE(id4, id5, id6);
1656 break;
1657 default:
1658 gLog << "<<Demo mode>>" << endl;
1659 ConstructorDemo();
1660 }
1661*/
1662
1663 gLog.SetOutputGui(fWin->GetLog(), kTRUE);
1664
1665 fZd1->SetDisplay(fWin->GetLabel2());
1666 fZd2->SetDisplay(fWin->GetLabel3());
1667 fAz->SetDisplay(fWin->GetLabel1());
1668
1669 fCom = new MDriveCom(this, addr, fOutRep);
1670// fCom->Start();
1671}
1672
1673void MCosy::TerminateApp()
1674{
1675 gLog << inf2 << "MCosy::TerminateApp()" << endl;
1676/*
1677 Int_t rc;
1678 TGMessageBox msg(this, gClient->GetRoot(),
1679 "Information",
1680 "Cosy is shutting down the system - this may take wa while!",
1681 kMBIconExclamation,
1682 kMBOK, //kMBClose
1683 &rc, 0);
1684*/
1685
1686 gLog.DisableOutputDevice(MLog::eGui);
1687 // FIXME: WHY DOES THIS CRASH THE APPLICATIOn WHILE TRAKING?
1688 // gLog.SetOutputGui(NULL, kFALSE);
1689
1690 gApplication->Terminate(0);
1691}
1692
1693MCosy::~MCosy()
1694{
1695 gLog << inf2 << "Deleting GUI timer." << endl;
1696 // FIXME: Wait until last Update was finished!!!
1697 delete fUpdateGui;
1698
1699 //fMutexGui.Lock();
1700
1701 // Now the files can safely be closed
1702 gLog << inf2 << "Closing output files." << endl;
1703 if (fOutTp)
1704 {
1705 *fOutTp << "END" << endl;
1706 delete fOutTp;
1707 }
1708 delete fOutRep;
1709
1710 gLog << inf2 << "Deleting CC communication." << endl;
1711 delete fCom;
1712
1713 gLog << inf2 << "Deleting Nodes." << endl;
1714 fZd1->SetReport(0);
1715 fZd2->SetReport(0);
1716 fAz->SetReport(0);
1717
1718 delete fAz;
1719 delete fZd1;
1720 delete fZd2;
1721 delete fMac1;
1722 delete fMac2;
1723 if (fMac3)
1724 delete fMac3;
1725
1726 gLog << inf2 << "Deleting MGCosy." << endl;
1727
1728 gLog.DisableOutputDevice(MLog::eGui);
1729
1730 delete fWin;
1731
1732 gLog << inf2 << "MGCosy destructed." << endl;
1733}
Note: See TracBrowser for help on using the repository browser.