Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Pinoxchio
apollo
提交
79d007cd
A
apollo
项目概览
Pinoxchio
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
79d007cd
编写于
7月 23, 2020
作者:
S
storypku
提交者:
Xiangquan Xiao
7月 23, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
cyber: add clock class
上级
e680a32f
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
158 addition
and
0 deletion
+158
-0
cyber/time/BUILD
cyber/time/BUILD
+14
-0
cyber/time/clock.cc
cyber/time/clock.cc
+61
-0
cyber/time/clock.h
cyber/time/clock.h
+83
-0
未找到文件。
cyber/time/BUILD
浏览文件 @
79d007cd
...
...
@@ -50,4 +50,18 @@ cc_test(
],
)
cc_library
(
name
=
"clock"
,
srcs
=
[
"clock.cc"
],
hdrs
=
[
"clock.h"
],
deps
=
[
":time"
,
"//cyber/common:global_data"
,
"//cyber/common:log"
,
"//cyber/common:macros"
,
"//cyber/common:util"
,
"//cyber/proto:run_mode_conf_cc_proto"
,
],
)
cpplint
()
cyber/time/clock.cc
0 → 100644
浏览文件 @
79d007cd
/******************************************************************************
* Copyright 2020 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#include "cyber/time/clock.h"
#include "cyber/common/global_data.h"
#include "cyber/common/log.h"
#include "cyber/common/util.h"
namespace
apollo
{
namespace
cyber
{
using
GlobalData
=
::
apollo
::
cyber
::
common
::
GlobalData
;
Clock
::
Clock
()
{
const
auto
&
cyber_config
=
GlobalData
::
Instance
()
->
Config
();
const
auto
&
clock_mode
=
cyber_config
.
run_mode_conf
().
clock_mode
();
mode_
=
clock_mode
;
mock_now_
=
Time
(
0
);
}
Time
Clock
::
Now
()
{
std
::
lock_guard
<
std
::
mutex
>
lk
(
mtx_
);
switch
(
mode_
)
{
case
ClockMode
::
MODE_CYBER
:
return
Time
::
Now
();
case
ClockMode
::
MODE_MOCK
:
return
mock_now_
;
default:
AFATAL
<<
"Unsupported clock mode: "
<<
apollo
::
cyber
::
common
::
ToInt
(
mode_
);
}
return
Time
::
Now
();
}
double
Clock
::
NowInSeconds
()
{
return
Now
().
ToSecond
();
}
void
Clock
::
SetNow
(
const
Time
&
now
)
{
std
::
lock_guard
<
std
::
mutex
>
lk
(
mtx_
);
if
(
mode_
!=
ClockMode
::
MODE_MOCK
)
{
AERROR
<<
"SetSimNow only works for ClockMode::MOCK"
;
return
;
}
mock_now_
=
now
;
}
}
// namespace cyber
}
// namespace apollo
cyber/time/clock.h
0 → 100644
浏览文件 @
79d007cd
/******************************************************************************
* Copyright 2020 The Apollo Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
#ifndef CYBER_TIME_CLOCK_H_
#define CYBER_TIME_CLOCK_H_
#include <mutex>
#include "cyber/common/macros.h"
#include "cyber/time/time.h"
#include "cyber/proto/run_mode_conf.pb.h"
namespace
apollo
{
namespace
cyber
{
using
::
apollo
::
cyber
::
proto
::
ClockMode
;
/**
* @class Clock
* @brief a singleton clock that can be used to get the current
* timestamp. The source can be either system(cyber) clock or a mock clock.
* Mock clock is for testing purpose mainly.
*/
class
Clock
{
public:
static
constexpr
int64_t
PRECISION
=
std
::
chrono
::
system_clock
::
duration
::
period
::
den
/
std
::
chrono
::
system_clock
::
duration
::
period
::
num
;
/// PRECISION >= 1000000 means the precision is at least 1us.
static_assert
(
PRECISION
>=
1000000
,
"The precision of the system clock should be at least 1 "
"microsecond."
);
/**
* @brief get current time.
* @return a Time object representing the current time.
*/
Time
Now
();
/**
* @brief gets the current time in second.
* @return the current time in second.
*/
double
NowInSeconds
();
/**
* @brief This is for mock clock mode only. It will set the timestamp
* for the mock clock.
*/
void
SetNow
(
const
Time
&
now
);
/**
* @brief This is for mock clock mode only. It will set the timestamp
* for the mock clock with UNIX timestamp in seconds.
*/
void
SetNowInSeconds
(
const
double
seconds
)
{
SetNow
(
Time
(
seconds
));
}
private:
ClockMode
mode_
;
Time
mock_now_
;
std
::
mutex
mtx_
;
DECLARE_SINGLETON
(
Clock
)
};
}
// namespace cyber
}
// namespace apollo
#endif // CYBER_TIME_CLOCK_H_
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录