Fast Research Interface Library  Manual and Documentation
src/FastResearchInterfaceLibrary/StartRobot.cpp
Go to the documentation of this file.
00001 //  ---------------------- Doxygen info ----------------------
00050 //  ----------------------------------------------------------
00051 //   For a convenient reading of this file's source code,
00052 //   please use a tab width of four characters.
00053 //  ----------------------------------------------------------
00054 
00055 
00056 #include <FastResearchInterface.h>
00057 #include <Console.h>
00058 #include <friComm.h>
00059 #include <OSAbstraction.h>
00060 #include <errno.h>
00061 #include <string.h>
00062 
00063 
00064 
00065 // ****************************************************************
00066 // StartRobot()
00067 //
00068 int FastResearchInterface::StartRobot(      const unsigned int &ControlMode
00069                                         ,   const float &TimeOutValueInSeconds)
00070 {
00071     int                 ResultValue             =   0;
00072 
00073     float               CommandValues[2 * LBR_MNJ]
00074                     ,   InitialSystemTimeValue  =   GetSystemTimeInSeconds(true);
00075 
00076     memset(CommandValues, 0x0, 2 * LBR_MNJ * sizeof(float));
00077 
00078     if (this->GetFRIMode() == FRI_STATE_OFF)
00079     {
00080         // wait until the FRI is in monitor mode
00081         while (     ((GetSystemTimeInSeconds() - InitialSystemTimeValue)    <   TimeOutValueInSeconds   )
00082                 &&  (this->GetFRIMode()                                     !=  FRI_STATE_MON           ))
00083         {
00084             delay(1);
00085         }
00086 
00087         if (this->GetFRIMode() == FRI_STATE_OFF)
00088         {
00089             this->OutputConsole->printf("FastResearchInterface::StartRobot(): ERROR, the KRC has not opened the FRI yet or no UDP connection can be established.\n");
00090             return(ENOTCONN);
00091         }
00092     }
00093 
00094     if (this->GetFRIMode() == FRI_STATE_CMD)
00095     {
00096         if (this->CurrentControlScheme  == ControlMode)
00097         {
00098             this->OutputConsole->printf("FastResearchInterface::StartRobot(): The robot has already been started.\n");
00099             return(EALREADY);
00100         }
00101         else
00102         {
00103             this->StopRobot();
00104         }
00105     }
00106 
00107     if (this->GetKRLIntValue(15) == 20)
00108     {
00109         this->StopRobot();
00110     }
00111 
00112 
00113     this->CurrentControlScheme  =   ControlMode;
00114 
00115     this->SetControlScheme(this->CurrentControlScheme);
00116 
00117     // The KRL program running on the robot controller waits for a
00118     // integer value change at position 15 (i.e., 16 in KRL).
00119     // A value of 10 lets the KRL program call friStart()
00120     this->SetKRLIntValue(15, 10);
00121 
00122     // wait for the next data telegram of the KRC unit
00123     pthread_mutex_lock(&(this->MutexForControlData));
00124     this->NewDataFromKRCReceived    =   false;
00125     pthread_mutex_unlock(&(this->MutexForControlData));
00126     ResultValue =   this->WaitForKRCTick(((unsigned int)(this->CycleTime * 3000000.0)));
00127 
00128     while (     ((GetSystemTimeInSeconds() - InitialSystemTimeValue)    < TimeOutValueInSeconds )
00129             &&  (this->GetFRIMode()                                     != FRI_STATE_CMD        )   )
00130     {
00131         ResultValue =   this->WaitForKRCTick((unsigned int)(this->CycleTime * 3000000.0));
00132 
00133         if (ResultValue != EOK)
00134         {
00135             this->OutputConsole->printf("FastResearchInterface::StartRobot(): ERROR, the KRC unit does not respond anymore. Probably, the FRI was closed or there is no UDP connection anymore.");
00136             return(ENOTCONN);
00137         }
00138     }
00139 
00140     if (this->GetFRIMode() == FRI_STATE_CMD)
00141     {
00142         while (     ((GetSystemTimeInSeconds() - InitialSystemTimeValue) < TimeOutValueInSeconds)
00143                 &&  (!(this->IsMachineOK()))
00144                 &&  (this->GetKRLIntValue(15) != 20)    )
00145         {
00146             ResultValue =   this->WaitForKRCTick(((unsigned int)(this->CycleTime * 3000000.0)));
00147 
00148             if (ResultValue != EOK)
00149             {
00150                 this->OutputConsole->printf("FastResearchInterface::StartRobot(): ERROR, the KRC unit does not respond anymore. Probably, the FRI was closed or there is no UDP connection anymore.");
00151                 return(ENOTCONN);
00152             }
00153         }
00154 
00155         if (this->IsMachineOK())
00156         {
00157             delay(12);  // KUKA interpolation cycle time
00158             return(EOK);
00159         }
00160         else
00161         {
00162             this->OutputConsole->printf("FastResearchInterface::StartRobot(): ERROR, command mode reached, but the robot could be turned on correctly. Check KRC state.");
00163             return(ETIME);
00164         }
00165     }
00166     else
00167     {
00168         this->OutputConsole->printf("FastResearchInterface::StartRobot(): ERROR, could not switch to command mode after timeout.\n");
00169         return(ETIME);
00170     }
00171 }
00172 
This document was generated with Doxygen on Thu Apr 12 2012 11:18:54. User documentation of the Fast Research Interface Library for the KUKA Lightweight Robot IV by the Stanford Robotics Research Group. Copyright 2010–2012.