Index: trunk/MagicSoft/Cosy/main/MCaos.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MCaos.cc	(revision 9435)
+++ trunk/MagicSoft/Cosy/main/MCaos.cc	(revision 9439)
@@ -354,5 +354,5 @@
 
     //          img  width height radius sigma
-    FilterLed f(img, 768, 576, fSizeBox, fCut);
+    FilterLed f(img, 768, 576, fSizeBox, fSizeBox, fCut);
 
     Int_t first=0;
@@ -506,5 +506,5 @@
 
     if (IsEnvDefined(env, prefix, "MinNumberLeds", print))
-        fMaxRadius = GetEnvValue(env, prefix, "MinNumberLeds", fMinNumberLeds);
+        fMinNumberLeds = GetEnvValue(env, prefix, "MinNumberLeds", fMinNumberLeds);
 
     if (IsEnvDefined(env, prefix, "SizeBox", print))
Index: trunk/MagicSoft/Cosy/main/MCosy.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 9435)
+++ trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 9439)
@@ -219,5 +219,5 @@
     if (d.Zd()<fMin.Zd())
     {
-        gLog << err << "ERROR: Requested Zenith Angle below negative endswitch." << endl;
+        gLog << err << "ERROR: Requested Zenith Angle " << d.Zd()*TMath::RadToDeg() << " below negative endswitch " << fMin.Zd()*TMath::RadToDeg() << endl;
         return kFALSE;
     }
@@ -225,5 +225,5 @@
     if (d.Zd()>fMax.Zd())
     {
-        gLog << err << "ERROR: Requested Zenith Angle behind positive endswitch." << endl;
+        gLog << err << "ERROR: Requested Zenith Angle " << d.Zd()*TMath::RadToDeg() << " behind positive endswitch " << fMax.Zd()*TMath::RadToDeg() << endl;
         return kFALSE;
     }
@@ -231,5 +231,8 @@
     if (d.Az()<fMin.Az())
     {
-        gLog << err << "ERROR: Requested Azimuth Angle below negative endswitch." << endl;
+        gLog << err << "ERROR: Requested Azimuth Angle " << d.Az()*TMath::RadToDeg() << " below negative endswitch " << fMin.Az()*TMath::RadToDeg() << endl;
+        if (TMath::Abs(d.Zd())<1)
+            gLog << "       Remember that there is a small inaccessible region around zenith!" << endl;
+
         return kFALSE;
     }
@@ -237,5 +240,7 @@
     if (d.Az()>fMax.Az())
     {
-        gLog << err << "ERROR: Requested Azimuth Angle behind positive endswitch." << endl;
+        gLog << err << "ERROR: Requested Azimuth Angle " << d.Az()*TMath::RadToDeg() << " behind positive endswitch " << fMax.Az()*TMath::RadToDeg() << endl;
+        if (TMath::Abs(d.Zd())<1)
+            gLog << "       Remember that there is a small inaccessible region around zenith!" << endl;
         return kFALSE;
     }
@@ -336,6 +341,10 @@
 
     // Default: point.SetPointAcc(0.03, 0.01);
+    // Default: point.SetPointVelocity(0.3);
     point.SetPointAcc(0.03, 0.01);
     point.SetPointVelocity(0.3);
+
+    //point.SetPointAcc(0.09, 0.03);
+    //point.SetPointVelocity(1.0);
 
     return point.SetPosition(dst, track);
@@ -513,10 +522,10 @@
     case WM_STARGTPOINT:
         if (fStarguider)
-            fStarguider->StartTPoint();
+            fStarguider->StartTPoint((char*)mp);
         return 0xca1c;
 
     case WM_STARGMODE:
         if (fStarguider)
-            fStarguider->StartStarguider();
+            fStarguider->StartStarguider(*((bool*)mp));
         return 0xca1c;
 
@@ -594,8 +603,8 @@
             cout << "Preposition command to " << preps << " received." << endl;
 
-            ifstream fin("prepos.txt");
+            ifstream fin(fFilePrepos);
             if (!fin)
             {
-                cout << "ERROR: cannot open prepos.txt." << endl;
+                cout << "ERROR: cannot open " << fFilePrepos << endl;
                 return 0xebb1;
             }
@@ -817,5 +826,6 @@
     {
         tout.Start(999);
-        fCom->SendReport(fStatus, fRaDec, fZdAzSoll, bendist, fTrackingError, armed);
+        fCom->SendReport(fStatus, fRaDec, fZdAzSoll, bendist, fTrackingError, armed,
+                         fStarguider ? fStarguider->GetStarguiderMode() : 0);
     }
 
@@ -876,58 +886,4 @@
 }
 
-// --------------------------------------------------------------------------
-//
-// Disable the synchronization by using a negative CAN Id for id2.
-//
-void MCosy::Constructor(Int_t id1, Int_t id2)
-{
-    //
-    // Create Nodes
-    //
-    gLog << inf << "- Setting up network." << endl;
-
-    fMac1=new Dkc(id1, "DKC/Az");
-    fMac2=new Dkc(id2, "DKC/Zd");
-
-    fMac1->SetReport(fOutRep);
-    fMac2->SetReport(fOutRep);
-
-    gLog << inf << "- Connecting devices to network." << endl;
-
-    //
-    // Connect the devices to the network
-    //
-    SetNode(fMac1);
-    SetNode(fMac2);
-
-    //
-    // Create Gui Event timer and Gui
-    //
-    gLog << inf << "- Initializing GUI Timer." << endl;
-    fUpdateGui = new TTimer(this, 100); // 100ms
-
-    gLog << all << "- Starting GUI." << endl;
-    fWin=new MGCosy(fObservatory, this);
-}
-
-/*
-void MCosy::ConstructorDemo()
-{
-    //
-    // Create Nodes
-    //
-    gLog << "- Setting up network." << endl;
-
-    //
-    // Create Gui Event timer and Gui
-    //
-    gLog << "- Initializing GUI Timer." << endl;
-    fUpdateGui = new TTimer(this, 100); // 100ms
-
-    gLog << "- Starting GUI." << endl;
-    fWin=new MGCosy(fObservatory, this, gClient->GetRoot(), 1, 1);
-}
-*/
-
 TString MCosy::GetFileName(const char *path, const char *name, const char *ext)
 {
@@ -963,5 +919,5 @@
 }
 
-MCosy::MCosy(MEnv &env, MDriveCom *com, const char *pointing)
+MCosy::MCosy(MEnv &env, MDriveCom *com)
 : Network(), fObservatory(MObservatory::kMagic1), fStarguider(NULL),
 fMac1(0), fMac2(0), fStatus(MDriveCom::kStopped), fOutTp(0), fOutRep(0)
@@ -969,4 +925,6 @@
     const Int_t id1 = env.GetValue("Az_Id", 1);
     const Int_t id2 = env.GetValue("Zd_Id", 3);
+
+    fFilePrepos = env.GetValue("FilePredefinedPositions", "prepos.txt");
 
     TString name = GetFileName("rep", "cosy", "rep");
@@ -978,27 +936,39 @@
     *fOutRep << "[Reports]" << endl;
 
-/*
-    gLog << "- Program in ";
-    switch (mode)
-    {
-    case 0:
-    gLog << "<<Standard mode>>" << endl;*/
-        gLog << all << "Reading pointing model from " << pointing << "..." << endl;
-        if (fBending.Load(pointing))
-            gLog << all << "Reading pointing model from " << pointing << " successfull." << endl;
-        else
-            gLog << err << "ERROR - Reading pointing model from " << pointing << endl;
-        Constructor(id1, id2);/*
-        break;
-    case 1:
-        gLog << "<<SE mode>>" << endl;
-        fBending.Load("bending.txt");
-        ConstructorSE(id4, id5, id6);
-        break;
-    default:
-        gLog << "<<Demo mode>>" << endl;
-        ConstructorDemo();
-    }
-*/
+    const TString pointing = env.GetValue("PointingModel", "bending.txt");
+
+    gLog << all << "Reading pointing model from " << pointing << "..." << endl;
+    if (fBending.Load(pointing))
+        gLog << all << "Reading pointing model from " << pointing << " successfull." << endl;
+    else
+        gLog << err << "ERROR - Reading pointing model from " << pointing << endl;
+
+    //
+    // Create Nodes
+    //
+    gLog << inf << "- Setting up network." << endl;
+
+    fMac1=new Dkc(id1, "DKC/Az");
+    fMac2=new Dkc(id2, "DKC/Zd");
+
+    fMac1->SetReport(fOutRep);
+    fMac2->SetReport(fOutRep);
+
+    gLog << inf << "- Connecting devices to network." << endl;
+
+    //
+    // Connect the devices to the network
+    //
+    SetNode(fMac1);
+    SetNode(fMac2);
+
+    //
+    // Create Gui Event timer and Gui
+    //
+    gLog << inf << "- Initializing GUI Timer." << endl;
+    fUpdateGui = new TTimer(this, 100); // 100ms
+
+    gLog << all << "- Starting GUI." << endl;
+    fWin=new MGCosy(fObservatory, fFilePrepos, this);
 
     gLog.SetOutputGui(fWin->GetLog(), kTRUE);
Index: trunk/MagicSoft/Cosy/main/MCosy.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MCosy.h	(revision 9435)
+++ trunk/MagicSoft/Cosy/main/MCosy.h	(revision 9439)
@@ -91,4 +91,6 @@
     MLog     *fOutRep;
 
+    TString fFilePrepos;
+
     ZdAz AlignTrackingPos(ZdAz pointing) const;
     Bool_t CheckRange(const ZdAz &d) const;
@@ -122,5 +124,5 @@
 
 public:
-    MCosy(MEnv &env, MDriveCom *com, const char *pointing);
+    MCosy(MEnv &env, MDriveCom *com);
     ~MCosy();
 
Index: trunk/MagicSoft/Cosy/main/MStarguider.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 9435)
+++ trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 9439)
@@ -1,3 +1,2 @@
-#undef EXPERT
 #undef EXPERT
 
@@ -307,6 +306,4 @@
 
     fGStarg = new MGStarg(this, 235);
-    fGStarg->DrawText("0.75'", "1.50'", "3.00'", "Mispointing [min]");
-    fGStarg->DrawHexagon();
     fGStarg->Move(530,596+5);
     fList->Add(fGStarg);
@@ -342,12 +339,14 @@
 
     fTPoint = new TGTextButton(this, "TPoint");
-    fTPoint->Move(4, fMenu->GetDefaultHeight()+785);
+    //fTPoint->Move(4, fMenu->GetDefaultHeight()+785);
+    fTPoint->Move(170, fMenu->GetDefaultHeight()+785);
     fTPoint->AllowStayDown(kTRUE);
     AddFrame(fTPoint);
-
-    fStargTPoint = new TGTextButton(this, "StargTPoint");
-    fStargTPoint->Move(170, fMenu->GetDefaultHeight()+785);
-    fStargTPoint->AllowStayDown(kTRUE);
-    AddFrame(fStargTPoint);
+    /*
+     fStargTPoint = new TGTextButton(this, "StargTPoint");
+     fStargTPoint->Move(170, fMenu->GetDefaultHeight()+785);
+     fStargTPoint->AllowStayDown(kTRUE);
+     AddFrame(fStargTPoint);
+    */
 
     fFps = new TGLabel(this, "---fps");
@@ -448,5 +447,5 @@
     MapSubwindows();
     fTPoint->UnmapWindow();
-    fStargTPoint->UnmapWindow();
+    //fStargTPoint->UnmapWindow();
     fGStarg->UnmapWindow();
     fGNumStars->UnmapWindow();
@@ -479,10 +478,11 @@
       fOutTp(0),
       fOutStargTp(0),
-      fOutRq(0),
+//      fOutRq(0),
       fDx((768-kZOOM)/2),
       fDy((512-kZOOM)/2),
       fStatus(MDriveCom::kStandby),
       fRadius(200),
-      fTPointFromCC(0)
+      fTPointFromCC(-1),
+      fFindStarCut(3.0), fFindStarBox(70)
 {
     gLog << warn << " #### FIXME: Make MCaos Thread safe!" << endl;
@@ -557,4 +557,15 @@
 
     fRadius = env.GetValue("Leds.Radius", fRadius);
+
+    fStarguiderW = env.GetValue("Starguider.Width",  fStarguiderW);
+    fStarguiderH = env.GetValue("Starguider.Height", fStarguiderH);
+    fStarguiderX = env.GetValue("Starguider.X",      fStarguiderX);
+    fStarguiderY = env.GetValue("Starguider.Y",      fStarguiderY);
+
+    fSkyOffsetX = env.GetValue("Starguider.SkyOffsetX", fSkyOffsetX);
+    fSkyOffsetY = env.GetValue("Starguider.SkyOffsetY", fSkyOffsetY);
+
+    fFindStarBox = env.GetValue("FindStar.SizeBox",       fFindStarBox);
+    fFindStarCut = env.GetValue("FindStar.CleaningLevel", fFindStarCut);
 }
 
@@ -583,6 +594,6 @@
         delete fOutStargTp;
 
-    if (fOutRq)
-        delete fOutRq;
+//    if (fOutRq)
+//        delete fOutRq;
 
     gLog << inf2 << "Camera Display destroyed." << endl;
@@ -649,8 +660,9 @@
 }
 
+/*
 void MStarguider::ToggleFindStar()
 {
     if (fDisplay->IsEntryChecked(IDM_kFindStar) && fCosy)
-        fTPoint->MapWindow();
+    fTPoint->MapWindow();
     else
     {
@@ -659,5 +671,5 @@
     }
 }
-
+*/
 void MStarguider::ToggleStarguider()
 {
@@ -671,5 +683,5 @@
         fOperations->EnableEntry(IDM_kStargAnalysis);
 
-        fStargTPoint->MapWindow();
+        //fStargTPoint->MapWindow();
 
         fPZdAz->MapWindow();
@@ -703,5 +715,4 @@
         SwitchOff(fChannel, IDM_kChannel2);
         fChannel->CheckEntry(IDM_kChannel1);
-
     }
     else
@@ -709,6 +720,6 @@
         fStatus = MDriveCom::kStandby;
 
-        fStargTPoint->UnmapWindow();
-        fStargTPoint->SetDown(kFALSE);
+        //fStargTPoint->UnmapWindow();
+        //fStargTPoint->SetDown(kFALSE);
 
         fPZdAz->UnmapWindow();
@@ -772,5 +783,5 @@
     MapSubwindows(); // maps everything, but we don't want that
     fTPoint->UnmapWindow();
-    fStargTPoint->UnmapWindow();
+    //fStargTPoint->UnmapWindow();
     fGStarg->UnmapWindow();
     fGNumStars->UnmapWindow();
@@ -863,5 +874,4 @@
                     //tpoint
                     SwitchOff(fDisplay, IDM_kFindStar);
-                    ToggleFindStar();
                     SwitchOff(fDisplay, IDM_kCaosFilter);
                     ToggleCaosFilter();
@@ -874,8 +884,10 @@
 
                     //check needed items
-                    fDisplay->CheckEntry(IDM_kStretch);
+                    fDisplay->UnCheckEntry(IDM_kStretch);
                     fDisplay->CheckEntry(IDM_kStargCaosFilter);
                     fDisplay->CheckEntry(IDM_kStarguider);
                     ToggleStarguider();
+
+                    fTPoint->MapWindow();
                 }
                 else
@@ -885,4 +897,5 @@
                     fDisplay->UnCheckEntry(IDM_kStargCaosFilter);
                     fDisplay->UnCheckEntry(IDM_kStarguider);
+                    fTPoint->UnmapWindow();
                     ToggleStarguider();
 
@@ -922,9 +935,9 @@
 
                     //checking needed items
-                    fDisplay->CheckEntry(IDM_kStretch);
+                    fDisplay->UnCheckEntry(IDM_kStretch);
                     fDisplay->CheckEntry(IDM_kCaosFilter);
                     ToggleCaosFilter();
                     fDisplay->CheckEntry(IDM_kFindStar);
-                    ToggleFindStar();
+                    fTPoint->MapWindow();
                 }
                 else
@@ -946,5 +959,5 @@
                     ToggleCaosFilter();
                     fDisplay->UnCheckEntry(IDM_kFindStar);
-                    ToggleFindStar();
+                    fTPoint->UnmapWindow();
                 }
                 return kTRUE;
@@ -956,5 +969,5 @@
             case IDM_kFindStar:
                 Toggle(fDisplay, IDM_kFindStar);
-                ToggleFindStar();
+                //ToggleFindStar();
                 return kTRUE;
 
@@ -1293,4 +1306,6 @@
 #endif
 
+    return fSao->CalcDeltaZdAzFromPix(mx, my)*TMath::RadToDeg();
+/*
     AltAz pos0 = fSao->CalcAltAzFromPix(768/2,    576/2)*kRad2Deg;
     AltAz pos1 = fSao->CalcAltAzFromPix(768/2+mx, 576/2+my)*kRad2Deg;
@@ -1298,15 +1313,24 @@
     pos1 -= pos0;
 
-    return ZdAz(-pos1.Alt(), pos1.Az());
-}
-
-Int_t MStarguider::CalcTrackingError(Leds &leds, MStarList &stars,
-                                     ZdAz &d, MTime &t, double &bright, Int_t &num)
-{
+    ZdAz res2 = ZdAz(-pos1.Alt(), pos1.Az());
+
+    return res1;
+*/
+}
+
+Int_t MStarguider::CalcTrackingError(const Leds &leds, MStarList &stars,
+                                     ZdAz &d, const MTime &t, const double &bright,
+                                     Int_t &num, const Ring &center, Int_t numleds, Int_t numrings)
+{
+    // Get tracking coordinates
+    const XY xy = fCRaDec->GetCoordinates();
+
     num = leds.GetEntries();
     if (num < 3) //was 1
     {
         gLog << warn << "Sorry, less than 3 detected spot in FOV!" << endl;
-        fStargTPoint->SetDown(kFALSE);
+        if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
+            fCosy->GetDriveCom()->SendTPoint(false, 'S', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings, num);    // Report
+        fTPoint->SetDown(kFALSE);
         return 0;
     }
@@ -1315,5 +1339,7 @@
     {
         gLog << warn << "Sorry, less than 3 stars in FOV!" << endl;
-        fStargTPoint->SetDown(kFALSE);
+        if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
+            fCosy->GetDriveCom()->SendTPoint(false, 'S', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings, num);    // Report
+        fTPoint->SetDown(kFALSE);
         return 0;
     }
@@ -1375,5 +1401,7 @@
     if (numcor<1)
     {
-        fStargTPoint->SetDown(kFALSE);
+        fTPoint->SetDown(kFALSE);
+        if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
+            fCosy->GetDriveCom()->SendTPoint(false, 'S', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings, num, numcor);    // Report
         return 0;
     }
@@ -1388,7 +1416,7 @@
 
     // Check StargTPoint data set request
-    if (!fStargTPoint->IsDown())
+    if (!fDisplay->IsEntryChecked(IDM_kStarguiderMode) || !fTPoint->IsDown())
         return numcor;
-    fStargTPoint->SetDown(kFALSE);
+    fTPoint->SetDown(kFALSE);
 
     // If no file open: open new file
@@ -1406,6 +1434,4 @@
     }
 
-    // Get tracking coordinates
-    const XY    xy = fCRaDec->GetCoordinates();
     const RaDec rd(xy.X()*MAstro::HorToRad(), xy.Y()*TMath::DegToRad());
 
@@ -1430,9 +1456,7 @@
 
     // Write real pointing position
-    //cout << "     Alt/Az: " << za0.Alt() << "° " << za0.Az() << "°" << endl;
     *fOutStargTp << setprecision(7) << za0.Az() << " " << za0.Alt() << " ";
 
     // Write system pointing position
-    //cout << "     SE-Pos: " << 90-cpos.Zd() << "° " << cpos.Az() << "°" << endl;
     *fOutStargTp << fmod(cpos.Az()+360, 360) << " " << 90-cpos.Zd();
 
@@ -1440,15 +1464,41 @@
     *fOutStargTp << " " << d.Zd() << " " << d.Az();
     *fOutStargTp << " " << setprecision(11) << t.GetMjd();
-    *fOutStargTp << " " << num;
-    *fOutStargTp << " " << bright;
+    *fOutStargTp << " " << setprecision(4) << center.GetMag();
+    *fOutStargTp << " " << star->GetMag();
+    *fOutStargTp << " " << center.GetX() << " " << center.GetY();
+    *fOutStargTp << " 0 0";
+    *fOutStargTp << " " << numleds << " " << numrings;
+    *fOutStargTp << " " << num << " " << numcor << " " << bright;
     *fOutStargTp << endl;
 
+    gLog << all << "Starguider TPoint successfully taken." << endl;
+
     fTimeFromTp.Clear();
 
+    if (!fCosy)
+        return numcor;
+
+    MDriveCom *com = fCosy->GetDriveCom();
+    if (!com)
+        return numcor;
+
+    com->SendTPoint(true, 'S', fTPointStarMag, fTPointStarName, za0, cpos, xy, d.Zd(), d.Az(), t, center, Led(), numleds, numrings, num, numcor);    // Report
+
     return numcor;
 }
 
-void MStarguider::FindStar(FilterLed &f, FilterLed &f2, Ring &center, MTime &t)
-{
+void MStarguider::FindStar(const FilterLed &f, const FilterLed &f2, const Ring &center, const MTime &t, Int_t numleds, Int_t numrings)
+{
+    // Get tracking coordinates
+    const XY xy = fCRaDec->GetCoordinates();  // [h, deg]
+
+    if (center.GetX()<=0 && center.GetY()<=0)
+    {
+        gLog << warn << "Couldn't determine center of the camera." << endl;
+        if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
+            fCosy->GetDriveCom()->SendTPoint(false, 'T', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings);    // Report
+        return;
+    }
+
     // Try to find the star
     Leds leds;
@@ -1459,9 +1509,7 @@
     if (!star)
     {
-        if (fTPoint->IsDown())
-        {
-            fTPoint->SetDown(kFALSE);
-            gLog << warn << "No star found. Couldn't take a tpoint." << endl;
-        }
+        gLog << warn << "No star found." << endl;
+        if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
+            fCosy->GetDriveCom()->SendTPoint(false, 'T', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings);    // Report
         return;
     }
@@ -1470,4 +1518,6 @@
     star->Print();
     f2.MarkPoint(star->GetX(), star->GetY(), 2<<2);
+
+    const RaDec rd(xy.X()*MAstro::HorToRad(), xy.Y()*TMath::DegToRad());
 
     // Initialize Star Catalog on the camera plane
@@ -1478,9 +1528,4 @@
     ac.SetObservatory(*fSao);
     ac.SetTime(t);
-
-    // Get tracking coordinates
-    const XY xy = fCRaDec->GetCoordinates();  // [h, deg]
-    const RaDec rd(xy.X()*MAstro::HorToRad(), xy.Y()*TMath::DegToRad());
-
     ac.SetRaDec(rd.Ra(), rd.Dec());
 
@@ -1500,8 +1545,6 @@
 
     // Check TPoint data set request
-    if (!fTPoint->IsDown())
+    if (!fDisplay->IsEntryChecked(IDM_kTpointMode) || !fTPoint->IsDown())
         return;
-
-    fTPoint->SetDown(kFALSE);
 
     // If no file open: open new file
@@ -1539,4 +1582,5 @@
     const ZdAz za1 = fCosy->GetSePos()*360; // [deg]
 
+
     // Write real pointing position
     //cout << "     Alt/Az: " << za0.Alt() << "° " << za0.Az() << "°" << endl;
@@ -1552,5 +1596,11 @@
     *fOutTp << " " << setprecision(4) << center.GetMag();
     *fOutTp << " " << star->GetMag();
+    *fOutTp << " " << center.GetX() << " " << center.GetY();
+    *fOutTp << " " << star->GetX() << " " << star->GetY();
+    *fOutTp << " " << numleds << " " << numrings;
+    *fOutTp << " 0 0 0";
     *fOutTp << endl;
+
+    gLog << all << "TPoint successfully taken." << endl;
 /*
     MLog &outrep = *fCosy->GetOutRep();
@@ -1570,4 +1620,13 @@
     
 //    return zdaz;
+
+    if (!fCosy)
+        return;
+
+    MDriveCom *com = fCosy->GetDriveCom();
+    if (!com)
+        return;
+
+    com->SendTPoint(true, 'T', fTPointStarMag, fTPointStarName, za0, za1, xy, -dzd, -daz, t, center, *star, numleds, numrings);    // Report
 }
 
@@ -1624,14 +1683,56 @@
 }
 
-void MStarguider::StartStarguider()
+void MStarguider::StartTPoint(char *cmd)
+{
+    fTPointStarName = "Dummy";
+    fTPointStarMag  = 0;
+
+    TString str(cmd);
+
+    TObjArray *arr = str.Tokenize(' ');
+
+    if (arr->GetEntries()==2)
+    {
+        fTPointStarName = (*arr)[0]->GetName();
+        fTPointStarMag  = atof((*arr)[1]->GetName());
+    }
+
+    delete arr;
+
+    if (fTPointFromCC<0)
+        fTPointFromCC=0;
+}
+
+void MStarguider::StartStarguider(bool on)
 {
     // Switch to starguider mode
-    cout << " * Switching to Starguider mode" << endl;
-    fMode->UnCheckEntry(IDM_kStarguiderMode);
-    ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStarguiderMode, 0);
+    if (on)
+    {
+        cout << " * Switching Starguider mode on" << endl;
+        fMode->UnCheckEntry(IDM_kStarguiderMode);
+        ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStarguiderMode, 0);
+    }
+    else
+    {
+        cout << " * Switching Starguider mode off" << endl;
+        fMode->CheckEntry(IDM_kStarguiderMode);
+        ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStarguiderMode, 0);
+    }
+}
+
+Int_t MStarguider::GetStarguiderMode() const
+{
+    return fMode->IsEntryChecked(IDM_kStarguiderMode) ? 1 : 2;
+
 }
 
 Bool_t MStarguider::DoTPoint()
 {
+    if (fTPoint->IsDown() && fTPointFromCC<0)
+    {
+        fTPointFromCC = 0;
+        fTPoint->SetDown(kFALSE);
+    }
+
     if (fTPointFromCC<0)
         return kTRUE;
@@ -1650,14 +1751,15 @@
 
     case 2:
+    case 3:
         cout << " * Waiting one frame" << endl;
         // Wait one frame
         return kFALSE;
 
-    case 3:
+    case 4:
         cout << " * Taking TPoint" << endl;
         fTPoint->SetDown(); // kTRUE
         return kTRUE;
 
-    case 4:
+    case 5:
         if (!fTimeFromTp) // TPoint failed
             break;
@@ -1669,16 +1771,24 @@
         return kFALSE;
 
-    case 5:
+    case 6:
+    case 7:
         cout << " * Waiting one frame" << endl;
         // Wait one frame
         return kFALSE;
 
-    case 6:
+    case 8:
         cout << " * Taking Starguider TPoint" << endl;
-        fStargTPoint->SetDown(); // kTRUE
+        fTPoint->SetDown(); // kTRUE
         return kTRUE;
 
-    case 7:
-        cout << " * Send Report" << endl;
+//    case 9:
+//        cout << " * Send Report" << endl;
+//        return kTRUE;
+
+    case 9:
+        cout << " * Switching to TPoint mode" << endl;
+        // Switch to tpoint mode
+        fMode->UnCheckEntry(IDM_kTpointMode);
+        ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kTpointMode, 0);
         break;
     }
@@ -1686,5 +1796,5 @@
     // Send report
     fTPointFromCC = -1;
-
+/*
     if (!fCosy)
         return kTRUE;
@@ -1702,5 +1812,5 @@
     //fNumStarsCorrelated = rc;
     com->SendTPoint(fNumStarsCorrelated>0);
-
+*/
     return kTRUE;
 }
@@ -1736,5 +1846,5 @@
     if (!fWritePictures->IsEntryEnabled(IDM_kStart) &&
         (!(n%fWrtRate) || fWriteType->IsEntryChecked(IDM_kOnce)))
-    {     
+    {
 
         if (fFileType->IsEntryChecked(IDM_kPNG))
@@ -1752,5 +1862,6 @@
         f.Execute();
 
-    DoTPoint();
+    if (!DoTPoint())
+        return;
 
     Int_t numleds = 0;
@@ -1758,5 +1869,6 @@
 
     // Find Center of Camera for Caos and Tpoints
-    Ring center(768/2, 576/2);
+//    Ring center;//(-1, -1);
+    Ring center(5, 5);//(-1, -1);
     if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
     {
@@ -1774,12 +1886,11 @@
 
     // Find Star at Center---for Tpoint Procedure
-    if (fDisplay->IsEntryChecked(IDM_kFindStar) &&
-        center.GetX()>0 && center.GetY()>0)
+    if (fDisplay->IsEntryChecked(IDM_kFindStar))
     {
         // Set search Paremeters (FIXME: Get them from user input!)
-        f.SetCut(3.0);
-        f.SetBox(70);
-
-        FindStar(f, f2, center, t);
+        f.SetCut(fFindStarCut);
+        f.SetBox(fFindStarBox);
+
+        FindStar(f, f2, center, t, numleds, numrings);
     }
 
@@ -1788,5 +1899,5 @@
 
     // Position corresponding to the camera center (53.2, 293.6)
-    Ring sgcenter(53.2, 293.6); // Center of camera in SG picture [px]
+    Ring sgcenter;//(-1, -1); // Center of camera in SG picture [px]
 
     // Find Center of Camera in Starfield Camera picture 
@@ -1817,11 +1928,8 @@
   	fCZdAz->SetCoordinates(fSao->GetZdAz());
 
-    	fSao->SetBox(230); // Region of interest around center
+    	fSao->SetBox(fStarguiderW, fStarguiderH); // 280 Region of interest around center
 
         // If center of camera cannot be determined sgcenter is (0,0)
         const Bool_t centerisvalid = sgcenter.GetX()>0 && sgcenter.GetY()>0;
-//        if (centerisvalid)
-//            skycenter.SetXY(sgcenter.GetX() -  53.2,
-//                            sgcenter.GetY() - 293.6);
 
 	// We determine the ideal starfield using camera sagging info
@@ -1830,15 +1938,19 @@
         // 53.2 and 293.6 are the "preliminary" camera center
         // -9 and 28.5 are the offsets of the pointing position in the sky
-        const XY off(sgcenter.GetX()- 53.2-9,
-                     sgcenter.GetY()-293.6+28.5);
-
-    	// we obtain stars in the effective star FOV and draw them.
-    	// coordinates are video frame coords.
-  	MStarList stars;
-        fSao->CalcStars(stars, 530, 292, TMath::FloorNint(off.X()), TMath::FloorNint(off.Y()));
-	fSao->DrawStars(stars, cimg);
-
-        // Position around which the circles are drawn.
-        const Ring skycenter(768/2-off.X(), 576/2+off.Y());
+        //const XY off(sgcenter.GetX()- 53.2-9,
+        //             sgcenter.GetY()-293.6+28.5);
+
+        // Skycenter
+        const XY off(sgcenter.GetX() + fSkyOffsetX,
+                     sgcenter.GetY() + fSkyOffsetY);
+
+        MStarList stars;
+        if (centerisvalid)
+        {
+            // we obtain stars in the effective star FOV and draw them.
+            // coordinates are video frame coords.
+            fSao->CalcStars(stars, fStarguiderX, fStarguiderY, off.X(), off.Y());
+            fSao->DrawStars(stars, cimg);
+        }
 
 	// There are two corrections to the mispointing
@@ -1853,9 +1965,9 @@
 
   	    Leds spots;
-  	    f.SetBox(230);
+            f.SetBox(fStarguiderW, fStarguiderH); // 280
 	    f.SetCut(cut);
 
             double bright;
-  	    f.ExecuteAndMark(spots, 530, 292, bright);
+ 	    f.ExecuteAndMark(spots, fStarguiderX, fStarguiderY, bright);
 
             ULong_t color;
@@ -1878,5 +1990,5 @@
 
             Int_t numstars = 0;
-            const Int_t rc = CalcTrackingError(spots, stars, fD, t, bright, numstars);
+            const Int_t rc = CalcTrackingError(spots, stars, fD, t, bright, numstars, sgcenter, numleds, numrings);
 
             const Bool_t monitoring = brightnessisvalid && centerisvalid && fNumStarsCorrelated>2;
@@ -1907,4 +2019,7 @@
         if (centerisvalid && fNumStarsCorrelated>2)
         {
+            // Position around which the circles are drawn.
+            const Ring skycenter(off.X(), off.Y());
+
             f2.DrawCircle(skycenter,   2.0,     0x0a);
 
@@ -1917,4 +2032,7 @@
 
     } //CalcStars
+
+    if (fTPoint->IsDown())
+        fTPoint->SetDown(kFALSE);
 
     // Draw Circles around center of Camera
@@ -1948,5 +2066,5 @@
 void MStarguider::UpdatePosZoom()
 {
-    MString txt;
+    MString txt;/*
     if (fDisplay->IsEntryChecked(IDM_kCatalog))
     {
@@ -1958,5 +2076,5 @@
         txt.Form("(%d, %d) %.1fd/%.1fd", fDx, fDy, -aa.Alt(), aa.Az()-180);
     }
-    else
+    else*/
         txt.Form("(%d, %d)", fDx, fDy);
     fPosZoom->SetText(txt);
Index: trunk/MagicSoft/Cosy/main/MStarguider.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MStarguider.h	(revision 9435)
+++ trunk/MagicSoft/Cosy/main/MStarguider.h	(revision 9439)
@@ -98,5 +98,5 @@
 
     TGButton      *fTPoint;
-    TGButton      *fStargTPoint;
+//    TGButton      *fStargTPoint;
 
     ZdAz          fPos;
@@ -121,5 +121,5 @@
     ofstream *fOutTp;
     ofstream *fOutStargTp;
-    ofstream *fOutRq;
+//    ofstream *fOutRq;
 
     Int_t fDx;
@@ -135,16 +135,30 @@
 
     Int_t fTPointFromCC;
+
+    Int_t fStarguiderW;
+    Int_t fStarguiderH;
+    Int_t fStarguiderX;
+    Int_t fStarguiderY;
+
+    Float_t fSkyOffsetX;  // Offset between camera center and sky position
+    Float_t fSkyOffsetY;  // Offset between camera center and sky position
+
+    Float_t fFindStarCut;
+    Int_t   fFindStarBox;
+
+    TString fTPointStarName;
+    Float_t fTPointStarMag;
 
     void Toggle(TGPopupMenu *p, UInt_t id);
     void SwitchOff(TGPopupMenu *p, UInt_t id);
     void ToggleStargAnalysis();
-    void ToggleFindStar();
+//    void ToggleFindStar();
     void ToggleStarguider();
     void ToggleCaosFilter();
     void SetChannel();
-    Int_t CalcTrackingError(Leds &, MStarList &, ZdAz &, MTime &, double &bright, Int_t &num);
+    Int_t CalcTrackingError(const Leds &, MStarList &, ZdAz &, const MTime &, const double &bright, Int_t &num, const Ring &center, Int_t numleds, Int_t numrings);
     ZdAz TrackingError(TArrayF &alt, TArrayF &az, TArrayF &mag, Int_t &num) const;
     bool Interpolate(const unsigned long n, byte *img) const;
-    void FindStar(FilterLed &f, FilterLed &f2, Ring &center, MTime &t);
+    void FindStar(const FilterLed &f, const FilterLed &f2, const Ring &center, const MTime &t, Int_t numleds, Int_t numrings);
 
     void InitGui(Int_t channel);
@@ -188,6 +202,8 @@
     void Print(Option_t *o) const { }
 
-    void StartTPoint() { if (fTPointFromCC<0) fTPointFromCC=0; }
-    void StartStarguider();
+    void StartTPoint(char *cmd=0);// { if (fTPointFromCC<0) fTPointFromCC=0; }
+    void StartStarguider(bool on);
+
+    Int_t GetStarguiderMode() const;
 
     ClassDef(MStarguider, 0)
Index: trunk/MagicSoft/Cosy/main/MTracking.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MTracking.cc	(revision 9435)
+++ trunk/MagicSoft/Cosy/main/MTracking.cc	(revision 9439)
@@ -33,12 +33,12 @@
     // Send the new velocities for both axes.
     //
-    fCosy->fMac2->SendSDO(0x3006, 1, (LWORD_t)(v.Zd()*vrzd));  // SetRpmVelocity [re/min]
-    fCosy->fMac1->SendSDO(0x3006, 1, (LWORD_t)(v.Az()*vraz));  // SetRpmVelocity [re/min]
+    fCosy->fMac2->SendSDO(0x3007, (LWORD_t)(v.Zd()*vrzd));  // SetRpmVelocity [re/min]
+    fCosy->fMac1->SendSDO(0x3007, (LWORD_t)(v.Az()*vraz));  // SetRpmVelocity [re/min]
 
     //
     // Wait for the objects to be acknoledged.
     //
-    fCosy->fMac2->WaitForSdo(0x3006, 1, 100);
-    fCosy->fMac1->WaitForSdo(0x3006, 1, 100);
+    fCosy->fMac2->WaitForSdo(0x3007, 0, 100);
+    fCosy->fMac1->WaitForSdo(0x3007, 0, 100);
 
     //
@@ -272,5 +272,12 @@
     {
         /*
-        sla.Now(1);
+         // Code for position control mode
+         // Set: S-0-0001
+         // Set: S-0-0002
+         // Set: P-0-0099
+         // Set: P-0-0187 (Spline)
+         // Replace S-0-0258 by S-0-0047 in communication
+
+        sla.Now();
         const ZdAz pointing = sla.CalcZdAz(fCosy->fRaDec); // [rad]
         ZdAz dest = fCosy->AlignTrackingPos(pointing);     // fix ambiguity
@@ -279,7 +286,7 @@
             break;
         dest *= 1./TMath::TwoPi(); //[rev]
-        fCosy->fMac2->SendSDO(0x6004, 0, (LWORD_t)(dest.Zd()*fCosy->fMac2->GetPosRes()+.5), false);
-        fCosy->fMac1->SendSDO(0x6004, 0, (LWORD_t)(dest.Az()*fCosy->fMac1->GetPosRes()+.5), false);
-        usleep(100000); // 1s
+        fCosy->fMac2->SendSDO(0x6004, (LWORD_t)(dest.Zd()*fCosy->fMac2->GetPosRes()+.5), false);
+        fCosy->fMac1->SendSDO(0x6004, (LWORD_t)(dest.Az()*fCosy->fMac1->GetPosRes()+.5), false);
+        usleep(10000); // 10ms
         continue;
         */
