Apollo 10.0
自动驾驶开放平台
py_time.cc
浏览该文件的文档.
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
18
19#include <set>
20#include <string>
21
22#include <Python.h>
23
27
28#define PYOBJECT_NULL_STRING PyString_FromStringAndSize("", 0)
29
30template <typename T>
31T PyObjectToPtr(PyObject* pyobj, const std::string& type_ptr) {
32 T obj_ptr = (T)PyCapsule_GetPointer(pyobj, type_ptr.c_str());
33 if (obj_ptr == nullptr) {
34 AERROR << "PyObjectToPtr failed,type->" << type_ptr << "pyobj: " << pyobj;
35 }
36 return obj_ptr;
37}
38
39PyObject* cyber_new_PyTime(PyObject* self, PyObject* args) {
40 uint64_t nanoseconds = 0;
41 if (!PyArg_ParseTuple(args, const_cast<char*>("K:cyber_new_PyTime"),
42 &nanoseconds)) {
43 AERROR << "cyber_new_PyTime parsetuple failed!";
44 Py_INCREF(Py_None);
45 return Py_None;
46 }
47
48 PyTime* pytime = new PyTime(nanoseconds);
49 return PyCapsule_New(pytime, "apollo_cybertron_pytime", nullptr);
50}
51
52PyObject* cyber_delete_PyTime(PyObject* self, PyObject* args) {
53 PyObject* pyobj_time = nullptr;
54 if (!PyArg_ParseTuple(args, const_cast<char*>("O:cyber_delete_PyTime"),
55 &pyobj_time)) {
56 Py_INCREF(Py_None);
57 return Py_None;
58 }
59
60 auto* pytime = reinterpret_cast<PyTime*>(
61 PyCapsule_GetPointer(pyobj_time, "apollo_cybertron_pytime"));
62 if (nullptr == pytime) {
63 AERROR << "cyber_delete_PyTime:time ptr is null!";
64 Py_INCREF(Py_None);
65 return Py_None;
66 }
67 delete pytime;
68 Py_INCREF(Py_None);
69 return Py_None;
70}
71
72PyObject* cyber_PyTime_to_sec(PyObject* self, PyObject* args) {
73 PyObject* pyobj_time = nullptr;
74 if (!PyArg_ParseTuple(args, const_cast<char*>("O:cyber_PyTime_to_sec"),
75 &pyobj_time)) {
76 AERROR << "cyber_PyTime_to_sec:PyArg_ParseTuple failed!";
77 return PyFloat_FromDouble(0);
78 }
79
80 auto* pytime = reinterpret_cast<PyTime*>(
81 PyCapsule_GetPointer(pyobj_time, "apollo_cybertron_pytime"));
82 if (nullptr == pytime) {
83 AERROR << "cyber_PyTime_to_sec ptr is null!";
84 return PyFloat_FromDouble(0);
85 }
86
87 double num = pytime->to_sec();
88 return PyFloat_FromDouble(num);
89}
90
91PyObject* cyber_PyTime_to_nsec(PyObject* self, PyObject* args) {
92 PyObject* pyobj_time = nullptr;
93 if (!PyArg_ParseTuple(args, const_cast<char*>("O:cyber_PyTime_to_nsec"),
94 &pyobj_time)) {
95 AERROR << "cyber_PyTime_to_nsec:PyArg_ParseTuple failed!";
96 return PyLong_FromUnsignedLongLong(0);
97 }
98
99 auto* pytime = reinterpret_cast<PyTime*>(
100 PyCapsule_GetPointer(pyobj_time, "apollo_cybertron_pytime"));
101 if (nullptr == pytime) {
102 AERROR << "cyber_PyTime_to_nsec ptr is null!";
103 return PyLong_FromUnsignedLongLong(0);
104 }
105
106 uint64_t num = pytime->to_nsec();
107 return PyLong_FromUnsignedLongLong(num);
108}
109
110PyObject* cyber_PyTime_sleep_until(PyObject* self, PyObject* args) {
111 PyObject* pyobj_time = nullptr;
112 uint64_t nanoseconds = 0;
113 if (!PyArg_ParseTuple(args, const_cast<char*>("OK:cyber_PyTime_sleep_until"),
114 &pyobj_time, &nanoseconds)) {
115 AERROR << "cyber_PyTime_sleep_until:PyArg_ParseTuple failed!";
116 Py_INCREF(Py_None);
117 return Py_None;
118 }
119
120 auto* pytime = reinterpret_cast<PyTime*>(
121 PyCapsule_GetPointer(pyobj_time, "apollo_cybertron_pytime"));
122 if (nullptr == pytime) {
123 AERROR << "cyber_PyTime_sleep_until ptr is null!";
124 Py_INCREF(Py_None);
125 return Py_None;
126 }
127
128 pytime->sleep_until(nanoseconds);
129 Py_INCREF(Py_None);
130 return Py_None;
131}
132
133PyObject* cyber_PyTime_now(PyObject* self, PyObject* args) {
134 PyTime now = PyTime::now();
135 return PyLong_FromUnsignedLongLong(now.to_nsec());
136}
137
138PyObject* cyber_PyTime_mono_time(PyObject* self, PyObject* args) {
139 PyTime mono_time = PyTime::mono_time();
140 return PyLong_FromUnsignedLongLong(mono_time.to_nsec());
141}
142
143// duration
144PyObject* cyber_new_PyDuration(PyObject* self, PyObject* args) {
145 uint64_t nanoseconds = 0;
146 if (!PyArg_ParseTuple(args, const_cast<char*>("L:cyber_new_PyDuration"),
147 &nanoseconds)) {
148 AERROR << "cyber_new_PyDuration parsetuple failed!";
149 Py_INCREF(Py_None);
150 return Py_None;
151 }
152
153 PyDuration* pyduration = new PyDuration(nanoseconds);
154 return PyCapsule_New(pyduration, "apollo_cybertron_pyduration", nullptr);
155}
156
157PyObject* cyber_delete_PyDuration(PyObject* self, PyObject* args) {
158 PyObject* pyobj_duration = nullptr;
159 if (!PyArg_ParseTuple(args, const_cast<char*>("O:cyber_delete_PyDuration"),
160 &pyobj_duration)) {
161 Py_INCREF(Py_None);
162 return Py_None;
163 }
164
165 auto* pyduration = reinterpret_cast<PyDuration*>(
166 PyCapsule_GetPointer(pyobj_duration, "apollo_cybertron_pyduration"));
167 if (nullptr == pyduration) {
168 AERROR << "cyber_delete_PyDuration:pyduration ptr is null!";
169 Py_INCREF(Py_None);
170 return Py_None;
171 }
172 delete pyduration;
173 Py_INCREF(Py_None);
174 return Py_None;
175}
176
177PyObject* cyber_PyDuration_sleep(PyObject* self, PyObject* args) {
178 PyObject* pyobj_duration = nullptr;
179 if (!PyArg_ParseTuple(args, const_cast<char*>("O:cyber_PyDuration_sleep"),
180 &pyobj_duration)) {
181 Py_INCREF(Py_None);
182 return Py_None;
183 }
184
185 auto* pyduration = reinterpret_cast<PyDuration*>(
186 PyCapsule_GetPointer(pyobj_duration, "apollo_cybertron_pyduration"));
187 if (nullptr == pyduration) {
188 AERROR << "cyber_PyDuration_sleep:pyduration ptr is null!";
189 Py_INCREF(Py_None);
190 return Py_None;
191 }
192 pyduration->sleep();
193 Py_INCREF(Py_None);
194 return Py_None;
195}
196
197// rate
198PyObject* cyber_new_PyRate(PyObject* self, PyObject* args) {
199 uint64_t nanoseconds = 0;
200 if (!PyArg_ParseTuple(args, const_cast<char*>("L:cyber_new_PyRate"),
201 &nanoseconds)) {
202 AERROR << "cyber_new_PyRate parsetuple failed!";
203 Py_INCREF(Py_None);
204 return Py_None;
205 }
206
207 PyRate* pyrate = new PyRate(nanoseconds);
208 return PyCapsule_New(pyrate, "apollo_cybertron_pyrate", nullptr);
209}
210
211PyObject* cyber_delete_PyRate(PyObject* self, PyObject* args) {
212 PyObject* pyobj_rate = nullptr;
213 if (!PyArg_ParseTuple(args, const_cast<char*>("O:cyber_delete_PyRate"),
214 &pyobj_rate)) {
215 Py_INCREF(Py_None);
216 return Py_None;
217 }
218
219 auto* pyrate = reinterpret_cast<PyRate*>(
220 PyCapsule_GetPointer(pyobj_rate, "apollo_cybertron_pyrate"));
221 if (nullptr == pyrate) {
222 AERROR << "cyber_delete_PyRate:rate ptr is null!";
223 Py_INCREF(Py_None);
224 return Py_None;
225 }
226 delete pyrate;
227 Py_INCREF(Py_None);
228 return Py_None;
229}
230
231PyObject* cyber_PyRate_sleep(PyObject* self, PyObject* args) {
232 PyObject* pyobj_rate = nullptr;
233 if (!PyArg_ParseTuple(args, const_cast<char*>("O:cyber_PyRate_sleep"),
234 &pyobj_rate)) {
235 Py_INCREF(Py_None);
236 return Py_None;
237 }
238
239 auto* pyrate = reinterpret_cast<PyRate*>(
240 PyCapsule_GetPointer(pyobj_rate, "apollo_cybertron_pyrate"));
241 if (nullptr == pyrate) {
242 AERROR << "cyber_PyRate_sleep:rate ptr is null!";
243 Py_INCREF(Py_None);
244 return Py_None;
245 }
246 pyrate->sleep();
247 Py_INCREF(Py_None);
248 return Py_None;
249}
250
251PyObject* cyber_PyRate_reset(PyObject* self, PyObject* args) {
252 PyObject* pyobj_rate = nullptr;
253 if (!PyArg_ParseTuple(args, const_cast<char*>("O:cyber_PyRate_reset"),
254 &pyobj_rate)) {
255 Py_INCREF(Py_None);
256 return Py_None;
257 }
258
259 auto* pyrate = reinterpret_cast<PyRate*>(
260 PyCapsule_GetPointer(pyobj_rate, "apollo_cybertron_pyrate"));
261 if (nullptr == pyrate) {
262 AERROR << "cyber_PyRate_reset:rate ptr is null!";
263 Py_INCREF(Py_None);
264 return Py_None;
265 }
266 pyrate->reset();
267 Py_INCREF(Py_None);
268 return Py_None;
269}
270
271PyObject* cyber_PyRate_get_cycle_time(PyObject* self, PyObject* args) {
272 PyObject* pyobj_rate = nullptr;
273 if (!PyArg_ParseTuple(args,
274 const_cast<char*>("O:cyber_PyRate_get_cycle_time"),
275 &pyobj_rate)) {
276 return PyLong_FromUnsignedLongLong(0);
277 }
278
279 auto* pyrate = reinterpret_cast<PyRate*>(
280 PyCapsule_GetPointer(pyobj_rate, "apollo_cybertron_pyrate"));
281 if (nullptr == pyrate) {
282 AERROR << "cyber_PyRate_get_cycle_time:rate ptr is null!";
283 return PyLong_FromUnsignedLongLong(0);
284 }
285 uint64_t ctime = pyrate->get_cycle_time();
286 return PyLong_FromUnsignedLongLong(ctime);
287}
288
289PyObject* cyber_PyRate_get_expected_cycle_time(PyObject* self, PyObject* args) {
290 PyObject* pyobj_rate = nullptr;
291 if (!PyArg_ParseTuple(
292 args, const_cast<char*>("O:cyber_PyRate_get_expected_cycle_time"),
293 &pyobj_rate)) {
294 return PyLong_FromUnsignedLongLong(0);
295 }
296
297 auto* pyrate = reinterpret_cast<PyRate*>(
298 PyCapsule_GetPointer(pyobj_rate, "apollo_cybertron_pyrate"));
299 if (nullptr == pyrate) {
300 AERROR << "cyber_PyRate_get_expected_cycle_time:rate ptr is null!";
301 return PyLong_FromUnsignedLongLong(0);
302 }
303 uint64_t exp_cycle_time = pyrate->get_expected_cycle_time();
304 return PyLong_FromUnsignedLongLong(exp_cycle_time);
305}
306
307static PyMethodDef _cyber_time_methods[] = {
308 // PyTime fun
309 {"new_PyTime", cyber_new_PyTime, METH_VARARGS, ""},
310 {"delete_PyTime", cyber_delete_PyTime, METH_VARARGS, ""},
311 {"PyTime_now", cyber_PyTime_now, METH_VARARGS, ""},
312 {"PyTime_mono_time", cyber_PyTime_mono_time, METH_VARARGS, ""},
313 {"PyTime_to_sec", cyber_PyTime_to_sec, METH_VARARGS, ""},
314 {"PyTime_to_nsec", cyber_PyTime_to_nsec, METH_VARARGS, ""},
315 {"PyTime_sleep_until", cyber_PyTime_sleep_until, METH_VARARGS, ""},
316
317 // PyDuration
318 {"new_PyDuration", cyber_new_PyDuration, METH_VARARGS, ""},
319 {"delete_PyDuration", cyber_delete_PyDuration, METH_VARARGS, ""},
320 {"PyDuration_sleep", cyber_PyDuration_sleep, METH_VARARGS, ""},
321
322 // PyRate
323 {"new_PyRate", cyber_new_PyRate, METH_VARARGS, ""},
324 {"delete_PyRate", cyber_delete_PyRate, METH_VARARGS, ""},
325 {"PyRate_sleep", cyber_PyRate_sleep, METH_VARARGS, ""},
326 {"PyRate_reset", cyber_PyRate_reset, METH_VARARGS, ""},
327 {"PyRate_get_cycle_time", cyber_PyRate_get_cycle_time, METH_VARARGS, ""},
328 {"PyRate_get_expected_cycle_time", cyber_PyRate_get_expected_cycle_time,
329 METH_VARARGS, ""},
330
331 {nullptr, nullptr, 0, nullptr} /* sentinel */
332};
333
335PyMODINIT_FUNC PyInit__cyber_time_wrapper(void) {
336 static struct PyModuleDef module_def = {
337 PyModuleDef_HEAD_INIT,
338 "_cyber_time_wrapper", // Module name.
339 "CyberTime module", // Module doc.
340 -1, // Module size.
341 _cyber_time_methods, // Module methods.
342 nullptr,
343 nullptr,
344 nullptr,
345 nullptr,
346 };
347
348 return PyModule_Create(&module_def);
349}
uint64_t to_nsec() const
Definition py_time.h:54
static PyTime mono_time()
Definition py_time.h:42
static PyTime now()
Definition py_time.h:36
#define AERROR
Definition log.h:44
PyObject * cyber_delete_PyTime(PyObject *self, PyObject *args)
Definition py_time.cc:52
PyMODINIT_FUNC PyInit__cyber_time_wrapper(void)
Init function of this module
Definition py_time.cc:335
PyObject * cyber_PyTime_to_nsec(PyObject *self, PyObject *args)
Definition py_time.cc:91
PyObject * cyber_PyRate_get_expected_cycle_time(PyObject *self, PyObject *args)
Definition py_time.cc:289
PyObject * cyber_PyTime_mono_time(PyObject *self, PyObject *args)
Definition py_time.cc:138
PyObject * cyber_new_PyRate(PyObject *self, PyObject *args)
Definition py_time.cc:198
PyObject * cyber_new_PyTime(PyObject *self, PyObject *args)
Definition py_time.cc:39
PyObject * cyber_delete_PyRate(PyObject *self, PyObject *args)
Definition py_time.cc:211
PyObject * cyber_PyTime_now(PyObject *self, PyObject *args)
Definition py_time.cc:133
PyObject * cyber_PyDuration_sleep(PyObject *self, PyObject *args)
Definition py_time.cc:177
T PyObjectToPtr(PyObject *pyobj, const std::string &type_ptr)
Definition py_time.cc:31
PyObject * cyber_PyTime_to_sec(PyObject *self, PyObject *args)
Definition py_time.cc:72
PyObject * cyber_delete_PyDuration(PyObject *self, PyObject *args)
Definition py_time.cc:157
PyObject * cyber_PyRate_sleep(PyObject *self, PyObject *args)
Definition py_time.cc:231
PyObject * cyber_new_PyDuration(PyObject *self, PyObject *args)
Definition py_time.cc:144
PyObject * cyber_PyRate_reset(PyObject *self, PyObject *args)
Definition py_time.cc:251
PyObject * cyber_PyTime_sleep_until(PyObject *self, PyObject *args)
Definition py_time.cc:110
PyObject * cyber_PyRate_get_cycle_time(PyObject *self, PyObject *args)
Definition py_time.cc:271