Index: /trunk/MagicSoft/Cosy/Changelog
===================================================================
--- /trunk/MagicSoft/Cosy/Changelog	(revision 8868)
+++ /trunk/MagicSoft/Cosy/Changelog	(revision 8869)
@@ -1,3 +1,53 @@
                                                                   -*-*- END -*-*-
+
+ 2008/02/21 Thomas Bretz (La Palma)
+
+   * cosy.cc:
+     - moved instantisation of MDriveCom out of MCosy
+     - adde options to set the ceco ports
+
+   * candrv/canopen.cc:
+     - changed logging output
+
+   * devdrv/macs.cc:
+     - added some more DKC errors
+     - SetZombie if not armed
+     - added some debug output
+
+   * devdrv/shaftencoder.cc:
+     - added some initialisations as suggested by valgrind
+
+   * main/MSlewing.h:
+     - removed obsolete base class MLog
+
+   * main/MTracking.[h,cc]:
+     - replaced some fixed numbers by variables
+     - added forward declaration for MLog
+
+   * tcpip/MCeCoCom.[h,cc]:
+     - moved fixed port numbers out of the constructor
+
+   * tcpip/MDriveCom.[h,cc]:
+     - secured called to fQueue with if-conditions
+     - added Dummy for TPOIN command
+     - implemented ARM command
+     - added arm status to drive report
+     - added summy for sending TPOINT-REPORT
+
+   * tcpip/MTcpIpIO.cc:
+     - added time to connection messages
+     - added 0 as backlog for opening the TServerSocket.
+       This seems to be necessary (why?)
+
+   * videodev/Camera.cc:
+     - added initialisation of fChannel which was missing
+
+   * videodev/FilterLed.cc:
+     - changed a detele to an array delete as suggested by valgrind
+
+   * videodev/MVideo.cc:
+     - initialize qctrl as suggested by valgrind
+
+
 
  2008/02/20 Thomas Bretz (La Palma)
Index: /trunk/MagicSoft/Cosy/candrv/canopen.cc
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/canopen.cc	(revision 8868)
+++ /trunk/MagicSoft/Cosy/candrv/canopen.cc	(revision 8869)
@@ -276,5 +276,5 @@
     if (ms && t.HasTimedOut())
     {
-        cout << "WaitForSdos timed out." << endl;
+        gLog << inf << "- CanOpen::WaitForSdos timed out." << endl;
         rc = false;
     }
@@ -312,5 +312,5 @@
     if (ms && t.HasTimedOut())
     {
-        cout << "WaitForSdo Node #" << (int)node << " " << idx << "/" << (int)subidx << " timed out." << endl;
+        //cout << "WaitForSdo Node #" << (int)node << " " << idx << "/" << (int)subidx << " timed out." << endl;
         rc = false;
     }
Index: /trunk/MagicSoft/Cosy/cosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/cosy.cc	(revision 8868)
+++ /trunk/MagicSoft/Cosy/cosy.cc	(revision 8869)
@@ -14,4 +14,5 @@
 #include "MCosy.h"
 #include "MTime.h"
+#include "MDriveCom.h"
 
 #include "MLogManip.h"
@@ -92,4 +93,6 @@
     const TString sps           = arg.GetStringAndRemove("--sps=", "sps");
     const TString ceco          = arg.GetStringAndRemove("--cc=", "ceco"); // ceco
+    const Int_t   ceco_tx       = arg.GetIntAndRemove("--cc-tx=", 7314);//7304);
+    const Int_t   ceco_rx       = arg.GetIntAndRemove("--cc-rx=", 7414);//7404);
     const TString pointing      = arg.GetStringAndRemove("--pointing-model=", "bending2.txt"); // ceco
     const TString kConfig       = arg.GetStringAndRemove("--config=", ".cosyrc"); // ceco
@@ -191,6 +194,11 @@
             break;
         }
-*/
-    MCosy *cosy = new MCosy(env, ceco, pointing);
+        */
+
+    MDriveCom com(ceco, ceco_tx, ceco_rx);
+
+    MCosy *cosy = new MCosy(env, &com, pointing);
+
+    com.SetMsgQueue(cosy);
 
     Interface *interface = new Ethernet(sps, 5357, 5358, cosy);
@@ -211,4 +219,5 @@
         gLog << all << "- Starting Camera." << endl;
         client=new MStarguider(MObservatory::kMagic1, channel);
+        //client.SetDriveCom(&com);
         cosy->SetStarguider(client);
         client->SetCosy(cosy);
@@ -239,4 +248,6 @@
     gLog << all << "Deleting cosy at " << MTime(-1) << endl;
 
+    //com.SetMsgQueue(NULL);
+
     delete cosy;
 
Index: /trunk/MagicSoft/Cosy/devdrv/macs.cc
===================================================================
--- /trunk/MagicSoft/Cosy/devdrv/macs.cc	(revision 8868)
+++ /trunk/MagicSoft/Cosy/devdrv/macs.cc	(revision 8869)
@@ -85,4 +85,5 @@
     case 0xc500: return "Error reset";
     case 0xe225: return "Motor overload";
+    case 0xe249: return "Positioning command velocity exceeds limit bipolar";
     case 0xe250: return "Drive overtemp warning";
     case 0xe251: return "Motor overtemp warning";
@@ -108,7 +109,7 @@
     case 0xf630: return "Negative sw end-switch";
     case 0xf634: return "Emergency-Stop";
-    case 0xf643: return "Positive hw end-switch";
-    case 0xf644: return "Negative hw end-switch";
-    case 0xf870: return "24V DC error.";
+    case 0xf643: return "Positive hardware end-switch activated";
+    case 0xf644: return "Negative hardware end-switch activated";
+    case 0xf870: return "24V DC error";
     case 0xf878: return "Velocity loop error";
     }
@@ -514,5 +515,5 @@
 
     // REMOVE THIS AND LET CC START THE NODE
-    //StartNode();
+    StartNode();
 
     gLog << inf2 << "- " << GetNodeName() << ": Checking armed status." << endl;
@@ -638,8 +639,9 @@
     {
         gLog << err << GetNodeName() << ": ERROR - Moving without being armed is not allowed." << endl;
-        return;
-    }
-
-    gLog << dbg << GetNodeName() << ": Starting abolsute positioning to " << (LWORD_t)pos << " ticks." << endl;
+        SetZombie();
+        return;
+    }
+
+    gLog << dbg << GetNodeName() << ": Starting abolsute positioning to " << (LWORDS_t)pos << " ticks." << endl;
     SendSDO(0x6004, 1, (LWORD_t)pos);
     fPosActive = kTRUE; // Make sure that the status is set correctly already before the first PDO
@@ -651,8 +653,9 @@
     {
         gLog << err << GetNodeName() << ": ERROR - Moving without being armed is not allowed." << endl;
-        return;
-    }
-
-    gLog << dbg << GetNodeName() << ": Starting relative positioning by " << (LWORD_t)pos << " ticks." << endl;
+        SetZombie();
+        return;
+    }
+
+    gLog << dbg << GetNodeName() << ": Starting relative positioning by " << (LWORDS_t)pos << " ticks." << endl;
     SendSDO(0x6004, 0, (LWORD_t)pos);
     fPosActive = kTRUE; // Make sure that the status is set correctly already before the first PDO
Index: /trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc
===================================================================
--- /trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc	(revision 8868)
+++ /trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc	(revision 8869)
@@ -15,9 +15,10 @@
 ShaftEncoder::ShaftEncoder(const BYTE_t nodeid, const char *name)
     : NodeDrv(nodeid, name),
-    fPos(0), fVel(0), fAcc(0), fTurn(0), fTicks(0),
+    fPos(0), fVel(0), fAcc(0), fTurn(0), fTicks(0), fTurns(0),
     fDirection(kUndefined), fHysteresisPos(0), fHysteresisNeg(0),
     fLabel(NULL), fUpdPos(0),
     fPosHasChanged(false), fDirHasChanged(false),
-    fReport(NULL), fMotor(0), fOffset(0), fDirChangedPos(0)
+    fReport(NULL), fMotor(NULL),
+    fOffset(0), fDirChangedPos(0), fDirChangedOffset(0)
 {
 }
Index: /trunk/MagicSoft/Cosy/main/MSlewing.h
===================================================================
--- /trunk/MagicSoft/Cosy/main/MSlewing.h	(revision 8868)
+++ /trunk/MagicSoft/Cosy/main/MSlewing.h	(revision 8869)
@@ -2,6 +2,6 @@
 #define COSY_MSlewing
 
-#ifndef MARS_MLog
-#include "MLog.h"
+#ifndef ROOT_TROOT
+#include <TROOT.h>
 #endif
 
@@ -10,5 +10,5 @@
 class ZdAz;
 
-class MSlewing : public MLog
+class MSlewing
 {
 protected:
Index: /trunk/MagicSoft/Cosy/main/MTracking.cc
===================================================================
--- /trunk/MagicSoft/Cosy/main/MTracking.cc	(revision 8868)
+++ /trunk/MagicSoft/Cosy/main/MTracking.cc	(revision 8869)
@@ -269,8 +269,13 @@
      const Int_t paz  = fCosy->fAz->GetPos();
 
+     const Int_t res1 = fCosy->fZd1->GetPhysRes();
+     const Int_t res2 = fCosy->fZd2->GetPhysRes();
+
      // Get current shaftencoder position of the telescope
-     Double_t seposzd1 = ((pzd1+8192)%16384)*re.X();
-     Double_t seposzd2 = ((pzd2+8192)%16384)*re.X();
-     Double_t seposaz  =   paz              *re.Y();
+     Double_t seposzd1 = ((pzd1+res1/2)%res1)*re.X();
+     Double_t seposzd2 = ((pzd2+res2/2)%res2)*re.X();
+     //Double_t seposzd1 = pzd1*re.X();
+     //Double_t seposzd2 = pzd2*re.X();
+     Double_t seposaz  = paz *re.Y();
 
      // distance between (To+dt) and To [re]
Index: /trunk/MagicSoft/Cosy/main/MTracking.h
===================================================================
--- /trunk/MagicSoft/Cosy/main/MTracking.h	(revision 8868)
+++ /trunk/MagicSoft/Cosy/main/MTracking.h	(revision 8869)
@@ -10,4 +10,5 @@
 #endif
 
+class MLog;
 class RaDec;
 class SlaStars;
Index: /trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h	(revision 8868)
+++ /trunk/MagicSoft/Cosy/tcpip/MCeCoCom.h	(revision 8869)
@@ -48,6 +48,6 @@
 
 public:
-    MCeCoCom(const char *addr, MLog *out)
-        : MTcpIpIO(addr, 7304, 7404), fOut(out), fStatus(0), fComStat(kNoCmdReceived),
+    MCeCoCom(const char *addr, const int tx=7304, const int rx=7404, MLog *out=NULL)
+        : MTcpIpIO(addr, tx, rx), fOut(out), fStatus(0), fComStat(kNoCmdReceived),
         fHumidity(0), fTemperature(0), fWindSpeed(0), fSolarRadiation(-1),
         fAlarmCounter(0)
@@ -55,4 +55,6 @@
     }
     //virtual bool SendMsg(const char *msg, bool force);
+
+    void SetOutRep(MLog *out) { fOut = out; }
 
     bool SendRep(const char *cmd, const char *str, bool force);
Index: /trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 8868)
+++ /trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 8869)
@@ -61,5 +61,6 @@
 
     //cout << "MDriveCom - TRACK... start." << endl;
-    fQueue->PostMsg(WM_TRACK, &rd, sizeof(rd));
+    if (fQueue)
+        fQueue->PostMsg(WM_TRACK, &rd, sizeof(rd));
     //cout << "MDriveCom - TRACK... done." << endl;
     return true;
@@ -87,5 +88,6 @@
 
     //cout << "MDriveCom - TRACK... start." << endl;
-    fQueue->PostMsg(WM_GRB, &rd, sizeof(rd));
+    if (fQueue)
+        fQueue->PostMsg(WM_GRB, &rd, sizeof(rd));
     //cout << "MDriveCom - TRACK... done." << endl;
     return true;
@@ -112,5 +114,6 @@
 
     //cout << "MDriveCom - POSITION... start." << endl;
-    fQueue->PostMsg(WM_POSITION, &za, sizeof(za));
+    if (fQueue)
+        fQueue->PostMsg(WM_POSITION, &za, sizeof(za));
     //cout << "MDriveCom - POSITION... done." << endl;
     return true;
@@ -136,6 +139,13 @@
 
     //cout << "MDriveCom - TRACK... start." << endl;
-    fQueue->PostMsg(WM_PREPS, (void*)str.Data(), str.Length()+1);
+    if (fQueue)
+        fQueue->PostMsg(WM_PREPS, (void*)str.Data(), str.Length()+1);
     //cout << "MDriveCom - TRACK... done." << endl;
+    return true;
+}
+
+bool MDriveCom::CommandTPOINT(TString &str)
+{
+    cout << "CC-COMMAND " << MTime(-1) << " TPOIN " << str << endl;
     return true;
 }
@@ -166,5 +176,6 @@
     bool lock = str=="lock";
 
-    fQueue->PostMsg(WM_ARM, &lock, sizeof(lock));
+    if (fQueue)
+        fQueue->PostMsg(WM_ARM, &lock, sizeof(lock));
     return true;
 }
@@ -176,5 +187,6 @@
         //cout << "MDriveCom - WAIT... start." << endl;
         cout << "CC-COMMAND " << MTime(-1) << " WAIT" << endl;
-        fQueue->PostMsg(WM_WAIT);
+        if (fQueue)
+            fQueue->PostMsg(WM_WAIT);
         //cout << "MDriveCom - WAIT... done." << endl;
         return true;
@@ -185,5 +197,6 @@
         //cout << "MDriveCom - STOP!... start." << endl;
         cout << "CC-COMMAND " << MTime(-1) << " STOP!" << endl;
-        fQueue->PostMsg(WM_STOP);
+        if (fQueue)
+            fQueue->PostMsg(WM_STOP);
         //cout << "MDriveCom - STOP!... done." << endl;
         return true;
@@ -202,4 +215,10 @@
         return CommandPREPS(str);
 
+    if (cmd==(TString)"TPOIN")
+        return CommandTPOINT(str);
+
+    if (cmd==(TString)"ARM")
+        return CommandARM(str);
+
     if (cmd.IsNull() && str.IsNull())
     {
@@ -223,5 +242,5 @@
 }
 
-bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er)
+bool MDriveCom::SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed)
 {
     // rd [rad]
@@ -259,5 +278,6 @@
     Print(str, is.Az());
     str += txt.Print("%08.3f ", er.Zd());
-    str += txt.Print("%08.3f", er.Az());
+    str += txt.Print("%08.3f ", er.Az());
+    str += armed ? "1 " : "0 ";
 
     return SendRep("DRIVE-REPORT", str, kFALSE);
@@ -303,2 +323,39 @@
     return SendRep("STARG-REPORT", str, kTRUE);
 }
+
+bool MDriveCom::SendTPoint(UInt_t stat)
+{
+    /*
+    // miss   [deg]
+    // nompos [deg]
+    const MTime t(-1);
+
+    miss *= 60;        // [arcmin]
+
+    // Set status flag
+    if (stat&kError)
+        SetStatus(0);
+    if (stat&kStandby)
+        SetStatus(2);
+    if (stat&kMonitoring)
+        SetStatus(4);
+    
+    MString txt;
+
+    TString str;
+    str += txt.Print("%05.3f ", miss.Zd());       //[arcmin]
+    str += txt.Print("%05.3f ", miss.Az());       //[arcmin]
+    Print(str, nompos.Zd());                      //[deg]
+    Print(str, nompos.Az());                      //[deg]
+    str += txt.Print("%05.1f ",   center.GetX()); //number
+    str += txt.Print("%05.1f ",   center.GetY()); //number
+    str += txt.Print("%04d ",   n);               //number of correleated stars
+    str += txt.Print("%03.1f ",  bright);
+    str += txt.Print("%12.6f ", t.GetMjd());      // mjd
+    str += txt.Print("%.1f ", x);
+    str += txt.Print("%.1f ", y);
+    str += txt.Print("%04d ", num);               //number of detected stars
+    */
+
+    return SendRep("TPOINT-REPORT", "", kTRUE);
+}
Index: /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 8868)
+++ /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 8869)
@@ -28,4 +28,5 @@
     bool CommandPREPS(TString &str);
     bool CommandARM(TString &str);
+    bool CommandTPOINT(TString &str);
 
 public:
@@ -41,8 +42,13 @@
     };
 
-    MDriveCom(MsgQueue *q, const char *addr, MLog *out) : MCeCoCom(addr, out), fQueue(q) {}
+    MDriveCom(MsgQueue *q, const char *addr, const int tx, const int rx, MLog *out=NULL) : MCeCoCom(addr, tx, rx, out), fQueue(q) {}
+    MDriveCom(const char *addr, const int tx, const int rx, MLog *out=NULL) : MCeCoCom(addr, tx, rx, out), fQueue(NULL) {}
 
-    bool SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er);
+    void SetMsgQueue(MsgQueue *q) { fQueue = q; }
+
+
+    bool SendReport(UInt_t stat, RaDec rd, ZdAz so, ZdAz is, ZdAz er, Bool_t armed);
     bool SendStargReport(UInt_t stat, ZdAz miss, ZdAz nompos, Ring center, Int_t num, Int_t n, Double_t bright, Double_t mjd, Double_t x, Double_t y);
+    bool SendTPoint(UInt_t stat);
     bool SendStatus(const char *stat);
 };
Index: /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 8868)
+++ /trunk/MagicSoft/Cosy/tcpip/MTcpIpIO.cc	(revision 8869)
@@ -217,5 +217,5 @@
             if (!ReadSocket(sock))
             {
-                gLog << warn << "WARNING - Connection lost to " << MTcpIpO::GetSocketAddress(sock) << endl;
+                gLog << warn << MTime(-1) << " WARNING - Connection lost to " << MTcpIpO::GetSocketAddress(sock) << endl;
                 return kFALSE;
             }
@@ -228,5 +228,5 @@
             if (timeout.HasTimedOut())
             {
-                gLog << warn << "WARNING - Connection to " << MTcpIpO::GetSocketAddress(sock) << " timed out." << endl;
+                gLog << warn << MTime(-1) << " WARNING - Connection to " << MTcpIpO::GetSocketAddress(sock) << " timed out." << endl;
                 return kFALSE;
             }
@@ -245,9 +245,7 @@
 void MTcpIpI::WaitForConnection(TServerSocket &server)
 {
-    gLog << all << " Wait for connection" << endl;
-
     while (!IsThreadCanceled())
     {
-        gLog << all << "Listening for new connection on port " << fPortRx << "..." << endl;
+        gLog << all << MTime(-1) << " Listening for new connection on port " << fPortRx << "..." << endl;
 
         // Check for a connection request (reminder: we are in non-blocking mode)
@@ -256,4 +254,5 @@
         while (!IsThreadCanceled())
         {
+            //cout << (int) IsThreadCanceled() << endl;
             // Wait for a new connection on RX port
             socket = server.Accept();
@@ -281,5 +280,5 @@
             return;
 
-        gLog << all << "Connection established to " << MTcpIpO::GetSocketAddress(*socket) << "..." << endl;
+        gLog << all << MTime(-1) << " Connection established to " << MTcpIpO::GetSocketAddress(*socket) << "..." << endl;
 
         if (!WaitForData(*socket))
@@ -296,5 +295,5 @@
     while (!IsThreadCanceled())
     {
-        TServerSocket *server=new TServerSocket(fPortRx, kTRUE);
+        TServerSocket *server=new TServerSocket(fPortRx, kTRUE, 0);
         server->SetOption(kNoBlock, 1);
 
Index: /trunk/MagicSoft/Cosy/videodev/Camera.cc
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/Camera.cc	(revision 8868)
+++ /trunk/MagicSoft/Cosy/videodev/Camera.cc	(revision 8869)
@@ -34,5 +34,5 @@
 using namespace std;
 
-Camera::Camera(PixClient &client, Int_t nch) : MThread("Camera"), fClient(client), fVideo(0), fNumFrame(0)
+Camera::Camera(PixClient &client, Int_t nch) : MThread("Camera"), fClient(client), fVideo(0), fNumFrame(0), fChannel(nch)
 {
     fVideo = new MVideo;
Index: /trunk/MagicSoft/Cosy/videodev/FilterLed.cc
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 8868)
+++ /trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 8869)
@@ -92,5 +92,5 @@
     ~ClusterFinder()
     {
-        delete fImg;
+        delete [] fImg;
     }
     Double_t GetSumX() const { return fSumX; }
Index: /trunk/MagicSoft/Cosy/videodev/MVideo.cc
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/MVideo.cc	(revision 8868)
+++ /trunk/MagicSoft/Cosy/videodev/MVideo.cc	(revision 8869)
@@ -185,4 +185,5 @@
 {
     struct v4l2_queryctrl qctrl;
+    memset(&qctrl, 0, sizeof(qctrl));
     qctrl.id = id;
 
