Fast Research Interface Library  Manual and Documentation
src/FastResearchInterfaceLibrary/TimerThreadMain.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 <stdio.h>
00059 #include <OSAbstraction.h>
00060 
00061 
00062 #ifdef _NTO_
00063 
00064 
00065 
00066 #include <sys/neutrino.h>
00067 #include <sched.h>
00068 
00069 
00070 #define TIMER_PULSE                 (_PULSE_CODE_MINAVAIL)
00071 
00072 
00073 
00074 // ****************************************************************
00075 // TimerThreadMain()
00076 //
00077 void* FastResearchInterface::TimerThreadMain(void *ObjectPointer)
00078 {
00079     int                             OurChannelID    =   0
00080                                 ,   ReceptionID     =   0;
00081 
00082     timer_t                         TimerID;
00083 
00084     struct sigevent                 Event;
00085 
00086     struct itimerspec               Timer;
00087 
00088     struct _pulse                   PulseMsg;
00089 
00090     FastResearchInterface           *ThisObjectPtr      =   (FastResearchInterface*)ObjectPointer;
00091 
00092     OurChannelID                =   ChannelCreate(0); //create communication channel
00093 
00094     // Initialize event data structure
00095     // attach the timer to the channel OurChannelID
00096     Event.sigev_notify          =   SIGEV_PULSE;
00097     Event.sigev_coid            =   ConnectAttach(0, 0, OurChannelID, _NTO_SIDE_CHANNEL, 0);
00098     Event.sigev_priority        =   getprio(0);
00099     Event.sigev_code            =   TIMER_PULSE;
00100 
00101     timer_create(CLOCK_REALTIME, &Event, &TimerID);
00102 
00103     // Configure the timer
00104     Timer.it_value.tv_sec       =   0L;
00105     Timer.it_value.tv_nsec      =   (long int)(1000000000.0 * ThisObjectPtr->CycleTime);    // wait one cycle time interval before start
00106     Timer.it_interval.tv_sec    =   0L;
00107     Timer.it_interval.tv_nsec   =   (long int)(1000000000.0 * ThisObjectPtr->CycleTime);
00108 
00109     pthread_mutex_lock(&(ThisObjectPtr->MutexForThreadCreation));
00110     ThisObjectPtr->ThreadCreated    =   true;
00111     pthread_mutex_unlock(&(ThisObjectPtr->MutexForThreadCreation));
00112 
00113     pthread_cond_signal(&(ThisObjectPtr->CondVarForThreadCreation));
00114 
00115     // Start the timer
00116     timer_settime(TimerID, 0, &Timer, NULL);
00117 
00118     pthread_mutex_lock(&(ThisObjectPtr->MutexForCondVarForTimer));
00119 
00120     while(!(ThisObjectPtr->TerminateTimerThread))
00121     {
00122         pthread_mutex_unlock(&(ThisObjectPtr->MutexForCondVarForTimer));
00123 
00124         ReceptionID = MsgReceive(OurChannelID, &PulseMsg, sizeof(PulseMsg), NULL);
00125 
00126         pthread_mutex_lock(&(ThisObjectPtr->MutexForCondVarForTimer));
00127         if (ReceptionID == 0)
00128         {
00129             ThisObjectPtr->TimerFlag = true;
00130             pthread_cond_signal(&(ThisObjectPtr->CondVarForTimer));
00131         }
00132     }
00133     pthread_mutex_unlock(&(ThisObjectPtr->MutexForCondVarForTimer));
00134 
00135     if (timer_delete(TimerID) != 0)
00136     {
00137         ThisObjectPtr->OutputConsole->printf("FastResearchInterface::TimerThreadMain(): ERROR, cannot delete timer...\n");
00138     }
00139 
00140     pthread_exit(NULL);
00141 }
00142 
00143 
00144 #endif // _NTO_
00145 
00146 
00147 #ifndef _NTO_
00148 
00149 
00150 
00151 // ****************************************************************
00152 // TimerThreadMain()
00153 //
00154 void* FastResearchInterface::TimerThreadMain(void *ObjectPointer)
00155 {
00156     int                             OurChannelID    =   0
00157                                 ,   ReceptionID     =   0;
00158 
00159     FastResearchInterface           *ThisObjectPtr  =   (FastResearchInterface*)ObjectPointer;
00160 
00161     pthread_mutex_lock(&(ThisObjectPtr->MutexForThreadCreation));
00162     ThisObjectPtr->ThreadCreated    =   true;
00163     pthread_mutex_unlock(&(ThisObjectPtr->MutexForThreadCreation));
00164 
00165     pthread_cond_signal(&(ThisObjectPtr->CondVarForThreadCreation));
00166 
00167     pthread_mutex_lock(&(ThisObjectPtr->MutexForCondVarForTimer));
00168 
00169     while(!(ThisObjectPtr->TerminateTimerThread))
00170     {
00171         pthread_mutex_unlock(&(ThisObjectPtr->MutexForCondVarForTimer));
00172 
00173         delay(1);
00174 
00175         pthread_mutex_lock(&(ThisObjectPtr->MutexForCondVarForTimer));
00176         if (ReceptionID == 0)
00177         {
00178             ThisObjectPtr->TimerFlag = true;
00179             pthread_cond_signal(&(ThisObjectPtr->CondVarForTimer));
00180         }
00181     }
00182     pthread_mutex_unlock(&(ThisObjectPtr->MutexForCondVarForTimer));
00183 
00184     pthread_exit(NULL);
00185     
00186     return (NULL);
00187 }
00188 
00189 
00190 #endif // WIN32
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.