# ROS 1 Gazebo中给Header消息赋值的时间总是变为0如何解决？

> 原文链接：https://robotics.hellobike.com/pitfalls/2026-02-22-robotics-se-118142
> 频道：pitfalls | 更新日期：2026-02-22 | 标签：auto, stackexchange, gazebo
> 核心摘要：用户在ROS 1 Gazebo仿真中给Header赋值时间总是变为0，将时间赋值操作移到循环内解决该问题。

# ROS 1 Gazebo中给Header消息赋值的时间总是变为0如何解决？

::: v-pre
> 本文由 AI 根据公开内容整理，可能有误，请以原文为准：[Why does a time published to a Header message always turns to 0?](https://robotics.stackexchange.com/questions/118142/why-does-a-time-published-to-a-header-message-always-turns-to-0)（Robotics Stack Exchange #118142）

**状态**：已解决

## 问题

在ROS 1 Gazebo仿真中使用rospy发布带stamped Header的消息时，`rospy.Time.now()`输出正常，但赋值给`msg.header.stamp`后总是变为0；添加`rospy.Duration(10)`则时间固定为10，忽略`rospy.Time.now()`，其余赋值正常。

## 环境

ROS 1、Gazebo仿真环境，`use_sim_time`设为true且`/clock`正常发布，使用rospy。

## 解决办法

1. 将`msg.header.stamp = rospy.Time.now()`这行代码移动到消息发布的while循环内部，每次发布前重新获取并赋值时间。

原问题作者：Andrii&#39;s Quest；采纳答案作者：Andrii&#39;s Quest。原内容依 CC BY-SA 4.0 授权，本页摘要同样依 CC BY-SA 4.0 授权。
:::

## 原始参考来源 (Sources)

- [Robotics Stack Exchange #118142：Why does a time published to a Header message always turns to 0?](https://robotics.stackexchange.com/questions/118142/why-does-a-time-published-to-a-header-message-always-turns-to-0)