Index: /trunk/MagicSoft/Cosy/Changelog
===================================================================
--- /trunk/MagicSoft/Cosy/Changelog	(revision 920)
+++ /trunk/MagicSoft/Cosy/Changelog	(revision 921)
@@ -1,3 +1,47 @@
                                                                   -*-*- END -*-*-
+ 2001/08/30 - Thomas Bretz:
+ 
+   * MCosy.[h,cc]
+     - fixed some small bugs in tracking and positioning
+     - reorganized StopMovement and CheckForError
+     - added code into LimitSpeed
+     - added cosy.err
+     - rewrote MTTalk not to ba detached
+     - reorganized the code of MTTalk
+     - added StopMovement, WaitForEndMovement and IsPositioning
+     
+   * Starguider.cc:
+     - ready for new MGCoordinates-Constructor
+     
+   * base/coord.h:
+     - added pre-'-' operator
+     
+   * devdrv/shaftencoder.[h,cc]:
+     - introduced an HasChanged - flag
+     
+   * gui/MGAccuracy.cc:
+     - replaced floats by doubles in accuracy calculation,
+       gave wrong values
+       
+   * gui/MGCoordinate.[h,cc]:
+     - made a bit wider
+     - added support for hms-system
+     
+   * gui/MGCoordinates.[h,cc]:
+     - changed to use two fixed systems RaDec (hms), ZdAz(dms)
+     
+   * gui/MGCosy.cc:
+     - added new MGCoordinates
+     - moved two MGCoordinates to CreateTabs
+     - renamed CreateButton to CreateTabs
+     - made MGCoordinates a Client of the two Tabs
+     - transform Ra of RaDec MGCoordinates to Degrees.
+     - Introduced StartPos, StartTrack
+     
+   * gui/MGVelocity.cc:
+     - added displaying values of speed
+
+
+     
  2001/08/29 - Thomas Bretz:
 
Index: /trunk/MagicSoft/Cosy/MCosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/MCosy.cc	(revision 920)
+++ /trunk/MagicSoft/Cosy/MCosy.cc	(revision 921)
@@ -54,9 +54,4 @@
     return rad*kGearRatio2.Y();
 }
-
-//double Rad2Deg(double rad)
-//{
-//    return 360.0/D2PI*rad;
-//}
 
 ZdAz MCosy::CorrectTarget(const ZdAz &src, const ZdAz &dst)
@@ -200,14 +195,7 @@
     cout << "Waiting for positioning..." << flush;
 
-    WaitForSdos();
-
-    cout << "SDO..." << flush;
-
-    while ((fMac1->IsPositioning() || fMac2->IsPositioning()) && !StopWaitingForSDO())
-        usleep(1);
+    WaitForEndMovement();
 
     cout << "done." << endl;
-
-    SetStatus(kStopped);
 }
 
@@ -215,5 +203,8 @@
 {
     if (!HasError())
+    {
+        SetStatus(kStopped);
         return;
+    }
 
     SetStatus(kError);
@@ -228,5 +219,5 @@
 int MCosy::SetPosition(const ZdAz &dst) // [rad]
 {
-    // FIXME: CORRECT BY fTrackingError !!!!!!!!!!
+    // FIXME: Correct by fOffset ?
 
     //
@@ -253,5 +244,5 @@
         ZdAz rd = dest-p; // [se]
 
-        ZdAz cd = rd;   // [se]
+        ZdAz cd = rd;     // [se]
         cd.Round();
 
@@ -300,6 +291,5 @@
     }
 
-    CheckForError();
-
+    StopMovement();
     lout << "Warning: Requested position not reached." << endl;
     return FALSE;
@@ -354,6 +344,4 @@
 void MCosy::LimitSpeed(ZdAz *vt, const ZdAz &vcalc) const
 {
-    return;
-
     //
     // How to limit the speed. If the wind comes and blowes
@@ -367,12 +355,16 @@
 #define sgn(x) (x<0?-1:1)
 
-    if (sgn(vt->Az()) != sgn(vcalc.Az()))
-        vt->Az(0.1*vcalc.Az());
+    const Float_t limit = 0.25;
+
+    if (sgn(vt->Az()) != sgn(vcalc.Az()) &&
+        fabs(vt->Az()) < limit*fabs(vcalc.Az()))
+        vt->Az(0);
     else
         if (fabs(vt->Az()) > 0.9*vraz)
             vt->Az(0.9*vraz*sgn(vt->Az()));
 
-    if (sgn(vt->Zd()) != sgn(vcalc.Zd()))
-        vt->Zd(0.1*vcalc.Zd());
+    if (sgn(vt->Zd()) != sgn(vcalc.Zd()) &&
+        fabs(vt->Zd()) < limit*fabs(vcalc.Zd()))
+        vt->Zd(0);
     else
         if (fabs(vt->Zd()) > 0.9*vrzd)
@@ -392,5 +384,5 @@
     if (!SetPosition(dest))
     {
-        lout << "ERROR: Cannot start tracking, unable to reach requested position." << endl;
+        lout << "Error: Cannot start tracking, positioning failed." << endl;
         return;
     }
@@ -421,6 +413,6 @@
 
     lout << "Start tracking:";
-    lout << " Ra: " << Rad2Deg(dst.Ra())  << "deg  ";
-    lout << "Dec: " << Rad2Deg(dst.Dec()) << "deg" << endl;
+    lout << " Ra: " << Rad2Deg(dst.Ra())  << "\xb0  ";
+    lout << "Dec: " << Rad2Deg(dst.Dec()) << "\xb0" << endl;
 
     //
@@ -431,5 +423,8 @@
 
     ofstream fout("cosy.pos");
-    fout << "   Mjd/10ms    V/re/min/4" << endl;
+    fout << "Tracking:";
+    fout << " Ra: " << Rad2Deg(dst.Ra())  << "\x9c  ";
+    fout << "Dec: " << Rad2Deg(dst.Dec()) << "\x9c" << endl << endl;
+    fout << "     Mjd/10ms    V/re/min/4" << endl;
 
     //
@@ -450,5 +445,5 @@
         dest = CorrectTarget(GetSePos(), sla.CalcZdAz(dst)); // [se]
 
-        //ZdAz vcalc = sla.GetApproxVel(dst) * kGearRatio2*4./60.;  // [re/min]
+        ZdAz vcalc = sla.GetApproxVel(dst) * kGearRatio2*4./60.;  // [re/min]
 
         //
@@ -478,5 +473,5 @@
         //
         ZdAz vt = v/4;
-        //LimitSpeed(&vt, vcalc);
+        LimitSpeed(&vt, vcalc);
         vt.Round();
 
@@ -484,7 +479,7 @@
         // check if the drive is fast enough to follow the star
         //
-        if (v.Zd()>.9*fMac1->GetVelRes() || v.Az()>.9*fMac2->GetVelRes())
+        if (vt.Zd()>.9*fMac1->GetVelRes() || vt.Az()>.9*fMac2->GetVelRes())
         {
-            lout << "Error: Tracking speed faster than possible maximum velocity." << endl;
+            lout << "Error: Tracking speed faster than 90% of possible maximum velocity." << endl;
             break;
         }
@@ -506,4 +501,10 @@
         fout << setw(4) << vt.Zd() << " ";
         fout << setw(4) << vt.Az() << endl;
+        //
+        // FIXME? Calculate an accuracy for the tracking system?
+        // How good do we reach the calculated position in 'real'
+        // re valus?
+        //
+
 
         //
@@ -516,18 +517,39 @@
 
     fTracking = kFALSE;
-
-    //
-    // Stop revolution mode
-    //
+    StopMovement();
+    lout << "Tracking stopped." << endl;
+}
+
+int MCosy::IsPositioning() const
+{
+    return (fMac1->IsPositioning() || fMac2->IsPositioning()) && !StopWaitingForSDO();
+}
+
+void MCosy::WaitForEndMovement()
+{
+    WaitForSdos();
+
+    while (IsPositioning())
+        usleep(1);
+}
+
+void MCosy::StopMovement()
+{
     SetStatus(kStopping);
 
+    cout << "Stopping  positioning..." << endl;
+    fMac1->SetDeceleration(0.5*fMac1->GetVelRes());
+    fMac2->SetDeceleration(0.5*fMac2->GetVelRes());
+
+    cout << "Stoping possible RPM mode..." << endl;
+    fMac1->SetRpmMode(FALSE);
     fMac2->SetRpmMode(FALSE);
-    fMac1->SetRpmMode(FALSE);
-
-    SetStatus(kStopped);
-
-    lout << "Tracking stopped." << endl;
+
+    cout << "Waiting for silence..." << endl;
+    WaitForEndMovement();
 
     CheckForError();
+
+    cout << "Movement stopped." << endl;
 }
 
@@ -541,16 +563,5 @@
 
     case WM_STOP:
-        cout << "Stopping  positioning..." << endl;
-        SetStatus(kStopping);
-        fMac1->SetDeceleration(0.5*fMac1->GetVelRes());
-        fMac2->SetDeceleration(0.5*fMac2->GetVelRes());
-        cout << "Stoping possible RPM mode..." << endl;
-        fMac1->SetRpmMode(FALSE);
-        fMac2->SetRpmMode(FALSE);
-        cout << "Stopping commands sent." << endl;
-        while (fMac1->IsPositioning() || fMac2->IsPositioning())
-            usleep(1);
-        SetStatus(kStopped);
-        cout << "Positioning stopped." << endl;
+        StopMovement();
         return NULL;
 
@@ -698,18 +709,41 @@
         // wait until a tracking session is started
         //
-        while (!fTracking)
+        while (!fTracking && !fTTalk->HasStopFlag())
             usleep(1);
 
+        if (fTTalk->HasStopFlag())
+            break;
+
+        ofstream fout("cosy.err");
+        fout << "Tracking:";
+        fout << " Ra: " << Rad2Deg(fRaDec.Ra())  << "\x9c  ";
+        fout << "Dec: " << Rad2Deg(fRaDec.Dec()) << "\x9c" << endl << endl;
+        fout << "     MjdZd/10ms    ErrZd/re";
+        fout << "     MjdAz/10ms    ErrAd/re" << endl;
+
         ZdAz old;
-        ZdAz ist   = fTrackingError/kGearRatio;     // [se]
-        ZdAz istre = fOffset;                       // [re]
-
-        //
-        // only update fTrackingError while tracking
-        //
+        ZdAz ist;
+
         ZdAz sollzd;
         ZdAz sollaz;
-        while (fTracking)
+
+        ZdAz istre = -fOffset;                // [re]
+        ZdAz time;
+
+        //
+        // only update fTrackingError while tracking
+        //
+        while (fTracking && !fTTalk->HasStopFlag())
         {
+            //
+            // Make changes (eg wind) smoother - attenuation of control function
+            //
+            const float weight = 0.3;
+
+            ZdAz offset(fOffset.Zd()*(1.-weight)+(ist.Zd()*kGearRatio.X()-istre.Zd())*weight,
+                        fOffset.Az()*(1.-weight)+(ist.Az()*kGearRatio.Y()-istre.Az())*weight);
+
+            fOffset = offset;
+
             //
             // This is the time constant which defines how fast
@@ -719,22 +753,7 @@
 
             //
-            // Make changes (eg wind) smoother - attenuation of control function
-            //
-            fTrackingError.Set((ist.Zd()-sollzd.Zd())*kGearRatio.X(),
-                               (ist.Az()-sollaz.Az())*kGearRatio.Y());
-
-
-            const float weight = 0.3;
-
-            ZdAz offset(fOffset.Zd()*(1.-weight)+(ist.Zd()*kGearRatio.X()-istre.Zd())*weight,
-                        fOffset.Az()*(1.-weight)+(ist.Az()*kGearRatio.Y()-istre.Az())*weight);
-
-            fOffset = offset;
-            // fTrackingError.Zd(((offset.Zd()>1000)||(offset.Zd()<-1000))?0:offset.Zd());
-            // fTrackingError.Az(((offset.Az()>1000)||(offset.Az()<-1000))?0:offset.Az());
-
-            //
             // get position, where we are
             //
+            old = ist;
             ist = GetSePos(); // [se]
 
@@ -750,7 +769,8 @@
             //
             // Get time from last shaftencoder position change (position: ist)
-            //
-            const double tzd = (fAlt1->GetMjd()+fAlt2->GetMjd())/2.0;
-            const double taz = fAz->GetMjd();
+            // FIXME: I cannot take the avarage
+            //
+            time.Zd((fAlt1->GetMjd()+fAlt2->GetMjd())/2.0);
+            time.Az(fAz->GetMjd());
 
             //
@@ -760,5 +780,5 @@
             if ((int)ist.Zd() != (int)old.Zd())
             {
-                sla.SetMjd(tzd);
+                sla.SetMjd(time.Zd());
                 sollzd = CorrectTarget(ist, sla.CalcZdAz(fRaDec)); // [se]
             }
@@ -766,10 +786,18 @@
             if ((int)ist.Az() != (int)old.Az())
             {
-                sla.SetMjd(taz);
+                sla.SetMjd(time.Az());
                 sollaz = CorrectTarget(ist, sla.CalcZdAz(fRaDec)); // [se]
             }
 
-            old = ist;
+            fTrackingError.Set((ist.Zd()-sollzd.Zd())*kGearRatio.X(),
+                               (ist.Az()-sollaz.Az())*kGearRatio.Y());
+
+            fout << setprecision(15) << setw(15) << time.Zd()*60.*60.*24. << " ";
+            fout << setprecision(5)  << setw(5)  << fTrackingError.Zd() << "  ";
+            fout << setprecision(15) << setw(15) << time.Az()*60.*60.*24. << " ";
+            fout << setprecision(5)  << setw(5)  << fTrackingError.Az() << endl;
         }
+
+        fout << endl << endl;
     }
 }
@@ -814,8 +842,8 @@
 void MCosy::Stop()
 {
-    cout << "- Stopping GUI update." << endl;
+
+    lout << "- Stopping GUI update." << endl;
     fUpdateGui->TurnOff();
-    //    delete fTGui;
-    lout << "- GUI Thread stopped." << endl;
+    lout << "- GUI Update stopped." << endl;
 
     delete fTTalk;
Index: /trunk/MagicSoft/Cosy/MCosy.h
===================================================================
--- /trunk/MagicSoft/Cosy/MCosy.h	(revision 920)
+++ /trunk/MagicSoft/Cosy/MCosy.h	(revision 921)
@@ -7,5 +7,5 @@
 #include "MThread.h"
 
-#define kDEG ((char)0xb0)  // Linux 'ø'
+#define kDEG ((char)0x9c)  // Linux 'ø'
 
 #define WM_WAIT      WM_NULL
@@ -33,5 +33,4 @@
     {
         SetPriority(10);
-        Detach();
         Start();
     }
@@ -100,5 +99,9 @@
 
     int StopWaitingForSDO() const;
+    int IsPositioning() const;
     void CheckForError();
+
+    void StopMovement();
+    void WaitForEndMovement();
 
 public:
Index: /trunk/MagicSoft/Cosy/Starguider.cc
===================================================================
--- /trunk/MagicSoft/Cosy/Starguider.cc	(revision 920)
+++ /trunk/MagicSoft/Cosy/Starguider.cc	(revision 921)
@@ -123,12 +123,10 @@
     fList.Add(fMenu);
 
-    fCRaDec = new MGCoordinates(this, kTRUE,
-                                "Right Ascension [\xb0]:", "Declination [\xb0]:");
+    fCRaDec = new MGCoordinates(this, kETypeRaDec);
     fCRaDec->Move(1, fMenu->GetDefaultHeight()+584);
     AddFrame(fCRaDec);
     fList.Add(fCRaDec);
 
-    fCZdAz = new MGCoordinates(this, kFALSE,
-                                "Zenith Dist. [\xb0]:", "Azimuth [\xb0]:");
+    fCZdAz = new MGCoordinates(this, kETypeZdAz);
     fCZdAz->Move(240+12, fMenu->GetDefaultHeight()+584);
     AddFrame(fCZdAz);
Index: /trunk/MagicSoft/Cosy/ToDo
===================================================================
--- /trunk/MagicSoft/Cosy/ToDo	(revision 920)
+++ /trunk/MagicSoft/Cosy/ToDo	(revision 921)
@@ -1,15 +1,23 @@
 
- - GUI is missing a display for the actual state of the machine
-   (tracking, positioning, stopping, stopped, etc.)
+  - Implement the ZdAz Positioning in a more proper way (using some fOffset)
 
- - Implement the ZdAz Positioning in a more proper way (using some fOffset)
+  - Add a MCallback interface to be able to get a notice if the
+    shaftencoder position changed (recalc fOffset, fTracking Error
+    as often as possible)
+   
+  - Check why it crashes if you exit the program while 'going home'
+ 
+  - calc trajec with respect to bright stars and planets
+  
+  - planet list
+  
+  - handshaking
+  
+  - calc tracking statistics
+  
+  - display histogram for accuracy
+  
+  - Add sanity checks to range of 'SetPosition' and 'TrackPosition'
+  
+  - Devide MTTalk into NetworkInit and MCalcTrackingError/Offset-Class
 
- - Add a MCallback interface to be able to get a notice if the
-   shaftencoder position changed (recalc fOffset, fTracking Error
-   as often as possible)
-   
- - Check why it crashes if you exit the program while 'going home'
- 
- - If one axis stops because of an error the other axis isn't stopped
-   ( SetPosition, software endswitch activated)
-
Index: /trunk/MagicSoft/Cosy/base/coord.h
===================================================================
--- /trunk/MagicSoft/Cosy/base/coord.h	(revision 920)
+++ /trunk/MagicSoft/Cosy/base/coord.h	(revision 921)
@@ -46,4 +46,5 @@
     XY operator+(const XY &c) const { return XY(fX+c.fX, fY+c.fY); }
     XY operator-(const XY &c) const { return XY(fX-c.fX, fY-c.fY); }
+    XY operator-() const { return XY(-fX, -fY); }
 
     double Sqr()   const { return fX*fX + fY*fY; }
@@ -72,4 +73,5 @@
     AltAz operator+(const AltAz &c) const { return AltAz(fX+c.fX, fY+c.fY); }
     AltAz operator-(const AltAz &c) const { return AltAz(fX-c.fX, fY-c.fY); }
+    AltAz operator-() const { return AltAz(-fX, -fY); }
 };
 
@@ -95,4 +97,5 @@
     ZdAz operator+(const ZdAz &c) const { return ZdAz(fX+c.fX, fY+c.fY); }
     ZdAz operator-(const ZdAz &c) const { return ZdAz(fX-c.fX, fY-c.fY); }
+    ZdAz operator-() const { return ZdAz(-fX, -fY); }
 };
 
@@ -110,4 +113,5 @@
     RaDec operator+(const RaDec &c) const { return RaDec(fX+c.fX, fY+c.fY); }
     RaDec operator-(const RaDec &c) const { return RaDec(fX-c.fX, fY-c.fY); }
+    RaDec operator-() const { return RaDec(-fX, -fY); }
 };
 
Index: /trunk/MagicSoft/Cosy/catalog/Slalib.cc
===================================================================
--- /trunk/MagicSoft/Cosy/catalog/Slalib.cc	(revision 920)
+++ /trunk/MagicSoft/Cosy/catalog/Slalib.cc	(revision 921)
@@ -17,6 +17,6 @@
     slaDaf2r( 9, 56, 36.0, &fElong, &status);
 
-    cout << "Latitude: 51ø38'48.0\" = " <<  360.0/D2PI*fPhi << "   ";
-    cout << "Longitude: 9ø56'36.0\" = " <<  360.0/D2PI*fElong << endl;
+    cout << "Latitude: 51\x9c 38'48.0\" = " <<  360.0/D2PI*fPhi << "   ";
+    cout << "Longitude: 9\x9c 56'36.0\" = " <<  360.0/D2PI*fElong << endl;
 }
 
Index: /trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc
===================================================================
--- /trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc	(revision 920)
+++ /trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc	(revision 921)
@@ -13,5 +13,5 @@
 #include <sys/resource.h>  // PRIO_PROCESS
 
-ShaftEncoder::ShaftEncoder(BYTE_t nodeid, MLog &out) : NodeDrv(nodeid, out), fLabel(NULL)
+ShaftEncoder::ShaftEncoder(BYTE_t nodeid, MLog &out) : NodeDrv(nodeid, out), fLabel(NULL), fPosHasChanged(false)
 {
 }
@@ -112,13 +112,19 @@
 }
 
-void ShaftEncoder::HandlePDOType0(BYTE_t *data)
+void ShaftEncoder::HandlePDOType0(BYTE_t *data, struct timeval *tv)
 {
     //
     // Decode information, we have a 14bit only
     //
-    fPos  = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
-}
-
-void ShaftEncoder::HandlePDOType1(BYTE_t *data)
+    LWORDS_t pos = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
+    if (pos==fPos)
+        return;
+
+    fPos = pos;
+    fTime.SetTimer(tv);
+    fPosHasChanged = true;
+}
+
+void ShaftEncoder::HandlePDOType1(BYTE_t *data, struct timeval *tv)
 {
     //
@@ -127,5 +133,12 @@
     LWORDS_t pos  = data[0] | (data[1]<<8) | (data[2]<<16); // | (data[3]<<24);
     BYTE_t   flag = data[4];
-    pos=pos;
+
+    if (fPos==pos)
+        return;
+
+    fPos=pos;
+    fTime.SetTimer(tv);
+    fPosHasChanged=true;
+
     flag=flag;
 }
@@ -141,16 +154,22 @@
     fAcc = data[6] | (data[7]<<8);
 
-    fTime.SetTimer(tv);
-
     const int uplim = 9*fTicks/10;
     const int dnlim = 1*fTicks/10;
 
+    int turn = fTurn;
+
     if (fPos > uplim && pos < dnlim)
-        fTurn++;
+        turn++;
 
     if (fPos < dnlim && pos > uplim)
-        fTurn--;
-
-    fPos = pos;
+        turn--;
+
+    if (fPos==pos && fTurn==fTurn)
+        return;
+
+    fPos  = pos;
+    fTurn = turn;
+    fTime.SetTimer(tv);
+    fPosHasChanged=true;
 }
 
Index: /trunk/MagicSoft/Cosy/devdrv/shaftencoder.h
===================================================================
--- /trunk/MagicSoft/Cosy/devdrv/shaftencoder.h	(revision 920)
+++ /trunk/MagicSoft/Cosy/devdrv/shaftencoder.h	(revision 921)
@@ -22,8 +22,10 @@
     WORDS_t   fUpdAcc;   // ticks per 25ms^2
 
+    bool fPosHasChanged;
+
     Timer fTime;
 
-    void HandlePDOType0(BYTE_t *data);
-    void HandlePDOType1(BYTE_t *data);
+    void HandlePDOType0(BYTE_t *data, struct timeval *tv);
+    void HandlePDOType1(BYTE_t *data, struct timeval *tv);
     void HandlePDOType2(BYTE_t *data, struct timeval *tv);
 
@@ -56,4 +58,7 @@
 
     void DisplayVal();
+
+    bool IsPosHasChanged() const { return fPosHasChanged; }
+    void ResetPosHasChanged() { fPosHasChanged = false; }
 };
 
Index: /trunk/MagicSoft/Cosy/gui/MGAccuracy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGAccuracy.cc	(revision 920)
+++ /trunk/MagicSoft/Cosy/gui/MGAccuracy.cc	(revision 921)
@@ -190,9 +190,10 @@
     aaz *= d2r;
 
-    const float dphi2 = aaz/2.;
-    const float cos2  = cos(dphi2)*cos(dphi2);
-    const float sin2  = sin(dphi2)*sin(dphi2);
-
-    float dist = acos(cos(azd)*cos2 - cos(2*pzd+azd)*sin2);
+    const double dphi2 = aaz/2.;
+    const double cos2  = cos(dphi2)*cos(dphi2);
+    const double sin2  = sin(dphi2)*sin(dphi2);
+    const double d     = cos(azd)*cos2 - cos(2*pzd+azd)*sin2;
+
+    double dist = acos(d);
 
     dist *= 3600./d2r;
Index: /trunk/MagicSoft/Cosy/gui/MGCoordinate.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGCoordinate.cc	(revision 920)
+++ /trunk/MagicSoft/Cosy/gui/MGCoordinate.cc	(revision 921)
@@ -23,8 +23,8 @@
 };
 
-MGCoordinate::MGCoordinate(const TGWindow* p,
+MGCoordinate::MGCoordinate(const TGWindow* p, const Int_t type=kETypeDeg,
                            const Bool_t flag, const char *txt,
                            const Int_t deg, const UInt_t min, const UInt_t sec)
-: TGFrame(p, 114, flag?76:46, kSunkenFrame|kFixedSize), fDeg(deg), fMin(min), fSec(sec)
+: TGFrame(p, 119, flag?76:46, kSunkenFrame|kFixedSize), fDeg(deg), fMin(min), fSec(sec)
 {
     // p = pointer to MainFrame (not owner)
@@ -37,7 +37,7 @@
         // fTextEntryMin->SetAlignment(kTextCenterX);
         // fTextEntrySec->SetAlignment(kTextCenterX);
-        fTextEntryDeg->Move( 4, 26);
-        fTextEntryMin->Move(44, 26);
-        fTextEntrySec->Move(78, 26);
+        fTextEntryDeg->Move( 7, 26);
+        fTextEntryMin->Move(47, 26);
+        fTextEntrySec->Move(81, 26);
         fTextEntryDeg->MapWindow();
         fTextEntryMin->MapWindow();
@@ -60,7 +60,7 @@
     fLabelMin->SetTextJustify(kTextRight);
     fLabelSec->SetTextJustify(kTextRight);
-    fLabelDeg->Move( 4+6, ypos);
-    fLabelMin->Move(44+6, ypos);
-    fLabelSec->Move(78+6, ypos);
+    fLabelDeg->Move(13, ypos);
+    fLabelMin->Move(53, ypos);
+    fLabelSec->Move(87, ypos);
     fLabelDeg->MapWindow();
     fLabelMin->MapWindow();
@@ -82,40 +82,44 @@
     TGLabel *label;
 
+    const char *deg = type==kETypeDeg ? "\xb0" : "h";
+    const char *min = type==kETypeDeg ? "'"    : "m";
+    const char *sec = type==kETypeDeg ? "\""   : "s";
+
     if (flag)
     {
-        label = new TGLabel(this, "\xb0");
+        label = new TGLabel(this, deg);
         label->SetTextJustify(kTextLeft);
-        label->Move(36, 26);
+        label->Move(39, 26);
         label->MapWindow();
         fList.Add(label);
 
-        label = new TGLabel(this, "'");
+        label = new TGLabel(this, min);
         label->SetTextJustify(kTextLeft);
-        label->Move(70, 26);
+        label->Move(73, 26);
         label->MapWindow();
         fList.Add(label);
 
-        label = new TGLabel(this, "\"");
+        label = new TGLabel(this, sec);
         label->SetTextJustify(kTextLeft);
-        label->Move(104, 26);
+        label->Move(107, 26);
         label->MapWindow();
         fList.Add(label);
     }
 
-    label = new TGLabel(this, "\xb0");
+    label = new TGLabel(this, deg);
     label->SetTextJustify(kTextLeft);
-    label->Move(36, ypos);
+    label->Move(39, ypos);
     label->MapWindow();
     fList.Add(label);
 
-    label = new TGLabel(this, "'");
+    label = new TGLabel(this, min);
     label->SetTextJustify(kTextLeft);
-    label->Move(70, ypos);
+    label->Move(73, ypos);
     label->MapWindow();
     fList.Add(label);
  
-    label = new TGLabel(this, "\"");
+    label = new TGLabel(this, sec);
     label->SetTextJustify(kTextLeft);
-    label->Move(104, ypos);
+    label->Move(107, ypos);
     label->MapWindow();
     fList.Add(label);
Index: /trunk/MagicSoft/Cosy/gui/MGCoordinate.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGCoordinate.h	(revision 920)
+++ /trunk/MagicSoft/Cosy/gui/MGCoordinate.h	(revision 921)
@@ -14,4 +14,9 @@
 #include "MGList.h"
 #endif
+
+enum {
+    kETypeDeg,
+    kETypeH
+};
 
 class TGLabel;
@@ -41,5 +46,5 @@
 
 public:
-    MGCoordinate(const TGWindow* p,
+    MGCoordinate(const TGWindow* p, const Int_t type=kETypeDeg,
                  const Bool_t flag=kTRUE, const char *txt="Coordinates:",
                  const Int_t deg=0, const UInt_t min=0, const UInt_t sec=0);
Index: /trunk/MagicSoft/Cosy/gui/MGCoordinates.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGCoordinates.cc	(revision 920)
+++ /trunk/MagicSoft/Cosy/gui/MGCoordinates.cc	(revision 921)
@@ -12,16 +12,20 @@
 
 MGCoordinates::MGCoordinates(const TGWindow* p,
+                             const Int_t type,
                              const Bool_t flag,
-                             const char *txt1, const char *txt2,
                              const Int_t deg1, const UInt_t min1, const UInt_t sec1,
                              const Int_t deg2, const UInt_t min2, const UInt_t sec2)
-: TGFrame(p, 234, 76, kFixedSize)
+: TGFrame(p, 244, 76, kFixedSize)
 {
+    const Int_t t    = type==kETypeZdAz ? kETypeDeg : kETypeH;
+    const char *txt1 = type==kETypeZdAz ? "Zenith Dist [\xb0]:" : "Right Ascension [h]:";
+    const char *txt2 = type==kETypeZdAz ? "Azimuth [\xb0]:" : "Declination [\xb0]:";
+
     // p = pointer to MainFrame (not owner)
-    fX = new MGCoordinate(this, flag, txt1, deg1, min1, sec1);
+    fX = new MGCoordinate(this, t, flag, txt1, deg1, min1, sec1);
     fX->Move(0, 0);
 
-    fY = new MGCoordinate(this, flag, txt2, deg2, min2, sec2);
-    fY->Move(120, 0);
+    fY = new MGCoordinate(this, kETypeDeg, flag, txt2, deg2, min2, sec2);
+    fY->Move(125, 0);
 
     MapWindow();
Index: /trunk/MagicSoft/Cosy/gui/MGCoordinates.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGCoordinates.h	(revision 920)
+++ /trunk/MagicSoft/Cosy/gui/MGCoordinates.h	(revision 921)
@@ -15,4 +15,9 @@
 #endif
 
+enum {
+    kETypeZdAz,
+    kETypeRaDec
+};
+
 class MGCoordinate;
 
@@ -23,8 +28,6 @@
 
 public:
-    MGCoordinates(const TGWindow* p,
+    MGCoordinates(const TGWindow* p, const Int_t type=kETypeZdAz,
                   const Bool_t flag=kTRUE,
-                  const char *txt1="Coordinate1:",
-                  const char *txt2="Coordinate2:",
                   const Int_t deg1=0, const UInt_t min1=0, const UInt_t sec1=0,
                   const Int_t deg2=0, const UInt_t min2=0, const UInt_t sec2=0);
Index: /trunk/MagicSoft/Cosy/gui/MGCosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGCosy.cc	(revision 920)
+++ /trunk/MagicSoft/Cosy/gui/MGCosy.cc	(revision 921)
@@ -191,6 +191,18 @@
 }
 
-void MGCosy::CreateButton(TGCompositeFrame *tf1, TGCompositeFrame *tf2)
-{
+void MGCosy::CreateTabs(TGTab *fTab)
+{
+    TGCompositeFrame *tf1 = fTab->AddTab("Position Zd/Az");
+    TGCompositeFrame *tf2 = fTab->AddTab("Track Ra/Dec");
+
+    fCZdAz = new MGCoordinates(tf1, kETypeZdAz);
+    fCZdAz->Move(27, 105);
+    fList->Add(fCZdAz);
+
+    fCRaDec = new MGCoordinates(tf2, kETypeRaDec);
+    fCRaDec->Move(27, 105);
+    fList->Add(fCRaDec);
+
+
     const int x=15;
     const int y=12;
@@ -235,39 +247,32 @@
     // red:   164, 192, 224, 232
     //
-
-    //    TGTextButton *fButton1 = new TGTextButton(this, "Position Zd/Az", kPB_POSITION);
-    //    TGTextButton *fButton2 = new TGTextButton(this, "Track   Ra/Dec", kPB_TRACK);
-    TGTextButton *fButton4 = new TGTextButton(this, "Calc Zd/Az",  kPB_CALCALTAZ);
-    TGTextButton *fButton5 = new TGTextButton(this, "Set Polaris", kPB_POLARIS);
-    //    fButton1->Move(120,  40);
-    //    fButton2->Move(120,  65);
-    fButton4->Resize(80, 25);
-    fButton5->Resize(80, 25);
-
-    fButton4->Move(40, 257);
-    fButton5->Move(40, 288);
-    //fButton4->SetBackgroundColor(12);
-    //fButton5->SetBackgroundColor(16);
-    //    fButton1->SetToolTipText("Move Telescope to Zd/Az position.");
-    //    fButton2->SetToolTipText("Track the coordinates given in Ra/Dec.");
-    fButton4->SetToolTipText("Calculate Zd/Az corresponding to Ra/Dec.");
-    fButton5->SetToolTipText("Set the actual position as the position of Polaris.");
-    //    fList->Add(fButton1);
-    //    fList->Add(fButton2);
-    fList->Add(fButton4);
-    fList->Add(fButton5);
-
-    TGTextButton *fStart = new TGTextButton(this, "Start", kPB_START);
-    TGTextButton *fStop  = new TGTextButton(this, "Stop",  kPB_STOP);
-    fStart->SetBackgroundColor(20);
-    fStop ->SetBackgroundColor(224);
-    fStart->Move(147, 275);
-    fStop ->Move(212, 275);
-    fStart->Resize(60, 25);
-    fStop ->Resize(60, 25);
-    fStart->SetToolTipText("Start a telescope movement.");
-    fStop ->SetToolTipText("Stop any movement of telescope.");
-    fList->Add(fStart);
-    fList->Add(fStop);
+    TGTextButton *but;
+
+    but= new TGTextButton(this, "Calc Zd/Az",  kPB_CALCALTAZ);
+    but->Resize(80, 25);
+    but->Move(37, 257);
+    but->SetToolTipText("Calculate Zd/Az corresponding to Ra/Dec.");
+    fList->Add(but);
+
+    but = new TGTextButton(this, "Set Polaris", kPB_POLARIS);
+    but->Resize(80, 25);
+    but->Move(37, 288);
+    but->SetToolTipText("Set the actual position as the position of Polaris.");
+    fList->Add(but);
+
+    but = new TGTextButton(this, "Start", kPB_START);
+    but->SetBackgroundColor(20);
+    but->Move(147, 275);
+    but->Resize(62, 25);
+    but->SetToolTipText("Start a telescope movement.");
+    fList->Add(but);
+
+    but = new TGTextButton(this, "Stop",  kPB_STOP);
+    but->SetBackgroundColor(224);
+    but->Move(212, 275);
+    but->Resize(62, 25);
+    but->SetToolTipText("Stop any movement of telescope.");
+    fList->Add(but);
+
 }
 
@@ -284,11 +289,9 @@
 
     fTab = new TGTab(f, 300, 300);
-    TGCompositeFrame *tf1 = fTab->AddTab("Position Zd/Az");
-    TGCompositeFrame *tf2 = fTab->AddTab("Track Ra/Dec");
     fList->Add(fTab);
 
     fSkyPosition = new MGSkyPosition(f, 300);
     fAccuracy    = new MGAccuracy   (f, 300);
-    fVelocity    = new MGVelocity   (f, "Velocity ['/min]", 300);
+    fVelocity    = new MGVelocity   (f, "Velocity [\"/min]", 300);
 //    fOffset      = new MGVelocity   (f, "Offset se-re [']", 300);
 
@@ -304,4 +307,5 @@
     fLog = new TGListBox(f, -1, kSunkenFrame);  //kSunkenFrame|kDoubleBorder,
     fLog->Resize(300, 300);
+    fLog->ChangeBackground(TGFrame::GetBlackPixel());
     fList->Add(fLog);
 
@@ -321,10 +325,5 @@
     // FIXME!
     //
-    fCoord = new MGCoordinates(this, kTRUE,
-                               "Coordinate 1 [\xb0]:", "Coordinate 2 [\xb0]:");
-    fCoord->Move(40, 165);
-    fList->Add(fCoord);
-
-    CreateButton(tf1, tf2);
+    CreateTabs(fTab);
     CreateLabel(frame);
 
@@ -384,5 +383,5 @@
     {
         zd = (Int_t)off.Zd();
-        sprintf(text, "%ld", zd);
+        sprintf(text, "%d", zd);
         fOffsetZd->SetText(new TGString(text));
     }
@@ -390,5 +389,5 @@
     {
         az = (Int_t)off.Az();
-        sprintf(text, "%ld", az);
+        sprintf(text, "%d", az);
         fOffsetAz->SetText(new TGString(text));
     }
@@ -442,24 +441,30 @@
 }
 
-void MGCosy::Start(UInt_t id)
-{
-    cout << "Start " << (id?"tracking.":"positioning.") << endl;
-
-    XY xy = fCoord->GetCoordinates();
-
-    if (id)
-    {
-        RaDec dest(xy.X(), xy.Y());
-        cout << dest.Ra() << kDEG << " " << dest.Dec() << kDEG << endl;
-        fQueue->PostMsg(WM_TRACK, &dest, sizeof(dest));
-    }
-    else
-    {
-        ZdAz dest(xy.X(), xy.Y());
-        cout << dest.Zd() << kDEG << " " << dest.Az() << kDEG << endl;
-        fQueue->PostMsg(WM_POSITION, &dest, sizeof(dest));
-    }
-
-    cout << "PostMsg (" << (id?"WM_Track":"WM_Position") << ") returned." << endl;
+void MGCosy::StartTrack()
+{
+    cout << "Start tracking." << endl;
+
+    XY xy = fCRaDec->GetCoordinates();
+    RaDec dest(xy.X()*15., xy.Y()); // xy.X()  [h]->[ø]
+
+    cout << dest.Ra() << "h " << dest.Dec() << "\xb0" << endl;
+
+    fQueue->PostMsg(WM_TRACK, &dest, sizeof(dest));
+
+    cout << "PostMsg (WM_Track) returned." << endl;
+}
+
+void MGCosy::StartPos()
+{
+    cout << "Start positioning." << endl;
+
+    XY xy = fCZdAz->GetCoordinates();
+    ZdAz dest(xy.X(), xy.Y());
+
+    cout << dest.Zd() << kDEG << " " << dest.Az() << kDEG << endl;
+
+    fQueue->PostMsg(WM_POSITION, &dest, sizeof(dest));
+
+    cout << "PostMsg (WM_Position) returned." << endl;
 }
 
@@ -482,4 +487,5 @@
             switch (mp1)
             {
+                /*
             case kPB_POSITION:
                 Start(0);
@@ -489,7 +495,7 @@
                 Start(1);
                 return kTRUE;
-
+             */
             case kPB_START:
-                Start(fTab->GetCurrent());
+                fTab->GetCurrent() ? StartTrack() : StartPos();
                 return kTRUE;
 
@@ -505,6 +511,6 @@
                     sla.SetMjd2Now();
 
-                    XY xy = fCoord->GetCoordinates();
-                    RaDec rd(xy.X(), xy.Y());
+                    XY xy = fCRaDec->GetCoordinates();
+                    RaDec rd(xy.X()*15., xy.Y());
 
                     cout << "Ra/Dec: " << rd.Ra() << kDEG << " " << rd.Dec() << kDEG << endl;
Index: /trunk/MagicSoft/Cosy/gui/MGCosy.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGCosy.h	(revision 920)
+++ /trunk/MagicSoft/Cosy/gui/MGCosy.h	(revision 921)
@@ -54,5 +54,7 @@
     TGLabel       *fOffsetAz;
 
-    MGCoordinates *fCoord;
+    MGCoordinates *fCZdAz;
+    MGCoordinates *fCRaDec;
+
     MGSkyPosition *fSkyPosition;
     MGAccuracy    *fAccuracy;
@@ -74,7 +76,8 @@
     void CreateMenu();
     void CreateLabel(TGCompositeFrame *f);
-    void CreateButton(TGCompositeFrame *tf1, TGCompositeFrame *tf2);
+    void CreateTabs(TGTab *fTab);
 
-    void Start(UInt_t id);
+    void StartPos();
+    void StartTrack();
 
     void EnableLabel(TGLabel *label, Bool_t stat);
Index: /trunk/MagicSoft/Cosy/gui/MGSkyPosition.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGSkyPosition.cc	(revision 920)
+++ /trunk/MagicSoft/Cosy/gui/MGSkyPosition.cc	(revision 921)
@@ -259,6 +259,6 @@
     Y = fy;
 
-    float/*int*/ xm = /*(int)floor*/(fmod(fabs(x), 60.));
-    float/*int*/ ym = /*(int)floor*/(fmod(fabs(y), 60.));
+    float xm = fmod(fabs(x), 60.);
+    float ym = fmod(fabs(y), 60.);
 
     char txt[100];
Index: /trunk/MagicSoft/Cosy/gui/MGVelocity.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGVelocity.cc	(revision 920)
+++ /trunk/MagicSoft/Cosy/gui/MGVelocity.cc	(revision 921)
@@ -167,4 +167,10 @@
     fText->Draw();
     fList->Add(fText);
+
+    fTextVel = new TText(2*70, 2*70, "");
+    fTextVel->SetTextAlign(33);  // right, top
+    fTextVel->SetTextColor(10);  // white
+    fTextVel->Draw();
+    fList->Add(fTextVel);
 }
 
@@ -198,5 +204,5 @@
         sprintf(txt,  "x%.0f", 1./fScale);
 
-    fText->SetText(fText->GetX()/*70*2*/, fText->GetY()/*70*2*/, txt);
+    fText->SetText(fText->GetX(), fText->GetY(), txt);
 }
 
@@ -263,4 +269,27 @@
 }
 
+void MGVelocity::UpdateVelText(Float_t vx, Float_t vy)
+{
+    static int X = ~0;
+    static int Y = ~0;
+
+    vx /= 60.;  //['/min]
+    vy /= 60.;  //['/min]
+
+    int fx = (int)floor(vx*10.);
+    int fy = (int)floor(vy*10.);
+
+    if (X==fx && Y==fy)
+        return;
+
+    X = fx;
+    Y = fy;
+
+    char txt[100];
+    sprintf(txt, "Zd=%.1f'\nAz=%.1f'",
+            vx, vy);
+
+    fTextVel->SetText(fTextVel->GetX(), fTextVel->GetY(), txt);
+}
 
 void MGVelocity::Update(ZdAz &zdaz)
@@ -272,9 +301,11 @@
     //    static int Y = 0xaffe;
 
-    float x = zdaz.Az()*3600.;
-    float y = zdaz.Zd()*3600.;
-
-    int pixx = (int)(x*fScale/fPix);
-    int pixy = (int)(y*fScale/fPix);
+    float vx = zdaz.Az()*3600.; // ["/min]
+    float vy = zdaz.Zd()*3600.; // ["/min]
+
+    UpdateVelText(vx, vy);
+
+    int pixx = (int)(vx*fScale/fPix);
+    int pixy = (int)(vy*fScale/fPix);
 
     //
@@ -284,5 +315,5 @@
 
     if (pixx || pixy)
-        rc = UpdateAvg(x, y);
+        rc = UpdateAvg(vx, vy);
 
     if (rc)
@@ -296,12 +327,12 @@
     //    cout << zdaz.Az()*3600. << " " << zdaz.Zd()*3600. << endl;
 
-    x *= fScale;
-    y *= fScale;
-
-    fArrow->SetX2(x);
-    fArrow->SetY2(y);
-
-    fArrowX->SetX2(x);
-    fArrowY->SetY2(y);
+    vx *= fScale;
+    vy *= fScale;
+
+    fArrow->SetX2(vx);
+    fArrow->SetY2(vy);
+
+    fArrowX->SetX2(vx);
+    fArrowY->SetY2(vy);
 
     fOld->Set(pixx, pixy);
@@ -309,4 +340,3 @@
     SetModified();
     UpdateCanvas();
-
-}
+}
Index: /trunk/MagicSoft/Cosy/gui/MGVelocity.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGVelocity.h	(revision 920)
+++ /trunk/MagicSoft/Cosy/gui/MGVelocity.h	(revision 921)
@@ -31,4 +31,5 @@
 
     TText *fText;
+    TText *fTextVel;
 
     XY *fOld;
@@ -43,4 +44,5 @@
     void DrawCoordinateSystem();
     void UpdateText();
+    void UpdateVelText(Float_t x, Float_t y);
 
 public:
