目录
一、回旋函数
C++
1.spinOnce()
2.spin()
python
二、时间
C++
1、时刻
2、持续时间
3、时间运算
4、设置运行频率
5、定时器
python
1、时刻
2、持续时间
3、时间运算
4、设置运行频率
5、定时器
一、回旋函数
C++
1.spinOnce()
一般应用场景:* 在循环体内,处理所有可用的回调函数
2.spin()
相同点:二者都用于处理回调函数;
不同点:ros::spin() 是进入了循环执行回调函数,而 ros::spinOnce() 只会执行一次回调函数(没有循环),在 ros::spin() 后的语句不会执行到,而 ros::spinOnce() 后的语句可以执行。
python
def spin():"""进入循环处理回调 """
二、时间
C++
1、时刻
ros::init(argc,argv,"hello_time");
ros::NodeHandle nh;//必须创建句柄,否则时间没有初始化,导致后续API调用失败
ros::Time right_now = ros::Time::now();//将当前时刻封装成对象
ROS_INFO("当前时刻:%.2f",right_now.toSec());//获取距离 1970年01月01日 00:00:00 的秒数
ROS_INFO("当前时刻:%d",right_now.sec);//获取距离 1970年01月01日 00:00:00 的秒数ros::Time someTime(100,100000000);// 参数1:秒数 参数2:纳秒
ROS_INFO("时刻:%.2f",someTime.toSec()); //100.10
ros::Time someTime2(100.3);//直接传入 double 类型的秒数
ROS_INFO("时刻:%.2f",someTime2.toSec()); //100.30
2、持续时间
ROS_INFO("当前时刻:%.2f",ros::Time::now().toSec());
ros::Duration du(10);//持续10秒钟,参数是double类型的,以秒为单位
du.sleep();//按照指定的持续时间休眠
ROS_INFO("持续时间:%.2f",du.toSec());//将持续时间换算成秒
ROS_INFO("当前时刻:%.2f",ros::Time::now().toSec());
3、时间运算
ROS_INFO("时间运算");
ros::Time now = ros::Time::now();
ros::Duration du1(10);
ros::Duration du2(20);
ROS_INFO("当前时刻:%.2f",now.toSec());
//1.time 与 duration 运算
ros::Time after_now = now + du1;
ros::Time before_now = now - du1;
ROS_INFO("当前时刻之后:%.2f",after_now.toSec());
ROS_INFO("当前时刻之前:%.2f",before_now.toSec());//2.duration 之间相互运算
ros::Duration du3 = du1 + du2;
ros::Duration du4 = du1 - du2;
ROS_INFO("du3 = %.2f",du3.toSec());
ROS_INFO("du4 = %.2f",du4.toSec());
//PS: time 与 time 不可以运算
// ros::Time nn = now + before_now;//异常
4、设置运行频率
ros::Rate rate(1);//指定频率
while (true)
{ROS_INFO("-----------code----------");rate.sleep();//休眠,休眠时间 = 1 / 频率。
}
5、定时器
ros::NodeHandle nh;//必须创建句柄,否则时间没有初始化,导致后续API调用失败// ROS 定时器/**
* \brief 创建一个定时器,按照指定频率调用回调函数。
*
* \param period 时间间隔
* \param callback 回调函数
* \param oneshot 如果设置为 true,只执行一次回调函数,设置为 false,就循环执行。
* \param autostart 如果为true,返回已经启动的定时器,设置为 false,需要手动启动。
*///Timer createTimer(Duration period, const TimerCallback& callback, bool oneshot = false,// bool autostart = true) const;// ros::Timer timer = nh.createTimer(ros::Duration(0.5),doSomeThing);ros::Timer timer = nh.createTimer(ros::Duration(0.5),doSomeThing,true);//只执行一次// ros::Timer timer = nh.createTimer(ros::Duration(0.5),doSomeThing,false,false);//需要手动启动// timer.start();ros::spin(); //必须 spinvoid doSomeThing(const ros::TimerEvent &event){ROS_INFO("-------------");ROS_INFO("event:%s",std::to_string(event.current_real.toSec()).c_str());
}
python
1、时刻
# 获取当前时刻
right_now = rospy.Time.now()
rospy.loginfo("当前时刻:%.2f",right_now.to_sec())
rospy.loginfo("当前时刻:%.2f",right_now.to_nsec())
# 自定义时刻
some_time1 = rospy.Time(1234.567891011)
some_time2 = rospy.Time(1234,567891011)
rospy.loginfo("设置时刻1:%.2f",some_time1.to_sec())
rospy.loginfo("设置时刻2:%.2f",some_time2.to_sec())# 从时间创建对象
# some_time3 = rospy.Time.from_seconds(543.21)
some_time3 = rospy.Time.from_sec(543.21) # from_sec 替换了 from_seconds
rospy.loginfo("设置时刻3:%.2f",some_time3.to_sec())
2、持续时间
# 持续时间相关API
rospy.loginfo("持续时间测试开始.....")
du = rospy.Duration(3.3)
rospy.loginfo("du1 持续时间:%.2f",du.to_sec())
rospy.sleep(du) #休眠函数
rospy.loginfo("持续时间测试结束.....")
3、时间运算
rospy.loginfo("时间运算")
now = rospy.Time.now()
du1 = rospy.Duration(10)
du2 = rospy.Duration(20)
rospy.loginfo("当前时刻:%.2f",now.to_sec())
before_now = now - du1
after_now = now + du1
dd = du1 + du2
# now = now + now #非法
rospy.loginfo("之前时刻:%.2f",before_now.to_sec())
rospy.loginfo("之后时刻:%.2f",after_now.to_sec())
rospy.loginfo("持续时间相加:%.2f",dd.to_sec())
4、设置运行频率
# 设置执行频率
rate = rospy.Rate(0.5)
while not rospy.is_shutdown():rate.sleep() #休眠rospy.loginfo("+++++++++++++++")
5、定时器
#定时器设置
"""
def __init__(self, period, callback, oneshot=False, reset=False):Constructor.@param period: 回调函数的时间间隔@type period: rospy.Duration@param callback: 回调函数@type callback: function taking rospy.TimerEvent@param oneshot: 设置为True,就只执行一次,否则循环执行@type oneshot: bool@param reset: if True, timer is reset when rostime moved backward. [default: False]@type reset: bool
"""
rospy.Timer(rospy.Duration(1),doMsg)
# rospy.Timer(rospy.Duration(1),doMsg,True) # 只执行一次
rospy.spin()def doMsg(event):rospy.loginfo("+++++++++++")rospy.loginfo("当前时刻:%s",str(event.current_real))