Apollo 10.0
自动驾驶开放平台
py_time.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#ifndef CYBER_PYTHON_INTERNAL_PY_TIME_H_
18#define CYBER_PYTHON_INTERNAL_PY_TIME_H_
19
20#include <unistd.h>
21#include <memory>
22
23#include "cyber/cyber.h"
24#include "cyber/init.h"
25#include "cyber/time/rate.h"
26#include "cyber/time/time.h"
27
28namespace apollo {
29namespace cyber {
30
31class PyTime {
32 public:
33 PyTime() = default;
34 explicit PyTime(uint64_t nanoseconds) { time_ = Time(nanoseconds); }
35
36 static PyTime now() {
37 PyTime t;
38 t.time_ = Time::Now();
39 return t;
40 }
41
42 static PyTime mono_time() {
43 PyTime t;
44 t.time_ = Time::MonoTime();
45 return t;
46 }
47
48 static void sleep_until(uint64_t nanoseconds) {
49 Time::SleepUntil(Time(nanoseconds));
50 }
51
52 double to_sec() const { return time_.ToSecond(); }
53
54 uint64_t to_nsec() const { return time_.ToNanosecond(); }
55
56 private:
57 Time time_;
58};
59
61 public:
62 explicit PyDuration(int64_t nanoseconds) {
63 duration_ = std::make_shared<Duration>(nanoseconds);
64 }
65
66 void sleep() const { return duration_->Sleep(); }
67
68 private:
69 std::shared_ptr<Duration> duration_ = nullptr;
70};
71
72class PyRate {
73 public:
74 explicit PyRate(uint64_t nanoseconds) {
75 rate_ = std::make_shared<Rate>(nanoseconds);
76 }
77
78 void sleep() const { return rate_->Sleep(); }
79 void reset() const { return rate_->Reset(); }
80 uint64_t get_cycle_time() const { return rate_->CycleTime().ToNanosecond(); }
81 uint64_t get_expected_cycle_time() const {
82 return rate_->ExpectedCycleTime().ToNanosecond();
83 }
84
85 private:
86 std::shared_ptr<Rate> rate_ = nullptr;
87};
88
89} // namespace cyber
90} // namespace apollo
91
92#endif // CYBER_PYTHON_INTERNAL_PY_TIME_H_
PyDuration(int64_t nanoseconds)
Definition py_time.h:62
uint64_t get_expected_cycle_time() const
Definition py_time.h:81
void reset() const
Definition py_time.h:79
uint64_t get_cycle_time() const
Definition py_time.h:80
PyRate(uint64_t nanoseconds)
Definition py_time.h:74
void sleep() const
Definition py_time.h:78
uint64_t to_nsec() const
Definition py_time.h:54
double to_sec() const
Definition py_time.h:52
static PyTime mono_time()
Definition py_time.h:42
static PyTime now()
Definition py_time.h:36
PyTime(uint64_t nanoseconds)
Definition py_time.h:34
static void sleep_until(uint64_t nanoseconds)
Definition py_time.h:48
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 MonoTime()
Definition time.cc:67
uint64_t ToNanosecond() const
convert time to nanosecond.
Definition time.cc:83
static Time Now()
get the current time.
Definition time.cc:57
double ToSecond() const
convert time to second.
Definition time.cc:77
class register implement
Definition arena_queue.h:37