参考学习资料:B站赵虚左的ROS课程
节点关闭
C++
节点关闭api
ros::shutdown()
修改之前的pub.cpp
ros::Publisher pub = n.advertise<std_msgs::String>("chongfu",1000,true);
std_msgs::String msg;
ros::Rate rate(1);
int count = 0;
while (ros::ok())
{
count++;
std::stringstream ss;
ss << "hello -->" << count;
msg.data = ss.str();
if(count <= 10)
{
pub.publish(msg);
ROS_INFO("发布数据:%s", msg.data.c_str());
rate.sleep();
}
else
{
ros::shutdown();
}
// pub.publish(msg);
// ROS_INFO("发布数据:%s", msg.data.c_str());
// rate.sleep();
ros::spinOnce();
// ROS_INFO("实验:看看这行代码运行情况");
}
return 0;
}
注意的点:
while循环的条件时ros节点是否运行,如果ros节点停止while停止循环,所以之前的操作是在控制台终端ctrl+c退出。
while (ros::ok())
修改之后,当计数大于10,就会将ros节点关闭,无需再用ctrl+c关闭。
if(count <= 10)
{
pub.publish(msg);
ROS_INFO("发布数据:%s", msg.data.c_str());
rate.sleep();
}
else
{
ros::shutdown();
}
运行结果:
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosrun sub_pub pub
[ INFO] [1667198596.265939436]: 发布数据:hello -->1
[ INFO] [1667198597.266422801]: 发布数据:hello -->2
[ INFO] [1667198598.266515382]: 发布数据:hello -->3
[ INFO] [1667198599.266077031]: 发布数据:hello -->4
[ INFO] [1667198600.266574903]: 发布数据:hello -->5
[ INFO] [1667198601.266281487]: 发布数据:hello -->6
[ INFO] [1667198602.266431555]: 发布数据:hello -->7
[ INFO] [1667198603.266338400]: 发布数据:hello -->8
[ INFO] [1667198604.266157405]: 发布数据:hello -->9
[ INFO] [1667198605.266765773]: 发布数据:hello -->10
python
节点关闭api
rospy.signal_shutdown("string")
修改之前的pub_p.py
#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import rospy
from std_msgs.msg import String
if __name__ == "__main__":
rospy.init_node("pub_py")
pub = rospy.Publisher ("chongfu_py", String, queue_size=10)
msg = String()
rate = rospy.Rate(1)
count = 0
rospy.sleep(3)
while not rospy.is_shutdown():
count += 1
if(count <= 10):
msg.data = "hello" + str(count)
pub.publish(msg)
rospy.loginfo("发布数据:%s", msg.data)
else:
rospy.signal_shutdown("退出循环")
rate.sleep()
注意的点!!!在调用节点关闭api时,是必须要给人类可读的原因,同时以字符串的形式给定。(@param reason: human-readable shutdown reason, if applicable @type reason: str)
不然报错:
Traceback (most recent call last):
File "/home/rosmelodic/catkin_ws/src/sub_pub/scripts/pub_p.py", line 21, in <module>
rospy.signal_shutdown()
TypeError: signal_shutdown() takes exactly 1 argument (0 given)
运行结果:
rosmelodic@rosmelodic-virtual-machine:~/catkin_ws$ rosrun sub_pub pub_p.py
[INFO] [1667199573.878112]: 发布数据:hello1
[INFO] [1667199573.879684]: 发布数据:hello2
[INFO] [1667199574.881232]: 发布数据:hello3
[INFO] [1667199575.881403]: 发布数据:hello4
[INFO] [1667199576.881529]: 发布数据:hello5
[INFO] [1667199577.881721]: 发布数据:hello6
[INFO] [1667199578.881504]: 发布数据:hello7
[INFO] [1667199579.881090]: 发布数据:hello8
[INFO] [1667199580.881624]: 发布数据:hello9
[INFO] [1667199581.881889]: 发布数据:hello10
总结
c++与python编程中通过加入节点关闭api代替了控制台终端的ctrl+c的关闭方式。
多种日志输出
C++
之前多用ROS_INFO("");
现在由严重程度给出了更多的日志输出方式。
ROS_DEBUG("hello,DEBUG"); //不会输出
ROS_INFO("hello,INFO"); //默认白色字体
ROS_WARN("Hello,WARN"); //默认黄色字体
ROS_ERROR("hello,ERROR");//默认红色字体
ROS_FATAL("hello,FATAL");//默认红色字体
python
之前多用rospy.loginfo("hello,info")
现在由严重程度给出了更多的日志输出方式。
rospy.logdebug("hello,debug") #不会输出
rospy.loginfo("hello,info") #默认白色字体
rospy.logwarn("hello,warn") #默认黄色字体
rospy.logerr("hello,error") #默认红色字体
rospy.logfatal("hello,fatal") #默认红色字体