Apollo  6.0
Open source self driving car software
py_time.h
Go to the documentation of this file.
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 
28 namespace apollo {
29 namespace cyber {
30 
31 class 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 
60 class PyDuration {
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 
72 class 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_
::apollo::cyber::Time Time
Definition: racobit_radar_message_manager.h:41
static PyTime now()
Definition: py_time.h:36
PyRate(uint64_t nanoseconds)
Definition: py_time.h:74
void sleep() const
Definition: py_time.h:66
static PyTime mono_time()
Definition: py_time.h:42
uint64_t get_cycle_time() const
Definition: py_time.h:80
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
static void SleepUntil(const Time &time)
Sleep Until time.
uint64_t to_nsec() const
Definition: py_time.h:54
Definition: py_time.h:60
static Time MonoTime()
static void sleep_until(uint64_t nanoseconds)
Definition: py_time.h:48
uint64_t get_expected_cycle_time() const
Definition: py_time.h:81
double to_sec() const
Definition: py_time.h:52
PyDuration(int64_t nanoseconds)
Definition: py_time.h:62
Definition: py_time.h:72
Definition: py_time.h:31
double ToSecond() const
convert time to second.
uint64_t ToNanosecond() const
convert time to nanosecond.
PyTime(uint64_t nanoseconds)
Definition: py_time.h:34
static Time Now()
get the current time.
Cyber has builtin time type Time.
Definition: time.h:31
void reset() const
Definition: py_time.h:79
void sleep() const
Definition: py_time.h:78