ros入门(4)使用C++编写Subscriber订阅者节点
cd catkin_ws/src/
catkin_create_pkg atr_pkg rospy roscpp std_msgs
#include<ros/ros.h>
#include<std_msgs/String.h>
void chao_callback(std_msgs::String msg)
{
printf(msg.data.c_str());
printf("\n");
}
int main(int argc, char *argv[])
{
ros::init(argc,argv,"ma_node");
ros::NodeHandle nh;
ros::Subscriber sub=nh.subscribe("kuaishangchekaiheiqun",10,chao_callback);
while (ros::ok())
{
ros::spinOnce();
}
return 0;
}
add_executable(ma_node src/ma_node.cpp)
target_link_libraries(ma_node
${catkin_LIBRARIES}
)
roscore
rosrun ssr_pkg chao_node
rosrun atr_pkg ma_node
优化和调整
#include<ros/ros.h>
#include<std_msgs/String.h>
void chao_callback(std_msgs::String msg)
{
// printf(msg.data.c_str());
// printf("\n");
ROS_INFO(msg.data.c_str());
}
int main(int argc, char *argv[])
{
ros::init(argc,argv,"ma_node");
ros::NodeHandle nh;
ros::Subscriber sub=nh.subscribe("kuaishangchekaiheiqun",10,chao_callback);
while (ros::ok())
{
ros::spinOnce();
}
return 0;
}
这是因为ROS_INFO()的编码方式受程序的Locale环境设置影响一般设置会让一些输出函数只支持英文字符的显示。
解决方案
将环境设置为中文环境集
setlocale(LC_ALL,"");(一般中文版ubuntu设置)
如果是英文版的ubuntu setlocale(LC_ALL,"zh_CN.UTF-8");
#include<ros/ros.h>
#include<std_msgs/String.h>
void chao_callback(std_msgs::String msg)
{
// printf(msg.data.c_str());
// printf("\n");
ROS_INFO(msg.data.c_str());
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
ros::init(argc,argv,"ma_node");
ros::NodeHandle nh;
ros::Subscriber sub=nh.subscribe("kuaishangchekaiheiqun",10,chao_callback);
while (ros::ok())
{
ros::spinOnce();
}
return 0;
}
订阅yao_node
#include<ros/ros.h>
#include<std_msgs/String.h>
void chao_callback(std_msgs::String msg)
{
// printf(msg.data.c_str());
// printf("\n");
ROS_INFO(msg.data.c_str());
}
void yao_callback(std_msgs::String msg)
{
// printf(msg.data.c_str());
// printf("\n");
ROS_WARN(msg.data.c_str());
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
ros::init(argc,argv,"ma_node");
ros::NodeHandle nh;
ros::Subscriber sub=nh.subscribe("kuaishangchekaiheiqun",10,chao_callback);
ros::Subscriber sub_2=nh.subscribe("gie_gie_dai_wo",10,yao_callback);
while (ros::ok())
{
ros::spinOnce();
}
return 0;
}
rqt_graph
原文地址:https://blog.csdn.net/tingwods/article/details/137927397
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!