Index: /trunk/MagicSoft/Cosy/Changelog
===================================================================
--- /trunk/MagicSoft/Cosy/Changelog	(revision 8832)
+++ /trunk/MagicSoft/Cosy/Changelog	(revision 8833)
@@ -71,4 +71,13 @@
    * videodev/VideodevLinkDef.h:
      - removed Camkera from dictionary
+
+   * main/MCosy.[h,cc]:
+     - replaced kDeg2Rad and kRad2Deg by TMath
+     - replaced lout by gLog
+     - made a few changes for the new coordinate classes
+     - added cancel points to thread where possible
+     - changed to new threading classes
+     - thread doesn't run detached anymore
+
 
 
Index: /trunk/MagicSoft/Cosy/main/MCosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 8832)
+++ /trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 8833)
@@ -4,9 +4,7 @@
 #include <fstream>
 
-#include <TROOT.h>
 #include <TEnv.h>
-#include <TSystem.h>
+#include <TTimer.h>
 #include <TApplication.h>
-#include <TTimer.h>
 
 #include <TH2.h>
@@ -14,4 +12,6 @@
 #include <TProfile.h>
 #include <TCanvas.h>
+
+#include "MLogManip.h"
 
 #include "MGCosy.h"
@@ -23,6 +23,4 @@
 #include "MTracking.h"
 
-#include "slalib/slalib.h"  // FIXME: REMOVE
-
 #include "macs.h"
 #include "shaftencoder.h"
@@ -56,5 +54,5 @@
 
     ZdAz source = src * 360.0/16384.0;
-    ZdAz dest   = dst * kRad2Deg;
+    ZdAz dest   = dst * TMath::RadToDeg();
 
     if (dest.Zd()>-3 && dest.Zd()<3)
@@ -207,12 +205,12 @@
 
     MTime t(-1);
-    lout << t << " - MCosy::WaitForEndMovement aborted...";
+    gLog << t << " - MCosy::WaitForEndMovement aborted...";
     if (Break())
-        lout << " Break signal...";
+        gLog << " Break signal...";
     if (HasError())
-        lout << " Network has error...";
+        gLog << " Network has error...";
     if (HasZombie())
-        lout << " Network has zombie...";
-    lout << endl;
+        gLog << " Network has zombie...";
+    gLog << endl;
 }
 
@@ -272,5 +270,5 @@
     if (d.Zd()<fMin.Zd())
     {
-        lout << "ERROR: Requested Zenith Angle below negative endswitch." << endl;
+        gLog << "ERROR: Requested Zenith Angle below negative endswitch." << endl;
         return kFALSE;
     }
@@ -278,5 +276,5 @@
     if (d.Zd()>fMax.Zd())
     {
-        lout << "ERROR: Requested Zenith Angle behind positive endswitch." << endl;
+        gLog << "ERROR: Requested Zenith Angle behind positive endswitch." << endl;
         return kFALSE;
     }
@@ -284,5 +282,5 @@
     if (d.Az()<fMin.Az())
     {
-        lout << "ERROR: Requested Azimuth Angle below negative endswitch." << endl;
+        gLog << "ERROR: Requested Azimuth Angle below negative endswitch." << endl;
         return kFALSE;
     }
@@ -290,5 +288,5 @@
     if (d.Az()>fMax.Az())
     {
-        lout << "ERROR: Requested Azimuth Angle behind positive endswitch." << endl;
+        gLog << "ERROR: Requested Azimuth Angle behind positive endswitch." << endl;
         return kFALSE;
     }
@@ -303,5 +301,5 @@
     // AlignTrackingPos [deg]
 
-    pointing *= kRad2Deg;
+    pointing *= TMath::RadToDeg();
 
     if (pointing.Zd()<0)
@@ -309,11 +307,11 @@
         pointing.Zd(-pointing.Zd());
         pointing.Az(pointing.Az()+180);
-        //lout << "ZD=-ZD Az+=180" << endl;
+        //gLog << "ZD=-ZD Az+=180" << endl;
     }
 
     const ZdAz se = GetSePos()*TMath::TwoPi()/kResSE;   // [rad]
-    const ZdAz unbendedse = fBending.CorrectBack(se)*kRad2Deg; // ist pointing
-
-    //lout << "Unbended: " << unbendedse.Zd() << " " << unbendedse.Az() << endl;
+    const ZdAz unbendedse = fBending.CorrectBack(se)*TMath::RadToDeg(); // ist pointing
+
+    //gLog << "Unbended: " << unbendedse.Zd() << " " << unbendedse.Az() << endl;
 
     do
@@ -323,16 +321,16 @@
             break;
 
-        //lout << "AZ += " << TMath::Sign(360., d) << endl;
+        //gLog << "AZ += " << TMath::Sign(360., d) << endl;
 
         pointing.Az(pointing.Az()+TMath::Sign(360., d));
     } while (1);
 
-    return pointing/kRad2Deg;
+    return pointing/TMath::RadToDeg();
 /*
     const Bool_t rc = CheckRange(pointing);
-    za = pointing/kRad2Deg; // [rad]
+    za = pointing/TMath::RadToDeg(); // [rad]
 
     if (!rc)
-        lout << "Error: Aligned position out of Range." << endl;
+        gLog << "Error: Aligned position out of Range." << endl;
 
     return rc;*/
@@ -353,5 +351,5 @@
         return -1;
 
-    ZdAz point=AlignTrackingPos(ZdAz(zd, az)/kRad2Deg);
+    ZdAz point=AlignTrackingPos(ZdAz(zd, az)/TMath::RadToDeg());
     /*
     if (!AlignTrackingPos(ZdAz(zd, az), point))
@@ -363,5 +361,5 @@
     // FIXME: Check Range missing!
 
-    const ZdAz diff = (dest-point)*kRad2Deg;
+    const ZdAz diff = (dest-point)*TMath::RadToDeg();
 
     if (diff.Zd()>5 || diff.Az()>5)
@@ -392,5 +390,5 @@
 int MCosy::SetPosition(const ZdAz &dst, Bool_t track) // [rad]
 {
-    MPointing point(this, lout);
+    MSlewing point(this);
 
 //#ifdef EXPERT
@@ -434,5 +432,5 @@
 void MCosy::TrackPosition(const RaDec &dst) // ra, dec [rad]
 {
-    MTracking track(this, lout);
+    MTracking track(this);
     track.SetOut(fOutRep);
 //#ifdef EXPERT
@@ -459,5 +457,5 @@
 void MCosy::TrackPositionGRB(const RaDec &dst) // ra, dec [rad]
 {
-    MTracking track(this, lout);
+    MTracking track(this);
     track.SetOut(fOutRep);
 //#ifdef EXPERT
@@ -563,5 +561,5 @@
 }
 
-void *MCosy::Proc(int msg, void *mp)
+Int_t MCosy::Proc(int msg, void *mp)
 {
     switch (msg)
@@ -569,12 +567,12 @@
     case WM_WAIT:
         cout << "Wait for execution of Proc(WM_*, ): done." << endl;
-        return NULL;
+        return 0;
 
     case WM_STOP:
         //cout << "MCosy::Proc: Stop." << endl;
         if (!CheckNetwork())
-            return (void*)0xebb0;
+            return 0xebb0;
         StopMovement();
-        return NULL;
+        return 0;
 /*
     case WM_PRESET:
@@ -605,5 +603,5 @@
             cout << "Calibrating to: " << rd.Ra()*24/360 << "h " << rd.Dec() << "°" << endl;
 
-            ZdAz za=sla.CalcZdAz(rd*kDeg2Rad)*16384.0/k2Pi;
+            ZdAz za=sla.CalcZdAz(rd*TMath::DegToRad())*16384.0/k2Pi;
 
             cout << "Calc Zd: " << za.Zd() << " Az: " << za.Az() << endl;
@@ -629,5 +627,5 @@
             cout << "TPoint Star: " << rd.Ra()/15 << "h " << rd.Dec() << "°" << endl;
 
-            AltAz za=sla.CalcAltAz(rd*kDeg2Rad)*kRad2Deg;
+            AltAz za=sla.CalcAltAz(rd*TMath::DegToRad())*TMath::RadToDeg();
 
             if (!fOutTp)
@@ -653,5 +651,5 @@
             ZdAz sepos = GetSePos()*TMath::TwoPi()/kResSE;
             za.Set(TMath::Pi()/2-sepos.Zd(), sepos.Az());
-            za *= kRad2Deg;
+            za *= TMath::RadToDeg();
 
             cout << "     SE-Pos: " << za.Alt() << "° " << za.Az() << "°" << endl;
@@ -668,5 +666,5 @@
             //cout << "WM_TPoint: done. (return 0xaffe)" << endl;
         }
-        return (void*)0xca1b;
+        return 0xca1b;
 
     case WM_TRACKPOS:
@@ -674,9 +672,9 @@
         {
             if (!CheckNetwork())
-                return (void*)0xebb0;
-
-            ZdAz dest = *((ZdAz*)mp) * kDeg2Rad;
+                return 0xebb0;
+
+            ZdAz dest = *((ZdAz*)mp) * TMath::DegToRad();
             if (!SetPosition(dest, kTRUE))
-                return (void*)0x1234;
+                return 0x1234;
 
             SlaStars sla(fObservatory);
@@ -687,5 +685,5 @@
         }
         //cout << "WM_TrackPosition: done. (return 0xabcd)" << endl;
-        return (void*)0xabcd;
+        return 0xabcd;
 /*
     case WM_ARM:
@@ -716,11 +714,11 @@
         {
             if (!CheckNetwork())
-                return (void*)0xebb0;
+                return 0xebb0;
 
             ZdAz dest = *((ZdAz*)mp);
-            SetPosition(dest*kDeg2Rad);
+            SetPosition(dest*TMath::DegToRad());
         }
         //cout << "WM_Position: done. (return 0x7777)" << endl;
-        return (void*)0x7777;
+        return 0x7777;
 
     case WM_POSITION1:
@@ -728,11 +726,11 @@
         {
             if (!CheckNetwork())
-                return (void*)0xebb0;
+                return 0xebb0;
 
             ZdAz dest = *((ZdAz*)mp);
-            SetPosition(dest*kDeg2Rad, kTRUE);
+            SetPosition(dest*TMath::DegToRad(), kTRUE);
         }
         //cout << "WM_Position: done. (return 0x7777)" << endl;
-        return (void*)0x7777;
+        return 0x7777;
 
     case WM_PREPS:
@@ -740,5 +738,5 @@
         {
             if (!CheckNetwork())
-                return (void*)0xebb0;
+                return 0xebb0;
 
             const char *preps = (const char*)mp;
@@ -749,5 +747,5 @@
             {
                 cout << "ERROR: cannot open prepos.txt." << endl;
-                return (void*)0xebb1;
+                return 0xebb1;
             }
 
@@ -767,6 +765,6 @@
                 {
                     ZdAz dest(zd, az);
-                    SetPosition(dest*kDeg2Rad);
-                    return (void*)0x7979;
+                    SetPosition(dest*TMath::DegToRad());
+                    return 0x7979;
                 }
                 cout << "ERROR - Requested preposition not found in file..." << endl;
@@ -774,5 +772,5 @@
         }
         //cout << "WM_Track: done. (return 0x8888)" << endl;
-        return (void*)0x7878;
+        return 0x7878;
 
     case WM_TESTSE:
@@ -780,5 +778,5 @@
         fBackground = mp ? kBgdSeTest : kBgdNone;
         //cout << "WM_TestSe: done. (return 0x1e51)" << endl;
-        return (void*)0x1e51;
+        return 0x1e51;
 
     case WM_GEAR:
@@ -786,5 +784,5 @@
         fBackground = mp ? kBgdGear : kBgdNone;
         //cout << "WM_Gear: done. (return 0xfeaf)" << endl;
-        return (void*)0xfeaf;
+        return 0xfeaf;
 
     case WM_DISPLAY:
@@ -792,5 +790,5 @@
         fTriggerDisplay = kTRUE;
         //cout << "WM_Disply: done. (return 0xd1e1)" << endl;
-        return (void*)0xd1e1;
+        return 0xd1e1;
 
     case WM_TRACK:
@@ -802,13 +800,13 @@
                 fStarguider->SetPointingPosition(((RaDec*)mp)[1]);
             if (!CheckNetwork())
-                return (void*)0xebb0;
+                return 0xebb0;
 
             if (msg==WM_TRACK)
-                TrackPosition(dest*kDeg2Rad);
+                TrackPosition(dest*TMath::DegToRad());
             else
-                TrackPositionGRB(dest*kDeg2Rad);
+                TrackPositionGRB(dest*TMath::DegToRad());
         }
         //cout << "WM_Track/GRB: done. (return 0x8888)" << endl;
-        return (void*)0x8888;
+        return 0x8888;
 
     case WM_NEWTRACK:
@@ -816,5 +814,5 @@
         fRaDec = *((RaDec*)mp);
         //cout << "WM_NewTrack: done. (return 0x9999)" << endl;
-        return (void*)0x9999;
+        return 0x9999;
 
     case WM_LOADBENDING:
@@ -822,5 +820,5 @@
         fBending.Load("bending.txt");
         //cout << "WM_LoadBending: done. (return 0xbe0d)" << endl;
-        return (void*)0xbe0d;
+        return 0xbe0d;
 
     case WM_RESETBENDING:
@@ -828,10 +826,10 @@
         fBending.Reset();
         //cout << "WM_ResetBending: done. (return 0xbe0e)" << endl;
-        return (void*)0xbe0e;
+        return 0xbe0e;
 
     case WM_HOME:
         //cout << "WM_Home: START" << endl;
         if (!CheckNetwork())
-            return (void*)0xebb0;
+            return 0xebb0;
         else
         {
@@ -846,5 +844,5 @@
             fMac2->SetHome(250000, env.GetValue("Zd_MaxTime2ReachHome[s]", 100));
 
-            lout << "SETHOME DONE" << endl;
+            gLog << "SETHOME DONE" << endl;
 
             SetStatus(HasError() ? MDriveCom::kError : MDriveCom::kStopped);
@@ -860,5 +858,5 @@
         }
         //cout << "WM_Home: done. (return 0x403e)" << endl;
-        return (void*)0x403e;
+        return 0x403e;
 
     case WM_CALCALTAZ:
@@ -872,8 +870,8 @@
             RaDec rd(xy.X()*15., xy.Y()); // [deg]
 
-            ZdAz a1 = sla.CalcZdAz(rd*kDeg2Rad); // [rad]
+            ZdAz a1 = sla.CalcZdAz(rd*TMath::DegToRad()); // [rad]
 
             cout << "Ra/Dec source: " << xy.X()  << "h " << xy.Y()  << "°" << endl;
-            cout << "Zd/Az target:  " << a1.Zd()*kRad2Deg << "° " << a1.Az()*kRad2Deg << "°" << endl;
+            cout << "Zd/Az target:  " << a1.Zd()*TMath::RadToDeg() << "° " << a1.Az()*TMath::RadToDeg() << "°" << endl;
 
             if (fZd1 && fZd2 && fAz)
@@ -882,5 +880,5 @@
             a1 = fBending(a1);
             CheckRange(a1);
-            a1 *= kRad2Deg;
+            a1 *= TMath::RadToDeg();
 
             const ZdAz a2 = a1*kResSE/360;
@@ -889,10 +887,10 @@
             cout << "SE bended:     " << a2.Zd() << "  " << a2.Az() << endl;
         }
-        return (void*)0xa17a;
+        return 0xa17a;
 
     case WM_ENDSWITCH:
         {
             ZdAz pos = GetSePos()*TMath::TwoPi()/kResSE;
-            pos = fBending.SubtractOffsets(pos)*kRad2Deg;
+            pos = fBending.SubtractOffsets(pos)*TMath::RadToDeg();
 
             cout << "Endswitch Position:  Zd=" << pos.Zd() << "°  Az=";
@@ -900,5 +898,5 @@
         }
 
-        return (void*)0x1010;
+        return 0x1010;
 
     case WM_QUIT:
@@ -906,19 +904,19 @@
         if (!CheckNetwork())
         {
-            lout << "ERROR: Cannot shutdown CANbus network." << endl;
-            return (void*)0xebb0;
+            gLog << "ERROR: Cannot shutdown CANbus network." << endl;
+            return 0xebb0;
         }
         TerminateApp();
         cout << "WM_Quit: done." << endl;
-        return (void*)0xaaaa;
+        return 0xaaaa;
     }
     cout << "MCosy::Proc: Unknown message 0x" << msg << endl;
-    return (void*)0xffffffff;
-}
-
-void *MTTalk::Thread()
+    return 0xffffffff;
+}
+
+Int_t MTTalk::Thread()
 {
     fCosy->TalkThread();
-    return NULL;
+    return 0;
 }
 
@@ -942,18 +940,40 @@
     cout << " * Max:  " << zmax << "deg  " << amax << "deg" << endl;
 
-    fMin = fBending.AddOffsets(fMin/kRad2Deg);
-    fMax = fBending.AddOffsets(fMax/kRad2Deg);
-
-    cout << " * Min': " << fMin.Zd()*kRad2Deg << "deg  " << fMin.Az()*kRad2Deg << "deg" << endl;
-    cout << " * Max': " << fMax.Zd()*kRad2Deg << "deg  " << fMax.Az()*kRad2Deg << "deg" << endl;
+    fMin = fBending.AddOffsets(fMin/TMath::RadToDeg());
+    fMax = fBending.AddOffsets(fMax/TMath::RadToDeg());
+
+    cout << " * Min': " << fMin.Zd()*TMath::RadToDeg() << "deg  " << fMin.Az()*TMath::RadToDeg() << "deg" << endl;
+    cout << " * Max': " << fMax.Zd()*TMath::RadToDeg() << "deg  " << fMax.Az()*TMath::RadToDeg() << "deg" << endl;
 
     cout << "Reading gear ratios..." << flush;
-    kGear.X(env.GetValue("Zd_GearRatio[U_mot/U_tel]", 1000.0));
-    kGear.Y(env.GetValue("Az_GearRatio[U_mot/U_tel]", 1000.0));
-
+//    kGear.X(env.GetValue("Zd_GearRatio[U_mot/U_tel]", 1000.0));
+//    kGear.Y(env.GetValue("Az_GearRatio[U_mot/U_tel]", 1000.0));
+    kGear.Set(env.GetValue("Zd_GearRatio[U_mot/U_tel]", 1000.0),
+              env.GetValue("Az_GearRatio[U_mot/U_tel]", 1000.0));
+
+    Bool_t mac1 = fMac1 && !fMac1->IsZombieNode();
+    Bool_t mac2 = fMac2 && !fMac2->IsZombieNode();
+    Bool_t mac3 = fMac3 && !fMac3->IsZombieNode();
+
+    Bool_t zd1  = fZd1  && !fZd1->IsZombieNode();
+    Bool_t zd2  = fZd2  && !fZd2->IsZombieNode();
+    Bool_t az   = fAz   && !fAz->IsZombieNode();
+
+    Double_t x = 0;
+    Double_t y = 0;
+    y = mac1 ? fMac1->GetRes() : (mac3 ? fMac3->GetRes() : env.GetValue("Az_ResRE[re/U_mot]", 1500));
+    x = mac2 ? fMac2->GetRes() : env.GetValue("Zd_ResRE[re/U_mot]", 1500);
+    kResRE.Set(x,y);
+
+    x = zd1 ? fZd1->GetPhysRes() : (zd2 ? fZd2->GetPhysRes() : env.GetValue("Zd_ResSE[se/U_mot]", 16384));
+    y = az  ? fAz->GetPhysRes() : env.GetValue("Az_ResSE[se/U_mot]", 16384);
+    kResSE.Set(x,y);
+
+    /*
     kResRE.Y(0);
     if (fMac1 && !fMac1->IsZombieNode())
         kResRE.Y(fMac1->GetRes());
     else
+        y = fMac3 && !fMac3->IsZombieNode() ? fMac3->GetRes() : env.GetValue("Az_ResRE[re/U_mot]", 1500);
         if (fMac3 && !fMac3->IsZombieNode())
             kResRE.Y(fMac3->GetRes());
@@ -981,4 +1001,5 @@
     else
         kResSE.Y(env.GetValue("Az_ResSE[se/U_mot]", 16384));
+        */
 
     // believing the Macs manual '*4' shouldn't be necessary, but it is.
@@ -986,5 +1007,6 @@
     // Calculating speeds we have to convert back to qc
     kResRE  *= 4;
-    kGearTot = kResRE*kGear;
+    kGearTot = Mul(kResRE, kGear);
+//    kGearTot = kResRE*kGear;
 
     cout << "done." << endl;
@@ -1000,5 +1022,5 @@
     if (!fMac3)
     {
-        lout << "Unable to Init Sync! Mac3 not available." << endl;
+        gLog << "Unable to Init Sync! Mac3 not available." << endl;
         return;
     }
@@ -1019,5 +1041,5 @@
     if (fHist)
     {
-        lout << "You are much too fast... try again." << endl;
+        gLog << "You are much too fast... try again." << endl;
         return;
     }
@@ -1032,5 +1054,5 @@
     int cnt = 0;
 
-    lout << "Starting Shaftencoder Test..." << endl;
+    gLog << "Starting Shaftencoder Test..." << endl;
 
     while (fBackground==kBgdSeTest)
@@ -1041,5 +1063,8 @@
         while (!fZd1->PosHasChanged() && !fZd2->PosHasChanged() &&
                fBackground==kBgdSeTest)
+        {
             usleep(1);
+            TThread::CancelPoint();
+        }
 
         const Double_t pos[3] = {
@@ -1064,9 +1089,9 @@
         Double_t apos = (pos[0]-pos[1])/2 * TMath::TwoPi() / kResSE.X();
 
-        ZdAz bend = fBending.CorrectBack(ZdAz(apos, pos[2]))*kRad2Deg;
+        ZdAz bend = fBending.CorrectBack(ZdAz(apos, pos[2]))*TMath::RadToDeg();
         fHist->Fill(bend.Zd(), pos[0]+pos[1]-offset);
     }
 
-    lout << "Shaftencoder Test Stopped... displaying Histogram." << endl;
+    gLog << "Shaftencoder Test Stopped... displaying Histogram." << endl;
 
     fBackground=kBgdSeTestDispl;
@@ -1080,5 +1105,5 @@
     if (fHist)
     {
-        lout << "You are much too fast... try again." << endl;
+        gLog << "You are much too fast... try again." << endl;
         return;
     }
@@ -1093,5 +1118,5 @@
     fHist->SetZTitle("Re/Se");
 
-    lout << "Starting Gear determination..." << endl;
+    gLog << "Starting Gear determination..." << endl;
 
     ZdAz se0 = GetSePos();
@@ -1106,5 +1131,8 @@
         while (!fZd1->PosHasChanged() && !fZd2->PosHasChanged() &&
                !fAz->PosHasChanged() && fBackground==kBgdGear)
+        {
             usleep(1);
+            TThread::CancelPoint();
+        }
 
         ZdAz se = GetSePos();
@@ -1121,5 +1149,5 @@
             se -= dse/2;
 
-            ZdAz bend = fBending.CorrectBack(se*TMath::TwoPi()/kResSE)*kRad2Deg;
+            ZdAz bend = fBending.CorrectBack(se*TMath::TwoPi()/kResSE)*TMath::RadToDeg();
             ((TH3*)fHist)->Fill(bend.Zd(), bend.Az(), dre.Zd()/dse.Zd());
         }
@@ -1132,9 +1160,9 @@
             se -= dse/2;
 
-            ZdAz bend = fBending.CorrectBack(se*TMath::TwoPi()/kResSE)*kRad2Deg;
+            ZdAz bend = fBending.CorrectBack(se*TMath::TwoPi()/kResSE)*TMath::RadToDeg();
             ((TH3*)fHist)->Fill(bend.Az(), bend.Az(), dre.Az()/dse.Az());
         }
     }
-    lout << "Gear Test Stopped... displaying Histogram." << endl;
+    gLog << "Gear Test Stopped... displaying Histogram." << endl;
 
     fBackground=kBgdGearDispl;
@@ -1170,5 +1198,8 @@
         //
         while (fBackground==kBgdNone)
+        {
             usleep(1);
+            TThread::CancelPoint();
+        }
 
         switch (fBackground)
@@ -1213,5 +1244,5 @@
     if (rc)
     {
-        lout << "* GUI update skipped due to locked mutex." << endl;
+        gLog << "* GUI update skipped due to locked mutex." << endl;
         return kTRUE;
     }
@@ -1251,5 +1282,5 @@
                  fRaDec, fZdAzSoll, fStatus, avail);
 
-    lout.UpdateGui();
+    gLog.UpdateGui();
 
     const Bool_t trigger = fTriggerDisplay;
@@ -1270,5 +1301,5 @@
 void MCosy::DisplayHistTestSe(Bool_t del)
 {
-    lout << "Displaying histogram..." << endl;
+    gLog << "Displaying histogram..." << endl;
 
     TH2F &hist = *(TH2F*)fHist;
@@ -1308,5 +1339,5 @@
 void MCosy::DisplayHistGear(Bool_t del)
 {
-    lout << "Displaying histogram..." << endl;
+    gLog << "Displaying histogram..." << endl;
 
     TH3F &hist = *(TH3F*)fHist;
@@ -1402,8 +1433,8 @@
     ReadConfig();
 
-    lout << "- Starting TX Thread." << endl;
+    gLog << "- Starting TX Thread." << endl;
     fTTalk = new MTTalk(this);
 
-    lout << "- Starting GUI update." << endl;
+    gLog << "- Starting GUI update." << endl;
     fUpdateGui->TurnOn();
 }
@@ -1419,12 +1450,19 @@
 void MCosy::Stop()
 {
-    lout << "- Stopping GUI update." << endl;
+    gLog << inf << "- Stopping GUI update." << endl;
     fUpdateGui->TurnOff();
-    lout << "- GUI Update stopped." << endl;
-
+    gLog << inf << "- GUI Update stopped." << endl;
+
+    gLog << inf << "- Stopping TX Thread." << endl;
     delete fTTalk;
-    lout << "- TX Thread stopped." << endl;
-
+    gLog << inf << "- TX Thread stopped." << endl;
+
+    gLog << inf << "- Stopping CAN network." << endl;
     Network::Stop();
+    gLog << inf << "- CAN network stopped." << endl;
+
+    gLog << inf << "- Stopping message queue." << endl;
+    CancelThread();
+    gLog << inf << "- Message queue stopped." << endl;
 }
 
@@ -1439,14 +1477,14 @@
     // Create Nodes
     //
-    lout << "- Setting up network." << endl;
-
-    fMac1=new Macs(id1, "Mac/Az", lout);
-    fMac2=new Macs(id3, "Mac/Zd", lout);
+    gLog << "- Setting up network." << endl;
+
+    fMac1=new Macs(id1, "Mac/Az", gLog);
+    fMac2=new Macs(id3, "Mac/Zd", gLog);
     if (id2>=0)
-        fMac3=new Macs(id2, "Mac/Az-Sync", lout);
-
-    fZd1=new ShaftEncoder(id4, "SE/Zd1", lout);
-    fZd2=new ShaftEncoder(id5, "SE/Zd2", lout);
-    fAz =new ShaftEncoder(id6, "SE/Az",  lout);
+        fMac3=new Macs(id2, "Mac/Az-Sync", gLog);
+
+    fZd1=new ShaftEncoder(id4, "SE/Zd1", gLog);
+    fZd2=new ShaftEncoder(id5, "SE/Zd2", gLog);
+    fAz =new ShaftEncoder(id6, "SE/Az",  gLog);
 
     fZd1->SetReport(fOutRep);
@@ -1458,5 +1496,5 @@
     fZd2->SetMotor(fMac2);
  
-    lout << "- Connecting devices to network." << endl;
+    gLog << "- Connecting devices to network." << endl;
 
     //
@@ -1474,8 +1512,8 @@
     // Create Gui Event timer and Gui
     //
-    lout << "- Initializing GUI Timer." << endl;
+    gLog << "- Initializing GUI Timer." << endl;
     fUpdateGui = new TTimer(this, 100); // 100ms
 
-    lout << "- Starting GUI." << endl;
+    gLog << "- Starting GUI." << endl;
     fWin=new MGCosy(fObservatory, this, gClient->GetRoot(), 1, 1);
 }
@@ -1486,11 +1524,11 @@
     // Create Nodes
     //
-    lout << "- Setting up network." << endl;
-
-    fZd1=new ShaftEncoder(id4, "SE/Zd1", lout);
-    fZd2=new ShaftEncoder(id5, "SE/Zd2", lout);
-    fAz =new ShaftEncoder(id6, "SE/Az",  lout);
-
-    lout << "- Connecting devices to network." << endl;
+    gLog << "- Setting up network." << endl;
+
+    fZd1=new ShaftEncoder(id4, "SE/Zd1", gLog);
+    fZd2=new ShaftEncoder(id5, "SE/Zd2", gLog);
+    fAz =new ShaftEncoder(id6, "SE/Az",  gLog);
+
+    gLog << "- Connecting devices to network." << endl;
 
     //
@@ -1504,8 +1542,8 @@
     // Create Gui Event timer and Gui
     //
-    lout << "- Initializing GUI Timer." << endl;
+    gLog << "- Initializing GUI Timer." << endl;
     fUpdateGui = new TTimer(this, 100); // 100ms
 
-    lout << "- Starting GUI." << endl;
+    gLog << "- Starting GUI." << endl;
     fWin=new MGCosy(fObservatory, this, gClient->GetRoot(), 1, 1);
 }
@@ -1516,13 +1554,13 @@
     // Create Nodes
     //
-    lout << "- Setting up network." << endl;
+    gLog << "- Setting up network." << endl;
 
     //
     // Create Gui Event timer and Gui
     //
-    lout << "- Initializing GUI Timer." << endl;
+    gLog << "- Initializing GUI Timer." << endl;
     fUpdateGui = new TTimer(this, 100); // 100ms
 
-    lout << "- Starting GUI." << endl;
+    gLog << "- Starting GUI." << endl;
     fWin=new MGCosy(fObservatory, this, gClient->GetRoot(), 1, 1);
 }
@@ -1562,6 +1600,9 @@
 }
 
-MCosy::MCosy(/*int mode,*/ const char *dev, const int baud, MLog &out)
-: Network(dev, baud, out), fObservatory(MObservatory::kMagic1), fStarguider(NULL), fZd1(0), fZd2(0), fAz(0), fMac1(0), fMac2(0), fMac3(0), fBackground(kBgdNone), fStatus(MDriveCom::kStopped), fOutTp(0), fOutRep(0)
+MCosy::MCosy()
+: Network(), fObservatory(MObservatory::kMagic1), fStarguider(NULL),
+fZd1(0), fZd2(0), fAz(0), fMac1(0), fMac2(0), fMac3(0),
+fBackground(kBgdNone), fTriggerDisplay(kFALSE), fStatus(MDriveCom::kStopped),
+fOutTp(0), fOutRep(0)
 {
     TEnv env(".cosyrc");
@@ -1582,23 +1623,23 @@
 
 /*
-    lout << "- Program in ";
+    gLog << "- Program in ";
     switch (mode)
     {
     case 0:
-        lout << "<<Standard mode>>" << endl;*/
+        gLog << "<<Standard mode>>" << endl;*/
         fBending.Load("bending.txt");
         Constructor(id1, id2, id3, id4, id5, id6);/*
         break;
     case 1:
-        lout << "<<SE mode>>" << endl;
+        gLog << "<<SE mode>>" << endl;
         fBending.Load("bending.txt");
         ConstructorSE(id4, id5, id6);
         break;
     default:
-        lout << "<<Demo mode>>" << endl;
+        gLog << "<<Demo mode>>" << endl;
         ConstructorDemo();
     }
 */
-    lout.SetOutputGui(fWin->GetLog(), kTRUE);
+    gLog.SetOutputGui(fWin->GetLog(), kTRUE);
 
     fZd1->SetDisplay(fWin->GetLabel2());
@@ -1606,6 +1647,6 @@
     fAz->SetDisplay(fWin->GetLabel1());
 
-    fCom = new MDriveCom(this, *fOutRep);
-    fCom->Start();
+    fCom = new MDriveCom(this, fOutRep);
+//    fCom->Start();
 }
 
@@ -1623,7 +1664,7 @@
 */
 
-    lout.DisableOutputDevice(MLog::eGui);
+    gLog.DisableOutputDevice(MLog::eGui);
     // FIXME: WHY DOES THIS CRASH THE APPLICATIOn WHILE TRAKING?
-    // lout.SetOutputGui(NULL, kFALSE);
+    // gLog.SetOutputGui(NULL, kFALSE);
 
     gApplication->Terminate(0);
@@ -1632,4 +1673,12 @@
 MCosy::~MCosy()
 {
+    cout << "Deleting GUI timer." << endl;
+    // FIXME: Wait until last Update was finished!!!
+    delete fUpdateGui;
+
+    //fMutexGui.Lock();
+
+    // Now the files can safely be closed
+    cout << "Closing output files." << endl;
     if (fOutTp)
     {
@@ -1639,11 +1688,8 @@
     delete fOutRep;
 
-    cout << "Deleting GUI timer." << endl;
-
-    delete fUpdateGui;
+    cout << "Deleting CC communication." << endl;
     delete fCom;
 
     cout << "Deleting Nodes." << endl;
-
     fZd1->SetReport(0);
     fZd2->SetReport(0);
@@ -1660,5 +1706,5 @@
     cout << "Deleting MGCosy." << endl;
 
-    lout.DisableOutputDevice(MLog::eGui);
+    gLog.DisableOutputDevice(MLog::eGui);
 
     delete fWin;
Index: /trunk/MagicSoft/Cosy/main/MCosy.h
===================================================================
--- /trunk/MagicSoft/Cosy/main/MCosy.h	(revision 8832)
+++ /trunk/MagicSoft/Cosy/main/MCosy.h	(revision 8833)
@@ -2,8 +2,12 @@
 #define COSY_MCosy
 
-#include "coord.h"
+//#include "coord.h"
 #include "msgqueue.h"
+#include "MPointing.h"
+
+#ifndef MARS_MThread
+#define COSY_MThread
 #include "MThread.h"
-#include "MBending.h"
+#endif
 
 #ifndef COSY_Network
@@ -49,19 +53,24 @@
 class MDriveCom;
 
-class MTTalk : public MThread
+class MTTalk : public MyThreadX
 {
 private:
     MCosy *fCosy;
 
-    void *Thread();
+    Int_t Thread();
 
 public:
-    MTTalk(MCosy *cosy) : MThread(false), fCosy(cosy)
-    {
-        SetPriority(10);
-        Detach();
-        Start();
+    MTTalk(MCosy *cosy) : MyThreadX("MTTalk"), fCosy(cosy)
+    {
+        //SetPriority(10);
+        //Detach();
+        RunThread();
     }
-    ~MTTalk() { std::cout << "~MTTalk::MTTalk" << std::endl; }
+    ~MTTalk()
+    {
+        std::cout << "~MTTalk::CancelThread" << std::endl;
+        CancelThread();
+        std::cout << "~MTTalk::MTTalk" << std::endl;
+    }
 };
 
@@ -70,5 +79,5 @@
 {
     friend class MTTalk;
-    friend class MPointing;
+    friend class MSlewing;
     friend class MTracking;
 
@@ -125,5 +134,5 @@
     XY kGearTot; // describing the resolution of the system [re/U_tel]
 
-    MBending fBending;
+    MPointing fBending;
 
     UInt_t fStatus;
@@ -176,5 +185,5 @@
 
 public:
-    MCosy(/*int mode,*/ const char *dev, const int baud, MLog &out=gLog);
+    MCosy();
     ~MCosy();
 
@@ -182,5 +191,5 @@
     void Stop();
 
-    void *Proc(int msg, void *mp);
+    Int_t Proc(int msg, void *mp);
 
     Bool_t HandleTimer(TTimer *t);
