# 如何从命令行发布 ros::Time::now()？

> 原文链接：https://robotics.hellobike.com/pitfalls/2024-08-18-robotics-se-83748
> 频道：pitfalls | 更新日期：2024-08-18 | 标签：auto, stackexchange, ros, std-msgs, ros-kinetic, time
> 核心摘要：在 ROS CLI 中发布当前时间可用 rostopic pub -s 的 now 关键字，答案已被采纳。

# 如何从命令行发布 ros::Time::now()？

::: v-pre
> 本文由 AI 根据公开内容整理，可能有误，请以原文为准：[Is there an easy way to publish std_msgs::Time from cli?](https://robotics.stackexchange.com/questions/83748/is-there-an-easy-way-to-publish-std-msgstime-from-cli)（Robotics Stack Exchange #83748）

**状态**：已解决

## 问题

想从命令行向某话题发布等价于 `ros::Time::now()` 的 `std_msgs::Time`，用于测试订阅该消息的节点报错。

## 环境

Linux / ROS 机器人软件与控制通用开发环境

## 解决办法

1. 使用 `rostopic pub` 的 `-s` 或 `--substitute-keywords` 启用关键字替换。
2. 用 `now` 作为消息内容发布当前时间，例如：`rostopic pub -s -r 1 /some_topic std_msgs/Time 'now'`，以 1 Hz 发布 `ros::Time::now()`。
3. 若只需发布固定时间，可用 `rostopic pub -r 1 /some_topic std_msgs/Time 'data: { secs: 1, nsecs: 2 }'`。

原问题作者：seanarm；采纳答案作者：gvdhoorn-rse。原内容依 CC BY-SA 4.0 授权，本页摘要同样依 CC BY-SA 4.0 授权。
:::

## 原始参考来源 (Sources)

- [Robotics Stack Exchange #83748：Is there an easy way to publish std_msgs::Time from cli?](https://robotics.stackexchange.com/questions/83748/is-there-an-easy-way-to-publish-std-msgstime-from-cli)