1、工作空间创建
(1)创建ros工作区
mkdir -p ~/ros/src
(2)进入工作区src文件夹
cd ~/ros/src
(3)生成工作区
catkin_init_workspace
(4)编译工作区
进入工作区
编译
catkin_make
生成了build和devel文件夹
(5)注册工作区
bash注册
source devel/setup.bash
验证注册
echo $ROS_PACKAGE_PATH
2、创建工程包
(1)切换工作区
cd ~/ros/src
(2)创建comm(通信)包,此包依靠std_msgs、roscpp、rospy
(3)编译工程包
进入工作区
cd ~/ros
编译
catkin_make
3、创建收发节点
节点(node)是连接到ROS网络中可执行的基本单元
此处创建收发节点即是发布者与订阅者
(1)发布者
转至工程包
cd ~/ros/src/comm
进入src子目录
cd src
创建talker.cpp文件,写一个发布(Publisher)节点
1)创建talker.cpp文件
touch talker.cpp
2)打开文件
gedit talker.cpp
3)替换为如下内容
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <sstream>
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, "talker");
/**
* 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 advertise() function is how you tell ROS that you want to
* publish on a given topic name. This invokes a call to the ROS
* master node, which keeps a registry of who is publishing and who
* is subscribing. After this advertise() call is made, the master
* node will notify anyone who is trying to subscribe to this topic name,
* and they will in turn negotiate a peer-to-peer connection with this
* node. advertise() returns a Publisher object which allows you to
* publish messages on that topic through a call to publish(). Once
* all copies of the returned Publisher object are destroyed, the topic
* will be automatically unadvertised.
*
* The second parameter to advertise() is the size of the message queue
* used for publishing messages. If messages are published more quickly
* than we can send them, the number here specifies how many messages to
* buffer up before throwing some away.
*/
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
/**
* A count of how many messages we have sent. This is used to create
* a unique string for each message.
*/
int count = 0;
while (ros::ok())
{
/**
* This is a message object. You stuff it with data, and then publish it.
*/
std_msgs::String msg;
std::stringstream ss;
ss << "hello world " << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
/**
* The publish() function is how you send messages. The parameter
* is the message object. The type of this object must agree with the type
* given as a template parameter to the advertise<>() call, as was done
* in the constructor above.
*/
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
(2)订阅者
创建listener.cpp文件,写一个订阅(Subscriber)节点
创建listener.cpp文件
touch listener.cpp
打开文件
gedit listener.cpp
替换为如下内容
#include "ros/ros.h"
#include "std_msgs/String.h"
/**
* This tutorial demonstrates simple receipt of messages over the ROS system.
*/
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]", msg->data.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;
}
(3)编辑文件
编辑Cmakelist.txt文件
(在工作区ros/src/comm下)
添加代码
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker comm_generate_messages_cpp)
add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener comm_generate_messages_cpp)
添加代码在文件末尾
相邻代码要紧挨不能空行
切换工作区
cd ~/ros
编译
catkin_make
catkin_make报错:
CMakeLists.txt中的cmake_minimum_required(VERSION 版本号)改为cmake_minimum_required(VERSION 2.8.3)
使用的ros版本为noetic
至此节点创建完成
4、测试程序正确性
(1)新开终端,启动roscore
保持终端开启状态
(2)注册程序在原来的终端
进入工作区
cd ~/ros
注册程序
source ./devel/setup.bash
(3)运行talker节点
输入命令,但先别回车确认
rosrun comm talker
(4)运行listener节点
新建终端
进入工作区
cd ~/ros
注册程序
source ./devel/setup.bash
运行listener节点
输入命令先别回车
(5)测试程序
在最开始的终端回车,然后依次回车
测试成功!listener成功接收talker信息!
5、总结
参考
https://blog.csdn.net/qq_42451251/article/details/104650192
https://blog.csdn.net/DamonSoap/article/details/124844640
总结
学习工作空间和功能包的创建与使用,同时学习如何进行节点通信