ROS master节点管理器,配置两台主机的时间戳同步

155 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。


在两台主机之间,使用tf变换时,因为主机时间戳不同步,导致tf变换超时

[ WARN] [1555379888.006898613]: Timed out waiting for transform from base_footprint to map to become available before running costmap, tf error: Lookup would require extrapolation into the past.  Requested time 1555379883.350785017 but the earliest data is at time 1555379884.917741476, when looking up transform from frame [base_footprint] to frame [map]. canTransform returned after 0.100909 timeout was 0.1.

可通过ntp来对局域网内的两台主机进行开机自动同步时钟 (1)配置master 1.安装ntp:

$ sudo apt install ntp

2.在/etc/ntp.conf中配置ntp参数

$ sudo vim /etc/ntp.conf

3.在文件中添加以下内容:

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap  # 192.168.1.0 即局域网网段
server 127.127.1.0  # 获取本地时钟
fudge 127.127.1.0 stratum 10

4.重启ntp服务:

$ sudo /etc/init.d/ntp restart

(2)配置客户端(无用,直接第3步) 1.安装ntp

$ sudo apt install ntp

2.在/etc/ntp.conf中配置ntp参数

server 192.168.1.101  # 服务端的ip地址

3.重启ntp服务:

$ sudo /etc/init.d/ntp restart

(3)开机自动同步时间 安装ntpdate,ntpdate和ntp冲突,在客户端安装ntpdate,卸载ntp

sudo apt-get install ntpdate

安装成功后,可以使用以下命令查看NTP服务器的配置:

cat /etc/default/ntpdate

rc.local脚本是一个ubuntu开机后会自动执行的脚本,该脚本位于/etc/路径下。 添加如下指令:

$ sudo /usr/sbin/ntpdate 192.168.1.101

注意保证两台电脑的时区一致,否则同步成功也会自动转换时区

如果出现服务已存在,需要手动把ntp停止掉再重新同步! 如果出现:no server suitable for synchronization found 检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。可以用命令

service iptables stop

来关掉iptables服务后再尝试从ntp客户端的同步 参考博客:完美解决ntp的错误问题

当用 ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个: 

错误1.Server dropped: Strata too high

在ntp客户端用 ntpdate –d serverIP 查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”

这是因为NTP server还没有和其自身或者它的server同步上。

以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。

server 127.127.1.0 fudge

127.127.1.0 stratum 8 

在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。

那么如何知道何时ntp server完成了和自身同步的过程呢?

在ntp server上使用命令:

# watch ntpq -p

注意LOCAL的这个就是与自身同步的ntp server。

注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。

如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。