Index: trunk/MagicSoft/Cosy/main/MStarguider.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 7230)
+++ trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 7297)
@@ -498,5 +498,5 @@
     fStargCaos = new MCaos;
     fStargCaos->ReadResources("stargleds.txt");
-    fStargCaos->SetMinNumberRings(2);
+    fStargCaos->SetMinNumberRings(3);
     fStargCaos->SetRadii(158,164);
 
@@ -1206,7 +1206,10 @@
     f.FindStarCircle(leds, (Int_t)center.GetX(), (Int_t)center.GetY());
 
+    if (leds.GetEntries()<0)
+        return ZdAz(0, 0);
+
     // Check whether star found
     Led *star = (Led*)leds.At(0);
-    if (!star || leds.GetEntries()<1)
+    if (!star)
         return ZdAz(.0,.0);
 
Index: trunk/MagicSoft/Cosy/main/MTracking.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MTracking.cc	(revision 7230)
+++ trunk/MagicSoft/Cosy/main/MTracking.cc	(revision 7297)
@@ -247,21 +247,26 @@
      const XY re = fCosy->kGearTot/fCosy->kResSE; //[re/se]
 
+     // Check wether moving direction has changed
      const bool bool1 = fCosy->fZd1->DirHasChanged();
      const bool bool2 = fCosy->fZd2->DirHasChanged();
 
-     if (bool1 && bool2) {
-       fCosy->fZd1->ResetDirHasChanged();
-       fCosy->fZd2->ResetDirHasChanged();
+     // If both directions have changed reset the flags
+     if (bool1 && bool2)
+     {
+         fCosy->fZd1->ResetDirHasChanged();
+         fCosy->fZd2->ResetDirHasChanged();
      }
- 
-     Int_t pzd1 = !bool1 ? fCosy->fZd1->GetPos() : fCosy->fZd1->GetDirChangedPos();
-     Int_t pzd2 = !bool2 ? fCosy->fZd2->GetPos() : fCosy->fZd2->GetDirChangedPos();
-     Int_t paz  =          fCosy->fAz->GetPos();
- 
+
+     // Get shaftencoder positions
+     // Ignore the shaftencoder which has not yet changed its value
+     const Int_t pzd1 = fCosy->fZd1->GetPosDirCorrected();
+     const Int_t pzd2 = fCosy->fZd2->GetPosDirCorrected();
+     const Int_t paz  = fCosy->fAz->GetPos();
+
      // 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();
- 
+
      // distance between (To+dt) and To [re]
      // position time difference < 5usec
@@ -269,8 +274,10 @@
      // Shaft- and the rotary encoders
      const ZdAz repos = pdo ? fCosy->GetRePosPdo() : fCosy->GetRePos();
- 
-     const Int_t offset1 = !bool1 ? fCosy->fZd1->GetOffset() : fCosy->fZd1->GetDirChangedOffset();
-     const Int_t offset2 = !bool2 ? fCosy->fZd2->GetOffset() : fCosy->fZd2->GetDirChangedOffset();
- 
+
+     // Get rotary encoder positions
+     // Get stored offset if one SE has not changed its direction yet
+     const Int_t offset1 = fCosy->fZd1->GetOffsetDirCorrected();
+     const Int_t offset2 = fCosy->fZd2->GetOffsetDirCorrected();
+
      // Calculate the part of one SE which the motors moved
      // since the last SE has changed its value
@@ -278,5 +285,5 @@
      const Double_t offzd2 = repos.Zd() - offset2;
      const Double_t offaz  = repos.Az() - fCosy->fAz->GetOffset();
- 
+
      // Correct for the direction in which the motor is moving
      // (in which the shaftencoders should change its values)
@@ -287,5 +294,18 @@
      if (offzd2<0)
          seposzd2 += re.X();
- 
+
+     // If the correction exceeds one shaftencoder step stop interpolation
+     // of shaftencoder positions using rotary encoder values.
+     //ofstream fout("offsets.log", ios::app);
+     //fout << MTime(-1) << " " << offaz << " " << offzd1 << " " << offzd2 << endl;
+     /*
+      if (TMath::Abs(offaz)>re.Y())
+         offaz = TMath::Sign(re.Y(), offaz);
+      if (TMath::Abs(offzd1)>re.X())
+         offzd1 = TMath::Sign(re.X(), offzd1);
+      if (TMath::Abs(offzd2)>re.X())
+         offzd2 = TMath::Sign(re.X(), offzd2);
+         */
+
      // and interpolate the shaftencoder steps using the motor
      // encoder positon (Be carefull the minus-sign is important)
@@ -293,21 +313,7 @@
      seposzd2 -= offzd2;
      seposaz  += offaz;
- 
-
 
      return ZdAz((seposzd1-seposzd2)/2, seposaz);
 }
-/*
-    if (fCosy->fZd1->DirHasChanged() != fCosy->fZd2->DirHasChanged())
-    {
-        pzd1 -= fCosy->fZd1->GetDirection();
-        pzd2 -= fCosy->fZd2->GetDirection();
-    }
-    else
-    {
-        fCosy->fZd1->ResetDirHasChanged();
-        fCosy->fZd2->ResetDirHasChanged();
-    }
-  */
 
 void MTracking::TrackPosition(const RaDec &dst) // ra, dec [rad]
