blob: 7a25f424068d798a8c65c69c6c17dd05b2e2b950 [file] [log] [blame]
Austin Schuh3333ec72022-12-29 16:21:06 -08001/* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2All rights reserved.
3This software was developed in the APRIL Robotics Lab under the
4direction of Edwin Olson, ebolson@umich.edu. This software may be
5available under alternative licensing terms; contact the address above.
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
81. Redistributions of source code must retain the above copyright notice, this
9 list of conditions and the following disclaimer.
102. 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.
13THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20ON 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
22SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23The views and conclusions contained in the software and documentation are those
24of the authors and should not be interpreted as representing official policies,
25either 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
32struct timeutil_rest
33{
34 int64_t acc_time;
35 int64_t start_time;
36};
37
38timeutil_rest_t *timeutil_rest_create()
39{
40 timeutil_rest_t *rest = calloc(1, sizeof(timeutil_rest_t));
41 return rest;
42}
43
44void timeutil_rest_destroy(timeutil_rest_t *rest)
45{
46 free(rest);
47}
48
49int64_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
56int64_t utime_get_seconds(int64_t v)
57{
58 return v/1000000;
59}
60
61int64_t utime_get_useconds(int64_t v)
62{
63 return v%1000000;
64}
65
66void 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
72void 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
78int32_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
90uint32_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
101int32_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
115void timeutil_timer_reset(timeutil_rest_t *rest)
116{
117 rest->start_time = utime_now();
118 rest->acc_time = 0;
119}
120
121void timeutil_timer_start(timeutil_rest_t *rest)
122{
123 rest->start_time = utime_now();
124}
125
126void timeutil_timer_stop(timeutil_rest_t *rest)
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
134bool 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
140int64_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
159int64_t timeutil_ms_to_us(int32_t ms)
160{
161 return ((int64_t) ms) * 1000;
162}