21. ROS programming learning: time python in ROS

Table of contents

1. Current time and specified time

Create time_p.py

time_p.py 

Add executable permission

compile + run node

2. Duration

3. Calculation of duration and time

4. Timer

V. Summary


Reference learning materials: Zhao Xuzuo's courses at Station B

1. Current time and specified time

Create time_p.py

I put it into the function package of the previous topic communication.

time_p.py 

#! /usr/bin/env python3
# -*- coding: UTF-8 -*-

import rospy

if __name__ == "__main__":
    # 初始化节点
    rospy.init_node("time_p")
    # 调用rospy库中的Time类中的now()函数.查询当前时间,参考系为1970年1月1日00:00.
    # @return: L{Time} instance for current time,返回当前时间的实例.
    time_now = rospy.Time.now()
    # 打印当前时间,利用to_sec()将得到的ros时间转化为浮点型.
    rospy.loginfo("当前时间为:%.2f", time_now.to_sec())
    # 指定时间,参考系为1970年1月1日00:00.
    # 方法一:直接传入浮点数
    time1 = rospy.Time(100.5)
    # 方法二:传入秒与纳秒,通过逗号分隔.
    time2 = rospy.Time(100,123456789)
    # 打印两个方法指定的时刻.
    rospy.loginfo("指定时刻1为:%.2f", time1.to_sec())
    rospy.loginfo("指定时刻2为:%.2f", time2.to_sec())

Add executable permission

chmod +x *.py

CMakeList.txt configuration

catkin_install_python(PROGRAMS
  scripts/time_p.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

compile + run node

rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosrun sub_pub time_p.py
[INFO] [1667358757.807380]: 当前时间为:1667358757.81
[INFO] [1667358757.808251]: 指定时刻1为:100.50
[INFO] [1667358757.808971]: 指定时刻2为:100.12

2. Duration

add duration part

#! /usr/bin/env python3
# -*- coding: UTF-8 -*-

import rospy

if __name__ == "__main__":
    # 初始化节点
    rospy.init_node("time_p")
    # 调用rospy库中的Time类中的now()函数.查询当前时间,参考系为1970年1月1日00:00.
    # @return: L{Time} instance for current time,返回当前时间的实例.
    time_now = rospy.Time.now()
    # 打印当前时间,利用to_sec()将得到的ros时间转化为浮点型.
    rospy.loginfo("当前时间为:%.2f", time_now.to_sec())
    # 指定时间,参考系为1970年1月1日00:00.
    # 方法一:直接传入浮点数
    time1 = rospy.Time(100.5)
    # 方法二:传入秒与纳秒,通过逗号分隔.
    time2 = rospy.Time(100,123456789)
    # 打印两个方法指定的时刻.
    rospy.loginfo("指定时刻1为:%.2f", time1.to_sec())
    rospy.loginfo("指定时刻2为:%.2f", time2.to_sec())
    # 持续时间部分
    # 测试两种方式,一个是传入浮点数,一个是传入秒与纳秒用逗号分隔开.
    du1 = rospy.Duration(5.1)
    du2 = rospy.Duration(5, 100000000)
    # 打印当前时刻
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())
    # 延时du1
    rospy.sleep(du1)
    # 打印当前时刻
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())
    # 延时du2
    rospy.sleep(du2)
    # 打印当前时刻
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())
    # 延时5秒
    rospy.sleep(5)
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())

The first

du1 = rospy.Duration(5.1)
rospy.sleep(du1)

the second

du2 = rospy.Duration(5, 100000000)
rospy.sleep(du2)

The third (commonly used)

rospy.sleep(5)

run node 

rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosrun sub_pub time_p.py
[INFO] [1667460064.152594]: 当前时间为:1667460064.15
[INFO] [1667460064.153262]: 指定时刻1为:100.50
[INFO] [1667460064.153888]: 指定时刻2为:100.12
[INFO] [1667460064.154418]: 当前时刻为:1667460064.154
[INFO] [1667460069.263867]: 当前时刻为:1667460069.264
[INFO] [1667460074.377712]: 当前时刻为:1667460074.378
[INFO] [1667460079.388063]: 当前时刻为:1667460079.388

After making a difference, it was found that it basically met the requirement of 5.1 seconds, and an error occurred at 0.01 seconds. Compared with the implementation of c++, it was found that the performance of python was not as good as that of c++.

(2 messages) 18. ROS programming: time c++ in icon-default.png?t=M85BROS .3001.5501

3. Calculation of duration and time

Add calculation part

#! /usr/bin/env python3
# -*- coding: UTF-8 -*-

import rospy

if __name__ == "__main__":
    # 初始化节点
    rospy.init_node("time_p")
    # 调用rospy库中的Time类中的now()函数.查询当前时间,参考系为1970年1月1日00:00.
    # @return: L{Time} instance for current time,返回当前时间的实例.
    time_now = rospy.Time.now()
    # 打印当前时间,利用to_sec()将得到的ros时间转化为浮点型.
    rospy.loginfo("当前时间为:%.2f", time_now.to_sec())
    # 指定时间,参考系为1970年1月1日00:00.
    # 方法一:直接传入浮点数
    time1 = rospy.Time(100.5)
    # 方法二:传入秒与纳秒,通过逗号分隔.
    time2 = rospy.Time(100,123456789)
    # 打印两个方法指定的时刻.
    rospy.loginfo("指定时刻1为:%.2f", time1.to_sec())
    rospy.loginfo("指定时刻2为:%.2f", time2.to_sec())
    # 持续时间部分
    # 测试两种方式,一个是传入浮点数,一个是传入秒与纳秒用逗号分隔开.
    du1 = rospy.Duration(5.1)
    du2 = rospy.Duration(5, 100000000)
    # 打印当前时刻
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())
    # 延时du1
    rospy.sleep(du1)
    # 打印当前时刻
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())
    # 延时du2
    rospy.sleep(du2)
    # 打印当前时刻
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())
    # 延时5秒
    rospy.sleep(5)
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())
    # 运算部分
    # 持续时间运算
    du3 = du1 + du2
    rospy.loginfo("持续时间du3:%.2f", du3.to_sec())
    # 时刻与持续时间运算
    # 加法
    time3 = time1 + du1
    # 减法
    time4 = time1 - du1
    rospy.loginfo("加法:%.2f,减法:%.2f", time3.to_sec(), time4.to_sec())

operation result

rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosrun sub_pub time_p.py
[INFO] [1667461232.350247]: 当前时间为:1667461232.35
[INFO] [1667461232.350882]: 指定时刻1为:100.50
[INFO] [1667461232.351696]: 指定时刻2为:100.12
[INFO] [1667461232.352490]: 当前时刻为:1667461232.352
[INFO] [1667461237.458726]: 当前时刻为:1667461237.459
[INFO] [1667461242.566056]: 当前时刻为:1667461242.566
[INFO] [1667461247.573198]: 当前时刻为:1667461247.573
[INFO] [1667461247.574506]: 持续时间du3:10.20
[INFO] [1667461247.575430]: 加法:105.60,减法:95.40

4. Timer

add timer section

#! /usr/bin/env python3
# -*- coding: UTF-8 -*-

import rospy

def huidiao(event):
    rospy.loginfo("--------")
    rospy.loginfo("调用回调函数的时刻:%.2f", event.current_real.to_sec())

if __name__ == "__main__":
    # 初始化节点
    rospy.init_node("time_p")
    # 调用rospy库中的Time类中的now()函数.查询当前时间,参考系为1970年1月1日00:00.
    # @return: L{Time} instance for current time,返回当前时间的实例.
    time_now = rospy.Time.now()
    # 打印当前时间,利用to_sec()将得到的ros时间转化为浮点型.
    rospy.loginfo("当前时间为:%.2f", time_now.to_sec())
    # 指定时间,参考系为1970年1月1日00:00.
    # 方法一:直接传入浮点数
    time1 = rospy.Time(100.5)
    # 方法二:传入秒与纳秒,通过逗号分隔.
    time2 = rospy.Time(100,123456789)
    # 打印两个方法指定的时刻.
    rospy.loginfo("指定时刻1为:%.2f", time1.to_sec())
    rospy.loginfo("指定时刻2为:%.2f", time2.to_sec())
    # 持续时间部分
    # 测试两种方式,一个是传入浮点数,一个是传入秒与纳秒用逗号分隔开.
    du1 = rospy.Duration(5.1)
    du2 = rospy.Duration(5, 100000000)
    # 打印当前时刻
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())
    # 延时du1
    rospy.sleep(du1)
    # 打印当前时刻
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())
    # 延时du2
    rospy.sleep(du2)
    # 打印当前时刻
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())
    # 延时5秒
    rospy.sleep(5)
    rospy.loginfo("当前时刻为:%.3f", rospy.Time.now().to_sec())
    # 运算部分
    # 持续时间运算
    du3 = du1 + du2
    rospy.loginfo("持续时间du3:%.2f", du3.to_sec())
    # 时刻与持续时间运算
    # 加法
    time3 = time1 + du1
    # 减法
    time4 = time1 - du1
    rospy.loginfo("加法:%.2f,减法:%.2f", time3.to_sec(), time4.to_sec())
    # 定时器部分
    rospy.Timer(period = rospy.Duration(1), callback = huidiao)
    rospy.spin()

main part

def huidiao(event):
    rospy.loginfo("--------")
    rospy.loginfo("调用回调函数的时刻:%.2f", event.current_real.to_sec())

rospy.Timer(period = rospy.Duration(1), callback = huidiao)
rospy.spin()

Through the Timer, an event will be given to the callback function, where the event is in the class TimerEvent class (query Timer through ctrl, and then query TimerEvent).

    @param last_expected: in a perfect world, this is when the previous callback should have happened
    @type  last_expected: rospy.Time
    @param last_real: when the callback actually happened
    @type  last_real: rospy.Time
    @param current_expected: in a perfect world, this is when the current callback should have been called
    @type  current_expected: rospy.Time
    @param current_real: when the current callback is actually being called
                         (rospy.Time.now() as of immediately before calling the callback)
    @type  current_real: rospy.Time
    @param last_duration: contains the duration of the last callback (end time minus start time) in seconds.
                          Note that this is always in wall-clock time.
    @type  last_duration: float

This program uses the current_real under the event. The return value of this function is the current time (rostime type data), which can be converted into a floating point type by .to_sec() and printed on the console terminal.

Note: If there is a callback function, there must be a return function spin(), otherwise it will not jump into the loop in the callback function.

run node

rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosrun sub_pub time_p.py
[INFO] [1667464504.012976]: 当前时间为:1667464504.01
[INFO] [1667464504.013651]: 指定时刻1为:100.50
[INFO] [1667464504.014580]: 指定时刻2为:100.12
[INFO] [1667464504.015244]: 当前时刻为:1667464504.015
[INFO] [1667464509.120141]: 当前时刻为:1667464509.120
[INFO] [1667464514.229800]: 当前时刻为:1667464514.230
[INFO] [1667464519.236495]: 当前时刻为:1667464519.236
[INFO] [1667464519.238351]: 持续时间du3:10.20
[INFO] [1667464519.239330]: 加法:105.60,减法:95.40
[INFO] [1667464520.242976]: --------
[INFO] [1667464520.244635]: 调用回调函数的时刻:1667464520.24
[INFO] [1667464521.244617]: --------
[INFO] [1667464521.245944]: 调用回调函数的时刻:1667464521.24
[INFO] [1667464522.242715]: --------
[INFO] [1667464522.244112]: 调用回调函数的时刻:1667464522.24
[INFO] [1667464523.242828]: --------
[INFO] [1667464523.243979]: 调用回调函数的时刻:1667464523.24
[INFO] [1667464524.244025]: --------
[INFO] [1667464524.245623]: 调用回调函数的时刻:1667464524.24

To loop only once, modify, assign a Boolean value to the oneshot parameter.

rospy.Timer(period = rospy.Duration(1), callback = huidiao, oneshot=True)

run node

rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosrun sub_pub time_p.py
[INFO] [1667465155.348593]: 当前时间为:1667465155.35
[INFO] [1667465155.349332]: 指定时刻1为:100.50
[INFO] [1667465155.350237]: 指定时刻2为:100.12
[INFO] [1667465155.351194]: 当前时刻为:1667465155.351
[INFO] [1667465160.460058]: 当前时刻为:1667465160.460
[INFO] [1667465165.570954]: 当前时刻为:1667465165.571
[INFO] [1667465170.580149]: 当前时刻为:1667465170.580
[INFO] [1667465170.581464]: 持续时间du3:10.20
[INFO] [1667465170.582339]: 加法:105.60,减法:95.40
[INFO] [1667465171.586173]: --------
[INFO] [1667465171.587353]: 调用回调函数的时刻:1667465171.59

V. Summary

Compared with the implementation of C++, python is more concise, and at the same time it is more convenient to find the official explanation of the formal parameters, which have detailed explanations in the official class. But from the duration section, the performance of python is far inferior to that of c++.

Supongo que te gusta

Origin blog.csdn.net/wzfafabga/article/details/127647473
Recomendado
Clasificación