Index: trunk/MagicSoft/Cosy/Changelog
===================================================================
--- trunk/MagicSoft/Cosy/Changelog	(revision 1882)
+++ trunk/MagicSoft/Cosy/Changelog	(revision 1953)
@@ -1,3 +1,86 @@
                                                                   -*-*- END -*-*-
+ 2003/04/12 - Thomas Bretz
+
+   * prepos_magic.txt:
+     - added
+     
+   * bend.cc: 
+     - added
+
+   * Makefile:
+     - bend added
+     
+   * bending_magic.txt:
+     - new bending
+
+   * cosy.cc:
+     - fixed typo
+     - disable console output
+     
+   * testse.cc:
+     - accept commandline options
+     
+   * base/MThread.cc, base/msgqueue.cc:
+     - added DEBUG-stuff
+   
+   * candrv/network.cc:
+     - fixed wrong handling of SDO messages with less than 4 bytes
+     
+   * candrv/nodedrv.cc:
+     - do not switch timer off if it isn't switched on
+   
+   * candrv/vmodican.cc:
+     - added EXPERT mode
+   
+   * catalog/SlaStars.h:
+     - added CalcAltAz/ZdAz with mjd
+   
+   * catalog/StarCatalog.[h,cc]:
+     - included rotation angle
+   
+   * devdrv/macs.cc:
+     - added request for present error code
+     - added #71 error message
+     
+   * devdrv/shaftencoder.[h,cc]:
+     - fixes to support multiturn encoders correctly
+     - removed velocity and acceleration from gui
+
+   * gui/MGAccuracy.cc:
+     - mega mini change
+   
+   * gui/MGCosy.[h,cc]:
+     - added EXPERT mode
+     - added TrackPos
+     - removed shaftencoder label for velocity and acceleration
+     - commented out MAC3
+     - fixed reading of predefined positions
+     - commented out everything related to the demo mode
+     - changed some displayed text
+
+   * gui/MGStarguider.[h,cc]:
+     - added support for rotation angle
+     
+   * main/MBending.h:
+     - added constructor with filename
+
+   * main/MCosy.[h,cc]:
+     - for gear display both axis
+     - some more output in WaitForEndMovement
+     - added CheckRange
+     - added AlignTrackingPos
+     - changed timing in tracking procedure from 3s/50ms to 5s/1s
+     - added TrackPos
+     - replaced some code by AlignTrackingPos
+     - commented out CALIB and PRESET
+
+   * slalib/oapqk.c:
+     - fixed a bug checking validity of arguments for atan2
+
+   * videodev/Writer.cc:
+     - fixed names
+
+
+
  2003/03/31 - Daniela Dorner:
  
Index: trunk/MagicSoft/Cosy/Makefile
===================================================================
--- trunk/MagicSoft/Cosy/Makefile	(revision 1882)
+++ trunk/MagicSoft/Cosy/Makefile	(revision 1953)
@@ -20,5 +20,5 @@
 #
 
-PROGRAMS = testse starg cosy
+PROGRAMS = testse starg cosy bend
 SOLIB    = cosy.so
 CINT     = M
Index: trunk/MagicSoft/Cosy/base/MThread.cc
===================================================================
--- trunk/MagicSoft/Cosy/base/MThread.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/base/MThread.cc	(revision 1953)
@@ -5,4 +5,6 @@
 #include <pthread.h>
 #include <sys/resource.h>  // PRIO_PROCESS
+
+#undef DEBUG
 
 // ----------------------------------------------------------------------
@@ -14,4 +16,7 @@
 MThread::MThread(bool start, int prio) : fIsRunning(false), fIsDetached(false), fThread(NULL), fReturn(NULL)
 {
+#ifdef DEBUG
+    cout << "MThread::MThread" << endl;
+#endif
     if (!start)
         return;
@@ -29,5 +34,7 @@
 MThread::~MThread()
 {
+#ifdef DEBUG
     cout << "~MThread::MThread" << endl;
+#endif
     Stop();
 }
@@ -122,5 +129,8 @@
 void MThread::Stop()
 {
+#ifdef DEBUG
     cout << "MThread::Stop: fThread=" << fThread << ", fIsRunning=" << (int)fIsRunning << endl;
+#endif
+
     if (!fThread || !fIsRunning)
         return;
@@ -128,5 +138,7 @@
     if (fIsDetached)
     {
+#ifdef DEBUG
         cout << "Stopping detached thread..." << flush;
+#endif
         pthread_cancel(*fThread);
         fIsRunning = false;
@@ -134,13 +146,19 @@
     else
     {
+#ifdef DEBUG
         cout << "Stopping thread..." << flush;
+#endif
         fStop = true;
         pthread_join(*fThread, &fReturn);
     }
+#ifdef DEBUG
     cout << "done." << endl;
+#endif
 
     delete fThread;
     fThread = NULL;
 
+#ifdef DEBUG
     cout << "MThread::Stop() done." << endl;
+#endif
 }
Index: trunk/MagicSoft/Cosy/base/msgqueue.cc
===================================================================
--- trunk/MagicSoft/Cosy/base/msgqueue.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/base/msgqueue.cc	(revision 1953)
@@ -6,4 +6,6 @@
 #include <sys/resource.h>  // PRIO_PROCESS
 
+#undef DEBUG
+
 // --------------------------------------------------------------------------
 //
@@ -22,5 +24,7 @@
 MsgQueue::~MsgQueue()
 {
+#ifdef DEBUG
     cout << "~MsgQueue::MsgQueue" << endl;
+#endif
     pthread_cancel(fThread);
     delete (unsigned char*)fMp;
@@ -89,8 +93,12 @@
         pthread_mutex_unlock(&fMuxMsg);
 
+#ifdef DEBUG
         cout << "MsgQueue::Thread: Processing Msg 0x" << hex << fMsg << endl;
+#endif
         // --- bool quit = fMsg==WM_QUIT;
         fRc=Proc(fMsg, fMp);
+#ifdef DEBUG
         cout << "MsgQueue::PostMsg: Msg 0x" << hex << fMsg << " processed (rc=" << fRc << ")" << endl;
+#endif
 
         // --- if (quit)
@@ -119,7 +127,11 @@
     // stopped and the messages are processed serialized
     //
+#ifdef DEBUG
     cout << "MsgQueue::PostMsg: Locking MsgQueue mutex..." << flush;
+#endif
     pthread_mutex_lock(&fMuxMsg);
+#ifdef DEBUG
     cout << "done." << endl;
+#endif
 
     //
@@ -134,9 +146,13 @@
     {
         pthread_mutex_unlock(&fMuxMsg);
+#ifdef DEBUG
         cout << "------------> MsgQueue::PostMsg: Proc still pending... Message IGNORED." << endl;
+#endif
         return NULL;
     }
     /**** NEW 20/01/2003 ****/
+#ifdef DEBUG
     cout << "MsgQueue::PostMsg: ---> Break <---" << endl;
+#endif
     fBreak = 1;
 
@@ -156,8 +172,12 @@
     // Start Proc()
     //
+#ifdef DEBUG
     cout << "MsgQueue::PostMsg: Releasing MsgQueue mutex..." << flush;
+#endif
     fStart = 1;
     pthread_mutex_unlock(&fMuxMsg);
+#ifdef DEBUG
     cout << "done." << endl;
+#endif
 
     /*
@@ -175,5 +195,7 @@
     //    usleep(1);
  
+#ifdef DEBUG
     cout << "MsgQueue::PostMsg: Returning rc = " << hex << rc << endl;
+#endif
     return rc;
 }
Index: trunk/MagicSoft/Cosy/bend.cc
===================================================================
--- trunk/MagicSoft/Cosy/bend.cc	(revision 1953)
+++ trunk/MagicSoft/Cosy/bend.cc	(revision 1953)
@@ -0,0 +1,98 @@
+#include <iomanip.h>
+#include <iostream.h>
+
+#include <MBending.h>
+
+/* ---------------------------------------------------------------------- */
+
+int main(int argc, char **argv)
+{
+    cout << "-" << endl;
+    cout << "Trying to open prepos.txt.." << endl;
+    ifstream fin("prepos.txt");
+    if (!fin)
+    {
+        cout << "ERROR: Predifined posiion in 'prepos.txt' not found." << endl;
+        return 1;
+    }
+
+    cout << "-" << endl;
+    cout << "Initializing bending correction from bending.txt.." << endl;
+    cout << "-" << endl;
+    MBending bend("bending.txt");
+    cout << "-" << endl;
+    cout << "Calculate system coordinates for predefined positions..." << endl;
+    cout << "-" << endl;
+
+    ofstream fout("prepos_system.txt");
+    if (!fout)
+    {
+        cout << "ERROR: Cannot open prepos_system.txt for writing." << endl;
+        return 1;
+    }
+
+    fout << setprecision(12);
+
+    while (1)
+    {
+        TString str;
+        Double_t zd, az;
+        fin >> str >> zd >> az;
+        if (!fin)
+            break;
+
+        cout << setprecision(7);
+        cout << " -- " << str << " --" << endl;
+
+        ZdAz za(zd, az);
+
+        ZdAz orig = za;
+
+        cout << "Pointing: \t" << za.Zd() << "\t" << za.Az() << endl;
+
+        za = bend(za*kDeg2Rad)*kRad2Deg;
+
+        cout << "Bended:   \t" << za.Zd() << "\t" << za.Az() << endl;
+        fout << str << " " << za.Zd() << " " << za.Az() << endl;
+
+        za = bend.CorrectBack(za*kDeg2Rad)*kRad2Deg;
+        cout << "Rebended: \t" << za.Zd() << "\t" << za.Az() << endl;
+
+        za -= orig;
+
+        if (za.Zd()*3600<.5)
+            za.Zd(0);
+        if (za.Az()*3600<.5)
+            za.Az(0);
+
+        cout << setprecision(3);
+        cout << "Diff:     \t" << za.Zd()*3600 << "\"\t" << za.Az()*3600 << "\"" << endl;
+
+    }
+    cout << "-" << endl;
+
+
+    Double_t min=360;
+    Double_t mini=0;
+    Double_t minj=0;
+    for (int i=-120; i<=120; i++)
+        for (int j=-120; j<=120; j++)
+        {
+            ZdAz za(i/3600., j/3600.);
+            ZdAz orig = za;
+            za = bend(za*kDeg2Rad)*kRad2Deg;
+            za = bend.CorrectBack(za*kDeg2Rad)*kRad2Deg;
+            za -= orig;
+            if (fabs(za.Zd())<min)
+            {
+                min = fabs(za.Zd());
+                mini = i;
+                minj = j;
+            }
+        }
+    cout << "Minimum Distance to Zenith: " << min*3600 << "\"";
+    cout << " @ " << mini << "\" " << minj << "\"" << endl;
+    cout << "-" << endl;
+
+    return 0;
+}
Index: trunk/MagicSoft/Cosy/bending_magic.txt
===================================================================
--- trunk/MagicSoft/Cosy/bending_magic.txt	(revision 1882)
+++ trunk/MagicSoft/Cosy/bending_magic.txt	(revision 1953)
@@ -1,19 +1,19 @@
-MAGIC1 2003/03/02 21:59:35.620239
+MAGIC1 2003/03/07 20:40:26.88604 
 S   00   000000   000000  0000000
- IA     -49.372136 -1
- IE     1.8923836 -1
- CA     -0.84914789 -1
- NPAE   -2.142261 -1
- AN     0.90833138 -1
- AW     0.93094796 -1
- NRX    24.647251 -1
- NRY    1.4925634 -1
+ IA     499.0188249 -1
+ IE     -10.741869 -1
+ CA     -0.23939033 -1
+ NPAE   2.3247554 -1
+ AN     0          -1
+ AW     -1.1901258 -1
+ NRX    0 -1
+ NRY    0 -1
  CRX    0 -1
  CRY    0 -1
- ECES   15.454204 -1
- ACES   0.090104657 -1
- ECEC   -4.7751393 -1
- ACEC   -0.082728729 -1
- MAGIC1 0.1424389 -1
+ ECES   0 -1
+ ACES   0 -1
+ ECEC   0 -1
+ ACEC   0 -1
+ MAGIC1 0 -1
  MAGIC2 0 -1
 END
Index: trunk/MagicSoft/Cosy/candrv/network.cc
===================================================================
--- trunk/MagicSoft/Cosy/candrv/network.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/candrv/network.cc	(revision 1953)
@@ -97,9 +97,9 @@
 
         case kSDO_TX3:       // answer to 0x40 with 2 bytes of data
-            fNodes[node]->HandleSDO(idx, subidx, data>>16, tv);
+            fNodes[node]->HandleSDO(idx, subidx, data&0xffff, tv);
             return;
 
         case kSDO_TX1:       // answer to 0x40 with 1 byte  of data
-            fNodes[node]->HandleSDO(idx, subidx, data>>24, tv);
+            fNodes[node]->HandleSDO(idx, subidx, data&0xff, tv);
             return;
 
Index: trunk/MagicSoft/Cosy/candrv/nodedrv.cc
===================================================================
--- trunk/MagicSoft/Cosy/candrv/nodedrv.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/candrv/nodedrv.cc	(revision 1953)
@@ -526,4 +526,7 @@
 void NodeDrv::StopGuarding()
 {
+    if (!fTimerOn)
+        return;
+
     fTimeout->TurnOff();
     fTimerOn = kFALSE;
Index: trunk/MagicSoft/Cosy/candrv/vmodican.cc
===================================================================
--- trunk/MagicSoft/Cosy/candrv/vmodican.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/candrv/vmodican.cc	(revision 1953)
@@ -880,6 +880,7 @@
 
     while (!Send(&msg));
-
+#ifdef EXPERT
     lout << "- CobId 0x" << hex << setfill('0') << setw(3) << cobid << " enabled." << endl;
+#endif
 }
 
Index: trunk/MagicSoft/Cosy/catalog/SlaStars.h
===================================================================
--- trunk/MagicSoft/Cosy/catalog/SlaStars.h	(revision 1882)
+++ trunk/MagicSoft/Cosy/catalog/SlaStars.h	(revision 1953)
@@ -40,4 +40,9 @@
     ZdAz   CalcZdAz (const RaDec &radec) const;
 
+    AltAz  CalcAltAz(const RaDec &radec, double mjd)
+    { SetMjd(mjd); return CalcAltAz(radec); }
+    ZdAz   CalcZdAz (const RaDec &radec, double mjd)
+    { SetMjd(mjd); return CalcZdAz(radec); }
+
     AltAz  CalcAltAzFast(const RaDec &radec) const;
     ZdAz   CalcZdAzFast (const RaDec &radec) const;
Index: trunk/MagicSoft/Cosy/catalog/StarCatalog.cc
===================================================================
--- trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 1953)
@@ -14,5 +14,5 @@
 ClassImp(StarCatalog);
 
-StarCatalog::StarCatalog(MObservatory::LocationName_t key) : SlaStars(key), fEntries(0)
+StarCatalog::StarCatalog(MObservatory::LocationName_t key) : SlaStars(key), fEntries(0), fSinAngle(0), fCosAngle(1)
 {
     // p = pointer to MainFrame (not owner)
@@ -505,6 +505,8 @@
     // Align alt/az[pix]
     //
-    const int xx = 767-(int)((fWidth-dx+DPI)/fPixSize);
-    const int yy =     (int)((fHeight+dy)/fPixSize);
+    const int xx = (int)(((dx-DPI)*fCosAngle - dy*fSinAngle + fWidth)/fPixSize);
+    const int yy = (int)(((dx-DPI)*fSinAngle + dy*fCosAngle + fHeight)/fPixSize);
+    //const int xx = 767-(int)((fWidth-dx+DPI)/fPixSize);
+    //const int yy =     (int)((fHeight+dy)/fPixSize);
 
     //
@@ -685,8 +687,8 @@
 
             //
-            // Align alt/az[pix]
-            //
-            const float xx = 768.0 - (fWidth -dx+DPI)/fPixSize;
-            const float yy =         (fHeight+dy)    /fPixSize;
+            // Align and rotate alt/az[pix]
+            //
+            float xx = ((dx-DPI)*fCosAngle - dy*fSinAngle + fWidth)/fPixSize;
+            float yy = ((dx-DPI)*fSinAngle + dy*fCosAngle + fHeight)/fPixSize;
 
             //
@@ -710,6 +712,12 @@
 AltAz StarCatalog::CalcAltAzFromPix(Double_t pixx, Double_t pixy) const
 {
-    Double_t dx = (pixx-768.0)*fPixSize + fWidth+DPI;
-    Double_t dy =         pixy*fPixSize - fHeight;
+    pixx *= fPixSize;
+    pixy *= fPixSize;
+
+    const double dx =  (pixx-fWidth)*fCosAngle + (pixy-fHeight)*fSinAngle;
+    const double dy = -(pixx-fWidth)*fSinAngle + (pixy-fHeight)*fCosAngle;
+
+    //const double dx = (pixx-768.0)*fPixSize + fWidth+DPI;
+    //const double dy =         pixy*fPixSize - fHeight;
 
     double ha, dec;
Index: trunk/MagicSoft/Cosy/catalog/StarCatalog.h
===================================================================
--- trunk/MagicSoft/Cosy/catalog/StarCatalog.h	(revision 1882)
+++ trunk/MagicSoft/Cosy/catalog/StarCatalog.h	(revision 1953)
@@ -28,4 +28,6 @@
     double   fWidth;    // size of display
     double   fHeight;   //
+    double   fSinAngle;
+    double   fCosAngle;
 
     float    fLimitMag; // [M] limiting magnitude for display
@@ -87,4 +89,5 @@
     void  SetPixSize(const double pixsize);
     void  SetLimitMag(const float mag) { fLimitMag = mag; }
+    void  SetRotationAngle(const float angle) { fSinAngle = sin(angle/kRad2Deg); fCosAngle = cos(angle/kRad2Deg); }
 
     double GetPixSize() const;
Index: trunk/MagicSoft/Cosy/cosy.cc
===================================================================
--- trunk/MagicSoft/Cosy/cosy.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/cosy.cc	(revision 1953)
@@ -25,5 +25,5 @@
     gLog << "==================================================" << endl;
     gLog << "                    Cosy V0.1                     " << endl;
-    gLog << "       Magic Drive Control System Softwware       " << endl;
+    gLog << "       Magic Drive Control System Software        " << endl;
     gLog << "            Compiled on <" << __DATE__ << ">"       << endl;
     gLog << "               Using ROOT v" << ROOTVER             << endl;
@@ -39,5 +39,4 @@
     MLog *l = new MLog("log/cosy.log", kTRUE);
     MLog &lout = *l;
-    lout.EnableOutputDevice(MLog::eStdout);
 
     clog("Starting Cosy at " << time.GetTimeStr() << " ...");
@@ -76,4 +75,5 @@
 
     clog("- Starting MCosy.");
+    lout.DisableOutputDevice(MLog::eStdout);
     cosy->Start();
 
Index: trunk/MagicSoft/Cosy/devdrv/macs.cc
===================================================================
--- trunk/MagicSoft/Cosy/devdrv/macs.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/devdrv/macs.cc	(revision 1953)
@@ -46,4 +46,12 @@
     switch (idx)
     {
+    case 0x1003:
+        // FIXME, see Init
+        if (subidx!=2)
+            return;
+        lout << "- " << GetNodeName() << ": Error[0]=" << dec << val << endl;
+        SetError(val);
+        return;
+
     case 0x100a:
         lout << "- " << GetNodeName() << ": Using Software Version V" << dec << (int)(val>>16) << "." << (int)(val&0xff) << endl;
@@ -221,7 +229,13 @@
 }
 
-
 void Macs::Init()
 {
+    //
+    // Request current error status (FIXME: is the first entry in the
+    // error list)
+    //
+    lout << "- " << GetNodeName() << ": Requesting Error[0]." << endl;
+    RequestSDO(0x1003, 2);
+    WaitForSdo(0x1003, 2);
     if (HasError())
     {
@@ -238,10 +252,6 @@
     RequestSDO(0x100a);
     WaitForSdo(0x100a);
-
     if (IsZombieNode())
-    {
-        lout << GetNodeName() << " - InitDevice failed!" << endl;
-        return;
-    }
+        return;
 
     // FIXME! Not statically linked!
@@ -538,4 +548,8 @@
         return;
 
+    case 71:
+        lout << "Array out of bound." << endl;
+        return;
+
     case 84:
         lout << "Too many (>12) ON TIME calls." << endl;
Index: trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc
===================================================================
--- trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc	(revision 1953)
@@ -40,8 +40,10 @@
             default:
                 lout << "?" << endl;
+                SetZombie();
                 return;
             }
         default:
             lout << "???" << endl;
+            SetZombie();
             return;
         }
@@ -88,7 +90,7 @@
 
     case 0x6502:
-        if (val==0)
-            val = 1;  // Single Turn = Multiturn with one turn
-        lout << "- Possible Turns: " << dec << val << endl;
+        //if (val==0)
+        //    val = 1;  // Single Turn = Multiturn with one turn
+        lout << "- Number of Revolutions: " << dec << val << endl;
         fTurns = val; 
         return;
@@ -105,10 +107,6 @@
     if (IsZombieNode())
     {
-        fLabel[0]->SetText(new TGString(""));
-        fLabel[1]->SetText(new TGString(""));
-        fLabel[2]->SetText(new TGString(""));
+        fLabel->SetText(new TGString(""));
         fUpdPos = ~fPos;
-        fUpdVel = ~fVel;
-        fUpdAcc = ~fAcc;
         return;
     }
@@ -116,23 +114,9 @@
     char text[21];
 
-    if (fPos!=fUpdPos && fLabel[0])
+    if (fPos!=fUpdPos && fLabel)
     {
         sprintf(text, "%ld", fPos);
-        fLabel[0]->SetText(new TGString(text));
+        fLabel->SetText(new TGString(text));
         fUpdPos = fPos;
-    }
-
-    if (fVel!=fUpdVel && fLabel[1])
-    {
-        sprintf(text, "%d", fVel);
-        fLabel[1]->SetText(new TGString(text));
-        fUpdVel = fVel;
-    }
-
-    if (fAcc!=fUpdAcc && fLabel[2])
-    {
-        sprintf(text, "%d", fAcc);
-        fLabel[2]->SetText(new TGString(text));
-        fUpdAcc = fAcc;
     }
 }
@@ -183,6 +167,6 @@
     fAcc = data[6] | (data[7]<<8);
 
-    const int uplim = 9*fTicks/10;
-    const int dnlim = 1*fTicks/10;
+    const int dnlim = fTicks/10;
+    const int uplim = fTurns*fTicks-dnlim;
 
     int turn = fTurn;
@@ -199,6 +183,4 @@
     fPos  = pos;
     fTurn = turn;
-
-    //fout << dec << (int)GetId() << " " << turn << " " << pos << endl;
 
     fTime.SetTimer(tv);
@@ -230,10 +212,6 @@
     RequestSDO(0x1000);
     WaitForSdo(0x1000);
-
     if (IsZombieNode())
-    {
-        lout << GetNodeName() << " - Init failed!" << endl;
-        return;
-    }
+        return;
 
     //
Index: trunk/MagicSoft/Cosy/devdrv/shaftencoder.h
===================================================================
--- trunk/MagicSoft/Cosy/devdrv/shaftencoder.h	(revision 1882)
+++ trunk/MagicSoft/Cosy/devdrv/shaftencoder.h	(revision 1953)
@@ -17,8 +17,8 @@
     WORD_t   fTurns; // Number of possible turns
 
-    TGLabel **fLabel;     //
+    TGLabel  *fLabel;     //
     LWORDS_t  fUpdPos;    // ticks
-    WORDS_t   fUpdVel;    // ticks per 5ms
-    WORDS_t   fUpdAcc;    // ticks per 25ms^2
+    //    WORDS_t   fUpdVel;    // ticks per 5ms
+    //    WORDS_t   fUpdAcc;    // ticks per 25ms^2
 
     bool fPosHasChanged;  //!
@@ -40,5 +40,5 @@
     void StopDevice();
 
-    void SetDisplay(TGLabel **label) { fLabel = label; }
+    void SetDisplay(TGLabel *label) { fLabel = label; }
 
     void HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, timeval_t *tv);
Index: trunk/MagicSoft/Cosy/gui/MGAccuracy.cc
===================================================================
--- trunk/MagicSoft/Cosy/gui/MGAccuracy.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/gui/MGAccuracy.cc	(revision 1953)
@@ -246,4 +246,5 @@
     //
     // calculate actual time for planet positions
+    // acc [deg]
     //
     static int X = ~0;
Index: trunk/MagicSoft/Cosy/gui/MGCosy.cc
===================================================================
--- trunk/MagicSoft/Cosy/gui/MGCosy.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/gui/MGCosy.cc	(revision 1953)
@@ -32,4 +32,5 @@
 
 #undef DEBUG
+#define EXPERT
 
 ClassImp(MGCosy);
@@ -44,5 +45,5 @@
     kPB_TRACK,
     kPB_STOP,
-    kPB_HOME,
+    kPB_TRACKPOS,
     kPB_CALCALTAZ,
     kPB_TPOINT,
@@ -126,54 +127,16 @@
     fList->Add(l);
 
-    fLabel1 = new TGLabel*[3];
-    fLabel1[0] = new TGLabel(f, "00000"); // Max: 16384
-    fLabel1[1] = new TGLabel(f, "000");   // Max: 256
-    fLabel1[2] = new TGLabel(f, "000");   // Max: 256
-    fLabel1[0]->SetTextJustify(kTextRight);
-    fLabel1[1]->SetTextJustify(kTextRight);
-    fLabel1[2]->SetTextJustify(kTextRight);
-    fLabel1[0]->Move(x,    y+40);
-    fLabel1[1]->Move(x+43, y+40);
-    fLabel1[2]->Move(x+70, y+40);
-    fList->Add(fLabel1[0]);
-    fList->Add(fLabel1[1]);
-    fList->Add(fLabel1[2]);
-    //f->AddFrame(fLabel1[0]);
-    //f->AddFrame(fLabel1[1]);
-    //f->AddFrame(fLabel1[2]);
-
-    fLabel2 = new TGLabel*[3];
-    fLabel2[0] = new TGLabel(f, "00000");
-    fLabel2[1] = new TGLabel(f, "000");
-    fLabel2[2] = new TGLabel(f, "000");
-    fLabel2[0]->SetTextJustify(kTextRight);
-    fLabel2[1]->SetTextJustify(kTextRight);
-    fLabel2[2]->SetTextJustify(kTextRight);
-    fLabel2[0]->Move(x,    y+57);
-    fLabel2[1]->Move(x+43, y+57);
-    fLabel2[2]->Move(x+70, y+57);
-    fList->Add(fLabel2[0]);
-    fList->Add(fLabel2[1]);
-    fList->Add(fLabel2[2]);
-    //f->AddFrame(fLabel2[0]);
-    //f->AddFrame(fLabel2[1]);
-    //f->AddFrame(fLabel2[2]);
-
-    fLabel3 = new TGLabel*[3];
-    fLabel3[0] = new TGLabel(f, "00000");
-    fLabel3[1] = new TGLabel(f, "000");
-    fLabel3[2] = new TGLabel(f, "000");
-    fLabel3[0]->SetTextJustify(kTextRight);
-    fLabel3[1]->SetTextJustify(kTextRight);
-    fLabel3[2]->SetTextJustify(kTextRight);
-    fLabel3[0]->Move(x,    y+74);
-    fLabel3[1]->Move(x+43, y+74);
-    fLabel3[2]->Move(x+70, y+74);
-    fList->Add(fLabel3[0]);
-    fList->Add(fLabel3[1]);
-    fList->Add(fLabel3[2]);
-    //f->AddFrame(fLabel3[0]);
-    //f->AddFrame(fLabel3[1]);
-    //f->AddFrame(fLabel3[2]);
+    fLabel1 = new TGLabel(f, "00000"); // Max: 16384
+    fLabel2 = new TGLabel(f, "00000");
+    fLabel3 = new TGLabel(f, "00000");
+    fLabel1->SetTextJustify(kTextRight);
+    fLabel2->SetTextJustify(kTextRight);
+    fLabel3->SetTextJustify(kTextRight);
+    fLabel1->Move(x, y+40);
+    fLabel2->Move(x, y+57);
+    fLabel3->Move(x, y+74);
+    fList->Add(fLabel1);
+    fList->Add(fLabel2);
+    fList->Add(fLabel3);
 
     l = new TGLabel(f, "Offset-Zd:");
@@ -250,5 +213,5 @@
     fAvailMac1 = new TGLabel(f, "- MAC1 -");
     fAvailMac2 = new TGLabel(f, "- MAC2 -");
-    fAvailMac3 = new TGLabel(f, "- MAC3 -");
+    //fAvailMac3 = new TGLabel(f, "- MAC3 -");
     fAvailSe1  = new TGLabel(f, "-SE/Zd1-");
     fAvailSe2  = new TGLabel(f, "-SE/Zd2-");
@@ -261,5 +224,5 @@
     fAvailMac1->SetBackgroundColor(color);
     fAvailMac2->SetBackgroundColor(color);
-    fAvailMac3->SetBackgroundColor(color);
+    //fAvailMac3->SetBackgroundColor(color);
     fAvailSe1->SetBackgroundColor(color);
     fAvailSe2->SetBackgroundColor(color);
@@ -281,5 +244,5 @@
     fAvailMac1->Move(10, 25+120);
     fAvailMac2->Move(10, 25+140);
-    fAvailMac3->Move(10, 25+160);
+    //fAvailMac3->Move(10, 25+160);
     fAvailSe1->Move(10, 25+180);
     fAvailSe2->Move(10, 25+200);
@@ -293,5 +256,5 @@
     fAvailMac1->Resize(60, 20);
     fAvailMac2->Resize(60, 20);
-    fAvailMac3->Resize(60, 20);
+    //fAvailMac3->Resize(60, 20);
     fAvailSe1->Resize(60, 20);
     fAvailSe2->Resize(60, 20);
@@ -305,5 +268,5 @@
     fList->Add(fAvailMac1);
     fList->Add(fAvailMac2);
-    fList->Add(fAvailMac3);
+    //fList->Add(fAvailMac3);
     fList->Add(fAvailSe1);
     fList->Add(fAvailSe2);
@@ -332,9 +295,11 @@
 
     int i=0;
-    while (!fin.eof())
+    while (1)
     {
         TString str;
         Double_t zd, az;
         fin >> str >> zd >> az;
+        if (!fin)
+            break;
         box->AddEntry(str, i++);
 
@@ -347,8 +312,10 @@
     TGCompositeFrame *tf1 = fTab->AddTab("Position");
     TGCompositeFrame *tf2 = fTab->AddTab("Track");
+#ifdef EXPERT
     TGCompositeFrame *tf4 = fTab->AddTab("Calib");
     TGCompositeFrame *tf5 = fTab->AddTab("Test SE");
     TGCompositeFrame *tf6 = fTab->AddTab("Gear");
-    /*TGCompositeFrame *tf3 =*/ fTab->AddTab("Demo");
+#endif
+    /*TGCompositeFrame *tf3 =*/// fTab->AddTab("Demo");
 
     fCZdAz = new MGCoordinates(tf1, kETypeZdAz);
@@ -362,7 +329,9 @@
     fList->Add(fCRaDec);
 
+#ifdef EXPERT
     fCCalib = new MGCoordinates(tf4, kETypeRaDec);
     fCCalib->Move(27, 105);
     fList->Add(fCCalib);
+#endif
 
     const int x=15;
@@ -372,7 +341,7 @@
     TGLabel *l1 = new TGLabel(tf1, "Move the telescope to a position given in");
     TGLabel *l2 = new TGLabel(tf1, "local coordinates. The given coordinates");
-    TGLabel *l3 = new TGLabel(tf1, "are pointing coordinates, which means that the");
-    TGLabel *l4 = new TGLabel(tf1, "coordinates are corrected for the bending of");
-    TGLabel *l5 = new TGLabel(tf1, "the telescope."); 
+    TGLabel *l3 = new TGLabel(tf1, "are pointing coordinates. Enter the coordinates");
+    TGLabel *l4 = new TGLabel(tf1, "manually or choose a predefined position");
+    TGLabel *l5 = new TGLabel(tf1, "from the combobox below.");
     l1->Move(x, y);
     l2->Move(x, y+h);
@@ -396,8 +365,10 @@
     fList->Add(l3);
 
+#ifdef EXPERT
     l1 = new TGLabel(tf4, "Start the calibration using the Start button.");
     l2 = new TGLabel(tf4, "Write a coordinate pair to a TPoint file using");
-    l3 = new TGLabel(tf4, "the TPoint button. To set the Shaftencoder offset");
-    l4 = new TGLabel(tf4, "use the Calib SE button.");
+    l3 = new TGLabel(tf4, "the TPoint button.");
+    //l3 = new TGLabel(tf4, "the TPoint button. To set the Shaftencoder offset");
+    //l4 = new TGLabel(tf4, "use the Calib SE button.");
     l1->Move(x, y);
     l2->Move(x, y+h);
@@ -425,4 +396,5 @@
     l1->Move(x, y);
     fList->Add(l1);
+#endif
 
     //
@@ -465,11 +437,19 @@
     fList->Add(but);
 
-    but = new TGTextButton(tf1, "Home", kPB_HOME);
-    but->Resize(80, 25);
-    but->Move(160, 197);
-    but->SetToolTipText("Initiate Home Drive.");
+    but = new TGTextButton(tf1, "Move'n'Track", kPB_TRACKPOS);
+    but->Resize(100, 25);
+    but->Move(25, 242);
+    but->SetToolTipText("Move telescope to a Zd/Az position and start tracking.");
     but->Associate(this);
     fList->Add(but);
 
+    but= new TGTextButton(tf2, "Calc Zd/Az",  kPB_CALCALTAZ);
+    but->Resize(80, 25);
+    but->Move(165, 197);
+    but->SetToolTipText("Calculate Zd/Az corresponding to Ra/Dec.");
+    but->Associate(this);
+    fList->Add(but);
+
+#ifdef EXPERT
     but = new TGTextButton(tf5, "Display", kPB_DISPLAY1);
     but->Resize(80, 25);
@@ -486,18 +466,5 @@
     fList->Add(but);
 
-    but= new TGTextButton(tf2, "Calc Zd/Az",  kPB_CALCALTAZ);
-    but->Resize(80, 25);
-    but->Move(165, 197);
-    but->SetToolTipText("Calculate Zd/Az corresponding to Ra/Dec.");
-    but->Associate(this);
-    fList->Add(but);
-
-    but = new TGTextButton(tf4, "TPoint", kPB_TPOINT);
-    but->Resize(80, 25);
-    but->Move(25, 190);
-    but->SetToolTipText("Trigger writing a tpoint coordinate pair.");
-    but->Associate(this);
-    fList->Add(but);
-
+    /*
     but = new TGTextButton(tf4, "Calib SE", kPB_CALIBSE);
     but->Resize(80, 25);
@@ -506,8 +473,8 @@
     but->Associate(this);
     fList->Add(but);
-
+    */
     but = new TGTextButton(tf4, "Load", kPB_LoadBending);
     but->Resize(80, 25);
-    but->Move(25, 220);
+    but->Move(25, 190);
     but->SetToolTipText("Load bending corrections from file 'bending.txt'");
     but->Associate(this);
@@ -516,8 +483,16 @@
     but = new TGTextButton(tf4, "Reset", kPB_ResetBending);
     but->Resize(80, 25);
-    but->Move(25, 250);
+    but->Move(25, 220);
     but->SetToolTipText("Reset bending correction (coefficients=0)");
     but->Associate(this);
     fList->Add(but);
+
+    but = new TGTextButton(tf4, "TPoint", kPB_TPOINT);
+    but->Resize(80, 25);
+    but->Move(25, 250);
+    but->SetToolTipText("Trigger writing a tpoint coordinate pair.");
+    but->Associate(this);
+    fList->Add(but);
+#endif
 
     ULong_t color;
@@ -958,4 +933,7 @@
                     ZdAz soll, UInt_t stat, UInt_t stat2)
 {
+    // acc [rad]
+    acc *= kRad2Deg;   // [deg]
+
     double mjd = UpdateTime();
     fSkyPosition->Update(pos, mjd);
@@ -984,5 +962,5 @@
     SetLabelColor(fAvailMac1, stat2&0x01);
     SetLabelColor(fAvailMac2, stat2&0x02);
-    SetLabelColor(fAvailMac3, stat2&0x04);
+    //SetLabelColor(fAvailMac3, stat2&0x04);
     SetLabelColor(fAvailSe1,  stat2&0x08);
     SetLabelColor(fAvailSe2,  stat2&0x10);
@@ -1033,4 +1011,19 @@
 }
 
+void MGCosy::StartTrackPos()
+{
+    cout << "Start tracking Zd/Az pos." << endl;
+
+    const XY &xy = fCZdAz->GetCoordinates();
+
+    ZdAz za(xy.X(), xy.Y());
+
+    cout << za.Zd() << "\xb0 " << za.Az() << "\xb0" << endl;
+
+    fQueue->PostMsg(WM_TRACKPOS, &za, sizeof(za));
+
+    cout << "PostMsg (WM_TrackPos) returned." << endl;
+}
+
 void MGCosy::StartPos()
 {
@@ -1050,4 +1043,5 @@
 // ************************** For demo purpose **********************
 //
+/*
 #include <TRandom.h>
 class MDemo : public MThread
@@ -1068,29 +1062,15 @@
             Timer tm;
             tm.Now();
-            /*
-            Float_t h = 2.+tm.H()+(10.7+tm.M())/60.;
-            RaDec dest(h*15, 129.7);
-
-            cout << dest.Ra()/15 << "h " << dest.Dec() << "°" << endl;
-
-            fQueue->PostMsg(WM_TRACK, &dest, sizeof(dest));
+
+            //fQueue->PostMsg(WM_STOP, 0, 0);
+
+            ZdAz dest1((float)fRand.Integer(120)-60., 0);//fRand.Integer(25)+90);
+
+            cout << "Demo: Zd=" << dest1.Zd() << "° Az=" << dest1.Az() << "°" << endl;
+
+            fQueue->PostMsg(WM_POSITION, &dest1, sizeof(dest1));
 
             int i = 0;
-            while (!HasStopFlag() && i++<60)  // 2.5min
-            usleep(1000000);
-            if (HasStopFlag())
-                break;
-            */
-
-            //fQueue->PostMsg(WM_STOP, 0, 0);
-
-            ZdAz dest1((float)fRand.Integer(120)-60., 0);//fRand.Integer(25)+90);
-
-            cout << "Demo: Zd=" << dest1.Zd() << "° Az=" << dest1.Az() << "°" << endl;
-
-            fQueue->PostMsg(WM_POSITION, &dest1, sizeof(dest1));
-
-            int i = 0;
-            while (!HasStopFlag() && i++<5/*5*/)  // 30s
+            while (!HasStopFlag() && i++<5)  // 30s
                 usleep(1000000);
             if (HasStopFlag())
@@ -1099,9 +1079,4 @@
             //ZdAz dest2(5, 30);
             //fQueue->PostMsg(WM_POSITION, &dest2, sizeof(dest2));
-            /*
-            i = 0;
-            while (!HasStopFlag() && i++<30)  // 30s
-                usleep(1000000);
-            */
             //if (HasStopFlag())
             //    break;
@@ -1129,5 +1104,6 @@
     demo.Stop();
 }
-
+*/
+/*
 void MGCosy::StartCalib()
 {
@@ -1141,5 +1117,5 @@
     cout << "PostMsg (WM_Calib) returned." << endl;
 }
-
+*/
 void MGCosy::StartTPoint()
 {
@@ -1213,7 +1189,9 @@
                     fQueue->Proc(WM_GEAR, (void*)1);
                     return kTRUE;
+                    /*
                 case 5:
                     StartDemo();
                     return kTRUE;
+                    */
                 }
                 return kTRUE;
@@ -1221,5 +1199,5 @@
             case kPB_STOP:
                 cout << "Sending stop movement msg." << endl;
-                StopDemo();
+                //StopDemo();
                 fQueue->PostMsg(WM_STOP, 0, 0);
                 if (fTab->GetCurrent()==3)
@@ -1270,7 +1248,9 @@
                 StartTPoint();
                 return kTRUE;
+                /*
             case kPB_CALIBSE:
                 StartCalib();
                 return kTRUE;
+                */
             case kPB_LoadBending:
                 fQueue->Proc(WM_LOADBENDING, NULL);
@@ -1279,6 +1259,11 @@
                 fQueue->Proc(WM_RESETBENDING, NULL);
                 return kTRUE;
+                /*
             case kPB_HOME:
                 fQueue->PostMsg(WM_HOME, 0, 0);
+                return kTRUE;
+                */
+            case kPB_TRACKPOS:
+                StartTrackPos();
                 return kTRUE;
             case kPB_DISPLAY1:
Index: trunk/MagicSoft/Cosy/gui/MGCosy.h
===================================================================
--- trunk/MagicSoft/Cosy/gui/MGCosy.h	(revision 1882)
+++ trunk/MagicSoft/Cosy/gui/MGCosy.h	(revision 1953)
@@ -49,7 +49,7 @@
     MGList        *fList;
 
-    TGLabel      **fLabel1;
-    TGLabel      **fLabel2;
-    TGLabel      **fLabel3;
+    TGLabel       *fLabel1;
+    TGLabel       *fLabel2;
+    TGLabel       *fLabel3;
 
     TGLabel       *fOffsetZd;
@@ -104,6 +104,7 @@
     void StartPos();
     void StartTrack();
+    void StartTrackPos();
     void StartDemo();
-    void StartCalib();
+    //void StartCalib();
     void StartTPoint();
 
@@ -121,7 +122,7 @@
     void CloseWindow();
 
-    TGLabel **GetLabel1() const { return fLabel1; }
-    TGLabel **GetLabel2() const { return fLabel2; }
-    TGLabel **GetLabel3() const { return fLabel3; }
+    TGLabel *GetLabel1() const { return fLabel1; }
+    TGLabel *GetLabel2() const { return fLabel2; }
+    TGLabel *GetLabel3() const { return fLabel3; }
 
     TGListBox *GetLog() const { return fLog; }
Index: trunk/MagicSoft/Cosy/gui/MGStarguider.cc
===================================================================
--- trunk/MagicSoft/Cosy/gui/MGStarguider.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/gui/MGStarguider.cc	(revision 1953)
@@ -74,4 +74,5 @@
     IDM_kLimMag9,
     IDM_kPixSize,
+    IDM_kAngle,
     IDM_kInterpol125,
     IDM_kInterpol25,
@@ -217,4 +218,5 @@
     fWriteType->AddEntry("&Once",      IDM_kOnce);
     fWriteType->AddEntry("&Continous", IDM_kContinous);
+    fWriteType->CheckEntry(IDM_kOnce);
     fWriteType->Associate(this);
     fList->Add(fWriteType);
@@ -334,5 +336,11 @@
     l = new TGLabel(this, "arcsec/pix");
     l->SetTextJustify(kTextLeft);
-    l->Move(605, fMenu->GetDefaultHeight()+619);
+    l->Move(605, fMenu->GetDefaultHeight()+619+13);
+    AddFrame(l);
+    fList->Add(l);
+
+    l = new TGLabel(this, "deg");
+    l->SetTextJustify(kTextLeft);
+    l->Move(605, fMenu->GetDefaultHeight()+619-10);
     AddFrame(l);
     fList->Add(l);
@@ -347,4 +355,5 @@
 
     fSao->SetPixSize(pixsize/3600);
+    fSao->SetRotationAngle(0);
 
     TString txt;
@@ -353,7 +362,13 @@
     fPixSize = new TGTextEntry(this, txt, IDM_kPixSize);
     fPixSize->SetAlignment(kTextCenterX);
-    fPixSize->Move(547, fMenu->GetDefaultHeight()+617);
+    fPixSize->Move(547, fMenu->GetDefaultHeight()+617+13);
     AddFrame(fPixSize);
     fList->Add(fPixSize);
+
+    fAngle = new TGTextEntry(this, "         0", IDM_kAngle);
+    fAngle->SetAlignment(kTextCenterX);
+    fAngle->Move(547, fMenu->GetDefaultHeight()+617-10);
+    AddFrame(fAngle);
+    fList->Add(fAngle);
 
     // TGHorizontal3DLine *fLineSep = new TGHorizontal3DLine(this);
@@ -582,9 +597,21 @@
     case kC_TEXTENTRY:
         if (GET_SUBMSG(msg)==kTE_ENTER)
-        {
-            const Float_t pixsize = atof(fPixSize->GetText());
-            cout << "Pixel Size changed to " << pixsize << "\"/pix" << endl;
-            fSao->SetPixSize(pixsize/3600);
-        }
+            switch (mp1)
+            {
+            case IDM_kPixSize:
+                {
+                    const Float_t pixsize = atof(fPixSize->GetText());
+                    cout << "Pixel Size changed to " << pixsize << "\"/pix" << endl;
+                    fSao->SetPixSize(pixsize/3600);
+                    return kTRUE;
+                }
+            case IDM_kAngle:
+                {
+                    const Float_t angle = atof(fAngle->GetText());
+                    cout << "Rotation Angle changed to " << angle << "deg" << endl;
+                    fSao->SetRotationAngle(angle);
+                    return kTRUE;
+                }
+            }
         return kTRUE;
 
@@ -896,9 +923,11 @@
     AltAz pos1 = fSao->CalcAltAzFromPix(768/2+g.GetMean(1), 576/2+g.GetMean(2))*kRad2Deg;
 
+    ofstream fout1("pointingpos.txt");
+    fout1 << setprecision(10) << fSao->GetMjd()-52000 << " " << -pos1.Alt() << " " << pos1.Az() << endl;
+
     pos1 -= pos0;
 
-    ofstream fout("tracking_error.txt");
-    fout << setprecision(10) << fSao->GetMjd()-52000 << " " << -pos1.Alt() << " " << pos1.Az() << endl;
-    fout.close();
+    ofstream fout2("tracking_error.txt");
+    fout2 << setprecision(10) << fSao->GetMjd()-52000 << " " << -pos1.Alt() << " " << pos1.Az() << endl;
 
     return ZdAz(-pos1.Alt(), pos1.Az());
@@ -1172,5 +1201,9 @@
     fDy = dy;
 
-    cout << "New coordinates for zoom: " << fDx << " " << fDy << endl;
+    fSao->Now();
+    AltAz aa = fSao->CalcAltAzFromPix(fDx, fDy)*kRad2Deg;
+
+    cout << "New coordinates for zoom - x, y   : " << fDx << " " << fDy << endl;
+    cout << "New coordinates for zoom - Alt, az: " << aa.Alt() << " " << aa.Az() << endl;
 
     return kTRUE;
Index: trunk/MagicSoft/Cosy/gui/MGStarguider.h
===================================================================
--- trunk/MagicSoft/Cosy/gui/MGStarguider.h	(revision 1882)
+++ trunk/MagicSoft/Cosy/gui/MGStarguider.h	(revision 1953)
@@ -89,4 +89,5 @@
 
     TGTextEntry   *fPixSize;
+    TGTextEntry   *fAngle;
 
     StarCatalog   *fSao;
Index: trunk/MagicSoft/Cosy/main/MBending.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MBending.h	(revision 1882)
+++ trunk/MagicSoft/Cosy/main/MBending.h	(revision 1953)
@@ -1,8 +1,4 @@
 #ifndef COSY_MBending
 #define COSY_MBending
-
-#include <TROOT.h>
-
-#include <fstream.h>
 
 #include "coord.h"
@@ -53,4 +49,5 @@
 public:
     MBending() { Clear(); }
+    MBending(const char *name) { Clear(); Load(name); }
 
     void Load(const char *name);
Index: trunk/MagicSoft/Cosy/main/MCosy.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 1953)
@@ -13,4 +13,5 @@
 
 #include <TH2.h>
+#include <TH3.h>
 #include <TProfile.h>
 #include <TCanvas.h>
@@ -76,4 +77,5 @@
 }
 
+/*
 ZdAz MCosy::CorrectTarget(const ZdAz &src, const ZdAz &dst)
 {
@@ -145,4 +147,5 @@
     return ret*(16384.0/360.0);
 }
+*/
 
 // --------------------------------------------------------------------------
@@ -329,10 +332,19 @@
 {
     // FIXME, what when waiting times out (Zombie)
+
     while ((fMac1->IsPositioning() || fMac2->IsPositioning()) &&
-           !(Break() || HasError()) && !HasZombie())
+           !(Break() || HasError() || HasZombie()))
         usleep(1);
 
     if (Break() || HasError() || HasZombie())
-        lout << "WaitForEndMovement aborted..." << endl;
+    {
+        lout << "WaitForEndMovement aborted... ";
+        if (Break())
+            lout << "Break signal." << endl;
+        if (HasError())
+            lout << "Network has error." << endl;
+        if (HasZombie())
+            lout << "Network has zombie." << endl;
+    }
 }
 
@@ -382,4 +394,25 @@
 }
 
+Bool_t MCosy::CheckRange(const ZdAz &d) const
+{
+    // d [deg]
+
+    if (d.Zd()<fMin.Zd() || d.Zd()>fMax.Zd())
+    {
+        lout << "ERROR: Requested Zenith Angle (" << d.Zd() << "deg) not ";
+        lout << "inside allowed range." << endl;
+        return kFALSE;
+    }
+
+    if (d.Az()<fMin.Az() || d.Az()>fMax.Az())
+    {
+        lout << "ERROR: Requested Azimuth Angle (" << d.Az() << "deg) not ";
+        lout << "inside allowed range." << endl;
+        return kFALSE;
+    }
+
+    return kTRUE;
+}
+
 // --------------------------------------------------------------------------
 //
@@ -396,16 +429,4 @@
 {
     // FIXME: Correct by fOffset ?
-/*
-    ZdAz
-
-    if (dst.Az()<-TMath::Pi()/2)
-        dst.Az(dst.Az() + TMath::Pi()*2);
-
-    if (dst.Az()>3*TMath::Pi()/2)
-        dst.Az(dst.Az() -TMath::Pi()*2);
-*/
- //   track=kFALSE;
-
-   // const ZdAz d1 = track ? CorrectTarget(src, dst) : dst*16384/TMath::Pi()/2; // [se]
 
     const ZdAz d = dst*kRad2Deg;
@@ -413,17 +434,6 @@
     lout << "Target Position: " << d.Zd() << "deg, " << d.Az() << "deg (Zd/Az)" << endl;
 
-    if (d.Zd()<fMin.Zd() || d.Zd()>fMax.Zd())
-    {
-        lout << "ERROR: Requested Zenith Angle (" << d.Zd() << "deg) not ";
-        lout << "inside allowed range." << endl;
+    if (!CheckRange(d))
         return kFALSE;
-    }
-
-    if (d.Az()<fMin.Az() || d.Az()>fMax.Az())
-    {
-        lout << "ERROR: Requested Azimuth Angle (" << d.Az() << "deg) not ";
-        lout << "inside allowed range." << endl;
-        return kFALSE;
-    }
 
     //
@@ -520,4 +530,5 @@
         cout << "Estimated1: " << 16384*dx + dk << endl;
         cout << "Estimated2: " << (16384*dx + dk)/1.085949688 << endl;
+        cout << "Estimated3: " << rd.Zd()*432 << endl;
         // ===========================================
 
@@ -775,4 +786,41 @@
 }
 
+Bool_t MCosy::AlignTrackingPos(ZdAz pointing, ZdAz &za)
+{
+    // pointing [deg]
+
+    if (pointing.Zd()<0)
+    {
+        pointing.Zd(-pointing.Zd());
+        pointing.Az(pointing.Az()+180);
+    }
+
+    const ZdAz se = GetSePos()*360/16384; // [deg]
+    const ZdAz unbendedse = fBending.CorrectBack(se); // ist pointing
+
+    do
+    {
+        const Double_t d = unbendedse.Az() - pointing.Az();
+        if (d>-180 && d<=180)
+            break;
+
+        pointing.Az(pointing.Az()+TMath::Sign(360., -d));
+    } while (1);
+  /*
+    while (pointing.Az()<fMin.Az())
+        pointing.Az(pointing.Az() + 360);
+
+    while (pointing.Az()>fMax.Az())
+        pointing.Az(pointing.Az() - 360);
+        */
+    const Bool_t rc = CheckRange(pointing);
+    za = fBending(pointing/kRad2Deg); // [rad]
+
+    if (!rc)
+        lout << "Error: Aligned position out of Range." << endl;
+
+    return rc;
+}
+
 void MCosy::TrackPosition(const RaDec &dst) // ra, dec [rad]
 {
@@ -787,9 +835,16 @@
     ZdAz dest = sla.CalcZdAz(dst);
 
+    // FIXME: Determin tracking start point by star culmination
     if (dest.Az()<-TMath::Pi()/2)
+    {
+        lout << "Adding 360deg to Azimuth " << dest.Az()*kRad2Deg << endl;
         dest.Az(dest.Az() + TMath::Pi()*2);
+    }
 
     if (dest.Az()>3*TMath::Pi()/2)
+    {
+        lout << "Substracting 360deg to Azimuth " << dest.Az()*kRad2Deg << endl;
         dest.Az(dest.Az() -TMath::Pi()*2);
+    }
 
     if (!SetPosition(dest, kTRUE))
@@ -849,6 +904,6 @@
     //
     // *OLD*const float dt = 1;  // 1 second
-    const float dt = 3;  // 2 second
-    while (!(Break() || HasError()) && !HasZombie())
+    const float dt = 5;//3;  // 2 second
+    while (!(Break() || HasError() || HasZombie()))
     {
         //
@@ -860,5 +915,14 @@
         // Request theoretical Position for a time in the future (To+dt) from CPU
         //
-        sla.SetMjd(sla.GetMjd()+dt/(60*60*24));
+        const Double_t mjd = sla.GetMjd()+dt/(60*60*24);
+        const ZdAz pointing = sla.CalcZdAz(fRaDec, mjd)*kRad2Deg; // soll pointing [deg]
+
+        ZdAz dest;
+        if (!AlignTrackingPos(pointing, dest))
+            break;
+
+        dest *= 16384/TMath::Pi()/2;  // [se]
+
+        /*
         ZdAz dummy1 = sla.CalcZdAz(fRaDec);
 
@@ -868,27 +932,20 @@
             dummy1.Az(dummy1.Az() -TMath::Pi()*2);
 
+        if (!CheckRange(dummy1*kRad2Deg))
+        {
+            lout << "Error: Tracking position out of Range." << endl;
+            break;
+        }
+
         ZdAz dummy = fBending(dummy1);
         dest = CorrectTarget(GetSePos(), dummy); // [se]
-
-        const ZdAz d = dest*360./16384; // [deg]
+        */
+
+//*LP*      const ZdAz d = dest*360./16384; // [deg]
         dest *= kGearRatio;  // [re]
 
-        ZdAz min = fBending(fMin/kRad2Deg)*kRad2Deg;
-        ZdAz max = fBending(fMax/kRad2Deg)*kRad2Deg;
-
-        /*
-        if (d.Zd()<min.Zd() || d.Az()<min.Az())
-        {
-            lout << "ERROR: Calculated position for T+dt not inside allowed range." << endl;
-            lout << "<  " << d.Zd() << " " << min.Zd() << "  " << d.Az() << " " << min.Az()  << endl;
-            break;
-        }
-        if (d.Zd()>max.Zd() || d.Az()>max.Az())
-        {
-            lout << "ERROR: Calculated position for T+dt not inside allowed range." << endl;
-            lout << ">  " <<  d.Zd() << " " << max.Zd() << "  " << d.Az() << " " << max.Az()  << endl;
-            break;
-        }
-        */
+//*LP*      ZdAz min = fBending(fMin/kRad2Deg)*kRad2Deg;
+//*LP*      ZdAz max = fBending(fMax/kRad2Deg)*kRad2Deg;
+
         ZdAz vcalc = sla.GetApproxVel(fRaDec) * kGearRatio2*4./60.;  // [re/min]
 
@@ -963,5 +1020,5 @@
         // (This is important on fast machines >500MHz)
         //
-        usleep(50000); // 0.25s
+        usleep(1000000); // 1s
         //usleep(50000); // 0.05s
     }
@@ -989,5 +1046,5 @@
     // set deceleration to 50%
     //
-    cout << "Stopping movement (dec=20%)..." << endl;
+    cout << "Stopping movement (dec=30%)..." << endl;
 
     fMac1->SetDeceleration(0.3*fMac1->GetVelRes());
@@ -1103,5 +1160,5 @@
         StopMovement();
         return NULL;
-
+/*
     case WM_PRESET:
         cout << "WM_Preset: start." << endl;
@@ -1113,5 +1170,6 @@
         cout << "WM_Preset: done. (return 0xaffe)" << endl;
         return (void*)0xaffe;
-
+*/
+        /*
     case WM_CALIB:
         {
@@ -1126,5 +1184,5 @@
 
             //RaDec rd(37.94, 89.2644);      // POLARIS
-            //RaDec rd(213.915417, 19.1825); // ACTURUS
+            //RaDec rd(213.915417, 19.1825); // ARCTURUS
 
             cout << "Calibrating to: " << rd.Ra()*24/360 << "h " << rd.Dec() << "°" << endl;
@@ -1144,4 +1202,5 @@
         }
         return (void*)0xaffe;
+        */
 
     case WM_TPOINT:
@@ -1170,4 +1229,25 @@
         }
         return (void*)0xca1b;
+
+    case WM_TRACKPOS:
+        cout << "WM_TrackPosition: start." << endl;
+        {
+            if (!CheckNetwork())
+                return (void*)0xebb0;
+
+            ZdAz dest = *((ZdAz*)mp) * kDeg2Rad;
+            //if (!SetPosition(dest))
+            //    return (void*)0x1234;
+
+            SlaStars sla(fObservatory);
+            sla.Now();
+
+            RaDec rd = sla.CalcRaDec(dest);
+            cout << dest.Zd()*180/3.1415 << " " << dest.Az()*180/3.1415 << endl;
+            cout << rd.Ra()*12/3.1415 << " " << rd.Dec()*180/3.1415 << endl;
+            TrackPosition(rd);
+        }
+        cout << "WM_TrackPosition: done. (return 0xabcd)" << endl;
+        return (void*)0xabcd;
 
     case WM_POSITION:
@@ -1273,15 +1353,18 @@
             RaDec rd(xy.X()*15., xy.Y()); // [deg]
 
-            ZdAz a0 = sla.CalcZdAz(rd*kDeg2Rad);
-            ZdAz a1 = fBending(a0);
-            ZdAz se = CorrectTarget(GetSePos(), a1);
-            a0 *= kRad2Deg;
-            a1 *= kRad2Deg;
-            ZdAz a2 = a1*16384/360;
+            const ZdAz a0 = sla.CalcZdAz(rd*kDeg2Rad);
+
+            ZdAz a1;
+            AlignTrackingPos(a0, a1);
+            a1 *= 180/TMath::Pi();
+
+            const ZdAz a2 = a1*16384/360;
+            const ZdAz se = a0*16384/360;
+
             cout << "Ra/Dec source: " << xy.X()  << "h " << xy.Y()  << "°" << endl;
-            cout << "Zd/Az source:  " << a0.Zd() << "° " << a0.Az() << "°" << endl;
+            cout << "Zd/Az target:  " << a0.Zd() << "° " << a0.Az() << "°" << endl;
             cout << "Zd/Az bended:  " << a1.Zd() << "° " << a1.Az() << "°" << endl;
+            cout << "SE target:     " << se.Zd() << "  " << se.Az() << endl;
             cout << "SE bended:     " << a2.Zd() << "  " << a2.Az() << endl;
-            cout << "SE target:     " << se.Zd() << "  " << se.Az() << endl;
         }
         return (void*)0xa17a;
@@ -1401,13 +1484,14 @@
 
     SlaStars sla(fObservatory);
+    sla.Now();
 
     ZdAz old;
-    ZdAz ist;
-
-    ZdAz sollzd;
-    ZdAz sollaz;
-
-    ZdAz istre = -fOffset;                // [re]
+    ZdAz ist = GetSePos();              // [se]
+
+//    ZdAz istre = -fOffset;              // [re]
     ZdAz time;
+
+    ZdAz sollzd = sla.CalcZdAz(fRaDec); // [rad]
+    ZdAz sollaz = sollzd;               // [rad]
 
     //
@@ -1456,5 +1540,5 @@
          continue;
          */
-        istre = GetRePosPdo();
+        ZdAz istre = GetRePosPdo();
 
         //
@@ -1479,9 +1563,9 @@
         if (phca1 || phca2 /*(int)ist.Zd() != (int)old.Zd()*/)
         {
-            sla.SetMjd(time.Zd());
-
+            sollzd = sla.CalcZdAz(fRaDec, time.Zd()); // [rad]
+            /*
             ZdAz dummy = fBending(sla.CalcZdAz(fRaDec));
             sollzd = CorrectTarget(ist, dummy); // [se]
-
+            */
             fOffset.Zd(fOffset.Zd()*(1.-weight)+(ist.Zd()*kGearRatio.X()-istre.Zd())*weight);
         }
@@ -1489,17 +1573,36 @@
         if (phcaz /*(int)ist.Az() != (int)old.Az()*/)
         {
-            sla.SetMjd(time.Az());
-
+            sollaz = sla.CalcZdAz(fRaDec, time.Az()); // [rad]
+            /*
             ZdAz dummy = fBending(sla.CalcZdAz(fRaDec));
             sollaz = CorrectTarget(ist, dummy); // [se]
-
+            */
             fOffset.Az(fOffset.Az()*(1.-weight)+(ist.Az()*kGearRatio.Y()-istre.Az())*weight);
         }
 
-        ZdAz soll(sollzd.Zd(), sollaz.Az()); // [se]
+        ZdAz soll(sollzd.Zd(), sollaz.Az()); // [rad]
+
+        AlignTrackingPos(soll*360/16384, fZdAzSoll);
+        /*
         fZdAzSoll = fBending.CorrectBack(soll*2*TMath::Pi()/16384);
 
-        fTrackingError.Set((ist.Zd()-sollzd.Zd())*kGearRatio.X(),
-                           (ist.Az()-sollaz.Az())*kGearRatio.Y());
+        // FIXME?
+        if (fZdAzSoll.Az()<-TMath::Pi()/2)
+            fZdAzSoll.Az(fZdAzSoll.Az() + TMath::Pi()*2);
+        if (fZdAzSoll.Az()>3*TMath::Pi()/2)
+            fZdAzSoll.Az(fZdAzSoll.Az() -TMath::Pi()*2);
+
+        // FIXME?
+        while (ist.Az()>3*16384/4/2)
+            ist.Az(ist.Az() - 16384);
+        while (soll.Az()>3*16384/4/2)
+            soll.Az(soll.Az() - 16384);
+        while (ist.Az()<-16384/4)
+            ist.Az(ist.Az() + 16384);
+        while (soll.Az()<-16384/4)
+            soll.Az(soll.Az() + 16384);
+            */
+        ist *= TMath::Pi()*2/16384;
+        fTrackingError.Set(ist.Zd()-fZdAzSoll.Zd(), ist.Az()-fZdAzSoll.Az());
 
         //---            fout << setprecision(15) << setw(17) << time.Zd()*60.*60.*24. << " ";
@@ -1586,10 +1689,12 @@
     }
 
-    //    fHist = new TH2F("Gear", "Gear Ratio Re/Se",
-    //                     201, fMin.Zd(), fMax.Zd(), 61, 349.5, 500.5);
-    fHist = new TH2F("Gear", "Gear Ratio Re/Se",
-                     201, fMin.Az(), fMax.Az(), 61, 419.5, 570.5);
-    fHist->SetXTitle("ZA [\\circ]");
-    fHist->SetYTitle("Re/Se");
+    fHist = new TH3F("Gear", "Gear Ratio Re/Se",
+                     (int)((fMax.Zd()-fMin.Zd())/2.5+1), fMin.Zd(), fMax.Zd(),
+                     (int)((fMax.Az()-fMin.Az())/2.5+1), fMin.Az(), fMax.Az(),
+                     61, 349.5, 500.5);
+
+    fHist->SetXTitle("Zd [\\circ]");
+    fHist->SetYTitle("Az [\\circ]");
+    fHist->SetZTitle("Re/Se");
 
     lout << "Starting Gear determination..." << endl;
@@ -1614,5 +1719,5 @@
         ZdAz dre = re-re0;
 
-        if (fabs(dse.Zd())*144>16384) // Each 2.5deg
+        if (fabs(dse.Zd())*144>16384) // Each 2.5deg (144)
         {
             se0.Zd(se.Zd());
@@ -1620,8 +1725,8 @@
 
             ZdAz bend = fBending.CorrectBack(se*2*TMath::Pi()/16384)*kRad2Deg;
-            fHist->Fill(bend.Zd(), dre.Zd()/dse.Zd());
+            ((TH3*)fHist)->Fill(bend.Zd(), bend.Az(), dre.Zd()/dse.Zd());
         }
 
-        if (fabs(dse.Az())*144>16384) // Each 2.5deg
+        if (fabs(dse.Az())*144>16384) // Each 2.5deg (144)
         {
             se0.Az(se.Az());
@@ -1629,34 +1734,6 @@
 
             ZdAz bend = fBending.CorrectBack(se*2*TMath::Pi()/16384)*kRad2Deg;
-            fHist->Fill(bend.Az(), dre.Az()/dse.Az());
-
-            cout << bend.Az() << ": " << dre.Az()/dse.Az() << endl;
+            ((TH3*)fHist)->Fill(bend.Az(), bend.Az(), dre.Az()/dse.Az());
         }
-
-        /*
-        const Double_t pos[3] = {
-            (fZd1->GetPos()+8192)%16384,
-            (fZd2->GetPos()+8192)%16384,
-            fAz->GetPos() };
-
-        //
-        //  Estimate Offset from the first ten positions
-        //
-        if (cnt++<10)
-        {
-            offset += pos[0]+pos[1];
-            continue;
-        }
-        if (cnt==11)
-        {
-            offset /= 10;
-            cnt++;
-        }
-
-        Double_t apos = (pos[0]-pos[1])/2 * TMath::Pi()*2 / 16384;
-
-        ZdAz bend = fBending.CorrectBack(ZdAz(apos, pos[2]))*kRad2Deg;
-        fHistTestSe->Fill(bend.Zd(), pos[0]+pos[1]-offset);
-*/
     }
     lout << "Gear Test Stopped... displaying Histogram." << endl;
@@ -1748,12 +1825,6 @@
     lout.UpdateGui();
 
-    fWin->Update(bendist*(360.0/2/TMath::Pi()), fTrackingError/kGearRatio2,
+    fWin->Update(bendist*(360.0/2/TMath::Pi()), fTrackingError,
                  fVelocity, fOffset, fRaDec, fZdAzSoll, fStatus, avail);
-
-    /*
-     cout << (int)(fMac1->GetStatus()&Macs::kOutOfControl) << " ";
-     cout << (int)(fMac2->GetStatus()&Macs::kOutOfControl) << " ";
-     cout << (int)(fMac3->GetStatus()&Macs::kOutOfControl) << endl;
-     */
 
     const Bool_t trigger = fTriggerDisplay;
@@ -1773,5 +1844,5 @@
     lout << "Displaying histogram..." << endl;
 
-    TH2F &hist = *fHist;
+    TH2F &hist = *(TH2F*)fHist;
 
     if (del)
@@ -1796,5 +1867,5 @@
     TH1F p2("spread", "Spread of the differences", hist.GetNbinsX(), hist.GetBinLowEdge(1),
             hist.GetBinLowEdge(hist.GetNbinsX()+1));
-    p2.SetXTitle("ZA [\\circ]");
+    p2.SetXTitle("Zd [\\circ]");
     for (int i=0; i<hist.GetNbinsX(); i++)
         p2.SetBinError(i, p->GetBinError(i));
@@ -1811,5 +1882,5 @@
     lout << "Displaying histogram..." << endl;
 
-    TH2F &hist = *fHist;
+    TH3F &hist = *(TH3F*)fHist;
 
     if (del)
@@ -1820,24 +1891,65 @@
 
     TCanvas *c=new TCanvas("c1", "", 1000, 1000);
-    c->Divide(1,2);
+    c->Divide(2,2);
+
+    // ----------
 
     c->cd(1);
-    TH2 *h=(TH2*)hist.DrawCopy();
-
-    TProfile *p = h->ProfileX("_pfx", -1, 9999, "s");
-    p->SetLineColor(kBlue);
-    p->Draw("same");
-    p->SetBit(kCanDelete);
+    TH2D &h1=*(TH2D*)hist.Project3D("zx"); // Zd
+    h1.SetTitle(" Gear Ratio Zenith Distance [re/se]  ");
+    h1.SetXTitle("Zd [\\circ]");
+    h1.Draw();
+    h1.SetBit(kCanDelete);
+
+    TProfile *p1 = h1.ProfileX("_pfx", -1, 9999, "s");
+    p1->SetLineColor(kBlue);
+    p1->Draw("same");
+    p1->SetBit(kCanDelete);
+
+    // ----------
 
     c->cd(2);
-
-    TH1F p2("spread", "Spread of the gear [16384/1500/4*U/U]", hist.GetNbinsX(), hist.GetBinLowEdge(1),
-            hist.GetBinLowEdge(hist.GetNbinsX()+1));
-    p2.SetXTitle("ZA [\\circ]");
-    for (int i=0; i<hist.GetNbinsX(); i++)
-        p2.SetBinError(i, p->GetBinError(i));
-    p2.SetLineColor(kRed);
-    p2.SetStats(0);
-    p2.DrawCopy();
+    TH2D &h2=*(TH2D*)hist.Project3D("zy"); // Az
+    h2.SetTitle(" Gear Ratio Azimuth [re/se]  ");
+    h2.SetXTitle("Zd [\\circ]");
+    h2.Draw();
+    h2.SetBit(kCanDelete);
+
+    TProfile *p2 = h2.ProfileX("_pfx", -1, 9999, "s");
+    p2->SetLineColor(kBlue);
+    p2->Draw("same");
+    p2->SetBit(kCanDelete);
+
+    // ----------
+
+    c->cd(3);
+
+    TAxis &axe1 = *h1.GetXaxis();
+
+    TH1F f1("spreadzd", " Spread Zenith Distance ",
+            axe1.GetNbins(), axe1.GetXmin(), axe1.GetXmax());
+    f1.SetXTitle("Zd [\\circ]");
+    for (int i=0; i<axe1.GetNbins(); i++)
+        f1.SetBinError(i, p1->GetBinError(i));
+    f1.SetLineColor(kRed);
+    f1.SetStats(0);
+    f1.DrawCopy();
+
+    c->cd(4);
+
+    // ----------
+
+    TAxis &axe2 = *h2.GetXaxis();
+
+    TH1F f2("spreadaz", " Spread Azimuth ",
+            axe2.GetNbins(), axe2.GetXmin(), axe2.GetXmax());
+    f2.SetXTitle("Az [\\circ]");
+    for (int i=0; i<axe2.GetNbins(); i++)
+        f2.SetBinError(i, p2->GetBinError(i));
+    f2.SetLineColor(kRed);
+    f2.SetStats(0);
+    f2.DrawCopy();
+
+    // ----------
 
     if (del)
Index: trunk/MagicSoft/Cosy/main/MCosy.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MCosy.h	(revision 1882)
+++ trunk/MagicSoft/Cosy/main/MCosy.h	(revision 1953)
@@ -15,9 +15,9 @@
 
 #define WM_WAIT         WM_NULL
-#define WM_PRESET       0x1000
+//#define WM_PRESET       0x1000
 #define WM_POSITION     0x1001
 #define WM_TRACK        0x1002
 #define WM_STOP         0x1003
-#define WM_CALIB        0x1004
+//#define WM_CALIB        0x1004
 #define WM_TPOINT       0x1005
 #define WM_NEWTRACK     0x1006
@@ -29,4 +29,5 @@
 #define WM_GEAR         0x100c
 #define WM_DISPLAY      0x100d
+#define WM_TRACKPOS     0x100e
 
 class ShaftEncoder;
@@ -34,5 +35,5 @@
 class MGCosy;
 class MCosy;
-class TH2F;
+class TH1;
 
 class MTTalk : public MThread
@@ -96,7 +97,7 @@
     BackgroundAction_t fBackground;    // Flag for present backgroundthread
 
-    ZdAz  fTrackingError; // Tracking Offset between SE and calc-pos [re]
+    ZdAz  fTrackingError; // [rad] Tracking Offset between SE and calc-pos
     ZdAz  fOffset;        // Offset between se and re coordinate system [re]
-    ZdAz  fZdAzSoll;      // Soll position when moving
+    ZdAz  fZdAzSoll;      // [rad] Soll position when moving
     RaDec fRaDec;         // Position to track
     ZdAz  fAccuracy;      // Actual accuracy of Tracking
@@ -105,5 +106,5 @@
     ZdAz  fMax;
 
-    TH2F  *fHist;
+    TH1  *fHist;
     Bool_t fTriggerDisplay;
 
@@ -116,4 +117,7 @@
 
     ofstream *tpout;
+
+    Bool_t AlignTrackingPos(ZdAz pointing, ZdAz &za);
+    Bool_t CheckRange(const ZdAz &d) const;
 
     double Rad2SE(double rad) const;
@@ -182,5 +186,5 @@
     Bool_t HandleTimer(TTimer *t);
 
-    static ZdAz CorrectTarget(const ZdAz &src, const ZdAz &dst);
+    // static ZdAz CorrectTarget(const ZdAz &src, const ZdAz &dst);
     //    static ZdAz RaDec2ZdAz(const double mjd, const RaDec &pos, const RaDec &pm=RaDec(0,0));
 
Index: trunk/MagicSoft/Cosy/prepos_magic.txt
===================================================================
--- trunk/MagicSoft/Cosy/prepos_magic.txt	(revision 1953)
+++ trunk/MagicSoft/Cosy/prepos_magic.txt	(revision 1953)
@@ -0,0 +1,5 @@
+Camera-Access   98.05333333  -9.846666666
+Celestial-Pole  61.23817667   0
+Park            90           -9.846666666
+Roque-Lamp      90            180
+Zenith          4             0
Index: trunk/MagicSoft/Cosy/testse.cc
===================================================================
--- trunk/MagicSoft/Cosy/testse.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/testse.cc	(revision 1953)
@@ -1,11 +1,3 @@
 #include <iostream.h>
-/*#include <iomanip.h>
-#include <fstream.h>
-
-#include <TROOT.h>
-#include <TSystem.h>
-#include <TApplication.h>
-
-*/
 
 #include "network.h"
@@ -21,10 +13,14 @@
 //    gLog.EnableOutputDevice(MLog::eStdout);
 
-    cout << "Creating Network..." << endl;
-    const int baud = 500;
+    cout << endl;
+    cout << "Usage: testse [nodeid [[speed]" << endl;
+    cout << endl;
+    const int baud = argc>2 ? atoi(argv[2]) : 500;
+    cout << "Creating Network (" << baud << "bps)..." << endl;
     Network net("/dev/dpm_00", baud);
 
-    cout << "Creating SE..." << endl;
-    const int nodeid = 6;
+    const int nodeid = argc>1 ? atoi(argv[1]) : 16;
+
+    cout << "Creating SE #" << nodeid << "..." << endl;
     ShaftEncoder se(nodeid, "SE/Zd1");
 
Index: trunk/MagicSoft/Cosy/videodev/Writer.cc
===================================================================
--- trunk/MagicSoft/Cosy/videodev/Writer.cc	(revision 1882)
+++ trunk/MagicSoft/Cosy/videodev/Writer.cc	(revision 1953)
@@ -14,10 +14,14 @@
                  struct timeval *date)
 {
+    Timer t(date);
+    TString mjd;
+    mjd += t.GetMjd()-52000;
+    mjd = mjd.Strip(TString::kBoth);
+    if (mjd.Length()<10)
+        mjd.Append('0', 10-mjd.Length());
+
     TString name = fname;
-
-    Timer t(date);
-
     name += "_";
-    name += t.GetMjd()-52000;
+    name += mjd;
     name += ".png";
 
