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


apriltag
Author(s): Edwin Olson , Max Krogius
autogenerated on Sun Apr 20 2025 02:08:47