Index: trunk/FACT++/src/drivectrl.cc
===================================================================
--- trunk/FACT++/src/drivectrl.cc	(revision 14494)
+++ trunk/FACT++/src/drivectrl.cc	(revision 14495)
@@ -346,5 +346,5 @@
             // Status 4: Tracking
             if (status1==0)
-                status1 = 0x100-Drive::State::kNotReady;
+                status1 = StateMachineImp::kSM_Error - Drive::State::kNotReady;
 
             const bool ready = (pdo3&0xef00ef)==0xef00ef;
@@ -673,4 +673,7 @@
     sources fSources;
 
+    string fLastCommand;  // Last tracking (RADEC) command
+    int fAutoResume;      // 0: disabled, 1: enables, 2: resuming
+
     // Status 0: Error
     // Status 1: Unlocked
@@ -720,4 +723,14 @@
     int SendCommand(const string &str, bool upd=true)
     {
+        // This happens if fLastCommand should be send,
+        // but the last command was not a tracking command
+        if (str.empty())
+        {
+            T::Info("Last command was not a tracking command. RESUME ignored.");
+            return T::GetCurrentState();
+        }
+
+        fLastCommand = str.compare(0, 6, "RADEC ")==0 ? str : "";
+
         fDrive.PostMessage(str);
         T::Message("Sending: "+str);
@@ -727,4 +740,15 @@
 
         return T::GetCurrentState();
+    }
+
+    int Resume()
+    {
+        if (fLastCommand.empty())
+        {
+            T::Info("Last command was not a tracking command. RESUME ignored.");
+            return T::GetCurrentState();
+        }
+
+        return SendCommand(fLastCommand, false);
     }
 
@@ -923,4 +947,14 @@
     }
 
+    int SetAutoResume(const EventImp &evt)
+    {
+        if (!CheckEventSize(evt.GetSize(), "SetAutoResume", 1))
+            return T::kSM_FatalError;
+
+        fAutoResume = evt.GetBool();
+
+        return T::GetCurrentState();
+    }
+
     int Unlock()
     {
@@ -1054,5 +1088,23 @@
         }
 
-        return fDrive.GetState();
+        const int state = fDrive.GetState();
+
+        if (!fLastCommand.empty())
+        {
+            // If auto resume is enabled and the drive is in error,
+            // resume tracking
+            if (fAutoResume==1 && state==StateMachineImp::kSM_Error)
+            {
+                Resume();
+                fAutoResume = 2;
+            }
+
+            // If drive got out of the error state,
+            // enable auto resume again
+            if (fAutoResume==2 && state!=StateMachineImp::kSM_Error)
+                fAutoResume = 1;
+        }
+
+        return state;
     }
 
@@ -1145,4 +1197,8 @@
              "|id:Wobble angle id (1 or 2)"
              "|Name[string]:Source name");
+
+        T::AddEvent("RESUME", StateMachineImp::kSM_Error)
+            (bind(&StateMachineDrive::Resume, this))
+            ("If drive is in Error state, this can b used to resume the last tracking command, if the last command sent to cosy was a tracking command.");
 
         T::AddEvent("MOON", State::kArmed, State::kTracking)
@@ -1192,9 +1248,13 @@
             ("Stop any kind of movement.");
 
+        T::AddEvent("SET_AUTORESUME", "B")
+            (bind(&StateMachineDrive::SetAutoResume, this, placeholders::_1))
+            ("Enable/disable auto resume"
+             "|resume[bool]:if enabled, drive is tracking and goes to error state, the last tracking command is repeated automatically.");
 
         // Verbosity commands
         T::AddEvent("SET_VERBOSE", "B")
             (bind(&StateMachineDrive::SetVerbosity, this, placeholders::_1))
-            ("set verbosity state"
+            ("Set verbosity state"
              "|verbosity[bool]:disable or enable verbosity for received data (yes/no), except dynamic data");
 
@@ -1304,4 +1364,6 @@
             }
         }
+
+        fAutoResume = conf.Get<bool>("auto-resume");
 
         if (conf.Has("source-database"))
@@ -1346,4 +1408,5 @@
         ("source-database", var<string>(), "Database link as in\n\tuser:password@server[:port]/database.")
         ("source", vars<string>(), "Additional source entry in the form \"name,hh:mm:ss,dd:mm:ss\"")
+        ("auto-resume", po_bool(false), "Enable auto result during tracking")
         ;
 
