Austin Schuh | 3333ec7 | 2022-12-29 16:21:06 -0800 | [diff] [blame^] | 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 | |
| 32 | struct timeutil_rest |
| 33 | { |
| 34 | int64_t acc_time; |
| 35 | int64_t start_time; |
| 36 | }; |
| 37 | |
| 38 | timeutil_rest_t *timeutil_rest_create() |
| 39 | { |
| 40 | timeutil_rest_t *rest = calloc(1, sizeof(timeutil_rest_t)); |
| 41 | return rest; |
| 42 | } |
| 43 | |
| 44 | void timeutil_rest_destroy(timeutil_rest_t *rest) |
| 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 | |
| 115 | void timeutil_timer_reset(timeutil_rest_t *rest) |
| 116 | { |
| 117 | rest->start_time = utime_now(); |
| 118 | rest->acc_time = 0; |
| 119 | } |
| 120 | |
| 121 | void timeutil_timer_start(timeutil_rest_t *rest) |
| 122 | { |
| 123 | rest->start_time = utime_now(); |
| 124 | } |
| 125 | |
| 126 | void 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 | |
| 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 | } |