timer.cpp
Go to the documentation of this file.
1 /**************************************************************************************************
2  Software License Agreement (BSD License)
3 
4  Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt
5  All rights reserved.
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted
8  provided that the following conditions are met:
9 
10  *Redistributions of source code must retain the above copyright notice, this list of
11  conditions and the following disclaimer.
12  *Redistributions in binary form must reproduce the above copyright notice, this list of
13  conditions and the following disclaimer in the documentation and/or other materials provided
14  with the distribution.
15  *Neither the name of the University of Aveiro nor the names of its contributors may be used to
16  endorse or promote products derived from this software without specific prior written permission.
17 
18  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***************************************************************************************************/
31 #ifndef _TIMER_CPP_
32 #define _TIMER_CPP_
33 
37 #include <atlasmv_base/timer.h>
38 
44 double lar_get_time()
45 {
46  struct timeval tv;
47  double t;
48 
49  if(gettimeofday(&tv, NULL)<0)
50  {
51  printf("\33[31mWARNING:\33[0m problem getting time\n");
52  }
53 
54  t = (tv.tv_sec + (tv.tv_usec/1000000.0));// function gettimeofday returns time in seconds with a precision of 0.01seconds
55 
56  return t;
57 }
62 {
63  // printf("class initialized\n");
64  for (int j=0;j<10;j++)
65  {
66  clock_set[j] = 0; //set all ipc flags to zero
67  lar_toc[j] = 0;
68  lar_tic[j] = 0.0;
69  }
70  //if(lar_ipc_sleep(0.01))
71  //{
72  //ipc_connected=0;
73  //}else
74  //{
75  //ipc_connected=1;
76  //};
77 
78  //pthread_mutex_init(&ipc_connected_mutex, NULL);
79  pthread_mutex_init(&clock_tic_mutex, NULL);
80  pthread_mutex_init(&clock_set_mutex, NULL);
81  pthread_mutex_init(&lar_tic_mutex, NULL);
82  pthread_mutex_init(&lar_toc_mutex, NULL);
83 
84 };
85 
90 {
91 }
92 
99 {
100  pthread_mutex_lock(&clock_set_mutex);
101  if(!clock_set[i])
102  {
103  clock_tic[i] = lar_get_time();
104  clock_set[i] = 1;
105  }
106  pthread_mutex_unlock(&clock_set_mutex);
107 };
108 
115 void c_timer::run_sleep(int i, double desired_freq)
116 {
117  double ts = (1/desired_freq);
118  double tsf;
119  unsigned int t;
120 
121  if(abs(i)<11)
122  {
123  //pthread_mutex_lock(&lar_toc_mutex);
124  //if(!lar_toc[i])
125  //{
126  //pthread_mutex_unlock(&lar_toc_mutex);
127  //toc(i);
128  //}
129  pthread_mutex_lock(&lar_tic_mutex);
130  tsf = ts - (ros::Time::now().toSec()-lar_tic[i]);
131  pthread_mutex_unlock(&lar_tic_mutex);
132  t=(tsf<0?100:tsf*1E6);
133  //printf("sleep:%d\n", t);
134  usleep(t);
135 }
136 
137 };
138 
148 char c_timer::run_ipc_sleep(double desired_freq, int i, char verbose)
149 {
150 // double ts = (1.0/desired_freq);
151 // double tsf = ts;
152 //
153 // pthread_mutex_lock(&clock_set_mutex);
154 // if(clock_set[i])
155 // {
156 // tsf = ts - (ros::time:now().to_sec()-clock_tic[i]);
157 //
158 // if(tsf <= MINIMUM_IPC_SLEEP)
159 // {
160 // if(verbose)
161 // {
162 // printf("\33[31mdesired frequency %3.1f NOK.\33[0m\n", desired_freq);
163 // printf("\33[31mactual frequency %3.2f.\33[0m\n", 1.0/(ros::time:now().to_sec()-clock_tic[i]));
164 // };
165 // tsf = MINIMUM_IPC_SLEEP;
166 // }else
167 // {
168 // if(verbose)
169 // {
170 // printf("\33[31mdesired frequency %3.1fHz OK.\33[0m\r", desired_freq); fflush(stdout);
171 // printf("\33[31mactual frequency %3.2f.\33[0m\n", 1.0/(ros::time:now().to_sec()-clock_tic[i]));
172 // }
173 // };
174 // }
175 //
176 //
177 // //carmen_ipc_sleep(tsf);
178 // char rt = lar_ipc_sleep(tsf);
179 // clock_set[i]=0;
180 // pthread_mutex_unlock(&clock_set_mutex);
181 
182  return 'A';
183 }
184 
190 void c_timer::tic(int i)
191 {
192  pthread_mutex_lock(&lar_tic_mutex);
193  if(abs(i)<11)
194  {
195  lar_tic[i] = ros::Time::now().toSec();
196  }
197  pthread_mutex_unlock(&lar_tic_mutex);
198 };
199 
205 void c_timer::toc(int i)
206 {
207  pthread_mutex_lock(&lar_toc_mutex);
208  pthread_mutex_lock(&lar_tic_mutex);
209  if(abs(i)<11)
210  lar_toc[i] = (ros::Time::now().toSec() - lar_tic[i]);
211 
212  pthread_mutex_unlock(&lar_tic_mutex);
213  pthread_mutex_unlock(&lar_toc_mutex);
214 };
215 
216 //added by dgameiro to allow get time stamp
217 double c_timer::get_toc(int i)
218 {
219  if(abs(i)<11)
220  {
221  pthread_mutex_lock(&lar_toc_mutex);
222  if(!lar_toc[i])
223  {
224  pthread_mutex_unlock(&lar_toc_mutex);
225  toc(i);
226  pthread_mutex_lock(&lar_toc_mutex);
227  }
228  double result = lar_toc[i];
229  pthread_mutex_unlock(&lar_toc_mutex);
230  return result;
231  }else{
232  // printf("error. can only exist 10");
233  }
234  return 0;
235 }
236 
237 #endif
238 
pthread_mutex_t clock_set_mutex
Definition: timer.h:82
char run_ipc_sleep(double, int, char)
to use this method is required to run set_ipc_sleep method previously
Definition: timer.cpp:148
void set_ipc_sleep(int)
method with run_ipc_sleep that allows that to set a refresh rate of a specified code.
Definition: timer.cpp:98
double lar_tic[10]
Definition: timer.h:85
double clock_tic[10]
Definition: timer.h:81
pthread_mutex_t lar_toc_mutex
Definition: timer.h:86
main header file for this module. Includes, global vars, funtion prototypes, etc. ...
void toc(int)
method when used with t1c enables you to know how many time have passed since
Definition: timer.cpp:205
pthread_mutex_t clock_tic_mutex
Definition: timer.h:80
double lar_toc[10]
Definition: timer.h:87
c_timer()
runned when created class
Definition: timer.cpp:61
void run_sleep(int, double)
method to use inside thread to avoid collisions between threads
Definition: timer.cpp:115
char clock_set[10]
Definition: timer.h:83
double lar_get_time()
function to get time
Definition: timer.cpp:44
pthread_mutex_t lar_tic_mutex
Definition: timer.h:84
~c_timer()
method ruuned when class is destroyed
Definition: timer.cpp:89
double get_toc(int)
Definition: timer.cpp:217
void tic(int)
method when used with toc enables you to know how many time have passed since
Definition: timer.cpp:190


atlasmv_base
Author(s): David Gameiro, Jorge Almeida
autogenerated on Mon Mar 2 2015 01:31:28