ROS探究(二)Clion编译项目
1 在beginner_tutorials包中添加测试代码文件,参考cn/ROS/Tutorials/WritingPublisherSubscriber(c++) - ROS Wiki
tzy@ubuntu:~/ROS_Example/catkin_ws/src/beginner_tutorials/src$ ls
listener.cpp talker.cpp
talker.cpp中我们尝试建立topic传输一个100M大文件内容test.log,代码如下
#include "ros/ros.h" #include "std_msgs/String.h" #include <sstream> #include <fstream>
// 将整个文件读取出并转化成string std::string FileToString(const std::string &filePath) { std::ifstream input_stream(filePath.c_str(), std::ifstream::in); if (!input_stream.is_open()) { return ""; } std::ostringstream sstr; sstr << input_stream.rdbuf(); return sstr.str(); } int main(int argc, char **argv) { ros::init(argc, argv, "talker"); ros::NodeHandle n; ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 5); // 每个文件100M大小,此处缓存队列不能太大 ros::Rate loop_rate(10000); // 加快循环频率 10000HZ,测试发送大字符串速度 int count = 0; while (ros::ok()) { std_msgs::String msg; std::stringstream ss; auto&& output = FileToString("test.log"); // test.log大概100M,使用右值引用优化内存 ss<<" size: "<<output.size()<<" count: "<<count<<"+++++"<<output; msg.data = ss.str(); ROS_INFO("%s", msg.data.substr(0, 50).c_str()); chatter_pub.publish(msg); // ros::spinOnce(); // spinonce时publish的信息会发送 loop_rate.sleep(); // loop_rate的sleep过程中publish会发送消息 ++count; if(count > 1000) break; } ros::spin(); // 程序会卡在这里 return 0; }
listener.cpp进行接收
#include "ros/ros.h" #include "std_msgs/String.h" void chatterCallback(const std_msgs::String::ConstPtr& msg) { ROS_INFO("I heard: [%s]", msg->data.substr(0, 50).c_str()); } int main(int argc, char **argv) { /** * The ros::init() function needs to see argc and argv so that it can perform * any ROS arguments and name remapping that were provided at the command line. * For programmatic remappings you can use a different version of init() which takes * remappings directly, but for most command-line programs, passing argc and argv is * the easiest way to do it. The third argument to init() is the name of the node. * * You must call one of the versions of ros::init() before using any other * part of the ROS system. */ ros::init(argc, argv, "listener"); /** * NodeHandle is the main access point to communications with the ROS system. * The first NodeHandle constructed will fully initialize this node, and the last * NodeHandle destructed will close down the node. */ ros::NodeHandle n; /** * The subscribe() call is how you tell ROS that you want to receive messages * on a given topic. This invokes a call to the ROS * master node, which keeps a registry of who is publishing and who * is subscribing. Messages are passed to a callback function, here * called chatterCallback. subscribe() returns a Subscriber object that you * must hold on to until you want to unsubscribe. When all copies of the Subscriber * object go out of scope, this callback will automatically be unsubscribed from * this topic. * * The second parameter to the subscribe() function is the size of the message * queue. If messages are arriving faster than they are being processed, this * is the number of messages that will be buffered up before beginning to throw * away the oldest ones. */ ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback); /** * ros::spin() will enter a loop, pumping callbacks. With this version, all * callbacks will be called from within this thread (the main one). ros::spin() * will exit when Ctrl-C is pressed, or the node is shutdown by the master. */ ros::spin(); return 0; }
在beginner_tutorials包的CMakeList.txt中添加如下内容
tzy@ubuntu:~/ROS_Example/catkin_ws/src/beginner_tutorials/src$ vim ../CMakeLists.txt
add_executable(talker src/talker.cpp) target_link_libraries(talker ${catkin_LIBRARIES}) add_executable(listener src/listener.cpp) target_link_libraries(listener ${catkin_LIBRARIES})
2 编译
tzy@ubuntu:~/ROS_Example/catkin_ws$ catkin_make
生成的可执行文件在 ~/ROS_Example/catkin_ws/devel/lib/beginner_tutorials目录下
tzy@ubuntu:~/ROS_Example/catkin_ws/devel/lib/beginner_tutorials$ ls
listener talker
3 测试
启动roscore
tzy@ubuntu:~/ROS_Example/catkin_ws$ roscore
启动listener
tzy@ubuntu:~/ROS_Example/catkin_ws$ source devel/setup.bash
tzy@ubuntu:~/ROS_Example/catkin_ws$ rosrun beginner_tutorials listener
启动talker
tzy@ubuntu:~/ROS_Example/catkin_ws$ source devel/setup.bash
tzy@ubuntu:~/ROS_Example/catkin_ws$ rosrun beginner_tutorials talker
获得输出为
listener输出: [ INFO] [1637052110.945022051]: I heard: [ size: 103943090 count: 0+++++618032 robot robo] [ INFO] [1637052111.305311242]: I heard: [ size: 103943090 count: 1+++++618032 robot robo] [ INFO] [1637052111.739413760]: I heard: [ size: 103943090 count: 2+++++618032 robot robo] [ INFO] [1637052112.136378662]: I heard: [ size: 103943090 count: 3+++++618032 robot robo] [ INFO] [1637052112.573668806]: I heard: [ size: 103943090 count: 4+++++618032 robot robo] [ INFO] [1637052112.966500051]: I heard: [ size: 103943090 count: 5+++++618032 robot robo] [ INFO] [1637052113.372856962]: I heard: [ size: 103943090 count: 6+++++618032 robot robo] [ INFO] [1637052113.752406199]: I heard: [ size: 103943090 count: 7+++++618032 robot robo] [ INFO] [1637052114.141449330]: I heard: [ size: 103943090 count: 8+++++618032 robot robo] [ INFO] [1637052114.537786114]: I heard: [ size: 103943090 count: 9+++++618032 robot robo] [ INFO] [1637052114.901102231]: I heard: [ size: 103943090 count: 10+++++618032 robot rob] [ INFO] [1637052115.270014135]: I heard: [ size: 103943090 count: 11+++++618032 robot rob] ...... talker输出为: [ INFO] [1637052110.740766881]: size: 103943090 count: 0+++++618032 robot robo [ INFO] [1637052111.161255732]: size: 103943090 count: 1+++++618032 robot robo [ INFO] [1637052111.573071874]: size: 103943090 count: 2+++++618032 robot robo [ INFO] [1637052111.984516081]: size: 103943090 count: 3+++++618032 robot robo [ INFO] [1637052112.409031092]: size: 103943090 count: 4+++++618032 robot robo [ INFO] [1637052112.808160636]: size: 103943090 count: 5+++++618032 robot robo [ INFO] [1637052113.206262870]: size: 103943090 count: 6+++++618032 robot robo [ INFO] [1637052113.598204760]: size: 103943090 count: 7+++++618032 robot robo [ INFO] [1637052113.990434510]: size: 103943090 count: 8+++++618032 robot robo [ INFO] [1637052114.370096664]: size: 103943090 count: 9+++++618032 robot robo [ INFO] [1637052114.748459862]: size: 103943090 count: 10+++++618032 robot rob [ INFO] [1637052115.120294768]: size: 103943090 count: 11+++++618032 robot rob
4 使用clion打开项目
注意先要在命令行引入ROS环境变量并在命令行打开Clion
tzy@ubuntu:~$ source /opt/ros/kinetic/setup.bash tzy@ubuntu:~$ cd ide/clion-2019.1.4/bin/ tzy@ubuntu:~/ide/clion-2019.1.4/bin$ ./clion.sh
项目打开路径为

项目结构为

加入Release编译选项

编译并运行

效果跟在命令行运行相同。

浙公网安备 33010602011771号