Reference learning materials: Zhao Xuzuo's ROS course at Station B
node down
C++
node shutdown api
ros::shutdown()
Modify the previous 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;
}
Points to note:
The condition of the while loop is whether the ros node is running, if the ros node stops while the loop is stopped, so the previous operation is to exit at the console terminal ctrl+c.
while (ros::ok())
After the modification, when the count is greater than 10, the ros node will be closed, and there is no need to close it with ctrl+c.
if(count <= 10)
{
pub.publish(msg);
ROS_INFO("发布数据:%s", msg.data.c_str());
rate.sleep();
}
else
{
ros::shutdown();
}
operation result:
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
node shutdown api
rospy.signal_shutdown("string")
Modify the previous 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()
Point of attention! ! ! When calling the node shutdown api, it is necessary to give a human-readable reason, and it is given in the form of a string. (@param reason: human-readable shutdown reason, if applicable @type reason: str)
Otherwise, an error will be reported:
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)
operation result:
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
Summarize
In c++ and python programming, the ctrl+c closing method of the console terminal is replaced by adding the node closing api.
Various log output
C++
Before using ROS_INFO("");
More logging options are now given by severity.
ROS_DEBUG("hello,DEBUG"); //不会输出
ROS_INFO("hello,INFO"); //默认白色字体
ROS_WARN("Hello,WARN"); //默认黄色字体
ROS_ERROR("hello,ERROR");//默认红色字体
ROS_FATAL("hello,FATAL");//默认红色字体
python
Previously used rospy.loginfo("hello, info")
More logging options are now given by severity.
rospy.logdebug("hello,debug") #不会输出
rospy.loginfo("hello,info") #默认白色字体
rospy.logwarn("hello,warn") #默认黄色字体
rospy.logerr("hello,error") #默认红色字体
rospy.logfatal("hello,fatal") #默认红色字体