Go to the documentation of this file.00001
00012 #ifndef __COMPONENT_H
00013 #define __COMPONENT_H
00014
00015 #include <ros/ros.h>
00016 #include <pthread.h>
00017 #include <string>
00018 #include <vector>
00019 #include <signal.h>
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <string.h>
00023
00025 #define DEFAULT_THREAD_DESIRED_HZ 40.0
00026 #define DEFAULT_THREAD_PRIORITY 25
00027
00028 #define USEC_PER_SEC 1000000
00029 #define NSEC_PER_SEC 1000000000
00030 #define Pi 3.141592654
00031
00033 enum ReturnValue{
00034 OK = 0,
00035 INITIALIZED,
00036 THREAD_RUNNING,
00037 ERROR = -1,
00038 NOT_INITIALIZED = -2,
00039 THREAD_NOT_RUNNING = -3,
00040 COM_ERROR = -4,
00041 NOT_ERROR = -5
00042 };
00044 enum States{
00045 INIT_STATE,
00046 STANDBY_STATE,
00047 READY_STATE,
00048 EMERGENCY_STATE,
00049 FAILURE_STATE,
00050 SHUTDOWN_STATE,
00051 UNKNOWN_STATE
00052 };
00053
00056 extern int clock_nanosleep(clockid_t __clock_id, int __flags,
00057 __const struct timespec *__req,
00058 struct timespec *__rem);
00059
00062 static inline void tsnorm(struct timespec *ts)
00063 {
00064 while (ts->tv_nsec >= NSEC_PER_SEC) {
00065 ts->tv_nsec -= NSEC_PER_SEC;
00066 ts->tv_sec++;
00067 }
00068 }
00070 static inline void radnorm(double* radians)
00071 {
00072 while (*radians >= (Pi)) {
00073 *radians -= 2.0 * Pi;
00074 }
00075 while (*radians <= (-Pi)) {
00076 *radians += 2.0 * Pi;
00077 }
00078 }
00081 static inline void radnorm2(double* radians)
00082 {
00083 while (*radians >= (Pi)) {
00084 *radians -= 2.0 * Pi;
00085 }
00086 while (*radians <= (-Pi)) {
00087 *radians += 2.0 * Pi;
00088 }
00089 }
00091 static inline long calcdiff(struct timespec t1, struct timespec t2)
00092 {
00093 long diff;
00094 diff = USEC_PER_SEC * ((int) t1.tv_sec - (int) t2.tv_sec);
00095 diff += ((int) t1.tv_nsec - (int) t2.tv_nsec) / 1000;
00096 return diff;
00097 }
00099 struct thread_param{
00100 int prio;
00101 int clock;
00102 };
00104 typedef struct thread_data{
00106 struct thread_param pthreadPar;
00108 double dDesiredHz;
00110 double dRealHz;
00112 pthread_t pthreadId;
00113 }thread_data;
00114
00116 void *AuxControlThread(void *threadParam);
00117
00118 using namespace std;
00119
00121 class Component{
00122 friend void *AuxControlThread(void *threadParam);
00123 protected:
00125 bool bInitialized;
00127 bool bRunning;
00129 thread_data threadData;
00131 vector<thread_data> vThreadData;
00133 States iState;
00135 States iOldState;
00137 string sComponentName;
00138 private:
00140 pthread_mutex_t mutexThread;
00141 public:
00143 Component(double desired_hz);
00145 virtual ~Component();
00150 virtual ReturnValue Setup();
00156 virtual ReturnValue ShutDown();
00162 virtual ReturnValue Start();
00167 virtual ReturnValue Stop();
00169 States GetState();
00171 virtual char *GetStateString();
00173 char *GetStateString(States state);
00176 double GetUpdateRate();
00177
00178 protected:
00181 virtual void ControlThread();
00185 virtual ReturnValue Open();
00189 virtual ReturnValue Close();
00192 virtual ReturnValue Allocate();
00195 virtual ReturnValue Free();
00199 virtual ReturnValue Configure();
00201 virtual void InitState();
00203 virtual void StandbyState();
00205 virtual void ReadyState();
00207 virtual void EmergencyState();
00209 virtual void FailureState();
00211 virtual void AllState();
00213 virtual void SwitchToState(States new_state);
00215 ReturnValue GetThreadData(thread_data *data);
00216 public:
00218 ReturnValue CreateTask(int prio, double frec, void *(*start_routine)(void*));
00219
00220
00221 };
00222
00223 #endif