Index: /trunk/MagicSoft/Cosy/Changelog
===================================================================
--- /trunk/MagicSoft/Cosy/Changelog	(revision 9438)
+++ /trunk/MagicSoft/Cosy/Changelog	(revision 9439)
@@ -104,4 +104,8 @@
    * videodev/FilterLed.[h,cc]:
      - allow the search boxs to be asymmetric
+
+   * bending_magic2.txt, leds_magic2.txt, stargleds_magic2.txt,
+     prepos_magic2.txt:
+     - added
 
 
Index: /trunk/MagicSoft/Cosy/bending_magic2.txt
===================================================================
--- /trunk/MagicSoft/Cosy/bending_magic2.txt	(revision 9439)
+++ /trunk/MagicSoft/Cosy/bending_magic2.txt	(revision 9439)
@@ -0,0 +1,26 @@
+MAGIC1 12.04.2009 17:20:57.365
+S   00   000000   000000  0000000
+     IA  -0.028889482    0.12099407
+     IE   -0.10075322     0.1777883
+   FLOP             0             0
+     AN             0             0
+     AW             0             0
+   NPAE   0.010193065    0.13877966
+     CA  -0.028210358     0.1726047
+     TF             0             0
+     TX             0             0
+   ECES  -0.047483152    0.14349464
+   ACES             0             0
+   ECEC  -0.052801057    0.12315945
+   ACEC             0             0
+    NRX             0             0
+    NRY             0             0
+    CRX  0.0075521444   0.010958031
+    CRY  0.0041852303   0.012130116
+ MAGIC1   -0.01576816   0.012376892
+ MAGIC2             0             0
+     PX             0             0
+     PY             0             0
+     DX             0             0
+     DY             0             0
+END
Index: /trunk/MagicSoft/Cosy/candrv/CandrvLinkDef.h
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/CandrvLinkDef.h	(revision 9438)
+++ /trunk/MagicSoft/Cosy/candrv/CandrvLinkDef.h	(revision 9439)
@@ -5,9 +5,6 @@
 #pragma link off all functions;
 
-#pragma link C++ class VmodIcan+;
 #pragma link C++ class CanOpen+;
 #pragma link C++ class Network+;
-#pragma link C++ class Ethernet+;
-//#pragma link C++ class Interface+;
 
 #pragma link C++ class NodeDrv+;
Index: /trunk/MagicSoft/Cosy/candrv/Makefile
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/Makefile	(revision 9438)
+++ /trunk/MagicSoft/Cosy/candrv/Makefile	(revision 9439)
@@ -17,5 +17,4 @@
 
 SRCFILES = nodedrv.cc \
- 	   vmodican.cc \
 	   sdolist.cc \
 	   canopen.cc \
Index: /trunk/MagicSoft/Cosy/candrv/canopen.h
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/canopen.h	(revision 9438)
+++ /trunk/MagicSoft/Cosy/candrv/canopen.h	(revision 9439)
@@ -4,4 +4,17 @@
 #ifndef ROOT_TCondition
 #include <TCondition.h>
+#endif
+
+#ifdef __CINT__
+typedef Byte_t   BYTE_t;
+typedef UShort_t WORD_t;
+typedef Short_t  WORDS_t;
+typedef UInt_t   LWORD_t;
+typedef Int_t    LWORDS_t;
+struct Message;
+struct FastMessage;
+#else
+#include "gendef.h"
+#include "dpm.h"
 #endif
 
@@ -12,9 +25,4 @@
 #ifndef COSY_MTimeout
 #include "MTimeout.h"
-#endif
-
-#ifdef __CINT__
-typedef UInt_t LWORD_t;
-typedef Short_t WORDS_t;
 #endif
 
Index: /trunk/MagicSoft/Cosy/candrv/ethernet.cc
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/ethernet.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/candrv/ethernet.cc	(revision 9439)
@@ -37,5 +37,5 @@
 #include <TSocket.h>
 
-ClassImp(Ethernet);
+//ClassImp(Ethernet);
 
 #undef DEBUG
@@ -56,5 +56,5 @@
 //
 Ethernet::Ethernet(const char *addr, const int tx, const int rx, CanOpen *receiver)
-    : MTcpIpI(rx),/* MTcpIpO(addr, tx),*/ Interface(receiver), fTxAddress(addr), fTxPort(tx)
+    : MTcpIpI(rx), /*MTcpIpO(addr, tx),*/ Interface(receiver), fTxAddress(addr), fTxPort(tx)
 {
     gLog << inf2 << "- Ethernet initialized." << endl;
@@ -68,5 +68,5 @@
 Ethernet::~Ethernet()
 {
-    CancelThread();
+    MTcpIpI::CancelThread();
     gLog << inf2 << "- Ethernet stopped." << endl;
 }
@@ -85,5 +85,5 @@
     const TString address = MTcpIpO::GetSocketAddress(rx);
 
-    while (!IsThreadCanceled())
+    while (!MTcpIpI::IsThreadCanceled())
     {
         unsigned char c;
@@ -281,4 +281,5 @@
 #endif
     MTcpIpO::SendFrame(fTxAddress, fTxPort, (char*)(msg.data+1), msg.len-1);
+//    Send((char*)(msg.data+1), msg.len-1);
 #ifdef DEBUG
     st.Print();
Index: /trunk/MagicSoft/Cosy/candrv/ethernet.h
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/ethernet.h	(revision 9438)
+++ /trunk/MagicSoft/Cosy/candrv/ethernet.h	(revision 9439)
@@ -20,8 +20,8 @@
 
     // Start/stop communication inherited from Interface
-    void Start() { RunThread(); }
-    void Stop()  { CancelThread(); }
+    void Start() { MTcpIpI::RunThread(); }
+    void Stop()  { MTcpIpI::CancelThread(); }
 
-    Bool_t HasConnection() const { return IsConnectionEstablished(); }
+    Bool_t HasConnection() const { return MTcpIpI::IsConnectionEstablished(); }
 
 public:
@@ -32,5 +32,5 @@
     void SendCanFrame(WORD_t cobid, BYTE_t m[8], BYTE_t rtr=0);
 
-    ClassDef(Ethernet, 0) // hardware interface to the vmodican can module (Janz)
+//    ClassDef(Ethernet, 0) // hardware interface to the vmodican can module (Janz)
 };
 
Index: /trunk/MagicSoft/Cosy/catalog/SlaPlanets.cc
===================================================================
--- /trunk/MagicSoft/Cosy/catalog/SlaPlanets.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/catalog/SlaPlanets.cc	(revision 9439)
@@ -40,5 +40,5 @@
     double ra, dec, diam;
 
-    // One can use TT instead of TDB for all plenets
+    // One can use TT instead of TDB for all planets
     // (except the moon)
     // TDB, planet, elong, phi, *ra, *dec, *diam
Index: /trunk/MagicSoft/Cosy/catalog/SlaStars.cc
===================================================================
--- /trunk/MagicSoft/Cosy/catalog/SlaStars.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/catalog/SlaStars.cc	(revision 9439)
@@ -49,5 +49,5 @@
              0, 0,                         // polar motion x, y-coordinate (radians)
              // 273.155, 1013.25, 0.5,     // temp, pressure, humidity
-             273.155+20, 1013.25, 0.5,     // temp, pressure, humidity
+             273.155+10, 780.0, 0.25,     // temp, pressure, humidity
              // 0.2, 0.0065,               // wavelength, tropo lapse rate
              0.55, 0.0065,                 // wavelength, tropo lapse rate
Index: /trunk/MagicSoft/Cosy/catalog/Slalib.cc
===================================================================
--- /trunk/MagicSoft/Cosy/catalog/Slalib.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/catalog/Slalib.cc	(revision 9439)
@@ -18,12 +18,6 @@
 Double_t Slalib::Trunc(Double_t val)
 {
-    /* dint(A) - truncate to nearest whole number towards zero (double) */
+    // dint(A) - truncate to nearest whole number towards zero (double)
     return val<0 ? TMath::Ceil(val) : TMath::Floor(val);
-}
-
-Double_t Slalib::Round(Double_t val)
-{
-    /* dnint(A) - round to nearest whole number (double) */
-    return val<0 ? TMath::Ceil(val-0.5) : TMath::Floor(val+0.5);
 }
 
@@ -40,5 +34,4 @@
     SetMjd(fTime.GetMjd()+offset/(24*60*60));
 }
-
 
 ZdAz Slalib::XYZ2ZdAz(double coord[3]) const
Index: /trunk/MagicSoft/Cosy/catalog/Slalib.h
===================================================================
--- /trunk/MagicSoft/Cosy/catalog/Slalib.h	(revision 9438)
+++ /trunk/MagicSoft/Cosy/catalog/Slalib.h	(revision 9439)
@@ -19,5 +19,4 @@
     double fAlpha;
 
-    static Double_t Round(Double_t val);
     static Double_t Trunc(Double_t val);
 
Index: /trunk/MagicSoft/Cosy/catalog/StarCatalog.cc
===================================================================
--- /trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 9439)
@@ -17,5 +17,5 @@
 using namespace std;
 
-StarCatalog::StarCatalog(MObservatory::LocationName_t key) : SlaStars(key), fW(768), fH(576), fAstro(0), /*fSao(NULL), fSrt(NULL), fEntries(0),*/ fSinAngle(0), fCosAngle(1), fBox(768)
+StarCatalog::StarCatalog(MObservatory::LocationName_t key) : SlaStars(key), fW(768), fH(576), fAstro(0), /*fSao(NULL), fSrt(NULL), fEntries(0),*/ fSinAngle(0), fCosAngle(1), fBoxX(768), fBoxY(576)
 {
     fAstro = new MAstroCatalog;
@@ -32,11 +32,12 @@
 {
     // pixsize [arcsec/pixel]
-    fPixSize = D2PI/360.0*pixsize/3600; // [rad / (deg*pixel)]
+    fPixSize = TMath::DegToRad()*pixsize/3600; // [rad / (deg*pixel)]
     fAstro->SetRadiusFOV(pixsize, 768, 576);
+    // fov = hypot(768, 576)/2*pixsize/3600;
 }
 
 double StarCatalog::GetPixSize() const
 {
-    return fPixSize*3600*360.0/D2PI;
+    return fPixSize*3600*TMath::RadToDeg();
 }
 
@@ -55,5 +56,5 @@
 void StarCatalog::SetAltAz(const AltAz &altaz)
 {
-    fAltAz = altaz * D2PI/360.0;
+    fAltAz = altaz * TMath::DegToRad();
     fRaDec = CalcRaDec(fAltAz);
 
@@ -72,5 +73,5 @@
 void StarCatalog::SetRaDec(const RaDec &radec)
 {
-    const RaDec rd = fRaDec*360.0/D2PI;;
+    const RaDec rd = fRaDec*TMath::RadToDeg();
 
     const Bool_t same =
@@ -78,5 +79,5 @@
         rd.Dec()>radec.Dec()-1e-5 && rd.Dec()<radec.Dec()+1e-5;
 
-    fRaDec = radec * D2PI/360.0;
+    fRaDec = radec * TMath::DegToRad();
     fAltAz = CalcAltAz(fRaDec);
 
@@ -153,26 +154,30 @@
 
 void StarCatalog::CalcStars(MStarList &list, int xc, int yc, 
-			    int xo, int yo) const
-{
-    // For an apropriate unit conversion to pixels [pix/rad]
-    const Double_t scale = TMath::RadToDeg()*TMath::Sqrt(768*768 + 576*576)/(fAstro->GetRadiusFOV()*2);
-
-    // Offsets to shift [-n/2;n/2] to [0;n] and to
-    // move the stars in the counterdirection of the LEDs
-    const Int_t offx = 768/2 + xo;
-    const Int_t offy = 576/2 + yo;
+			    float offx, float offy) const
+{
+//    const Int_t offx = 768/2 + xo;
+//    const Int_t offy = 576/2 + yo;
 
     // Allow catalog stars to be a bit outside [0.2deg] of the
     // monitored window. To get the std behaviour set offset=0
-    const Int_t offset = TMath::Nint(0.2*TMath::DegToRad()*scale);
-    const Int_t box    = fBox+offset;
+    const Int_t offset = TMath::Nint(0.2*TMath::DegToRad()/fPixSize);
+    const Int_t boxx   = fBoxX+offset;
+    const Int_t boxy   = fBoxY+offset;
 
     // CalcStars flips the picture in X defaultwise now
     // This defined the box in which stars are really returned
+    const int x0 = TMath::Max((768-xc)-boxx,   -offset);
+    const int y0 = TMath::Max(yc-boxy,         -offset);
+    const int x1 = TMath::Min((768-xc)+boxx, fW+offset);
+    const int y1 = TMath::Min(yc+boxy,       fH+offset);
+/*
     const int x0 = TMath::Max((768-xc)-box,   -offset);
-    const int y0 = TMath::Max(yc-box,         -offset);
     const int x1 = TMath::Min((768-xc)+box, fW+offset);
-    const int y1 = TMath::Min(yc+box,       fH+offset);
-
+
+    const int y0 = TMath::Max(yc-box-100,         -offset);
+    const int y1 = TMath::Min(yc+box+100,       fH+offset);
+ */
+
+/*
     // Align stars into telescope system
     // (Move the telescope to pointing position)
@@ -181,4 +186,8 @@
     align.RotateY(-(TMath::Pi()/2-fAltAz.Alt()));
     align.RotateZ(TMath::Pi()/2);
+ */
+
+    TRotation rot;
+    rot.RotateY(-(TMath::Pi()/2-fAltAz.Alt()));
 
     // Get List of stars from catalog
@@ -200,4 +209,24 @@
         const ZdAz  za(CalcZdAz(rd));
 
+        TVector3 v;
+        //v.SetMagThetaPhi(1., TMath::Pi()/2-za.Alt(), za.Az()-fAltAz.Az());
+        v.SetMagThetaPhi(1., za.Zd(), za.Az()-fAltAz.Az());
+        v *= rot;
+
+        if (v(2)<0)
+            continue;
+
+        // Stretch such, that the Z-component is alwas the same. Now
+        // X and Y contains the intersection point between the star-light
+        // and the plain of a virtual plain screen (ccd...)
+        v *= 1./v(2);
+
+        // Do unit conversion to pixels
+        v *= 1./fPixSize;
+
+        const Double_t x = -v.Y();
+        const Double_t y =  v.X();
+
+/*
         // Virtually move telescope to pointing position
         TVector3 loc;
@@ -215,13 +244,17 @@
 
         // Do an apropriate unit conversion to pixels
-        loc *= scale;
-
+        loc *= 1./fPixSize;
+
+        const Double_t x = loc.X();
+        const Double_t y = loc.Y();
+*/
         // if (loc.Mod2()>fRadiusFOV*fRadiusFOV)
         //     continue;
 
+
         // Rotate by the rotation angle of the video camera
         // and add the offsets on both axis
-        Float_t xx = loc.X()*fCosAngle - loc.Y()*fSinAngle  +  offx;
-        Float_t yy = loc.X()*fSinAngle + loc.Y()*fCosAngle  +  offy;
+        const Double_t xx = x*fCosAngle - y*fSinAngle + 768 - offx;
+        const Double_t yy = x*fSinAngle + y*fCosAngle + offy;
 
         // Check if the resulting star is in the
@@ -235,4 +268,5 @@
 }
 
+/*
 AltAz StarCatalog::CalcAltAzFromPix(Double_t pixx, Double_t pixy) const
 {
@@ -251,2 +285,38 @@
     return AltAz(-dec, ha+fAltAz.Az());
 }
+*/
+
+ZdAz StarCatalog::CalcDeltaZdAzFromPix(Double_t dpixx, Double_t dpixy) const
+{
+    double dx =  dpixx*fCosAngle + dpixy*fSinAngle;
+    double dy = -dpixx*fSinAngle + dpixy*fCosAngle;
+
+    TVector3 loc(dy, -dx, 1./fPixSize);
+
+    loc.RotateY(TMath::Pi()/2-fAltAz.Alt());
+
+    return ZdAz(loc.Theta()-TMath::Pi()/2+fAltAz.Alt(), -loc.Phi());
+
+    /*
+    // Align stars into telescope system
+    // (Move the telescope to pointing position)
+    TRotation align;
+    align.RotateZ(-fAltAz.Az());
+    align.RotateY(-(TMath::Pi()/2-fAltAz.Alt()));
+    align.RotateZ(TMath::Pi()/2);
+
+
+    TVector3 loc(dx, dy, 1./fPixSize);
+
+    loc *= align.Inverse();
+
+    cout << (TMath::Pi()/2-loc.Theta()-alt)*TMath::RadToDeg() << " " << (loc.Phi()-az)*TMath::RadToDeg() << endl;
+
+
+
+    TVector3 loc(dx, -dy, 1./fPixSize);
+
+    loc *= align.Inverse();
+
+    return ZdAz(loc.Theta(), loc.Phi());*/
+}
Index: /trunk/MagicSoft/Cosy/catalog/StarCatalog.h
===================================================================
--- /trunk/MagicSoft/Cosy/catalog/StarCatalog.h	(revision 9438)
+++ /trunk/MagicSoft/Cosy/catalog/StarCatalog.h	(revision 9439)
@@ -41,11 +41,13 @@
 
     void   SetRaDec(const RaDec &radec);
-    void   SetAltAz(const AltAz &altaz);
 
-    int    fBox;
+    int    fBoxX;
+    int    fBoxY;
 
 public:
     StarCatalog(MObservatory::LocationName_t key);
     virtual ~StarCatalog();
+
+    void   SetAltAz(const AltAz &altaz);
 
     void GetImg(byte *img, byte *cimg, MStarList &list) const;
@@ -65,5 +67,6 @@
     double GetPixSize() const;
 
-    AltAz CalcAltAzFromPix(Double_t pixx, Double_t pixy) const;
+    //AltAz CalcAltAzFromPix(Double_t pixx, Double_t pixy) const;
+    ZdAz  CalcDeltaZdAzFromPix(Double_t pixx, Double_t pixy) const;
 
     virtual void SetMjd(double mjd);
@@ -75,8 +78,9 @@
 
     //void   CalcStars(MStarList &list);
-    void   CalcStars(MStarList &list, int xc, int yc, int xo, int yo) const;
+    void   CalcStars(MStarList &list, int xc, int yc, float xo, float yo) const;
     static void DrawStars(MStarList &list, byte *img);
 
-    void SetBox(int box)   { fBox = box; }
+    void SetBox(int box)   { fBoxX = fBoxY = box; }
+    void SetBox(int boxx, int boxy)   { fBoxX = boxx; fBoxY=boxy; }
 
     ClassDef(StarCatalog, 0)
Index: /trunk/MagicSoft/Cosy/cosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/cosy.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/cosy.cc	(revision 9439)
@@ -74,4 +74,6 @@
         return kTRUE;
 
+    gLog << all << "- Trying to switch " << (on?"on":"off") << " " << (port==kPortTPoint?"TPoint":"Starguider") << " ccd via " << ip << endl;
+
     TSocket s(ip, 80);
     if (!s.IsValid())
@@ -160,5 +162,5 @@
     const TString ceco        = env.GetValue("IpAddressCentralControl", "161.72.130.60");
     const TString powerswitch = env.GetValue("IpAddressPowerSwitch", "");
-    const TString pointing    = env.GetValue("PointingModel", "bending.txt");
+    const Int_t   telescope   = env.GetValue("Telescope", 1);
 
     // And move the resource options from the command line to the MEnv
@@ -238,7 +240,7 @@
 
     MDriveCom *com = new MDriveCom(ceco, ceco_tx, ceco_rx);
-    com->SetTelescope(2);
-
-    MCosy *cosy = new MCosy(env, com, pointing);
+    com->SetTelescope(telescope);
+
+    MCosy *cosy = new MCosy(env, com);
 
     com->SetMsgQueue(cosy);
@@ -254,5 +256,4 @@
     if (kDebugEnv>0)
         env.PrintUntouched();
-
 
     MStarguider *client=0;
Index: /trunk/MagicSoft/Cosy/devdrv/dkc.cc
===================================================================
--- /trunk/MagicSoft/Cosy/devdrv/dkc.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/devdrv/dkc.cc	(revision 9439)
@@ -22,8 +22,4 @@
     fStatus(0), fStatusDKC(0), fStatusPdo3(0xff), fArmed(false),
     fReport(NULL),fLabel(NULL),fUpdPos(0)
-{
-}
-
-Dkc::~Dkc()
 {
 }
@@ -165,15 +161,10 @@
     {
     case 0x1000:
-        if (subidx==1)
-        {
-            gLog << inf2 << "- " << GetNodeName() << ": Node is" << (val?" ":" not ") << "armed." << endl;
-            fArmed = val==1;
-        }
+        gLog << inf2 << "- " << GetNodeName() << ": Node is" << (val?" ":" not ") << "armed." << endl;
+        fArmed = val==1;
         return;
 
     case 0x1003:
         // FIXME, see Init
-        if (subidx!=2)
-            return;
         gLog << inf2 << "- " << GetNodeName() << ": Error[0]=" << hex << val << dec << endl;
         CheckErrorDKC(val);
@@ -196,16 +187,13 @@
 
     case 0x6004:
-        if (subidx==0)
-        {
-            fPdoPos1 = (LWORDS_t)val;
-            fPdoTime1.Set(tv);
-            fHasChangedPos1 = true;
-        }
-        if (subidx==1)
-        {
-            fPdoPos2 = (LWORDS_t)val;
-            fPdoTime2.Set(tv);
-            fHasChangedPos2 = true;
-        }
+        fPdoPos1 = (LWORDS_t)val;
+        fPdoTime1.Set(tv);
+        fHasChangedPos1 = true;
+        return;
+
+    case 0x6005:
+        fPdoPos2 = (LWORDS_t)val;
+        fPdoTime2.Set(tv);
+        fHasChangedPos2 = true;
         return;
 
@@ -246,44 +234,26 @@
     {
     case 0x1000:
-        switch (subidx)
-        {
-        case 1:
-            //lout << ddev(MLog::eGui);
-            gLog << inf2 << "- " << GetNodeName() << ": State of node set." << endl;
-            //lout << edev(MLog::eGui);
-            return;
-        }
-        break;
+        gLog << inf2 << "- " << GetNodeName() << ": State of node set." << endl;
+        return;
+
+    case 0x1001:
+        gLog << inf2 << "- " << GetNodeName() << ": PDOs requested." << endl;
+        return;
 
     case 0x2002:
-        //lout << ddev(MLog::eGui);
         gLog << inf2 << "- " << GetNodeName() << ": Velocity set." << endl;
-        //lout << edev(MLog::eGui);
         return;
 
     case 0x2003:
-        //lout << ddev(MLog::eGui);
         gLog << inf2 << "- " << GetNodeName() << ": Acceleration set." << endl;
-        //lout << edev(MLog::eGui);
         return;
 
     case 0x3006:
-        switch (subidx)
-        {
-        case 0:
-            //lout << ddev(MLog::eGui);
-            gLog << inf2 << "- " << GetNodeName() << ": RPM mode switched." << endl;
-            //lout << edev(MLog::eGui);
-            return;
-
-        case 1:
-            /*
-             lout << ddev(MLog::eGui);
-             lout << "- Velocity set (" << GetNodeName() << ")" << endl;
-             lout << edev(MLog::eGui);
-             */
-            return;
-        }
-        break;
+        gLog << inf2 << "- " << GetNodeName() << ": RPM mode switched." << endl;
+        return;
+
+    case 0x3007:
+        //gLog << inf2 << "- Velocity set (" << GetNodeName() << ")" << endl;
+        return;
 
     case 0x4000:
@@ -292,36 +262,22 @@
 
     case 0x6000:
-        //lout << ddev(MLog::eGui);
         gLog << inf2 << "- " << GetNodeName() << ": Rotation direction set." << endl;
-        //lout << edev(MLog::eGui);
         return;
 
     case 0x6002:
-        //lout << ddev(MLog::eGui);
-        gLog << inf2 << "- " << GetNodeName() << ": Velocitz resolution set." << endl;
-        //lout << edev(MLog::eGui);
+        gLog << inf2 << "- " << GetNodeName() << ": Velocity resolution set." << endl;
         return;
 
     case 0x6004:
-        switch (subidx)
-        {
-        case 0:
-            //lout << ddev(MLog::eGui);
-            gLog << inf2 << "- " << GetNodeName() << ": Absolute positioning started." << endl;
-            fPosActive = kTRUE; // Make sure that the status is set correctly already before the first PDO
-            //lout << edev(MLog::eGui);
-            return;
-
-        case 1:
-            //lout << ddev(MLog::eGui);
-            gLog << inf2 << "- " << GetNodeName() << ": Relative positioning started." << endl;
-            fPosActive = kTRUE; // Make sure that the status is set correctly already before the first PDO
-            //lout << edev(MLog::eGui);
-            return;
-        }
-        break;
-
-
-    }
+        gLog << inf2 << "- " << GetNodeName() << ": Absolute positioning started." << endl;
+        fPosActive = kTRUE; // Make sure that the status is set correctly already before the first PDO
+        return;
+
+    case 0x6005:
+        gLog << inf2 << "- " << GetNodeName() << ": Relative positioning started." << endl;
+        fPosActive = kTRUE; // Make sure that the status is set correctly already before the first PDO
+        return;
+    }
+
     NodeDrv::HandleSDOOK(idx, subidx, data, tv);
 }
@@ -348,26 +304,16 @@
 }
 
-void Dkc::StartNode()
-{
-    //
-    // Switch node from pre-operational state to operational state
-    // (This is not CANOpen compatible)
-    // After this the MACS will react on real movement commands.
-    //
-    gLog << inf2 << "- " << GetNodeName() << ": Starting Node." << endl;
-    SendSDO(0x1000, 1, (LWORD_t)1);
-    WaitForSdo(0x1000, 1);
-}
-
 void Dkc::Arm()
 {
-    StartNode();
+    gLog << inf2 << "- " << GetNodeName() << ": Arming node." << endl;
+    SendSDO(0x1000, (LWORD_t)1);
+    WaitForSdo(0x1000);
 }
 
 void Dkc::Disarm()
 {
-    gLog << inf2 << "- " << GetNodeName() << ": Stopping Node." << endl;
-    SendSDO(0x1000, 1, (LWORD_t)0);
-    WaitForSdo(0x1000, 1);
+    gLog << inf2 << "- " << GetNodeName() << ": Disarming Node." << endl;
+    SendSDO(0x1000, (LWORD_t)0);
+    WaitForSdo(0x1000);
 }
 
@@ -385,6 +331,6 @@
     //
     gLog << inf2 << "- " << GetNodeName() << ": Requesting Error[0]." << endl;
-    RequestSDO(0x1003, 2);
-    WaitForSdo(0x1003, 2);
+    RequestSDO(0x1003);
+    WaitForSdo(0x1003);
 
     /*
@@ -404,11 +350,14 @@
     ReqVelMax(); // Init fVelMax
 
+    // Request to send all PDOs at least once
+    ReqPDOs();
+
 #ifdef EXPERT
-    StartNode();
+    Arm();
 #endif
 
     gLog << inf2 << "- " << GetNodeName() << ": Checking armed status." << endl;
-    RequestSDO(0x1000, 1);
-    WaitForSdo(0x1000, 1);
+    RequestSDO(0x1000);
+    WaitForSdo(0x1000);
 }
 
@@ -422,9 +371,17 @@
 }
 
+void Dkc::ReqPDOs()
+{
+    gLog << inf2 << "- " << GetNodeName() << ": Requesting all PDOs." << endl;
+
+    SendSDO(0x1001, (LWORD_t)1);
+    WaitForSdo(0x1001);
+}
+
 void Dkc::ReqPos1()
 {
     gLog << inf2 << "- " << GetNodeName() << ": Requesting position feedback 1." << endl;
-    RequestSDO(0x6004, 0);
-    WaitForSdo(0x6004, 0);
+    RequestSDO(0x6004);
+    WaitForSdo(0x6004);
 }
 
@@ -432,6 +389,6 @@
 {
     gLog << inf2 << "- " << GetNodeName() << ": Requesting position feedback 2." << endl;
-    RequestSDO(0x6004, 1);
-    WaitForSdo(0x6004, 1);
+    RequestSDO(0x6005);
+    WaitForSdo(0x6005);
 }
 
@@ -460,15 +417,8 @@
 {
     gLog << dbg << "- Setting acceleration to: " << acc << endl;
-    SendSDO(0x2003, 0, acc);  // acceleration
-    WaitForSdo(0x2003, 0);
-}
-/*
-void Dkc::SetDeceleration(LWORD_t dec)
-{
-    gLog << dbg << "- Setting deceleration to: " << dec << endl;
-    SendSDO(0x2003, 1, dec);
-    WaitForSdo(0x2003, 1);
-}
-*/
+    SendSDO(0x2003, acc);  // acceleration
+    WaitForSdo(0x2003);
+}
+
 void Dkc::SetRpmMode(BYTE_t mode)
 {
@@ -476,12 +426,12 @@
     // SetRpmMode(FALSE) stop the motor, but lets the position control unit on
     //
-    SendSDO(0x3006, 0, mode ? string('s','t','r','t') : string('s','t','o','p'));
-    WaitForSdo(0x3006, 0);
+    SendSDO(0x3006, mode ? string('s','t','r','t') : string('s','t','o','p'));
+    WaitForSdo(0x3006);
 }
 
 void Dkc::SetRpmVelocity(LWORDS_t cvel)
 {
-    SendSDO(0x3006, 1, (LWORD_t)cvel);
-    WaitForSdo(0x3006, 1);
+    SendSDO(0x3007, (LWORD_t)cvel);
+    WaitForSdo(0x3007);
 }
 
@@ -496,5 +446,5 @@
 
     gLog << dbg << GetNodeName() << ": Starting relative positioning by " << (LWORDS_t)pos << " ticks." << endl;
-    SendSDO(0x6004, 1, (LWORD_t)pos);
+    SendSDO(0x6005, (LWORD_t)pos);
     fPosActive = kTRUE; // Make sure that the status is set correctly already before the first PDO
 }
@@ -510,5 +460,5 @@
 
     gLog << dbg << GetNodeName() << ": Starting absolute positioning to " << (LWORDS_t)pos << " ticks." << endl;
-    SendSDO(0x6004, 0, (LWORD_t)pos);
+    SendSDO(0x6004, (LWORD_t)pos);
     fPosActive = kTRUE; // Make sure that the status is set correctly already before the first PDO
 }
@@ -526,28 +476,6 @@
 
     gLog << dbg << GetNodeName() << ": Starting absolute positioning to " << p << " ticks." << endl;
-    SendSDO(0x6004, 0, p);
+    SendSDO(0x6004, p);
     fPosActive = kTRUE; // Make sure that the status is set correctly already before the first PDO
-}
-
-void Dkc::StartVelSync()
-{
-    //
-    // The syncronization mode is disabled by a 'MOTOR STOP'
-    // or by a positioning command (POSA, ...)
-    //
-    gLog << inf2 << "- " << GetNodeName() << ": Starting RPM Sync Mode." << endl;
-    SendSDO(0x3007, 0, string('s', 'y', 'n', 'c'));
-    WaitForSdo(0x3007, 0);
-}
-
-void Dkc::StartPosSync()
-{
-    //
-    // The syncronization mode is disabled by a 'MOTOR STOP'
-    // or by a positioning command (POSA, ...)
-    //
-    gLog << inf2 << "- " << GetNodeName() << ": Starting Position Sync Mode." << endl;
-    SendSDO(0x3007, 1, string('s', 'y', 'n', 'c'));
-    WaitForSdo(0x3007, 1);
 }
 
Index: /trunk/MagicSoft/Cosy/devdrv/dkc.h
===================================================================
--- /trunk/MagicSoft/Cosy/devdrv/dkc.h	(revision 9438)
+++ /trunk/MagicSoft/Cosy/devdrv/dkc.h	(revision 9439)
@@ -73,5 +73,5 @@
 public:
     Dkc(const BYTE_t nodeid, const char *name=NULL);
-    virtual ~Dkc();
+    virtual ~Dkc() { }
 
     static LWORD_t string(BYTE_t b0=0, BYTE_t b1=0, BYTE_t b2=0, BYTE_t b3=0)
@@ -84,4 +84,5 @@
                  BYTE_t d3=0, BYTE_t d4=0, BYTE_t d5=0);
 
+    void ReqPDOs();
     void ReqPos1();
     void ReqPos2();
@@ -91,19 +92,13 @@
     void ReqVelMax();
     void SetAcceleration(LWORD_t acc);
-    void SetDeceleration(LWORD_t dec);
+//    void SetDeceleration(LWORD_t dec);
     void SetVelocity(LWORD_t vel);
     void SetVelocityRel(Double_t vel);
     void SetRpmMode(BYTE_t mode=TRUE);
     void SetRpmVelocity(LWORDS_t cvel);
-    void SetPDO1On(BYTE_t flag=TRUE);
-
-    void StartVelSync();
-    void StartPosSync();
 
     void StartRelPos(LWORDS_t pos);
     void StartAbsPos(LWORDS_t pos);
     void StartAbsPosRev(Double_t pos);
-
-    void StartNode();
 
     bool IsArmed() const { return fArmed; }
Index: /trunk/MagicSoft/Cosy/gui/MGCosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGCosy.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/gui/MGCosy.cc	(revision 9439)
@@ -318,5 +318,5 @@
 }
 
-void MGCosy::CreatePredefinedPos(TGCompositeFrame *tf1)
+void MGCosy::CreatePredefinedPos(TGCompositeFrame *tf1, const char *prepos)
 {
     TGComboBox *box = new TGComboBox(tf1, kCB_PredefPos);
@@ -331,8 +331,8 @@
     fList->Add(lay);
 
-    ifstream fin("prepos.txt");
+    ifstream fin(prepos);
     if (!fin)
     {
-        gLog << err << "ERROR: Predifined positions in 'prepos.txt' not found." << endl;
+        gLog << err << "ERROR: Predifined positions in '" << prepos << "' not found." << endl;
         return;
     }
@@ -391,5 +391,5 @@
 }
 
-void MGCosy::CreateTabs()
+void MGCosy::CreateTabs(const char *prepos)
 {
     TGCompositeFrame *tf1 = fTab->AddTab("Position");
@@ -408,5 +408,5 @@
     fList->Add(fCZdAz);
 
-    CreatePredefinedPos(tf1);
+    CreatePredefinedPos(tf1, prepos);
     CreateStarList(tf4);
 
@@ -728,5 +728,5 @@
 }
 
-MGCosy::MGCosy(MObservatory::LocationName_t key, MsgQueue *q)
+MGCosy::MGCosy(MObservatory::LocationName_t key, const char *prepos, MsgQueue *q)
     : TGMainFrame(gClient->GetRoot(), 1, 1), fObservatory(key), fQueue(q)
 {
@@ -755,5 +755,7 @@
 
     fAccuracy = new MGAccuracy(f1, 300);
-    fAccuracy->DrawText("0.5se", "1.0se", "2.0se", "Ctrl Deviation [min]");
+    fAccuracy->SetColorScale(1);
+    fAccuracy->DrawText("1.0'", "2.0'", "3.0'", "Ctrl Deviation [min]");
+    //fAccuracy->DrawText("0.5se", "1.0se", "2.0se", "");
     fAccuracy->DrawCircles();
 
@@ -815,5 +817,5 @@
     AddFrame(f2, hints2);
 
-    CreateTabs();
+    CreateTabs(prepos);
     CreateLabel(frame);
 
Index: /trunk/MagicSoft/Cosy/gui/MGCosy.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGCosy.h	(revision 9438)
+++ /trunk/MagicSoft/Cosy/gui/MGCosy.h	(revision 9439)
@@ -114,6 +114,6 @@
     void CreateMenu();
     void CreateLabel(TGCompositeFrame *f);
-    void CreateTabs();
-    void CreatePredefinedPos(TGCompositeFrame *tf1);
+    void CreateTabs(const char *prepos);
+    void CreatePredefinedPos(TGCompositeFrame *tf1, const char *prepos);
     void CreateStarList(TGCompositeFrame *tf1);
 
@@ -133,5 +133,5 @@
 
 public:
-    MGCosy(MObservatory::LocationName_t key, MsgQueue *q);
+    MGCosy(MObservatory::LocationName_t key, const char *prepos, MsgQueue *q);
     ~MGCosy();
 
Index: /trunk/MagicSoft/Cosy/gui/MGStarg.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGStarg.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/gui/MGStarg.cc	(revision 9439)
@@ -23,4 +23,6 @@
 : MGAccuracy(p, w)
 {
-    SetColorScale(1.5);
+    SetColorScale(1);
+    DrawText("1.0'", "2.0'", "3.0'", "Mispointing [min]");
+    DrawHexagon();
 }
Index: /trunk/MagicSoft/Cosy/leds_magic2.txt
===================================================================
--- /trunk/MagicSoft/Cosy/leds_magic2.txt	(revision 9438)
+++ /trunk/MagicSoft/Cosy/leds_magic2.txt	(revision 9439)
@@ -5,4 +5,4 @@
 370 518 0 0
 170 405 0 0
-166 174 0 0
-251  87 0 0
+#166 174 0 0 
+252  82 0 0
Index: /trunk/MagicSoft/Cosy/main/MCaos.cc
===================================================================
--- /trunk/MagicSoft/Cosy/main/MCaos.cc	(revision 9438)
+++ /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 9438)
+++ /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 9438)
+++ /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 9438)
+++ /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 9438)
+++ /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 9438)
+++ /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;
         */
Index: /trunk/MagicSoft/Cosy/prepos_magic2.txt
===================================================================
--- /trunk/MagicSoft/Cosy/prepos_magic2.txt	(revision 9438)
+++ /trunk/MagicSoft/Cosy/prepos_magic2.txt	(revision 9439)
@@ -1,1 +1,1 @@
-93.724 0.0  Park
+93.62 0.0  Park
Index: /trunk/MagicSoft/Cosy/stargleds_magic2.txt
===================================================================
--- /trunk/MagicSoft/Cosy/stargleds_magic2.txt	(revision 9438)
+++ /trunk/MagicSoft/Cosy/stargleds_magic2.txt	(revision 9439)
@@ -1,4 +1,5 @@
-35     139    0 0
-90     195    0 0
-113    271    0 0 
-34     402    0 0
+91  136 0 0
+148 192 0 0
+169 267 0 0
+91  400 0 0
+20  420 0 0
Index: /trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/tcpip/MCeCoCom.cc	(revision 9439)
@@ -104,5 +104,6 @@
     cout << "Zd/Az: " << zd << "/" << az << "  ";
     cout << "Ra/Dec: " << ra/15 << "h/" << dec << "  ";
-    cout << "SolRad: " << solar << "W/m²" << endl;
+    //cout << "SolRad: " << solar << "W/m²";
+    cout << endl;
 
     fComStat = kCmdReceived;
@@ -122,22 +123,22 @@
         return InterpreteReport(str);
 
-    const bool rc = InterpreteCmd(cmd, str.Strip(TString::kBoth));
-    fComStat = rc ? kCmdReceived : kComProblem;
-    return rc;
+    return InterpreteCmd(cmd, str.Strip(TString::kBoth));
 }
 
 bool MCeCoCom::SendRep(const char *cmd, const char *str, bool force)
 {
-    MTime t;
-    t.Now();
 
     UShort_t y1, y2, ms1, ms2;
     Byte_t mon1, mon2, d1, d2, h1, h2, m1, m2, s1, s2;
 
+    fT.GetDate(y2, mon2, d2);
+    fT.GetTime(h2, m2, s2, ms2);
+
+    MTime t(-1);
     t.GetDate(y1, mon1, d1);
     t.GetTime(h1, m1, s1, ms1);
 
-    fT.GetDate(y2, mon2, d2);
-    fT.GetTime(h2, m2, s2, ms2);
+    if (t-fT>20)
+        fComStat = kComProblem;
 
     const TString msg =
@@ -152,5 +153,4 @@
     // Send report to CC
     const bool rc = Send(msg.Data(), msg.Length());
-    fComStat = rc ? kNoCmdReceived : kComProblem;
 
     // Write report to stream
Index: /trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h	(revision 9438)
+++ /trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h	(revision 9439)
@@ -19,7 +19,6 @@
     enum ComStatus_t
     {
-        kCmdReceived,
-        kNoCmdReceived,
-        kComProblem
+        kCmdReceived = 1,
+        kComProblem  = 2
     };
 
@@ -52,5 +51,5 @@
     MCeCoCom(const char *addr, const int tx=7304, const int rx=7404, MLog *out=NULL)
         : MTcpIpIO(addr, tx, rx), fOut(out), fStatus(0),
-        fTelescope(1), fComStat(kNoCmdReceived),
+        fTelescope(1), fComStat(kComProblem),
         fHumidity(0), fTemperature(0), fWindSpeed(0), fSolarRadiation(-1),
         fAlarmCounter(0)
Index: /trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 9439)
@@ -7,4 +7,8 @@
 #include "MString.h"
 #include "Ring.h"
+#include "Led.h"
+
+#include "MLog.h"
+#include "MLogManip.h"
 
 using namespace std;
@@ -45,14 +49,14 @@
     if (!ReadPosition(str, ra, dec))
     {
-        cout << "ERROR - Reading position from RADEC" << endl;
+        gLog << err << "ERROR - Reading position from RADEC" << endl;
         return false;
     }
     if (!str.IsNull())
     {
-        cout << "ERROR - Too many bytes in command RADEC" << endl;
-        return false;
-    }
-
-    cout << "CC-COMMAND " << MTime(-1) << " RADEC " << ra << "h " << dec << "d '" << str << "'" << endl;
+        gLog << err << "ERROR - Too many bytes in command RADEC" << endl;
+        return false;
+    }
+
+    gLog << all << "CC-COMMAND " << MTime(-1) << " RADEC " << ra << "h " << dec << "d '" << str << "'" << endl;
 
     ra *= 15; // h -> deg
@@ -72,14 +76,14 @@
     if (!ReadPosition(str, ra, dec))
     {
-        cout << "ERROR - Reading position from GRB" << endl;
+        gLog << err << "ERROR - Reading position from GRB" << endl;
         return false;
     }
     if (!str.IsNull())
     {
-        cout << "ERROR - Too many bytes in command GRB" << endl;
-        return false;
-    }
-
-    cout << "CC-COMMAND " << MTime(-1) << " GRB " << ra << "h " << dec << "d '" << str << "'" << endl;
+        gLog << err << "ERROR - Too many bytes in command GRB" << endl;
+        return false;
+    }
+
+    gLog << all << "CC-COMMAND " << MTime(-1) << " GRB " << ra << "h " << dec << "d '" << str << "'" << endl;
 
     ra *= 15; // h -> deg
@@ -99,5 +103,5 @@
     if (!ReadPosition(str, zd, az))
     {
-        cout << "ERROR - Reading position from ZDAZ" << endl;
+        gLog << err << "ERROR - Reading position from ZDAZ" << endl;
         return false;
     }
@@ -105,9 +109,9 @@
     if (!str.IsNull())
     {
-        cout << "ERROR - Too many bytes in command ZDAZ" << endl;
-        return false;
-    }
-
-    cout << "CC-COMMAND " << MTime(-1) << " ZDAZ " << zd << "deg " << az << "deg" << endl;
+        gLog << err << "ERROR - Too many bytes in command ZDAZ" << endl;
+        return false;
+    }
+
+    gLog << all << "CC-COMMAND " << MTime(-1) << " ZDAZ " << zd << "deg " << az << "deg" << endl;
 
     ZdAz za(zd, az);
@@ -125,10 +129,10 @@
     if (str.IsNull())
     {
-        cout << "ERROR - No identifier for preposition (PREPS) given." << endl;
+        gLog << err << "ERROR - No identifier for preposition (PREPS) given." << endl;
         return false;
     }
     if (str.First(' ')>=0)
     {
-        cout << "ERROR - PREPS command syntax error (contains whitespaces)." << endl;
+        gLog << err << "ERROR - PREPS command syntax error (contains whitespaces)." << endl;
         return false;
     }
@@ -136,5 +140,5 @@
     str.ToLower();
 
-    cout << "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl;
+    gLog << all << "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl;
 
     //cout << "MDriveCom - TRACK... start." << endl;
@@ -147,8 +151,19 @@
 bool MDriveCom::CommandTPOINT(TString &str)
 {
-    cout << "CC-COMMAND " << MTime(-1) << " TPOIN " << str << endl;
-
-    if (fQueue)
-        fQueue->PostMsg(WM_TPOINT);//, (void*)str.Data(), str.Length()+1);
+    gLog << all << "CC-COMMAND " << MTime(-1) << " TPOIN " << str << endl;
+
+    TObjArray *arr = str.Tokenize(' ');
+
+    if (arr->GetEntries()!=2)
+    {
+        delete arr;
+        gLog << err << "ERROR - Wrong number of arguments in TPOIN command" << endl;
+        return false;
+    }
+
+    delete arr;
+
+    if (fQueue)
+        fQueue->PostMsg(WM_STARGTPOINT, (void*)str.Data(), str.Length()+1);//, (void*)str.Data(), str.Length()+1);
 
     return true;
@@ -157,8 +172,10 @@
 bool MDriveCom::CommandSTGMD(TString &str)
 {
-    cout << "CC-COMMAND " << MTime(-1) << "STGMD" << endl;
-
-    if (fQueue)
-        fQueue->PostMsg(WM_STARGMODE);//, (void*)str.Data(), str.Length()+1);
+    gLog << all << "CC-COMMAND " << MTime(-1) << " STGMD " << str << endl;
+
+    bool on = str=="ON";
+
+    if (fQueue)
+        fQueue->PostMsg(WM_STARGMODE, &on, sizeof(bool));//, (void*)str.Data(), str.Length()+1);
 
     return true;
@@ -170,10 +187,10 @@
     if (str.IsNull())
     {
-        cout << "ERROR - No identifier for ARM command." << endl;
+        gLog << err << "ERROR - No identifier for ARM command." << endl;
         return false;
     }
     if (str.First(' ')>=0)
     {
-        cout << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
+        gLog << err << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
         return false;
     }
@@ -182,9 +199,9 @@
     if (str!="lock" && str!="unlock")
     {
-        cout << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
-        return false;
-    }
-
-    cout << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
+        gLog << err << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
+        return false;
+    }
+
+    gLog << all << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
 
     bool lock = str=="lock";
@@ -200,5 +217,5 @@
     {
         //cout << "MDriveCom - WAIT... start." << endl;
-        cout << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
+        gLog << all << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
         if (fQueue)
             fQueue->PostMsg(WM_WAIT);
@@ -210,5 +227,5 @@
     {
         //cout << "MDriveCom - STOP!... start." << endl;
-        cout << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
+        gLog << all << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
         if (fQueue)
             fQueue->PostMsg(WM_STOP);
@@ -240,9 +257,9 @@
     if (cmd.IsNull() && str.IsNull())
     {
-        cout << "CC-COMMAND " << MTime(-1) << " Empty command (single '\\n') received." << endl;
-        return false;
-    }
-
-    cout << "CC-COMMAND " << MTime(-1) << " Syntax error: '" << cmd << "':'" << str << "'" << endl;
+        gLog << all << "CC-COMMAND " << MTime(-1) << " Empty command (single '\\n') received." << endl;
+        return false;
+    }
+
+    gLog << err << "CC-COMMAND " << MTime(-1) << " Syntax error: '" << cmd << "':'" << str << "'" << endl;
     return false;
 }
@@ -258,5 +275,5 @@
 }
 
-bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed)
+bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed, Int_t stargmd)
 {
     // rd [rad]
@@ -294,4 +311,5 @@
     str += MString::Format("%08.3f ", er.Az());
     str += armed ? "1 " : "0 ";
+    str += MString::Format("%d ", stargmd);           // Starguider mode: 0=none, 1=starguider, 2=starguider off
 
     return SendRep("DRIVE-REPORT", str.Data(), kFALSE);
@@ -336,25 +354,41 @@
 }
 
-bool MDriveCom::SendTPoint(UInt_t stat)
-{
-    //const MTime t(-1);
-
+bool MDriveCom::SendTPoint(bool stat, char type, Float_t mag, const char *name, const AltAz &za0, const ZdAz &za1,
+                           const TVector2 &xy, Float_t dzd, Float_t daz, const MTime &t,
+                           const Ring &center, const Led &star, Int_t numleds, Int_t numrings,
+                           Int_t numstars, Int_t numcor, Float_t bright)
+{
     SetStatus(stat);
-    /*
-     TString str;
-     str += name;  // star name
-     str += " ";
-     str += nominalaz;
-     str += nominalel;
-     str += realaz;
-     str += realel;
-     str += nomra;
-     str += nomdec;
-     str += diffaz;
-     str += diffel;
-     str += mjd;
-     str += numleds;
-     str += artmag;
-     */
-    return SendRep("TPOINT-REPORT", "", kTRUE);
-}
+
+    TString str = type;
+    str += MString::Format(" %8.4f ", za0.Az());
+    str += MString::Format("%8.4f ", za0.Alt());
+    str += MString::Format("%8.4f ", fmod(za1.Az()+360, 360));
+    str += MString::Format("%8.4f ", 90-za1.Zd());
+    str += MString::Format("%8.4f ", xy.X());
+    str += MString::Format("%8.4f ", xy.Y());
+    str += MString::Format("%8.4f ", dzd);
+    str += MString::Format("%8.4f ", daz);
+    str += MString::Format("%12.6f ", t.GetMjd());
+    str += MString::Format("%f ", center.GetMag());
+    str += MString::Format("%f ", star.GetMag());
+    str += MString::Format("%f ", center.GetX());
+    str += MString::Format("%f ", center.GetY());
+    str += MString::Format("%f ", star.GetX());
+    str += MString::Format("%f ", star.GetY());
+    str += numleds;
+    str += " ";
+    str += numrings;
+    str += " ";
+    str += numstars;
+    str += " ";
+    str += numcor;
+    str += " ";
+    str += bright;
+    str += " ";
+    str += mag;
+    str += " ";
+    str += name;
+
+    return SendRep("TPOINT-REPORT", str, kTRUE);
+}
Index: /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 9438)
+++ /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 9439)
@@ -7,8 +7,12 @@
 
 class RaDec;
+class AltAz;
 class ZdAz;
 class MsgQueue;
 class Ring;
+class Led;
 class MLog;
+class MTime;
+class TVector2;
 
 class MDriveCom : public MCeCoCom
@@ -49,7 +53,11 @@
 
 
-    bool SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed);
+    bool SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed, Int_t stargmd);
     bool SendStargReport(UInt_t stat, ZdAz miss, ZdAz nompos, Ring center, Int_t num, Int_t n, Double_t bright, Double_t mjd, Int_t numleds, Int_t numrings);
-    bool SendTPoint(UInt_t stat);
+//    bool SendTPoint(char type);
+    bool SendTPoint(bool stat, char type, Float_t mag, const char *name, const AltAz &za0, const ZdAz &za1,
+                    const TVector2 &xy, Float_t dzd, Float_t daz, const MTime &t,
+                    const Ring &center, const Led &star, Int_t numleds, Int_t numrings,
+                    Int_t numstars=0, Int_t numcor=0, Float_t bright=0);
     bool SendStatus(const char *stat);
 };
Index: /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 9439)
@@ -43,10 +43,17 @@
 MTcpIpO::MTcpIpO(const char *addr, Int_t tx) : fPortTx(tx)
 {
-    gLog << inf2 << "- Open send socket to " << addr << ":" << tx << endl;
+    // was "inf2" but nobody knows what the prg is doing if it times out
+    gLog << all << "- Open send socket to " << addr << ":" << tx << endl;
+
     fTxSocket = new TSocket(addr, tx);
+    fTxSocket->SetOption(kNoBlock, 1);
+
+    MTcpIpO::RunThread();
 }
 
 MTcpIpO::~MTcpIpO()
 {
+    CancelThread();
+
     // Now delete all TCP/IP objects
     delete fTxSocket;
@@ -55,5 +62,5 @@
 MTcpIpIO::MTcpIpIO(const char *addr, Int_t tx, Int_t rx) : MTcpIpI(rx), MTcpIpO(addr, tx)
 {
-    RunThread();
+    MTcpIpI::RunThread();
 }
 
@@ -74,16 +81,15 @@
 }
 
+/*
 TString MTcpIpO::GetSocketAddress() const
 {
+    TLockGuard guard(const_cast<TMutex*>(&fMutex));
     return GetSocketAddress(*fTxSocket);
 }
-
+*/
 bool MTcpIpO::SendFrame(TSocket &tx, const char *msg, int len)
 {
     if (!tx.IsValid())
-    {
-        //gLog << warn << "WARNING - No transmission to " << GetSocketAddress(tx) << " possible." << endl;
         return false;
-    }
 
 #ifdef DEBUG
@@ -91,5 +97,14 @@
 #endif
 
-    const Int_t l = tx.SendRaw(msg, len);
+    const Int_t l = tx.SendRaw(msg, len, kDontBlock);
+
+    // Frame not sent, EWOULDBLOCK
+    if (l==-4)
+    {
+        gLog << err << "ERROR - Sending to " << GetSocketAddress(tx) << " would block." << endl;
+/*--->*/        tx.Close(); // ?????
+        return false;
+    }
+
     if (l<0)
     {
@@ -100,5 +115,5 @@
     if (l!=len)
     {
-        gLog << err << "ERROR - Sent wrong number (" << l << ") of bytes to " << GetSocketAddress(tx) << endl;
+        gLog << err << "ERROR - Could only sent " << l << " out of "  << len << " bytes to " << GetSocketAddress(tx) << endl;
         return false;
     }
@@ -109,5 +124,4 @@
 bool MTcpIpO::SendFrame(const char *addr, int port, const char *msg, int len)
 {
-    //    R__LOCKGUARD2(myMutex);
 #ifdef DEBUG
     cout << "Connecting to " << addr << ":" << port << endl;
@@ -117,49 +131,68 @@
     TSocket tx(addr, port);
     return SendFrame(tx, msg, len);
-/*
-    if (!tx.IsValid())
-    {
-        gLog << warn << "WARNING - No transmission to " << addr << ":" << port << " possible." << endl;
+}
+
+bool MTcpIpO::Send(const char *msg, Int_t len)
+{
+    const Int_t mtx = fMutex.TryLock();
+    if (mtx==13)
+        gLog << warn << "MTcpIpO::Send - mutex is already locked by this thread." << endl;
+
+    // If Mutex cannot be locked, i.e. we are currently reopening
+    // the send socket we cannot wait, because we would block the
+    // executing threrad.
+    if (mtx)
         return false;
-    }
-
-    gLog << dbg << "Sending to " << addr << ":" << port << endl;
-
-    const Int_t l = tx.SendRaw(msg, len, kDontBlock);
-    if (l<0)
-    {
-        gLog << err << "ERROR - Sending TCP/IP frame to " << addr << ":" << port << endl;
-        return false;
-    }
-
-    if (l!=len)
-    {
-        gLog << err << "ERROR - Sent wrong number (" << l << ") of bytes to " << addr << ":" << port << endl;
-        return false;
-    }
-
-#ifdef DEBUG
-    cout << "Tx: " << msg << flush;
-#endif
-
-    return true;
-    */
-}
-
-bool MTcpIpO::Send(const char *msg, Int_t len)
-{
-    if (!fTxSocket->IsValid())
-    {
-        const TInetAddress &a = fTxSocket->GetInetAddress();
-        if (!a.IsValid())
-            return false;
+
+    const bool rc = SendFrame(*fTxSocket, msg, len);
+
+    fMutex.UnLock();
+
+    return rc;
+}
+
+Int_t MTcpIpO::Thread()
+{
+    const TInetAddress &a = fTxSocket->GetInetAddress();
+    if (!a.IsValid())
+    {
+        gLog << err << "ERROR: Send socket address invalid." << endl;
+        return 0;
+    }
+
+    Int_t wait = 1000; /// Wait a ms
+
+    while (!IsThreadCanceled())
+    {
+        if (fTxSocket->IsValid())
+        {
+            MThread::Sleep(1000); // Wait a ms
+            wait = 1000;
+            continue;
+        }
+
+
 #ifdef DEBUG
         cout << "- Reopen send socket to " << a.GetHostAddress() << ":" << fPortTx << endl;
 #endif
+
+        fMutex.Lock();
+
         delete fTxSocket;
+
         fTxSocket = new TSocket(a.GetHostAddress(), fPortTx);
-    }
-
-    return SendFrame(*fTxSocket, msg, len);
+        fTxSocket->SetOption(kNoBlock, 1);
+
+        fMutex.UnLock();
+
+        if (fTxSocket->IsValid())
+            continue;
+
+        MThread::Sleep(wait); // Wait a ms
+        if (wait<30000000) // 30s
+            wait *= 2;
+    }
+
+    return 1;
 }
 
@@ -174,5 +207,5 @@
     TString str;
 
-    while (!IsThreadCanceled())
+    while (!MTcpIpI::IsThreadCanceled())
     {
         char c;
Index: /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 9438)
+++ /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.h	(revision 9439)
@@ -2,9 +2,12 @@
 #define COSY_MTcpIpIO
 
-#ifndef MARS_MThread
-#include "MThread.h"
+#ifndef COSY_MsgQueue
+#include "msgqueue.h"
 #endif
 #ifndef MARS_MTime
 #include "MTime.h"
+#endif
+#ifndef ROOT_TMutex
+#include <TMutex.h>
 #endif
 
@@ -22,4 +25,6 @@
 
     Bool_t fConnectionEstablished;
+
+    MTime  fLast;
 
     Bool_t WaitForData(TSocket &sock);
@@ -39,10 +44,13 @@
 
 // A generalized class for sending over tcp/ip
-class MTcpIpO
+class MTcpIpO : public MThread
 {
 private:
     TSocket *fTxSocket;
+    TMutex   fMutex;
 
     Int_t fPortTx;
+
+    Int_t Thread();
 
 public:
@@ -54,6 +62,5 @@
     static bool    SendFrame(const char *addr, int port, const char *msg, int len);
 
-    TString GetSocketAddress() const;
-    bool    Send(const char *msg, int len);
+    bool Send(const char *msg, int len);
 };
 
Index: /trunk/MagicSoft/Cosy/videodev/FilterLed.cc
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 9438)
+++ /trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 9439)
@@ -247,5 +247,6 @@
 
 int FilterLed::GetMeanPosition(const int x, const int y,
-                               const int box, float &mx, float &my, unsigned int &sum) const
+                               const int boxx, const int boxy,
+                               float &mx, float &my, unsigned int &sum) const
 {
     unsigned int sumx=0;
@@ -253,6 +254,6 @@
 
     sum=0;
-    for (int dx=x-box; dx<x+box+1; dx++)
-        for (int dy=y-box; dy<y+box+1; dy++)
+    for (int dx=x-boxx; dx<x+boxx+1; dx++)
+        for (int dy=y-boxy; dy<y+boxy+1; dy++)
         {
             const byte &m = fImg[dy*fW+dx];
@@ -269,23 +270,23 @@
 }
 
-int FilterLed::GetMeanPosition(const int x, const int y, const int box) const
+int FilterLed::GetMeanPosition(const int x, const int y, const int boxx, const int boxy) const
 {
     float mx, my;
     unsigned int sum;
-    return GetMeanPosition(x, y, box, mx, my, sum);
+    return GetMeanPosition(x, y, boxx, boxy, mx, my, sum);
 }
 
 int FilterLed::GetMeanPositionBox(const int x, const int y,
-                                  const int box, float &mx,
-                                  float &my, unsigned int &sum) const
+                                  const int boxx, const int boxy,
+                                  float &mx, float &my, unsigned int &sum) const
 {
     //-------------------------------
     // Improved algorithm:
     // 1. Look for the largest five-pixel-cross signal inside the box
-    int x0 = TMath::Max(x-box+1,   0);
-    int y0 = TMath::Max(y-box+1,   0);
-
-    int x1 = TMath::Min(x+box+1-1, fW);
-    int y1 = TMath::Min(y+box+1-1, fH);
+    int x0 = TMath::Max(x-boxx+1,   0);
+    int y0 = TMath::Max(y-boxy+1,   0);
+
+    int x1 = TMath::Min(x+boxx+1-1, fW);
+    int y1 = TMath::Min(y+boxy+1-1, fH);
 
     int maxx=0;
@@ -330,9 +331,9 @@
 
 int FilterLed::GetMeanPositionBox(const int x, const int y,
-                                  const int box) const
+                                  const int boxx, const int boxy) const
 {
     float mx, my;
     unsigned int sum;
-    return GetMeanPositionBox(x, y, box, mx, my, sum);
+    return GetMeanPositionBox(x, y, boxx, boxy, mx, my, sum);
 }
 
@@ -374,8 +375,8 @@
 void FilterLed::Execute(Leds &leds, int xc, int yc, double &bright) const
 {
-    const int x0 = TMath::Max(xc-fBox, 0);
-    const int y0 = TMath::Max(yc-fBox, 0);
-    const int x1 = TMath::Min(xc+fBox, fW);
-    const int y1 = TMath::Min(yc+fBox, fH);
+    const int x0 = TMath::Max(xc-fBoxX, 0);
+    const int y0 = TMath::Max(yc-fBoxY, 0);
+    const int x1 = TMath::Min(xc+fBoxX, fW);
+    const int y1 = TMath::Min(yc+fBoxY, fH);
 
     const int wx = x1-x0;
@@ -432,8 +433,8 @@
     // Define inner box in which to search the signal
     //
-    const int x0 = TMath::Max(xc-fBox, 0);
-    const int y0 = TMath::Max(yc-fBox, 0);
-    const int x1 = TMath::Min(xc+fBox, fW);
-    const int y1 = TMath::Min(yc+fBox, fH);
+    const int x0 = TMath::Max(xc-fBoxX, 0);
+    const int y0 = TMath::Max(yc-fBoxY, 0);
+    const int x1 = TMath::Min(xc+fBoxX, fW);
+    const int y1 = TMath::Min(yc+fBoxY, fH);
 
     //
@@ -444,8 +445,8 @@
     const double sqrt2 = sqrt(2.);
 
-    const int xa = TMath::Max(xc-(int)rint(fBox*sqrt2), 0);
-    const int ya = TMath::Max(yc-(int)rint(fBox*sqrt2), 0);
-    const int xb = TMath::Min(xc+(int)rint(fBox*sqrt2), fW);
-    const int yb = TMath::Min(yc+(int)rint(fBox*sqrt2), fH);
+    const int xa = TMath::Max(xc-TMath::Nint(fBoxX*sqrt2), 0);
+    const int ya = TMath::Max(yc-TMath::Nint(fBoxY*sqrt2), 0);
+    const int xb = TMath::Min(xc+TMath::Nint(fBoxX*sqrt2), fW);
+    const int yb = TMath::Min(yc+TMath::Nint(fBoxY*sqrt2), fH);
 
     //
@@ -518,5 +519,6 @@
     float mx, my;
     unsigned int mag;
-    int pos = box ? GetMeanPositionBox(xc, yc, fBox-1, mx, my, mag) : GetMeanPosition(xc, yc, fBox-1, mx, my, mag);
+    int pos = box ? GetMeanPositionBox(xc, yc, fBoxX-1, fBoxY-1, mx, my, mag)
+        : GetMeanPosition(xc, yc, fBoxX-1, fBoxY-1, mx, my, mag);
 
     if (pos<0 || pos>=fW*fH || fImg[pos]<sum+fCut*sdev)
Index: /trunk/MagicSoft/Cosy/videodev/FilterLed.h
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/FilterLed.h	(revision 9438)
+++ /trunk/MagicSoft/Cosy/videodev/FilterLed.h	(revision 9439)
@@ -17,5 +17,6 @@
     int fW;
     int fH;
-    int fBox;
+    int fBoxX;
+    int fBoxY;
     float fCut;
 
@@ -24,12 +25,12 @@
 
     void GetMinMax(const int offset, byte *min, byte *max) const;
-    int  GetMeanPosition(const int x, const int y, const int box) const;
-    int  GetMeanPosition(const int x, const int y, const int box, 
+    int  GetMeanPosition(const int x, const int y, const int boxx, const int boxy) const;
+    int  GetMeanPosition(const int x, const int y, const int boxx, const int boxy,
 			 float &mx, float &my, unsigned int &sum) const;
 
     int  GetMeanPositionBox(const int x, const int y,
-                            const int box) const;
+                            const int boxx, const int boxy) const;
     int  GetMeanPositionBox(const int x, const int y,
-                            const int box, float &mx, float &my,
+                            const int boxx, const int boxy, float &mx, float &my,
                             unsigned int &sum) const;
 
@@ -40,15 +41,16 @@
 public:
     FilterLed(byte *img, int w, int h, double cut=2.5)
-      : fImg(img), fW(w), fH(h), fBox(w>h?w:h), fCut(cut)
+      : fImg(img), fW(w), fH(h), fBoxX(w), fBoxY(h), fCut(cut)
     {
     }
 
-    FilterLed(byte *img, int w, int h, int box, double cut=2.5)
-      : fImg(img), fW(w), fH(h), fBox(box), fCut(cut)
+    FilterLed(byte *img, int w, int h, int boxx, int boxy, double cut=2.5)
+      : fImg(img), fW(w), fH(h), fBoxX(boxx), fBoxY(boxy), fCut(cut)
     {
     }
     virtual ~FilterLed() { }
 
-    void SetBox(int box)   { fBox = box; }
+    void SetBox(int box)   { fBoxX = fBoxY = box; }
+    void SetBox(int boxx, int boxy)   { fBoxX = boxx; fBoxY = boxy; }
     void SetCut(float cut) { fCut = cut; } 
     void FindStar(Leds &leds, int xc, int yc, bool circle=false) const;
