Fast Research Interface Library  Manual and Documentation
src/FastResearchInterfaceTest/FastResearchInterfaceTest.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 <time.h>
00058 #include <pthread.h>
00059 #include <stdlib.h>
00060 #include <math.h>
00061 #include <string.h>
00062 #include <errno.h>
00063 #include <OSAbstraction.h>
00064 #include <FastResearchInterfaceTest.h>
00065 
00066 
00067 
00068 #ifndef PI
00069 #define PI  3.1415926535897932384626433832795
00070 #endif
00071 
00072 #define NUMBER_OF_CYCLES_FOR_QUAULITY_CHECK     2000
00073 
00074 #define SIZE_OF_TRANSFER_STRING                 32
00075 
00076 //*******************************************************************************************
00077 // main()
00078 
00079 int main(int argc, char *argv[])
00080 {
00081     bool                    Run                         =   true
00082                         ,   StartRobotCalled            =   false;
00083 
00084     char                    c                           =   0
00085                         ,   d                           =   0
00086                         ,   TransferString[SIZE_OF_TRANSFER_STRING];
00087 
00088     unsigned int            ControlScheme               =   FastResearchInterface::JOINT_POSITION_CONTROL
00089                         ,   i                           =   0
00090                         ,   LoopValue                   =   0;
00091 
00092     int                     ResultValue                 =   0;
00093 
00094     float                   FloatValues[FRI_USER_SIZE]
00095                         ,   TmpFloatValues[FRI_USER_SIZE]
00096                         ,   DesiredTorqueValues[LBR_MNJ]
00097                         ,   JointStiffnessValues[LBR_MNJ]
00098                         ,   JointDampingValues[LBR_MNJ]
00099                         ,   CartStiffnessValues[FRI_CART_VEC]
00100                         ,   CartDampingValues[FRI_CART_VEC];
00101 
00102     FastResearchInterface   *FRI;
00103 
00104 
00105 
00106     memset(TransferString       , 0x0   , SIZE_OF_TRANSFER_STRING   * sizeof(char)  );
00107     memset(FloatValues          , 0x0   , FRI_USER_SIZE             * sizeof(float) );
00108     memset(TmpFloatValues       , 0x0   , FRI_USER_SIZE             * sizeof(float) );
00109     memset(DesiredTorqueValues  , 0x0   , LBR_MNJ                   * sizeof(float) );
00110 
00111 #ifdef WIN32
00112     FRI = new FastResearchInterface("E:\\Stanford\\Research\\SourceCode\\LWR_Public\\etc\\980039-FRI-Driver.init");
00113 #endif
00114 
00115 #ifdef __LINUX__
00116     fprintf(stdout, "You may need superuser permission to run this program.\n");
00117     fflush(stdout);
00118     FRI = new FastResearchInterface("/home/torsten/etc/980039-FRI-Driver.init");
00119 #endif
00120 
00121 #ifdef __MACOS__
00122     FRI = new FastResearchInterface("/Users/torsten/Documents/SourceCode/LWR_Public/LWR_Public/etc/980039-FRI-Driver.init");
00123 #endif
00124 
00125 #ifdef _NTO_
00126     FRI = new FastResearchInterface("/home/lwrcontrol/etc/980039-FRI-Driver2ms.init");
00127 #endif
00128 
00129     for (i = 0; i < LBR_MNJ; i++)
00130     {
00131         JointStiffnessValues    [i] =   (float)10.0;
00132         JointDampingValues      [i] =   (float)0.7;
00133     }
00134 
00135     for (i = 0; i < FRI_CART_VEC; i++)
00136     {
00137         CartStiffnessValues     [i] =   (float)10.0;
00138         CartDampingValues       [i] =   (float)0.7;
00139     }
00140 
00141     FRI->SetCommandedCartDamping(CartStiffnessValues);
00142     FRI->SetCommandedCartStiffness(CartDampingValues);
00143     FRI->SetCommandedJointDamping(JointDampingValues);
00144     FRI->SetCommandedJointStiffness(JointStiffnessValues);
00145 
00146     while (Run)
00147     {
00148         printf("---------------------------------------------------------------------------------------\n");
00149         printf("Press     q  for exit this program\n");
00150         printf("          s  for starting the KUKA Fast Research Interface\n");
00151         printf("          x  for stopping the KUKA Fast Research Interface\n");
00152         printf("          p  for printing system information\n");
00153         printf("          d  for changing 'D' (damping term) of the joint impedance controller\n");
00154         printf("          k  for changing 'k' (stiffness term) of the joint impedance controller\n");
00155         printf("          e  for changing 'D' (damping term) of the Cartesian impedance controller\n");
00156         printf("          l  for changing 'k' (stiffness term) of the Cartesian impedance controller\n");
00157         printf("          m  for getting the current parameters of the joint impedance controller\n");
00158         printf("          n  for getting the current parameters of the Cartesian impedance controller\n");
00159         printf("          g  for getting the current joint positions\n");
00160         printf("          h  for getting the current joint torques\n");
00161         printf("          i  for starting writing to an output file\n");
00162         printf("          j  for stopping writing to an output file\n");
00163         printf("          t  for start the joint position controller and run a simple trajectory\n");
00164         printf("          c  for moving to the candle position\n");
00165         printf("---------------------------------------------------------------------------------------\n\n");
00166         printf("Please press any key...\n");
00167 
00168         c   =   WaitForKBCharacter(NULL);
00169         
00170         printf("\n\n\n");
00171 
00172         switch (c)
00173         {
00174         case 'q':
00175         case 'Q':
00176             Run =   false;
00177             break;
00178         case 's':
00179         case 'S':
00180             printf("Starting the robot through the FRI...\n");
00181             printf("Please select one of the following control strategies:\n\n");
00182             printf(" 1: Joint position control\n");
00183             printf(" 2: Cartesian impedance control\n");
00184             printf(" 3: Joint impedance control\n");
00185             printf(" 9: Joint torque control\n");
00186             printf(" a: Abort\n\n");
00187             d   =   0;
00188             while ( (d != '1') && (d != '2') && (d != '3') && (d != '9') && (d != 'a') && (d != 'A'))
00189             {
00190                 d   =   WaitForKBCharacter(NULL);
00191                 printf("%c\n", c);
00192             }
00193             if ( (d == 'a') || (d == 'A'))
00194             {
00195                 printf("Control strategy remains unchanged.\n");
00196                 break;
00197             }
00198 
00199             switch (d)
00200             {
00201             case '1':
00202                 ControlScheme   =   FastResearchInterface::JOINT_POSITION_CONTROL;
00203                 printf("Control strategy set to joint position control.\n");
00204                 break;
00205             case '2':
00206                 ControlScheme   =   FastResearchInterface::CART_IMPEDANCE_CONTROL;
00207                 printf("Control strategy set to Cartesian impedance control.\n");
00208                 break;
00209             case '3':
00210                 ControlScheme   =   FastResearchInterface::JOINT_IMPEDANCE_CONTROL;
00211                 printf("Control strategy set to joint impedance control.\n");
00212                 break;
00213             }
00214 
00215             ResultValue =   FRI->StartRobot(ControlScheme);
00216 
00217             if (ResultValue != EOK)
00218             {
00219                 printf("An error occurred during starting up the robot...\n");
00220             }
00221             else
00222             {
00223                 StartRobotCalled    =   true;
00224             }
00225             break;
00226         case 'x':
00227         case 'X':
00228             printf("Stopping the FRI...\n");
00229             ResultValue =   FRI->StopRobot();
00230             StartRobotCalled    =   false;
00231 
00232             if (ResultValue != EOK)
00233             {
00234                 printf("An error occurred during stopping the robot...\n");
00235             }
00236             break;
00237         case 'p':
00238         case 'P':
00239             printf("Printing system information...\n");
00240             printf("%s\n", FRI->GetCompleteRobotStateAndInformation());
00241             delay(200);
00242             break;
00243         case 'd':
00244         case 'D':
00245         case 'k':
00246         case 'K':
00247         case 'e':
00248         case 'E':
00249         case 'l':
00250         case 'L':
00251             if ( (c == 'd') || (c == 'D'))
00252             {
00253                 printf("Changing the damping term of the joint impedance controller...\n");
00254             }
00255             else
00256             {
00257                 if ( (c == 'k') || (c == 'K'))
00258                 {
00259                     printf("Changing the stiffness term of the joint impedance controller...\n");
00260                 }
00261                 else
00262                 {
00263                     if ( (c == 'e') || (c == 'E') )
00264                     {
00265                         printf("Changing the damping term of the Cartesian impedance controller...\n");
00266                     }
00267                     else
00268                     {
00269                         printf("Changing the stiffness term of the Cartesian impedance controller...\n");
00270                     }
00271                 }
00272 
00273             }
00274 
00275             if ( (c == 'd') || (c == 'D') || (c == 'k') || (c == 'K') )
00276             {
00277                 LoopValue   =    LBR_MNJ;
00278             }
00279             else
00280             {
00281                 LoopValue   =    FRI_CART_VEC;
00282             }
00283 
00284             printf("\nWould you like to enter one value for all vector elements (a) or for each individual one (i)?\n");
00285 
00286             d   =   0;
00287             while ( (d != 'a') && (d != 'A') && (d != 'i') && (d != 'I') )
00288             {
00289                 d   =   WaitForKBCharacter(NULL);
00290                 printf("%c\n", c);
00291             }
00292 
00293             if ( (d == 'a') || (d == 'A') )
00294             {
00295                 printf("Please enter a new value for all vector elements:\n");
00296                 printf(">");
00297                 int tmp = scanf("%s", TransferString);  // "int tmp" to supress compiler warnings
00298                 for (i = 0; i < LoopValue; i++)
00299                 {
00300                     FloatValues[i] = atof(TransferString);
00301                 }
00302             }
00303             else
00304             {
00305                 if ( (d == 'i') || (d == 'I') )
00306                 {
00307                     printf("Please enter new values for each vector element:\n");
00308                     for (i = 0; i < LoopValue; i++)
00309                     {
00310                         printf("Element %d >", i);
00311                         int tmp = scanf("%s", TransferString);  // "int tmp" to supress compiler warnings
00312                         FloatValues[i] = atof(TransferString);
00313                     }
00314                 }
00315             }
00316 
00317             if ( (c == 'd') || (c == 'D'))
00318             {
00319                 printf("New damping term of the joint impedance controller:");
00320             }
00321             else
00322             {
00323                 if ( (c == 'k') || (c == 'K'))
00324                 {
00325                     printf("New stiffness term of the joint impedance controller:");
00326                 }
00327                 else
00328                 {
00329                     if ( (c == 'e') || (c == 'L'))
00330                     {
00331                         printf("New damping term of the Cartesian impedance controller:");
00332                     }
00333                     else
00334                     {
00335                         printf("New stiffness term of the Cartesian impedance controller:");
00336                     }
00337                 }
00338             }
00339 
00340             for (i = 0; i < LoopValue; i++)
00341             {
00342                 printf("%8.3f " , FloatValues[i]);
00343             }
00344 
00345             printf("\n\nIs this correct? (y/n)\n");
00346             d   =   0;
00347             while ( (d != 'y') && (d != 'Y') && (d != 'n') && (d != 'N') )
00348             {
00349                 d   =   WaitForKBCharacter(NULL);
00350                 printf("%c\n", c);
00351             }
00352 
00353             if ( (d == 'y') || (d == 'Y') )
00354             {
00355                 if ( (c == 'd') || (c == 'D'))
00356                 {
00357                     FRI->SetCommandedJointDamping(FloatValues);
00358                     for (i = 0; i < LoopValue; i++)
00359                     {
00360                         JointDampingValues[i]   =   FloatValues[i];
00361                     }
00362                     printf("New joint damping values are set.\n");
00363                 }
00364                 else
00365                 {
00366                     if ( (c == 'k') || (c == 'K'))
00367                     {
00368                         FRI->SetCommandedJointStiffness(FloatValues);
00369                         for (i = 0; i < LoopValue; i++)
00370                         {
00371                             JointStiffnessValues[i] =   FloatValues[i];
00372                         }
00373                         printf("New joint stiffness values are set.\n");
00374                     }
00375                     else
00376                     {
00377                         if ( (c == 'e') || (c == 'L'))
00378                         {
00379                             FRI->SetCommandedCartDamping(FloatValues);
00380                             for (i = 0; i < LoopValue; i++)
00381                             {
00382                                 CartDampingValues[i]    =   FloatValues[i];
00383                             }
00384                             printf("New Cartesian damping values are set.\n");
00385                         }
00386                         else
00387                         {
00388                             FRI->SetCommandedCartStiffness(FloatValues);
00389                             for (i = 0; i < LoopValue; i++)
00390                             {
00391                                 CartStiffnessValues[i]  =   FloatValues[i];
00392                             }
00393                             printf("New Cartesian stiffness values are set.\n");
00394                         }
00395                     }
00396                 }
00397             }
00398             else
00399             {
00400                 printf("Values remain unchanged.\n");
00401             }
00402             break;
00403         case 'g':
00404         case 'G':
00405             printf("Getting joint position values...\n");
00406             FRI->GetMeasuredJointPositions(FloatValues);
00407             for (i = 0; i < LBR_MNJ; i++)
00408             {
00409                 printf("Joint position %d: %8.3f degrees\n", i, FloatValues[i] * 180.0 / PI);
00410             }
00411             printf("\n\n");
00412             break;
00413         case 'm':
00414         case 'M':
00415             printf("Getting the current parameters of the joint impedance controller\n");
00416             for (i = 0; i < LBR_MNJ; i++)
00417             {
00418                 printf("Joint stiffness values %d: %8.3f Nm\n", i, JointStiffnessValues[i]);
00419             }
00420             printf("\n");
00421             for (i = 0; i < LBR_MNJ; i++)
00422             {
00423                 printf("Joint damping values %d: %8.3f Nms\n", i, JointDampingValues[i]);
00424             }
00425             printf("\n");
00426             break;
00427         case 'n':
00428         case 'N':
00429             printf("Getting the current parameters of the Cartesian impedance controller\n");
00430             for (i = 0; i < 3; i++)
00431             {
00432                 printf("Cartesian stiffness values %d: %8.3f N/m\n", i, CartStiffnessValues[i]);
00433             }
00434             for (i = 3; i < 6; i++)
00435             {
00436                 printf("Cartesian stiffness values %d: %8.3f Nm\n", i, CartStiffnessValues[i]);
00437             }
00438             printf("\n");
00439             for (i = 0; i < 3; i++)
00440             {
00441                 printf("Cartesian damping values %d: %8.3f Ns/m\n", i, CartDampingValues[i]);
00442             }
00443             for (i = 3; i < 6; i++)
00444             {
00445                 printf("Cartesian damping values %d: %8.3f Nms\n", i, CartDampingValues[i]);
00446             }
00447             printf("\n");
00448             break;
00449         case 'h':
00450         case 'H':
00451             printf("Getting joint torque values...\n");
00452             FRI->GetMeasuredJointTorques(FloatValues);
00453             for (i = 0; i < LBR_MNJ; i++)
00454             {
00455                 printf("Joint position %d: %8.3f Nm\n", i, FloatValues[i]);
00456             }
00457             printf("\n\n");
00458             break;
00459         case 'i':
00460         case 'I':
00461             printf("Starting to write an output file...\n");
00462             ResultValue =   FRI->PrepareLogging("Test");
00463             if (ResultValue == EOK)
00464             {
00465                 printf("Logging successfully prepared.\n");
00466             }
00467             else
00468             {
00469                 printf( "Error at FRI->PrepareLogging(): %s\n", strerror(ResultValue));
00470             }
00471 
00472             ResultValue =   FRI->StartLogging();
00473             if (ResultValue == EOK)
00474             {
00475                 printf("Logging successfully started.\n");
00476             }
00477             else
00478             {
00479                 printf( "Error at FRI->StartLogging(): %s\n", strerror(ResultValue));
00480             }
00481             break;
00482 
00483         case 'j':
00484         case 'J':
00485             printf("Stopping to write an output file...\n");
00486             ResultValue =   FRI->StopLogging();
00487             if (ResultValue == EOK)
00488             {
00489                 printf("Logging successfully stopped.\n");
00490             }
00491             else
00492             {
00493                 printf( "Error at FRI->StopLogging(): %s\n", strerror(ResultValue));
00494             }
00495 
00496             ResultValue =   FRI->WriteLoggingDataFile();
00497             if (ResultValue == EOK)
00498             {
00499                 printf("Logging data file successfully written.\n");
00500             }
00501             else
00502             {
00503                 printf( "Error at FRI->WriteLoggingDataFile(): %s\n", strerror(ResultValue));
00504             }
00505             break;
00506 
00507         case 't':
00508         case 'T':
00509             printf("Moving along a simple trajectory... (please wait)\n");
00510             RunTrajectorySimple(FRI);
00511             printf("Motion completed.\n");
00512             break;
00513             
00514         case 'c':
00515         case 'C':
00516             printf("Moving to the candle position... (please wait)\n");
00517             MoveToCandle(FRI);
00518             break;
00519 
00520         default:
00521             printf("This key is not supported yet...\n");
00522             break;
00523         }
00524     }
00525 
00526     delete FRI;
00527 
00528     printf("\nGood bye.\n\n");
00529 
00530     return(EXIT_SUCCESS);
00531 }
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.