Fast Research Interface Library  Manual and Documentation
src/FastResearchInterfaceLibrary/SetControlScheme.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 <pthread.h>
00058 #include <errno.h>
00059 #include <string.h>
00060 #include <friComm.h>
00061 #include <OSAbstraction.h>
00062 
00063 
00064 // ****************************************************************
00065 // SetControlScheme()
00066 //
00067 int FastResearchInterface::SetControlScheme(const unsigned int &ControlScheme)
00068 {
00069     unsigned int        i               =   0;
00070 
00071     int                 ResultValue     =   0;
00072 
00073     float               FloatValues[2 * FRI_CART_FRM_DIM];
00074 
00075     memset(FloatValues, 0x0, 2 * LBR_MNJ * sizeof(float));
00076 
00077     if (this->GetFRIMode() == FRI_STATE_MON)
00078     {
00079         switch (ControlScheme)
00080         {
00081         case FastResearchInterface::JOINT_POSITION_CONTROL:
00082             pthread_mutex_lock(&(this->MutexForControlData));
00083             this->CommandData.cmd.cmdFlags  =   0;
00084             this->CommandData.cmd.cmdFlags  |=  FRI_CMD_JNTPOS;
00085             pthread_mutex_unlock(&(this->MutexForControlData));
00086 
00087             // let the KRL program start the joint position controller
00088             this->SetKRLIntValue(14, 10);
00089             break;
00090         case FastResearchInterface::CART_IMPEDANCE_CONTROL:
00091             pthread_mutex_lock(&(this->MutexForControlData));
00092             this->CommandData.cmd.cmdFlags  =   0;
00093             this->CommandData.cmd.cmdFlags  |=  FRI_CMD_CARTPOS;
00094             this->CommandData.cmd.cmdFlags  |=  FRI_CMD_TCPFT;
00095             this->CommandData.cmd.cmdFlags  |=  FRI_CMD_CARTSTIFF;
00096             this->CommandData.cmd.cmdFlags  |=  FRI_CMD_CARTDAMP;
00097             pthread_mutex_unlock(&(this->MutexForControlData));
00098 
00099             // let the KRL program start the Cartesian impedance controller
00100             this->SetKRLIntValue(14, 20);
00101             break;
00102         case FastResearchInterface::JOINT_IMPEDANCE_CONTROL:
00103             pthread_mutex_lock(&(this->MutexForControlData));
00104             this->CommandData.cmd.cmdFlags  =   0;
00105             this->CommandData.cmd.cmdFlags  |=  FRI_CMD_JNTPOS;
00106             this->CommandData.cmd.cmdFlags  |=  FRI_CMD_JNTTRQ;
00107             this->CommandData.cmd.cmdFlags  |=  FRI_CMD_JNTSTIFF;
00108             this->CommandData.cmd.cmdFlags  |=  FRI_CMD_JNTDAMP;
00109             pthread_mutex_unlock(&(this->MutexForControlData));
00110 
00111             // let the KRL program start the joint impedance controller
00112             this->SetKRLIntValue(14, 30);
00113             break;
00114         default:
00115             return(EINVAL);
00116         }
00117 
00118         if (ControlScheme == FastResearchInterface::CART_IMPEDANCE_CONTROL)
00119         {
00120             this->SetKRLIntValue(13, 0);
00121             this->SetCommandedCartForcesAndTorques(FloatValues);
00122             for (i = 0; i < FRI_CART_FRM_DIM; i++)
00123             {
00124                 FloatValues[i]  =   (float)0.7;
00125             }
00126             this->SetCommandedCartDamping(FloatValues);
00127             for (i = 0; i < FRI_CART_FRM_DIM; i++)
00128             {
00129                 FloatValues[i]  =   (i < 3)?(1000.0):(100.0);
00130             }
00131             this->SetCommandedCartStiffness(FloatValues);
00132 
00133             this->GetCommandedCartPose(&(FloatValues[0]));
00134             this->GetCommandedCartPoseOffsets(&(FloatValues[FRI_CART_FRM_DIM]));
00135 
00136             // Regarding the documentation, we should do this
00137             /* -------------------------------------------------------------
00138             for (i = 0; i < FRI_CART_FRM_DIM; i++)
00139             {
00140                 FloatValues[i]  +=  FloatValues[i + FRI_CART_FRM_DIM];
00141             }
00142             //------------------------------------------------------------- */
00143 
00144             this->SetCommandedCartPose(FloatValues);
00145         }
00146         else
00147         {
00148             this->SetCommandedJointTorques(FloatValues);
00149             this->SetKRLIntValue(13, 0);
00150             this->GetMeasuredJointPositions(&(FloatValues[0]));
00151             this->GetCommandedJointPositionOffsets(&(FloatValues[LBR_MNJ]));
00152 
00153             // Regarding the documentation, we should do this
00154             /* -------------------------------------------------------------
00155             for (i = 0; i < LBR_MNJ; i++)
00156             {
00157                 FloatValues[i] +=   FloatValues[i + LBR_MNJ];
00158             }
00159             ------------------------------------------------------------- */
00160 
00161             this->SetCommandedJointPositions(FloatValues);
00162         }
00163 
00164         // wait for the next data telegram of the KRC unit
00165         pthread_mutex_lock(&(this->MutexForControlData));
00166         this->NewDataFromKRCReceived    =   false;
00167         pthread_mutex_unlock(&(this->MutexForControlData));
00168         ResultValue =   this->WaitForKRCTick(((unsigned int)(this->CycleTime * 3000000.0)));
00169 
00170         if (ResultValue != EOK)
00171         {
00172             this->OutputConsole->printf("FastResearchInterface::StopRobot(): ERROR, the KRC unit does not respond anymore. Probably, the FRI was closed or there is no UDP connection anymore.");
00173             return(ENOTCONN);
00174         }
00175 
00176         return(EOK);
00177     }
00178     else
00179     {
00180         if (this->GetFRIMode() == FRI_STATE_CMD)
00181         {
00182             return(EBUSY);
00183         }
00184         else
00185         {
00186             return(ENOTCONN);
00187         }
00188     }
00189 }
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.