time_util.c
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2 All rights reserved.
3 
4 This software was developed in the APRIL Robotics Lab under the
5 direction of Edwin Olson, ebolson@umich.edu. This software may be
6 available under alternative licensing terms; contact the address above.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 The views and conclusions contained in the software and documentation are those
29 of the authors and should not be interpreted as representing official policies,
30 either expressed or implied, of the Regents of The University of Michigan.
31 */
32 
33 #include <stdlib.h>
34 #include <math.h>
35 #include "time_util.h"
36 
38 {
39  int64_t acc_time;
40  int64_t start_time;
41 };
42 
44 {
45  timeutil_rest_t *rest = calloc(1, sizeof(timeutil_rest_t));
46  return rest;
47 }
48 
50 {
51  free(rest);
52 }
53 
54 int64_t utime_now() // blacklist-ignore
55 {
56  struct timeval tv;
57  gettimeofday (&tv, NULL); // blacklist-ignore
58  return (int64_t) tv.tv_sec * 1000000 + tv.tv_usec;
59 }
60 
61 int64_t utime_get_seconds(int64_t v)
62 {
63  return v/1000000;
64 }
65 
66 int64_t utime_get_useconds(int64_t v)
67 {
68  return v%1000000;
69 }
70 
71 void utime_to_timeval(int64_t v, struct timeval *tv)
72 {
73  tv->tv_sec = (time_t) utime_get_seconds(v);
74  tv->tv_usec = (suseconds_t) utime_get_useconds(v);
75 }
76 
77 void utime_to_timespec(int64_t v, struct timespec *ts)
78 {
79  ts->tv_sec = (time_t) utime_get_seconds(v);
80  ts->tv_nsec = (suseconds_t) utime_get_useconds(v)*1000;
81 }
82 
83 int32_t timeutil_usleep(int64_t useconds)
84 {
85  // unistd.h function, but usleep is obsoleted in POSIX.1-2008.
86  // TODO: Eventually, rewrite this to use nanosleep
87  return usleep(useconds);
88 }
89 
90 uint32_t timeutil_sleep(unsigned int seconds)
91 {
92  // unistd.h function
93  return sleep(seconds);
94 }
95 
96 int32_t timeutil_sleep_hz(timeutil_rest_t *rest, double hz)
97 {
98  int64_t max_delay = 1000000L/hz;
99  int64_t curr_time = utime_now();
100  int64_t diff = curr_time - rest->start_time;
101  int64_t delay = max_delay - diff;
102  if (delay < 0) delay = 0;
103 
104  int32_t ret = timeutil_usleep(delay);
105  rest->start_time = utime_now();
106 
107  return ret;
108 }
109 
111 {
112  rest->start_time = utime_now();
113  rest->acc_time = 0;
114 }
115 
117 {
118  rest->start_time = utime_now();
119 }
120 
122 {
123  int64_t curr_time = utime_now();
124  int64_t diff = curr_time - rest->start_time;
125 
126  rest->acc_time += diff;
127 }
128 
129 bool timeutil_timer_timeout(timeutil_rest_t *rest, double timeout_s)
130 {
131  int64_t timeout_us = (int64_t)(1000000L*timeout_s);
132  return rest->acc_time > timeout_us;
133 }
134 
135 int64_t time_util_hhmmss_ss_to_utime(double time)
136 {
137  int64_t utime = 0;
138 
139  int itime = ((int) time);
140 
141  double seconds = fmod(time, 100.0);
142  uint8_t minutes = (itime % 10000) / 100;
143  uint8_t hours = itime / 10000;
144 
145  utime += seconds * 100;
146  utime += minutes * 6000;
147  utime += hours *360000;
148 
149  utime *= 10000;
150 
151  return utime;
152 }
bool timeutil_timer_timeout(timeutil_rest_t *rest, double timeout_s)
Definition: time_util.c:129
void timeutil_timer_start(timeutil_rest_t *rest)
Definition: time_util.c:116
int64_t utime_get_useconds(int64_t v)
Definition: time_util.c:66
int64_t acc_time
Definition: time_util.c:39
timeutil_rest_t * timeutil_rest_create()
Definition: time_util.c:43
uint32_t timeutil_sleep(unsigned int seconds)
Definition: time_util.c:90
int64_t start_time
Definition: time_util.c:40
void utime_to_timeval(int64_t v, struct timeval *tv)
Definition: time_util.c:71
int32_t timeutil_usleep(int64_t useconds)
Definition: time_util.c:83
int64_t utime_now()
Definition: time_util.c:54
int64_t utime_get_seconds(int64_t v)
Definition: time_util.c:61
void utime_to_timespec(int64_t v, struct timespec *ts)
Definition: time_util.c:77
int64_t time_util_hhmmss_ss_to_utime(double time)
Definition: time_util.c:135
void timeutil_timer_stop(timeutil_rest_t *rest)
Definition: time_util.c:121
int32_t timeutil_sleep_hz(timeutil_rest_t *rest, double hz)
Definition: time_util.c:96
void timeutil_rest_destroy(timeutil_rest_t *rest)
Definition: time_util.c:49
void timeutil_timer_reset(timeutil_rest_t *rest)
Definition: time_util.c:110


apriltags2
Author(s): Danylo Malyuta
autogenerated on Fri Oct 19 2018 04:02:32