Fast Research Interface Library  Manual and Documentation
src/FastResearchInterfaceLibrary/KRCCommunicationThreadMain.cpp
Go to the documentation of this file.
00001 //  ---------------------- Doxygen info ----------------------
00051 //  ----------------------------------------------------------
00052 //   For a convenient reading of this file's source code,
00053 //   please use a tab width of four characters.
00054 //  ----------------------------------------------------------
00055 
00056 
00057 #include <FastResearchInterface.h>
00058 #include <pthread.h>
00059 #include <sched.h>
00060 #include <string.h>
00061 #include <stdio.h>
00062 #include <friudp.h>
00063 #include <friComm.h>
00064 #include <OSAbstraction.h>
00065 
00066 
00067 #ifdef WIN32// \ToDo Make this clean through the OSAbstraction
00068 #include <Windows.h>    
00069 #endif
00070 
00071 
00072 
00073 // ****************************************************************
00074 // KRCCommunicationThreadMain()
00075 //
00076 void* FastResearchInterface::KRCCommunicationThreadMain(void *ObjectPointer)
00077 {
00078     int                             SequenceCounter                 =   0
00079                                 ,   ResultValue                     =   0;
00080 
00081     float                           ZeroVector[LBR_MNJ];
00082 
00083     friUdp                          KRC;
00084 
00085     tFriMsrData                     LocalReadData;
00086 
00087     tFriCmdData                     LocalCommandData;
00088 
00089     FastResearchInterface           *ThisObjectPtr                  =   (FastResearchInterface*)ObjectPointer;
00090 
00091     memset(ZeroVector, 0x0, LBR_MNJ * sizeof(float));
00092     
00093 #ifdef WIN32
00094     // \ToDo Make this clean through the OSAbstraction
00095     SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
00096 #endif  
00097 
00098     pthread_mutex_lock(&(ThisObjectPtr->MutexForThreadCreation));
00099     ThisObjectPtr->ThreadCreated    =   true;
00100     pthread_mutex_unlock(&(ThisObjectPtr->MutexForThreadCreation));
00101 
00102     pthread_cond_signal(&(ThisObjectPtr->CondVarForThreadCreation));
00103 
00104     for(;;)
00105     {
00106         // receive data from the KRC unit
00107         ResultValue =   KRC.Recv(&LocalReadData);
00108 
00109         if (ResultValue != 0)
00110         {
00111             ThisObjectPtr->OutputConsole->printf("FastResearchInterface::KRCCommunicationThreadMain(): ERROR during the reception of a UDP data package.\n");
00112         }
00113 
00114         pthread_mutex_lock(&(ThisObjectPtr->MutexForControlData));
00115 
00116         ThisObjectPtr->NewDataFromKRCReceived   =   true;
00117         ThisObjectPtr->ReadData             =   LocalReadData;
00118 
00119         if (ThisObjectPtr->TerminateKRCCommunicationThread)
00120         {
00121             pthread_mutex_unlock(&(ThisObjectPtr->MutexForControlData));
00122             break;
00123         }
00124 
00125         LocalCommandData    =   ThisObjectPtr->CommandData;
00126 
00127         SequenceCounter++;
00128         LocalCommandData.head.sendSeqCount  =   SequenceCounter;
00129         LocalCommandData.head.reflSeqCount  =   LocalReadData.head.sendSeqCount;
00130         LocalCommandData.head.datagramId    =   FRI_DATAGRAM_ID_CMD;
00131         LocalCommandData.head.packetSize    =   sizeof(tFriCmdData);
00132 
00133         pthread_mutex_unlock(&(ThisObjectPtr->MutexForControlData));
00134 
00135         pthread_cond_broadcast(&(ThisObjectPtr->CondVarForDataReceptionFromKRC));
00136 
00137         // send data to KRC unit
00138         ResultValue                     =   KRC.Send(&LocalCommandData);
00139 
00140         if (ResultValue != 0)
00141         {
00142             ThisObjectPtr->OutputConsole->printf("FastResearchInterface::KRCCommunicationThreadMain(): ERROR during the sending of a UDP data package.\n");
00143         }
00144 
00145         pthread_mutex_lock(&(ThisObjectPtr->MutexForLogging));
00146 
00147         if (ThisObjectPtr->LoggingIsActive)
00148         {
00149             pthread_mutex_unlock(&(ThisObjectPtr->MutexForLogging));
00150             ThisObjectPtr->DataLogger->AddEntry(        LocalReadData
00151                                                 ,   LocalCommandData);
00152         }
00153         else
00154         {
00155             pthread_mutex_unlock(&(ThisObjectPtr->MutexForLogging));
00156         }
00157     }
00158 
00159     pthread_exit(NULL);
00160     
00161     return (NULL);
00162 }
00163 
00164 
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.