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

Last change on this file since 9005 was 8875, checked in by tbretz, 17 years ago
*** empty log message ***
File size: 44.7 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.SetPointAccDec(0.25, 0.25);
409// point.SetPointVelocity(0.4);
410//#else
411
412// original settings
413//
414 point.SetPointAccDec(0.2, 0.1);
415 point.SetPointVelocity(0.1);
416
417
418//#endif
419
420// original
421 return point.SetPosition(dst, track);
422
423// test
424// return point.SetPosition(dst, kTRUE);
425}
426
427void MCosy::SetTrackingPosRE(ZdAz za)
428{
429 za /= kGearTot; // [U_tel]
430 za *= TMath::TwoPi(); // [rad]
431 //cout << "RE1: " << za.Zd()*180/3.1415 << " " << za.Az()*180/3.1415 << endl;
432 fTrackingPosRaw = za*TMath::RadToDeg();
433 fTrackingPos = fBending.CorrectBack(za)*TMath::RadToDeg();
434 //cout << "RE2: " << fTrackingPos.Zd() << " " << fTrackingPos.Az() << endl;
435}
436
437void MCosy::TrackPosition(const RaDec &dst) // ra, dec [rad]
438{
439 MTracking track(this);
440 track.SetOut(fOutRep);
441//#ifdef EXPERT
442// track.SetPointAccDec(0.4, 0.4);
443// track.SetPointVelocity(0.2); // fast: 0.6, slow: 0.2
444//#else
445
446
447
448 track.SetPointAccDec(0.2, 0.1);
449 track.SetPointVelocity(0.1);
450
451 // track.SetPointAccDec(0.4, 0.4);
452 // track.SetPointVelocity(0.4);
453
454
455
456//#endif
457 track.SetTrackAccDec(0.1, 0.1);
458
459 track.TrackPosition(dst);
460}
461
462void MCosy::TrackPositionGRB(const RaDec &dst) // ra, dec [rad]
463{
464 MTracking track(this);
465 track.SetOut(fOutRep);
466//#ifdef EXPERT
467// track.SetPointAccDec(0.4, 0.4);
468// track.SetPointVelocity(0.2); // fast: 0.6, slow: 0.2
469//#else
470 track.SetPointAccDec(0.4, 0.4);
471 track.SetPointVelocity(0.3);
472//#endif
473 track.SetTrackAccDec(0.1, 0.1);
474
475 track.TrackPosition(dst);
476}
477
478// --------------------------------------------------------------------------
479//
480// Stops the movement of both motors.
481//
482// Sets the status to stopping. Sets the deceleration to 50% of the maximum.
483// stops. Quits the revolution mode and wait for the end of the movement.
484//
485void MCosy::StopMovement()
486{
487 //
488 // Set status to Stopping
489 //
490 SetStatus(MDriveCom::kStopping);
491
492 //
493 // set deceleration to 50%
494 //
495 gLog << inf2 << "Stopping movement (dec=30%)..." << endl;
496 if (fMac1 && fMac2)
497 {
498#ifdef EXPERT
499 fMac1->SetDeceleration(TMath::Nint(0.5*fMac1->GetVelRes()));
500 fMac2->SetDeceleration(TMath::Nint(0.5*fMac2->GetVelRes()));
501#else
502 fMac1->SetDeceleration(TMath::Nint(0.3*fMac1->GetVelRes()));
503 fMac2->SetDeceleration(TMath::Nint(0.3*fMac2->GetVelRes()));
504#endif
505 fMac1->SetRpmMode(FALSE);
506 fMac2->SetRpmMode(FALSE);
507 }
508
509/*
510 fMac1->SetDeceleration(0.3*fMac1->GetVelRes());
511 fMac2->SetDeceleration(0.3*fMac2->GetVelRes());
512
513 fMac2->SendSDO(0x3000, Macs::string('s','t','o','p'));
514 fMac1->SendSDO(0x3000, Macs::string('s','t','o','p'));
515 fMac2->WaitForSdo(0x3000, 0);
516 fMac1->WaitForSdo(0x3000, 0);
517 fMac1->SetRpmMode(FALSE);
518 fMac2->SetRpmMode(FALSE);
519 */
520
521 //
522 // Wait for the movement to really be finished.
523 //
524#ifdef EXPERT
525 cout << "Waiting for end of movement..." << endl;
526#endif
527 WaitForEndMovement();
528
529 //
530 // Check whether everything works fine.
531 //
532 CheckForError();
533#ifdef EXPERT
534 cout << "Movement stopped." << endl;
535#endif
536}
537
538bool MCosy::CheckNetwork()
539{
540 //return kTRUE;
541 //CheckConnections();
542
543 CheckForError();
544
545 if (HasZombie())
546 {
547 gLog << warn << "- Found Zombies in Network..." << endl;
548 if (!RebootZombies())
549 return false;
550 }
551
552 /*
553 FIXME HANDLING ERROR
554 */
555 if (HasError())
556 {
557 fMac1->HandleError();
558 fMac2->HandleError();
559 //if (fMac3)
560 // fMac3->HandleError();
561 if (HasError() || HasZombie())
562 return false;
563 }
564
565 CheckForError();
566 return true;
567}
568
569Int_t MCosy::Proc(int msg, void *mp)
570{
571 switch (msg)
572 {
573 case WM_WAIT:
574 gLog << inf2 << "Wait for execution of Proc(WM_*, ): done." << endl;
575 return 0;
576
577 case WM_STOP:
578 //cout << "MCosy::Proc: Stop." << endl;
579 if (!CheckNetwork())
580 return 0xebb0;
581 StopMovement();
582 return 0;
583/*
584 case WM_PRESET:
585 cout << "WM_Preset: start." << endl;
586 if (!CheckNetwork())
587 return (void*)0xebb0;
588 fZd1->SetPreset();
589 fZd2->SetPreset();
590 fAz->SetPreset();
591 cout << "WM_Preset: done. (return 0xaffe)" << endl;
592 return (void*)0xaffe;
593*/
594 /*
595 case WM_CALIB:
596 {
597 cout << "WM_Calib: start." << endl;
598 if (!CheckNetwork())
599 return (void*)0xebb0;
600
601 SlaStars sla(fObservatory);
602 sla.Now();
603
604 RaDec rd = *((RaDec*)mp);
605
606 //RaDec rd(37.94, 89.2644); // POLARIS
607 //RaDec rd(213.915417, 19.1825); // ARCTURUS
608
609 cout << "Calibrating to: " << rd.Ra()*24/360 << "h " << rd.Dec() << "°" << endl;
610
611 ZdAz za=sla.CalcZdAz(rd*TMath::DegToRad())*16384.0/k2Pi;
612
613 cout << "Calc Zd: " << za.Zd() << " Az: " << za.Az() << endl;
614
615 ZdAz sepos = GetSePos();
616 cout << "Got Zd: " << sepos.Zd() << " Az: " << sepos.Az() << endl;
617
618 fZd1->SetPreset(za.Zd());
619 fZd2->SetPreset(-za.Zd());
620 fAz->SetPreset(za.Az());
621
622 cout << "WM_Calib: done. (return 0xaffe)" << endl;
623 }
624 return (void*)0xaffe;
625 */
626 case WM_TPOINT:
627 {
628 //cout << "WM_TPoint: start." << endl;
629 SlaStars sla(fObservatory);
630 sla.Now();
631
632 RaDec rd = *((RaDec*)mp);
633 cout << "TPoint Star: " << rd.Ra()/15 << "h " << rd.Dec() << "°" << endl;
634
635 AltAz za=sla.CalcAltAz(rd*TMath::DegToRad())*TMath::RadToDeg();
636
637 if (!fOutTp)
638 {
639 //
640 // open tpoint file
641 //
642 const TString name = GetFileName("tpoint", "old-tpoint", "txt");
643 cout << "TPoint-Cosy File ********* " << name << " ********** " << endl;
644
645 fOutTp = new ofstream(name);
646 *fOutTp << "Magic Model TPOINT data file" << endl;
647 *fOutTp << ": ALTAZ" << endl;
648 *fOutTp << "49 48 0 ";
649 *fOutTp << sla.GetTime().Year() << " " << sla.GetTime().Month() << " " << sla.GetTime().Day() << " ";
650 *fOutTp << /*"20 1013.25 300 0.5 0.55 0.0065" <<*/ endl;
651 // temp(°C) pressure(mB) height(m) humidity(1) wavelength(microm) troplapserate(K/m)
652 }
653
654 cout << " Alt/Az: " << za.Alt() << "° " << za.Az() << "°" << endl;
655 *fOutTp << setprecision(7) << za.Az() << " " << za.Alt() << " ";
656
657 ZdAz sepos = GetSePos()*TMath::TwoPi()/kResSE;
658 za.Set(TMath::Pi()/2-sepos.Zd(), sepos.Az());
659 za *= TMath::RadToDeg();
660
661 cout << " SE-Pos: " << za.Alt() << "° " << za.Az() << "°" << endl;
662 *fOutTp << fmod(za.Az()+360, 360) << " " << za.Alt() << " ";
663
664 if (fStarguider)
665 {
666 XY tp = fStarguider->GetCoordinates();
667 *fOutTp << 90-tp.X() << " " << tp.Y() << " ";
668 }
669
670 *fOutTp << rd.Ra()/15 << " " << rd.Dec() << " " << setprecision(11) << sla.GetMjd() << endl;
671
672 //cout << "WM_TPoint: done. (return 0xaffe)" << endl;
673 }
674 return 0xca1b;
675
676 case WM_TRACKPOS:
677 //cout << "WM_TrackPosition: start." << endl;
678 {
679 if (!CheckNetwork())
680 return 0xebb0;
681
682 ZdAz dest = *((ZdAz*)mp) * TMath::DegToRad();
683 if (!SetPosition(dest, kTRUE))
684 return 0x1234;
685
686 SlaStars sla(fObservatory);
687 sla.Now();
688
689 RaDec rd = sla.CalcRaDec(dest);
690 TrackPosition(rd);
691 }
692 //cout << "WM_TrackPosition: done. (return 0xabcd)" << endl;
693 return 0xabcd;
694
695 case WM_ARM:
696 //cout << "WM_Position: start." << endl;
697 {
698 if (!CheckNetwork())
699 return 0xebb0;
700
701 const bool arm = mp ? *((bool*)mp) : true;
702 if (arm)
703 {
704 fMac1->Arm();
705 fMac2->Arm();
706 }
707 else
708 {
709 fMac1->Disarm();
710 fMac2->Disarm();
711 }
712 }
713 //cout << "WM_Position: done. (return 0x7777)" << endl;
714 return 0x9999;
715
716 case WM_POSITION:
717 //cout << "WM_Position: start." << endl;
718 {
719 if (!CheckNetwork())
720 return 0xebb0;
721
722 ZdAz dest = *((ZdAz*)mp);
723 SetPosition(dest*TMath::DegToRad());
724 }
725 //cout << "WM_Position: done. (return 0x7777)" << endl;
726 return 0x7777;
727
728 case WM_POSITION1:
729 //cout << "WM_Position1: start." << endl;
730 {
731 if (!CheckNetwork())
732 return 0xebb0;
733
734 ZdAz dest = *((ZdAz*)mp);
735 SetPosition(dest*TMath::DegToRad(), kTRUE);
736 }
737 //cout << "WM_Position: done. (return 0x7777)" << endl;
738 return 0x7777;
739
740 case WM_PREPS:
741 //cout << "WM_Track: START" << endl;
742 {
743 if (!CheckNetwork())
744 return 0xebb0;
745
746 const char *preps = (const char*)mp;
747 cout << "Preposition command to " << preps << " received." << endl;
748
749 ifstream fin("prepos.txt");
750 if (!fin)
751 {
752 cout << "ERROR: cannot open prepos.txt." << endl;
753 return 0xebb1;
754 }
755
756 while (1)
757 {
758 Double_t zd, az;
759 fin >> zd >> az;
760
761 TString str;
762 str.ReadLine(fin);
763 if (!fin)
764 break;
765
766 str.ToLower();
767
768 if (str.Strip(TString::kBoth)==preps)
769 {
770 ZdAz dest(zd, az);
771 SetPosition(dest*TMath::DegToRad());
772 return 0x7979;
773 }
774 cout << "ERROR - Requested preposition not found in file..." << endl;
775 }
776 }
777 //cout << "WM_Track: done. (return 0x8888)" << endl;
778 return 0x7878;
779
780 case WM_TESTSE:
781 //cout << "WM_TestSe: start." << endl;
782 fBackground = mp ? kBgdSeTest : kBgdNone;
783 //cout << "WM_TestSe: done. (return 0x1e51)" << endl;
784 return 0x1e51;
785
786 case WM_GEAR:
787 //cout << "WM_Gear: start." << endl;
788 fBackground = mp ? kBgdGear : kBgdNone;
789 //cout << "WM_Gear: done. (return 0xfeaf)" << endl;
790 return 0xfeaf;
791
792 case WM_DISPLAY:
793 //cout << "WM_Display: start." << endl;
794 fTriggerDisplay = kTRUE;
795 //cout << "WM_Disply: done. (return 0xd1e1)" << endl;
796 return 0xd1e1;
797
798 case WM_TRACK:
799 case WM_GRB:
800 //cout << "WM_Track/GRB: START" << endl;
801 {
802 RaDec dest = ((RaDec*)mp)[0];
803 if (fStarguider)
804 fStarguider->SetPointingPosition(((RaDec*)mp)[1]);
805 if (!CheckNetwork())
806 return 0xebb0;
807
808 if (msg==WM_TRACK)
809 TrackPosition(dest*TMath::DegToRad());
810 else
811 TrackPositionGRB(dest*TMath::DegToRad());
812 }
813 //cout << "WM_Track/GRB: done. (return 0x8888)" << endl;
814 return 0x8888;
815
816 case WM_NEWTRACK:
817 //cout << "WM_NewTrack: START" << endl;
818 fRaDec = *((RaDec*)mp);
819 //cout << "WM_NewTrack: done. (return 0x9999)" << endl;
820 return 0x9999;
821
822 case WM_LOADBENDING:
823 //cout << "WM_LoadBending: START" << endl;
824 fBending.Load("bending.txt");
825 //cout << "WM_LoadBending: done. (return 0xbe0d)" << endl;
826 return 0xbe0d;
827
828 case WM_RESETBENDING:
829 //cout << "WM_ResetBending: START" << endl;
830 fBending.Reset();
831 //cout << "WM_ResetBending: done. (return 0xbe0e)" << endl;
832 return 0xbe0e;
833
834 case WM_HOME:
835 //cout << "WM_Home: START" << endl;
836 if (!CheckNetwork())
837 return 0xebb0;
838 else
839 {
840 cout << "HOME NOT ALLOWED... for Magic." << endl;
841 /*
842 cout << "Going Home..." << endl;
843 TEnv env(".cosyrc");
844
845 SetStatus(MDriveCom::kMoving);
846
847 fMac1->SetHome(250000, env.GetValue("Az_MaxTime2ReachHome[s]", 100));
848 fMac2->SetHome(250000, env.GetValue("Zd_MaxTime2ReachHome[s]", 100));
849
850 gLog << "SETHOME DONE" << endl;
851
852 SetStatus(HasError() ? MDriveCom::kError : MDriveCom::kStopped);
853
854 fAz->SetPreset();
855 fZd1->SetPreset();
856 fZd2->SetPreset();
857
858 fMac1->ReqPos();
859 fMac2->ReqPos();
860 fMac3->StopMotor();
861 */
862 }
863 //cout << "WM_Home: done. (return 0x403e)" << endl;
864 return 0x403e;
865
866 case WM_CALCALTAZ:
867 {
868 cout << endl;
869
870 SlaStars sla(fObservatory);
871 sla.Now();
872
873 XY xy = *((XY*)mp);
874 RaDec rd(xy.X()*15., xy.Y()); // [deg]
875
876 ZdAz a1 = sla.CalcZdAz(rd*TMath::DegToRad()); // [rad]
877
878 cout << "Ra/Dec source: " << xy.X() << "h " << xy.Y() << "°" << endl;
879 cout << "Zd/Az target: " << a1.Zd()*TMath::RadToDeg() << "° " << a1.Az()*TMath::RadToDeg() << "°" << endl;
880
881 if (fZd1 && fZd2 && fAz)
882 a1 = AlignTrackingPos(a1);
883
884 a1 = fBending(a1);
885 CheckRange(a1);
886 a1 *= TMath::RadToDeg();
887
888 const ZdAz a2 = a1*kResSE/360;
889
890 cout << "Zd/Az bended: " << a1.Zd() << "° " << a1.Az() << "°" << endl;
891 cout << "SE bended: " << a2.Zd() << " " << a2.Az() << endl;
892 }
893 return 0xa17a;
894
895 case WM_ENDSWITCH:
896 {
897 ZdAz pos = GetSePos()*TMath::TwoPi()/kResSE;
898 pos = fBending.SubtractOffsets(pos)*TMath::RadToDeg();
899
900 cout << "Endswitch Position: Zd=" << pos.Zd() << "° Az=";
901 cout << pos.Az() << "°" << endl;
902 }
903
904 return 0x1010;
905
906 case WM_QUIT:
907 cout << "WM_Quit: now." << endl;
908 if (!CheckNetwork())
909 {
910 gLog << err << "ERROR: Cannot shutdown CANbus network." << endl;
911 return 0xebb0;
912 }
913 TerminateApp();
914 cout << "WM_Quit: done." << endl;
915 return 0xaaaa;
916 }
917 cout << "MCosy::Proc: Unknown message 0x" << msg << endl;
918 return 0xffffffff;
919}
920
921Int_t MTTalk::Thread()
922{
923 fCosy->TalkThread();
924 return 0;
925}
926
927void MCosy::ReadConfig(MEnv &env)
928{
929 gLog << inf2 << "Reading telescope range..." << flush;
930 const Double_t amin = env.GetValue("Az_Min[deg]", -95.0);
931 const Double_t zmin = env.GetValue("Zd_Min[deg]", -75.0);
932 fMin.Set(zmin, amin);
933
934 const Double_t amax = env.GetValue("Az_Max[deg]", 305.0);
935 const Double_t zmax = env.GetValue("Zd_Max[deg]", 98.25);
936 fMax.Set(zmax, amax);
937 gLog << "done." << endl;
938
939 gLog << all << flush;
940
941 gLog << " * Min: " << zmin << "deg " << amin << "deg" << endl;
942 gLog << " * Max: " << zmax << "deg " << amax << "deg" << endl;
943
944 fMin = fBending.AddOffsets(fMin*TMath::DegToRad());
945 fMax = fBending.AddOffsets(fMax*TMath::DegToRad());
946
947 gLog << " * Min': " << fMin.Zd()*TMath::RadToDeg() << "deg " << fMin.Az()*TMath::RadToDeg() << "deg" << endl;
948 gLog << " * Max': " << fMax.Zd()*TMath::RadToDeg() << "deg " << fMax.Az()*TMath::RadToDeg() << "deg" << endl;
949
950 kGear.Set(env.GetValue("Zd_GearRatio[U_mot/U_tel]", 1000.0),
951 env.GetValue("Az_GearRatio[U_mot/U_tel]", 1000.0));
952
953 Bool_t mac1 = fMac1 && !fMac1->IsZombieNode();
954 Bool_t mac2 = fMac2 && !fMac2->IsZombieNode();
955 //Bool_t mac3 = fMac3 && !fMac3->IsZombieNode();
956
957 Bool_t zd1 = fZd1 && !fZd1->IsZombieNode();
958 Bool_t zd2 = fZd2 && !fZd2->IsZombieNode();
959 Bool_t az = fAz && !fAz->IsZombieNode();
960
961 Double_t x = 0;
962 Double_t y = 0;
963// y = mac1 ? fMac1->GetRes() : (mac3 ? fMac3->GetRes() : env.GetValue("Az_ResRE[re/U_mot]", 1500));
964 y = mac1 ? fMac1->GetRes() : env.GetValue("Az_ResRE[re/U_mot]", 1500);
965 x = mac2 ? fMac2->GetRes() : env.GetValue("Zd_ResRE[re/U_mot]", 1500);
966 kResRE.Set(x,y);
967
968 gLog << " * Az RE resolution: " << x << " re/U_mot" << endl;
969 gLog << " * Zd RE resolution: " << y << " re/U_mot" << endl;
970
971 x = zd1 ? fZd1->GetPhysRes() : (zd2 ? fZd2->GetPhysRes() : env.GetValue("Zd_ResSE[se/U_tel]", 16384));
972 y = az ? fAz->GetPhysRes() : env.GetValue("Az_ResSE[se/U_tel]", 16384);
973 kResSE.Set(x,y);
974
975 gLog << " * Az SE resolution: " << x << " se/U_tel" << endl;
976 gLog << " * Zd SE resolution: " << y << " se/U_tel" << endl;
977
978 /*
979 kResRE.Y(0);
980 if (fMac1 && !fMac1->IsZombieNode())
981 kResRE.Y(fMac1->GetRes());
982 else
983 y = fMac3 && !fMac3->IsZombieNode() ? fMac3->GetRes() : env.GetValue("Az_ResRE[re/U_mot]", 1500);
984 if (fMac3 && !fMac3->IsZombieNode())
985 kResRE.Y(fMac3->GetRes());
986 else
987 kResRE.Y(env.GetValue("Az_ResRE[re/U_mot]", 1500));
988
989 kResRE.X(0);
990 if (fMac2 && !fMac2->IsZombieNode())
991 kResRE.X(fMac2->GetRes());
992 else
993 kResRE.X(env.GetValue("Zd_ResRE[re/U_mot]", 1500));
994
995 kResSE.X(0);
996 if (fZd1 && !fZd1->IsZombieNode())
997 kResSE.X(fZd1->GetPhysRes());
998 else
999 if (fZd2 && !fZd2->IsZombieNode())
1000 kResSE.X(fZd2->GetPhysRes());
1001 else
1002 kResSE.X(env.GetValue("Zd_ResSE[se/U_mot]", 16384));
1003
1004 kResSE.Y(0);
1005 if (fAz && !fAz->IsZombieNode())
1006 kResSE.Y(fAz->GetPhysRes());
1007 else
1008 kResSE.Y(env.GetValue("Az_ResSE[se/U_mot]", 16384));
1009 */
1010
1011 // believing the Macs manual '*4' shouldn't be necessary, but it is.
1012 // Because the a RE is 4 quad counts.
1013 // Calculating speeds we have to convert back to qc
1014 kResRE *= 4;
1015 kGearTot = Mul(kResRE, kGear);
1016// kGearTot = kResRE*kGear;
1017
1018// gLog << all;
1019// gLog << " * Setting Gear Ratios:" << endl;
1020 // gLog << " --------------------" << endl;
1021 gLog << " * X: " << kGear.X() << "*" << kResRE.X()/4 << "/" << kResSE.X() << "=4*" << kGearTot.X() << "/" << kResSE.X() << endl;
1022 gLog << " * Y: " << kGear.Y() << "*" << kResRE.Y()/4 << "/" << kResSE.Y() << "=4*" << kGearTot.Y() << "/" << kResSE.Y() << endl;
1023}
1024/*
1025void MCosy::InitSync()
1026{
1027 if (!fMac3)
1028 {
1029 gLog << "Unable to Init Sync! Mac3 not available." << endl;
1030 return;
1031 }
1032
1033 const int res = fMac3->GetVelRes();
1034
1035 fMac3->SetVelocity(0.3*res);
1036 fMac3->SetAcceleration(0.2*res);
1037 fMac3->SetDeceleration(0.2*res);
1038 fMac3->StartPosSync();
1039}
1040*/
1041void MCosy::TalkThreadSeTest()
1042{
1043// if (fZd1->IsZombieNode() || fZd2->IsZombieNode())
1044 // return;
1045
1046 if (fHist)
1047 {
1048 gLog << err << "You are much too fast... try again." << endl;
1049 return;
1050 }
1051
1052 fHist = new TH2F("Diff", "Difference of SE values",
1053 201, fMin.Zd(), fMax.Zd(), 41, -10.5, 10.5);
1054 fHist->SetXTitle("ZA [\\circ]");
1055 fHist->SetYTitle("\\Delta SE");
1056
1057 Double_t offset = 0;
1058
1059 int cnt = 0;
1060
1061 gLog << inf2 << "Starting Shaftencoder Test..." << endl;
1062
1063 gLog << err << "Build in values!!!" << endl;
1064
1065 while (fBackground==kBgdSeTest)
1066 {
1067 fZd1->ResetPosHasChanged();
1068 fZd2->ResetPosHasChanged();
1069
1070 while (!fZd1->PosHasChanged() && !fZd2->PosHasChanged() &&
1071 fBackground==kBgdSeTest)
1072 {
1073 usleep(1);
1074 TThread::CancelPoint();
1075 }
1076
1077 const Double_t pos[3] = {
1078 (fZd1->GetPos()+8192)%16384,
1079 (fZd2->GetPos()+8192)%16384,
1080 fAz->GetPos() };
1081
1082 //
1083 // Estimate Offset from the first ten positions
1084 //
1085 if (cnt++<10)
1086 {
1087 offset += pos[0]+pos[1];
1088 continue;
1089 }
1090 if (cnt==11)
1091 {
1092 offset /= 10;
1093 cnt++;
1094 }
1095
1096 Double_t apos = (pos[0]-pos[1])/2 * TMath::TwoPi() / kResSE.X();
1097
1098 ZdAz bend = fBending.CorrectBack(ZdAz(apos, pos[2]))*TMath::RadToDeg();
1099 fHist->Fill(bend.Zd(), pos[0]+pos[1]-offset);
1100 }
1101
1102 gLog << inf2 << "Shaftencoder Test Stopped... displaying Histogram." << endl;
1103
1104 fBackground=kBgdSeTestDispl;
1105}
1106
1107void MCosy::TalkThreadGear()
1108{
1109// if (fZd1->IsZombieNode() || fZd2->IsZombieNode())
1110 // return;
1111
1112 if (fHist)
1113 {
1114 gLog << err << "You are much too fast... try again." << endl;
1115 return;
1116 }
1117
1118 fHist = new TH3F("Gear", "Gear Ratio Re/Se",
1119 (int)((fMax.Zd()-fMin.Zd())/2.5+1), fMin.Zd(), fMax.Zd(),
1120 (int)((fMax.Az()-fMin.Az())/2.5+1), fMin.Az(), fMax.Az(),
1121 61, 349.5, 500.5);
1122
1123 fHist->SetXTitle("Zd [\\circ]");
1124 fHist->SetYTitle("Az [\\circ]");
1125 fHist->SetZTitle("Re/Se");
1126
1127 gLog << inf2 << "Starting Gear determination..." << endl;
1128
1129 ZdAz se0 = GetSePos();
1130 ZdAz re0 = GetRePosPdo();
1131
1132 while (fBackground==kBgdGear)
1133 {
1134 fZd1->ResetPosHasChanged();
1135 fZd2->ResetPosHasChanged();
1136 fAz->ResetPosHasChanged();
1137
1138 while (!fZd1->PosHasChanged() && !fZd2->PosHasChanged() &&
1139 !fAz->PosHasChanged() && fBackground==kBgdGear)
1140 {
1141 usleep(1);
1142 TThread::CancelPoint();
1143 }
1144
1145 ZdAz se = GetSePos();
1146 ZdAz re = GetRePosPdo();
1147
1148 ZdAz dse = se-se0;
1149 ZdAz dre = re-re0;
1150
1151 if (fabs(dse.Zd())*144>kResSE.X()) // Each 2.5deg (144)
1152 {
1153 se0.Zd(se.Zd());
1154 re0.Zd(re.Zd());
1155
1156 se -= dse/2;
1157
1158 ZdAz bend = fBending.CorrectBack(se*TMath::TwoPi()/kResSE)*TMath::RadToDeg();
1159 ((TH3*)fHist)->Fill(bend.Zd(), bend.Az(), dre.Zd()/dse.Zd());
1160 }
1161
1162 if (fabs(dse.Az())*144>kResSE.Y()) // Each 2.5deg (144)
1163 {
1164 se0.Az(se.Az());
1165 re0.Az(re.Az());
1166
1167 se -= dse/2;
1168
1169 ZdAz bend = fBending.CorrectBack(se*TMath::TwoPi()/kResSE)*TMath::RadToDeg();
1170 ((TH3*)fHist)->Fill(bend.Az(), bend.Az(), dre.Az()/dse.Az());
1171 }
1172 }
1173 gLog << inf2 << "Gear Test Stopped... displaying Histogram." << endl;
1174
1175 fBackground=kBgdGearDispl;
1176}
1177
1178void MCosy::TalkThread()
1179{
1180 /* ========== FIXME? =============
1181 if (fMac1->IsZombieNode() || fMac2->IsZombieNode())
1182 return;
1183 */
1184
1185 if (fMac1 && fMac2)
1186 {
1187 fMac1->ReqPos();
1188 fMac2->ReqPos();
1189 }
1190
1191 //InitSync();
1192
1193 /*** FOR DEMO MODE ***/
1194 if (!fZd1 || !fZd2 || !fAz)
1195 return;
1196 /*** FOR DEMO MODE ***/
1197
1198 //
1199 // Start the Network
1200 //
1201 while (1)
1202 {
1203 //
1204 // wait until a tracking session is started
1205 //
1206 while (fBackground==kBgdNone)
1207 {
1208 usleep(1);
1209 TThread::CancelPoint();
1210 }
1211
1212 switch (fBackground)
1213 {
1214 case kBgdNone:
1215 continue;
1216/*#ifndef NEWALGO
1217 case kBgdTracking:
1218 TalkThreadTracking();
1219 continue;
1220#endif*/
1221 case kBgdSeTest:
1222 TalkThreadSeTest();
1223 continue;
1224
1225 case kBgdGear:
1226 TalkThreadGear();
1227 continue;
1228
1229 default:
1230 continue;
1231 }
1232 }
1233}
1234
1235ZdAz MCosy::GetPointingPos() const
1236{
1237 if (fZd1->IsZombieNode() || fZd2->IsZombieNode() || fAz->IsZombieNode())
1238 return ZdAz(0, 0);
1239
1240 // GetPointingPos [deg]
1241 const ZdAz seist = GetSePos()*TMath::TwoPi()/kResSE; // [rad]
1242 return fBending.CorrectBack(seist)*TMath::RadToDeg();
1243}
1244
1245Bool_t MCosy::HandleTimer(TTimer *t)
1246{
1247 const Int_t rc = fMutexGui.TryLock();
1248 if (rc==13)
1249 gLog << warn << "MCosy::HandleTimer - mutex is already locked by this thread" << endl;
1250
1251 if (rc)
1252 {
1253 gLog << warn << "* GUI update skipped due to locked mutex." << endl;
1254 return kTRUE;
1255 }
1256
1257 //
1258 // Update Gui, foremer MTGui.
1259 //
1260 if (fZd1)
1261 fZd1->DisplayVal();
1262 if (fZd2)
1263 fZd2->DisplayVal();
1264 if (fAz)
1265 fAz->DisplayVal();
1266
1267 Byte_t avail = 0;
1268
1269 avail |= (fMac1 && !fMac1->IsZombieNode()) ? 0x01 : 0;
1270 avail |= (fMac2 && !fMac2->IsZombieNode()) ? 0x02 : 0;
1271 //avail |= (fMac3 && !fMac3->IsZombieNode()) ? 0x04 : 0;
1272 avail |= (fZd1 && !fZd1->IsZombieNode()) ? 0x08 : 0;
1273 avail |= (fZd2 && !fZd2->IsZombieNode()) ? 0x10 : 0;
1274 avail |= (fAz && !fAz->IsZombieNode()) ? 0x20 : 0;
1275// avail |= (!(fStatus&MDriveCom::kError) && 1 ? 0x40 : 0;
1276
1277 Bool_t armed = kTRUE;
1278
1279 armed &= fMac1 && fMac1->IsArmed();
1280 armed &= fMac2 && fMac2->IsArmed();
1281
1282 if (HasError())
1283 SetStatus(MDriveCom::kError);
1284
1285
1286 ZdAz bendist = fStatus&MDriveCom::kTracking ? fTrackingPos : GetPointingPos();
1287
1288 //cout << (fStatus&MDriveCom::kTracking?"TRA: ":"POS: ") << bendist.Zd() << " " << bendist.Az() << endl;
1289
1290 fCom->SendReport(fStatus, fRaDec, fZdAzSoll, bendist, fTrackingError, armed);
1291
1292 fWin->UpdateWeather(*fCom);
1293 fWin->Update(bendist, fTrackingError, fVelocity, /*fOffset,*/
1294 fRaDec, fZdAzSoll, fStatus, avail, armed);
1295
1296 gLog.UpdateGui();
1297
1298 const Bool_t trigger = fTriggerDisplay;
1299 fTriggerDisplay = kFALSE;
1300
1301 if (fBackground==kBgdSeTestDispl || (trigger&&fBackground==kBgdSeTest))
1302 DisplayHistTestSe(!trigger);
1303
1304 if (fBackground==kBgdGearDispl || (trigger&&fBackground==kBgdGear))
1305 DisplayHistGear(!trigger);
1306
1307 if (fMutexGui.UnLock()==13)
1308 gLog << warn << "MCosy::HandleTimer - tried to unlock mutex locked by other thread." << endl;
1309
1310 return kTRUE;
1311}
1312
1313void MCosy::DisplayHistTestSe(Bool_t del)
1314{
1315 gLog << inf2 << "Displaying histogram..." << endl;
1316
1317 TH2F &hist = *(TH2F*)fHist;
1318
1319 if (del)
1320 {
1321 fHist = NULL;
1322 fBackground = kBgdNone;
1323 }
1324
1325 TCanvas *c=new TCanvas("c1", "", 1000, 1000);
1326 c->Divide(1,2);
1327
1328 c->cd(1);
1329 TH2 *h=(TH2*)hist.DrawCopy();
1330
1331 TProfile *p = h->ProfileX("_pfx", -1, 9999, "s");
1332 p->SetLineColor(kBlue);
1333 p->Draw("same");
1334 p->SetBit(kCanDelete);
1335
1336 c->cd(2);
1337
1338 TH1F p2("spread", "Spread of the differences", hist.GetNbinsX(), hist.GetBinLowEdge(1),
1339 hist.GetBinLowEdge(hist.GetNbinsX()+1));
1340 p2.SetXTitle("Zd [\\circ]");
1341 for (int i=0; i<hist.GetNbinsX(); i++)
1342 p2.SetBinError(i, p->GetBinError(i));
1343 p2.SetLineColor(kRed);
1344 p2.SetStats(0);
1345 p2.DrawCopy();
1346
1347 if (del)
1348 delete &hist;
1349}
1350
1351void MCosy::DisplayHistGear(Bool_t del)
1352{
1353 gLog << inf2 << "Displaying histogram..." << endl;
1354
1355 TH3F &hist = *(TH3F*)fHist;
1356
1357 if (del)
1358 {
1359 fHist = NULL;
1360 fBackground = kBgdNone;
1361 }
1362
1363 TCanvas *c=new TCanvas("c1", "", 1000, 1000);
1364 c->Divide(2,2);
1365
1366 // ----------
1367
1368 c->cd(1);
1369 TH2D &h1=*(TH2D*)hist.Project3D("zx"); // Zd
1370 h1.SetTitle(" Gear Ratio Zenith Distance [re/se] ");
1371 h1.SetXTitle("Zd [\\circ]");
1372 h1.Draw();
1373 h1.SetBit(kCanDelete);
1374
1375 TProfile *p1 = h1.ProfileX("_pfx", -1, 9999, "s");
1376 p1->SetLineColor(kBlue);
1377 p1->Draw("same");
1378 p1->SetBit(kCanDelete);
1379
1380 // ----------
1381
1382 c->cd(2);
1383 TH2D &h2=*(TH2D*)hist.Project3D("zy"); // Az
1384 h2.SetTitle(" Gear Ratio Azimuth [re/se] ");
1385 h2.SetXTitle("Zd [\\circ]");
1386 h2.Draw();
1387 h2.SetBit(kCanDelete);
1388
1389 TProfile *p2 = h2.ProfileX("_pfx", -1, 9999, "s");
1390 p2->SetLineColor(kBlue);
1391 p2->Draw("same");
1392 p2->SetBit(kCanDelete);
1393
1394 // ----------
1395
1396 c->cd(3);
1397
1398 TAxis &axe1 = *h1.GetXaxis();
1399
1400 TH1F f1("spreadzd", " Spread Zenith Distance ",
1401 axe1.GetNbins(), axe1.GetXmin(), axe1.GetXmax());
1402 f1.SetXTitle("Zd [\\circ]");
1403 for (int i=0; i<axe1.GetNbins(); i++)
1404 f1.SetBinError(i, p1->GetBinError(i));
1405 f1.SetLineColor(kRed);
1406 f1.SetStats(0);
1407 f1.DrawCopy();
1408
1409 c->cd(4);
1410
1411 // ----------
1412
1413 TAxis &axe2 = *h2.GetXaxis();
1414
1415 TH1F f2("spreadaz", " Spread Azimuth ",
1416 axe2.GetNbins(), axe2.GetXmin(), axe2.GetXmax());
1417 f2.SetXTitle("Az [\\circ]");
1418 for (int i=0; i<axe2.GetNbins(); i++)
1419 f2.SetBinError(i, p2->GetBinError(i));
1420 f2.SetLineColor(kRed);
1421 f2.SetStats(0);
1422 f2.DrawCopy();
1423
1424 // ----------
1425
1426 if (del)
1427 delete &hist;
1428}
1429
1430// --------------------------------------------------------------------------
1431//
1432// Start the work of the application:
1433//
1434// Start the Can-Network.
1435// Start the MCosy::TalkThread thread.
1436// turn on the gui update
1437//
1438void MCosy::Start(MEnv &env)
1439{
1440 // Don't call this function twice!
1441 Network::Start();
1442
1443 CheckForError();
1444
1445 ReadConfig(env);
1446
1447 gLog << inf << "- Starting TX Thread." << endl;
1448 fTTalk = new MTTalk(this);
1449
1450 gLog << inf << "- Starting GUI update." << endl;
1451 fUpdateGui->TurnOn();
1452}
1453
1454// --------------------------------------------------------------------------
1455//
1456// Start the work of the application:
1457//
1458// Turn of the gui update
1459// stop the MCosy::TalkThread thread.
1460// Stop the network
1461//
1462void MCosy::Stop()
1463{
1464 gLog << inf << "- Stopping GUI update." << endl;
1465 fUpdateGui->TurnOff();
1466 gLog << inf << "- GUI Update stopped." << endl;
1467
1468 gLog << inf << "- Stopping TX Thread." << endl;
1469 delete fTTalk;
1470 gLog << inf << "- TX Thread stopped." << endl;
1471
1472 gLog << inf << "- Stopping CAN network." << endl;
1473 Network::Stop();
1474 gLog << inf << "- CAN network stopped." << endl;
1475
1476 gLog << inf << "- Stopping message queue." << endl;
1477 CancelThread();
1478 gLog << inf << "- Message queue stopped." << endl;
1479}
1480
1481// --------------------------------------------------------------------------
1482//
1483// Disable the synchronization by using a negative CAN Id for id2.
1484//
1485void MCosy::Constructor(Int_t id1, Int_t id2, Int_t id3,
1486 Int_t id4, Int_t id5, Int_t id6)
1487{
1488 //
1489 // Create Nodes
1490 //
1491 gLog << inf << "- Setting up network." << endl;
1492
1493 fMac1=new Macs(id1, "Mac/Az");
1494 fMac2=new Macs(id3, "Mac/Zd");
1495 //if (id2>=0)
1496 // fMac3=new Macs(id2, "Mac/Az-Sync");
1497
1498 fZd1=new ShaftEncoder(id4, "SE/Zd1");
1499 fZd2=new ShaftEncoder(id5, "SE/Zd2");
1500 fAz =new ShaftEncoder(id6, "SE/Az");
1501
1502 fZd1->SetReport(fOutRep);
1503 fZd2->SetReport(fOutRep);
1504 fAz->SetReport(fOutRep);
1505
1506 fAz->SetMotor(fMac1);
1507 fZd1->SetMotor(fMac2);
1508 fZd2->SetMotor(fMac2);
1509
1510 gLog << inf << "- Connecting devices to network." << endl;
1511
1512 //
1513 // Connect the devices to the network
1514 //
1515 SetNode(fMac1);
1516 SetNode(fMac2);
1517 //if (id2>=0)
1518 // SetNode(fMac3);
1519 SetNode(fZd1);
1520 SetNode(fZd2);
1521 SetNode(fAz);
1522
1523 //
1524 // Create Gui Event timer and Gui
1525 //
1526 gLog << inf << "- Initializing GUI Timer." << endl;
1527 fUpdateGui = new TTimer(this, 100); // 100ms
1528
1529 gLog << all << "- Starting GUI." << endl;
1530 fWin=new MGCosy(fObservatory, this, gClient->GetRoot(), 1, 1);
1531}
1532/*
1533void MCosy::ConstructorSE(Int_t id4, Int_t id5, Int_t id6)
1534{
1535 //
1536 // Create Nodes
1537 //
1538 gLog << "- Setting up network." << endl;
1539
1540 fZd1=new ShaftEncoder(id4, "SE/Zd1", gLog);
1541 fZd2=new ShaftEncoder(id5, "SE/Zd2", gLog);
1542 fAz =new ShaftEncoder(id6, "SE/Az", gLog);
1543
1544 gLog << "- Connecting devices to network." << endl;
1545
1546 //
1547 // Connect the devices to the network
1548 //
1549 SetNode(fZd1);
1550 SetNode(fZd2);
1551 SetNode(fAz);
1552
1553 //
1554 // Create Gui Event timer and Gui
1555 //
1556 gLog << "- Initializing GUI Timer." << endl;
1557 fUpdateGui = new TTimer(this, 100); // 100ms
1558
1559 gLog << "- Starting GUI." << endl;
1560 fWin=new MGCosy(fObservatory, this, gClient->GetRoot(), 1, 1);
1561}
1562
1563void MCosy::ConstructorDemo()
1564{
1565 //
1566 // Create Nodes
1567 //
1568 gLog << "- Setting up network." << endl;
1569
1570 //
1571 // Create Gui Event timer and Gui
1572 //
1573 gLog << "- Initializing GUI Timer." << endl;
1574 fUpdateGui = new TTimer(this, 100); // 100ms
1575
1576 gLog << "- Starting GUI." << endl;
1577 fWin=new MGCosy(fObservatory, this, gClient->GetRoot(), 1, 1);
1578}
1579*/
1580
1581TString MCosy::GetFileName(const char *path, const char *name, const char *ext)
1582{
1583 // FIXME: Timeout missing
1584
1585 while (1)
1586 {
1587 MTime time(-1);
1588
1589 // This is the full qualified date which is part of the name
1590 const TString clock = time.GetStringFmt("%Y%m%d_%H%M%S");
1591
1592 // This gives the night in which the date belongs to
1593 time.SetMjd(TMath::Nint(time.GetMjd()));
1594
1595 const TString night = time.GetStringFmt("%Y_%m_%d");
1596
1597 const TString dir = Form("%s/%s", path, night.Data());
1598 const TString fname = Form("%s_%s.%s", name, clock.Data(), ext);
1599
1600 const TString full = Form("%s/%s", dir.Data(), fname.Data());
1601
1602 gSystem->mkdir(dir, kTRUE);
1603
1604 if (gSystem->AccessPathName(full, kFileExists))
1605 return full;
1606
1607 break;// !!!!!!!!!!!!!!!!!!!!!!!
1608
1609 usleep(1000);
1610 }
1611 return "";
1612}
1613
1614MCosy::MCosy(MEnv &env, MDriveCom *com, const char *pointing)
1615: Network(), fObservatory(MObservatory::kMagic1), fStarguider(NULL),
1616fZd1(0), fZd2(0), fAz(0), fMac1(0), fMac2(0), /*fMac3(0),*/
1617fBackground(kBgdNone), fTriggerDisplay(kFALSE), fStatus(MDriveCom::kStopped),
1618fOutTp(0), fOutRep(0)
1619{
1620 const Int_t id1 = env.GetValue("Az_Id-MAC1", 1); //1
1621 const Int_t id2 = env.GetValue("Az_Id-MAC2", 2); //2
1622 const Int_t id3 = env.GetValue("Zd_Id-MAC", 3); //3
1623 const Int_t id4 = env.GetValue("Zd_Id-SE1", 4); //4
1624 const Int_t id5 = env.GetValue("Zd_Id-SE2", 5); //5
1625 const Int_t id6 = env.GetValue("Az_Id-SE", 6); //6
1626
1627 TString name = GetFileName("rep", "cosy", "rep");
1628 gLog << inf << "Open Repfile: " << name << endl;
1629 fOutRep = new MLog(name, kTRUE);
1630 *fOutRep << all << "[Drive Report File]" << endl;
1631 *fOutRep << "Version <cvs>" << endl;
1632 *fOutRep << "Date " << MTime(-1) << endl;
1633 *fOutRep << "[Reports]" << endl;
1634
1635/*
1636 gLog << "- Program in ";
1637 switch (mode)
1638 {
1639 case 0:
1640 gLog << "<<Standard mode>>" << endl;*/
1641 gLog << all << "Reading pointing model from " << pointing << "..." << endl;
1642 if (fBending.Load(pointing))
1643 gLog << all << "Reading pointing model from " << pointing << " successfull." << endl;
1644 else
1645 gLog << err << "ERROR - Reading pointing model from " << pointing << endl;
1646 Constructor(id1, id2, id3, id4, id5, id6);/*
1647 break;
1648 case 1:
1649 gLog << "<<SE mode>>" << endl;
1650 fBending.Load("bending.txt");
1651 ConstructorSE(id4, id5, id6);
1652 break;
1653 default:
1654 gLog << "<<Demo mode>>" << endl;
1655 ConstructorDemo();
1656 }
1657*/
1658
1659 gLog.SetOutputGui(fWin->GetLog(), kTRUE);
1660
1661 fZd1->SetDisplay(fWin->GetLabel2());
1662 fZd2->SetDisplay(fWin->GetLabel3());
1663 fAz->SetDisplay(fWin->GetLabel1());
1664
1665 fCom = com;//new MDriveCom(this, addr, tx, rx, fOutRep);
1666 fCom->SetOutRep(fOutRep);
1667 // fCom->Start();
1668}
1669
1670void MCosy::TerminateApp()
1671{
1672 gLog << inf2 << "MCosy::TerminateApp()" << endl;
1673/*
1674 Int_t rc;
1675 TGMessageBox msg(this, gClient->GetRoot(),
1676 "Information",
1677 "Cosy is shutting down the system - this may take wa while!",
1678 kMBIconExclamation,
1679 kMBOK, //kMBClose
1680 &rc, 0);
1681*/
1682
1683 gLog.DisableOutputDevice(MLog::eGui);
1684 // FIXME: WHY DOES THIS CRASH THE APPLICATIOn WHILE TRAKING?
1685 // gLog.SetOutputGui(NULL, kFALSE);
1686
1687 gApplication->Terminate(0);
1688}
1689
1690MCosy::~MCosy()
1691{
1692 if(fCom)
1693 {
1694 fCom->SetMsgQueue(NULL);
1695 fCom->SetOutRep(NULL);
1696 }
1697
1698 gLog << inf2 << "Deleting GUI timer." << endl;
1699 // FIXME: Wait until last Update was finished!!!
1700 delete fUpdateGui;
1701
1702 //fMutexGui.Lock();
1703
1704 // Now the files can safely be closed
1705 gLog << inf2 << "Closing output files." << endl;
1706 if (fOutTp)
1707 {
1708 *fOutTp << "END" << endl;
1709 delete fOutTp;
1710 }
1711
1712 delete fOutRep;
1713
1714 //gLog << inf2 << "Deleting CC communication." << endl;
1715 //delete fCom;
1716
1717 gLog << inf2 << "Deleting Nodes." << endl;
1718 fZd1->SetReport(0);
1719 fZd2->SetReport(0);
1720 fAz->SetReport(0);
1721
1722 delete fAz;
1723 delete fZd1;
1724 delete fZd2;
1725 delete fMac1;
1726 delete fMac2;
1727 //if (fMac3)
1728 // delete fMac3;
1729
1730 gLog << inf2 << "Deleting MGCosy." << endl;
1731
1732 gLog.DisableOutputDevice(MLog::eGui);
1733
1734 delete fWin;
1735
1736 gLog << inf2 << "MGCosy destructed." << endl;
1737}
Note: See TracBrowser for help on using the repository browser.