Apollo 10.0
自动驾驶开放平台
rate.cc
浏览该文件的文档.
1/*********************************************************************
2 *
3 * Software License Agreement (BSD License)
4 *
5 * Copyright (c) 2009, Willow Garage, Inc.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * * Neither the name of the Willow Garage nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 *
35 * Author: Eitan Marder-Eppstein
36 *********************************************************************/
37
38/******************************************************************************
39 * Copyright 2018 The Apollo Authors. All Rights Reserved.
40 *
41 * Licensed under the Apache License, Version 2.0 (the "License");
42 * you may not use this file except in compliance with the License.
43 * You may obtain a copy of the License at
44 *
45 * http://www.apache.org/licenses/LICENSE-2.0
46 *
47 * Unless required by applicable law or agreed to in writing, software
48 * distributed under the License is distributed on an "AS IS" BASIS,
49 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50 * See the License for the specific language governing permissions and
51 * limitations under the License.
52 *****************************************************************************/
53
54#include "cyber/time/rate.h"
55
56#include "cyber/common/log.h"
57
58namespace apollo {
59namespace cyber {
60
61Rate::Rate(double frequency)
62 : start_(Time::Now()),
63 expected_cycle_time_(1.0 / frequency),
64 actual_cycle_time_(0.0) {}
65
66Rate::Rate(uint64_t nanoseconds)
67 : start_(Time::Now()),
68 expected_cycle_time_(static_cast<int64_t>(nanoseconds)),
69 actual_cycle_time_(0.0) {}
70
72 : start_(Time::Now()), expected_cycle_time_(d), actual_cycle_time_(0.0) {}
73
75 Time expected_end = start_ + expected_cycle_time_;
76
77 Time actual_end = Time::Now();
78
79 // detect backward jumps in time
80 if (actual_end < start_) {
81 AWARN << "Detect backward jumps in time";
82 expected_end = actual_end + expected_cycle_time_;
83 }
84
85 // calculate the time we'll sleep for
86 Duration sleep_time = expected_end - actual_end;
87
88 // set the actual amount of time the loop took in case the user wants to kNow
89 actual_cycle_time_ = actual_end - start_;
90
91 // make sure to reset our start time
92 start_ = expected_end;
93
94 // if we've taken too much time we won't sleep
95 if (sleep_time < Duration(0.0)) {
96 AWARN << "Detect forward jumps in time";
97 // if we've jumped forward in time, or the loop has taken more than a full
98 // extra cycle, reset our cycle
99 if (actual_end > expected_end + expected_cycle_time_) {
100 start_ = actual_end;
101 }
102 // return false to show that the desired rate was not met
103 return;
104 }
105
106 Time::SleepUntil(expected_end);
107}
108
109void Rate::Reset() { start_ = Time::Now(); }
110
111Duration Rate::CycleTime() const { return actual_cycle_time_; }
112
113} // namespace cyber
114} // namespace apollo
Duration CycleTime() const
Definition rate.cc:111
Rate(double frequency)
Definition rate.cc:61
Cyber has builtin time type Time.
Definition time.h:31
static void SleepUntil(const Time &time)
Sleep Until time.
Definition time.cc:112
static Time Now()
get the current time.
Definition time.cc:57
#define AWARN
Definition log.h:43
class register implement
Definition arena_queue.h:37