目录
- 一、创建工作空间
- 二、ROS通信编程
-
- 五、总结
- 六、参考资料
本文内容:
学习古月居 ROS 教学课件和相关视频,练习课件上的话题通信、服务通信编程代码示例。
一、创建工作空间
1.1 什么是工作空间
- 工作空间(workspace)是一个存放工程开放相关文件的文件夹,它包括:
文件夹 | 含义 |
---|
src | 代码空间(Source Space) |
build | 编译空间(Build Space) |
devel | 开发空间(Development Space) |
install | 安装空间(Install Space) |
- 一个工作空间的大致文件层次如下图所示:
- 而我们后面的编程文件主要是放在 package 下面。
1.2 创建工作空间
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
- 编译工作空间(后续的编译都是以这两句命令编译的)。
cd ~/catkin_ws/
catkin_make
- 下面设置环境变量,让其对所有终端都有效。
- 首先编辑环境文件:
sudo nano ~/.bashrc
- 在文件末尾添加一条命令:
source /home/zcj/catkin_ws/devel/setup.bash
(其中 zcj 是用户名,修改为自己的用户名)。
- 修改完毕后,使用下面的命令立即生效文件。
source ~/.bashrc
- 如果想要知道环境变量,可以使用下面的命令检查环境变量。
echo $ROS_PACKAGE_PATH
1.3 创建功能包
命令格式:
catkin_create_pkg <package_name> [depend1] [depend2] [depend3]
同一个工作空间下,不允许存在同名功能包
不同工作空间下,允许存在同名功能包
cd ~/catkin_ws/src
catkin_create_pkg learning_communication std_msgs rospy roscpp
cd ~/catkin_ws
catkin_make
二、ROS通信编程
2.1 话题编程
2.1.1 话题编程流程
步骤 | 过程 |
---|
第一步 | 创建发布者 |
第二步 | 创建订阅者 |
第三步 | 添加编译选项 |
第四步 | 运行可执行程序 |
2.1.2 发布者
- 首先在 ~/catkin_ws/src/learning_communication/src 下创建一个 talker.cpp 文件
cd ~/catkin_ws/src/learning_communication/src
gedit talker
#include <sstream>
#include "ros/ros.h"
#include "std_msgs/String.h"
int main(int argc, char **argv)
{
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
int count = 0;
while(ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss<<"hello world"<<count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
++count;
}
return 0;
}
2.1.3 订阅者:
- 实现步骤:
- 初始化 ROS 节点
- 订阅需要的话题
- 循环等待话题消息,接收到消息后进入回调函数
- 在回调函数中完成消息处理
- 首先在同目录下创建一个 listener.cpp 文件。
- 复制粘贴以下代码。
#include "ros/ros.h"
#include "std_msgs/String.h"
void chatterCallback(const std_mmsgs::String::ConstPtr& msg)
{
ROS_INFO("I heard: [%s]",msg->data.c_str());
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
ros::spin();
return 0;
}
2.1.4 编译代码
- 首先要配置编译文件 CMakeLists.txt。
- 进入到 ~/catkin_ws/src/learning_commuication 下,会有一个 CMakeLists.txt 文件,编辑此文件。
cd ..
ll
gedit 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})
cd ~/catkin_ws
catkin_make
- 如果编译时出现了大量未定义的引用错误,那么就是刚刚在 CMakeLists.txt 内配置时打错了字母,我就遇到了。。。折腾了好久。
2.1.5 运行可执行文件
- 编译完成后,重新打开两个终端:一个运行发布者;另一个运行订阅者。
- 然后现在有三个终端了,首先第一个终端运行
roscore
命令,然后下一个终端运行 talker
命令实现发布者端口,最后一个终端运行 listener
命令实现订阅者端口。
roscore
rosrun learning_communication talker
rosrun learning_communication listener
2.1.6 自定义话题消息
mkdir ~/catkin_ws/src/learning_communication/msg
cd ~/catkin_ws/src/learning_communication/msg
sudo nano Person.msg
string name
uint8 sex
uint8 age
uint8 unknown=0
uint8 male=1
uint8 female=2
- 返回到上级目录,编辑 package.xml 文件。
cd ..
gedit package.xml
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
说明:
部分 ROS 版本中 exec_depend 需要修改为 run_depend
- 然后使用命令
gedit CMakeLists.txt
编辑编译文件。
- 添加如下内容(打字眼神不好的可以复制粘贴):
- message_generation
- 注意别看错了位置。
- CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
- 开始编译。
cd ~/catkin_ws
catkin_make
rosmsg show Person
2.2 服务编程
2.2.1 服务编程流程
步骤 | 过程 |
---|
第一步 | 创建服务器 |
第二步 | 创建客户端 |
第三步 | 添加编译选项 |
第四步 | 运行可执行程序 |
2.2.2 自定义服务请求与应答
mkdir ~/catkin_ws/src/learning_communication/srv
cd ~/catkin_ws/src/learning_communication/srv
sudo nano AddTwoInts.srv
int64 a
int64 b
---
int64 sum
说明:
在第二部分添加过了就可以不用再添加了。
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
- 使用命令
gedit CMakeLists.txt
编辑 CMakeLists.txt 文件。
- 在文件中添加编译选项:
- 上面两个修改的部分,在话题编程中已经添加过了。
- 新修改的地方如下,添加一个服务文件:
2.2.3 实现一个服务器与客户端
- 在
.../learning_communication/src
下创建 server.cpp 和 client.cpp 文件并复制粘贴相应的代码。
- server.cpp
#include<ros/ros.h>
#include"learning_communication/AddTwoInts.h"
bool add(learning_communication::AddTwoInts::Request &req,learning_communication::AddTwoInts::Response &res)
{
res.sum=req.a+req.b;
ROS_INFO("request: x=%1d,y=%1d",(long int)req.a,(long int)req.b);
ROS_INFO("sending back response:[%1d]",(long int)res.sum);
return true;
}
int main(int argc,char **argv)
{
ros::init(argc,argv,"add_two_ints_server");
ros::NodeHandle n;
ros::ServiceServer service=n.advertiseService("add_two_ints",add);
ROS_INFO("Ready to add two ints.");
ros::spin();
return 0;
}
#include<cstdlib>
#include<ros/ros.h>
#include"learning_communication/AddTwoInts.h"
int main(int argc,char **argv)
{
ros::init(argc,argv,"add_two_ints_client");
if(argc!=3)
{
ROS_INFO("usage:add_two_ints_client X Y");
return 1;
}
ros::NodeHandle n;
ros::ServiceClient client=n.serviceClient<learning_communication::AddTwoInts>("add_two_ints");
learning_communication::AddTwoInts srv;
srv.request.a=atoll(argv[1]);
srv.request.b=atoll(argv[2]);
if(client.call(srv))
{
ROS_INFO("sum: %1d",(long int)srv.response.sum);
}
else
{
ROS_INFO("Failed to call service add_two_ints");
return 1;
}
return 0;
}
2.2.4 编译代码
- 首先返回上一目录,并编辑 CMakeLists.txt 文件。
- 设置编译文件、链接库和依赖。
add_executable(server src/server.cpp)
target_link_libraries(server ${catkin_LIBRARIES})
add_dependencies(server ${PROJECT_NAME}_gencpp)
add_executable(client src/client.cpp)
target_link_libraries(client ${catkin_LIBRARIES})
add_dependencies(client ${PROJECT_NAME}_gencpp)
cd ~/catkin_ws
catkin_make
- 有一些警告信息,可以忽视。
- 如果编译错误,那就是眼神不好打错字母了,重新对照一样找出错误就行了。
2.2.5 运行可执行文件
- 还是跟第二部分的话题编程一样执行方式:用三个终端分别运行一条命令。
roscore
rosrun learning_communication server
rosrun learning_communication client 整数1 整数2
2.3 动作编程
2.3.1 动作编程简介
- 什么是动作?
- 一种问答通信机制;
- 带有连续反馈;
- 可以在任务过程中止运行;
- 基于 ROS 的消息机制实现。
接口 | 含义 |
---|
goal | 发布任务目标 |
cancel | 请求取消任务 |
status | 通知客户端当前的状态 |
feedback | 周期反馈任务运行的监控数据; |
result | 向客户端发送任务的执行结果,只发布一次 |
2.3.2 自定义动作消息
mkdir ~/catkin_ws/src/learning_communication/action
cd ~/catkin_ws/src/learning_communication/action
sudo nano DoDishes.action
#定义目标信息
uint32 dishwasher_id
---
#定义结果信息
uint32 total_dishes_cleaned
---
#定义周期反馈的消息
float32 percent_complete
- 返回上一级,并打开编辑 package.xml 文件。
cd ..
gedit package.xml
<build_depend>actionlib</build_depend>
<build_depend>actionlib_msgs</build_depend>
<exec_depend>actionlib</exec_depend>
<exec_depend>actionlib_msgs</exec_depend>
- 编辑 CMakeLists.txt 编译文件。
- 修改位置如下:
- DIRECTORY action FILES DoDishes.action
- DEPENDENCIES actionlib_msgs
2.3.3 实现动作服务器与客户端
- 进入到 src 文件夹下,创建 DoDishes_server.cpp 与 DoDishes_client.cpp 文件,并在里面粘贴如下代码:
- DoDishes_server.cpp
#include "ros/ros.h"
#include "actionlib/server/simple_action_server.h"
#include "learning_communication/DoDishesAction.h"
typedef actionlib::SimpleActionServer<learning_communication::DoDishesAction> Server;
void execute(const learning_communication::DoDishesGoalConstPtr &goal, Server *as)
{
ros::Rate r(1);
learning_communication::DoDishesFeedback feedback;
ROS_INFO("Dishwasher %d is working.", goal->dishwasher_id);
for(int i = 1; i <= 10; i++)
{
feedback.percent_complete = i * 10;
as->publishFeedback(feedback);
r.sleep();
}
ROS_INFO("Dishwasher %d finish working.", goal->dishwasher_id);
as->setSucceeded();
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "do_dishes_server");
ros::NodeHandle hNode;
Server server(hNode, "do_dishes", boost::bind(&execute, _1, &server), false);
server.start();
ros::spin();
return 0;
}
#include "ros/ros.h"
#include "actionlib/client/simple_action_client.h"
#include "learning_communication/DoDishesAction.h"
typedef actionlib::SimpleActionClient<learning_communication::DoDishesAction> Client;
void doneCallback(const actionlib::SimpleClientGoalState &state
, const learning_communication::DoDishesResultConstPtr &result)
{
ROS_INFO("Yay! The dishes are now clean");
ros::shutdown();
}
void activeCallback()
{
ROS_INFO("Goal just went active");
}
void feedbackCallback(const learning_communication::DoDishesFeedbackConstPtr &feedback)
{
ROS_INFO("percent_complete : %f", feedback->percent_complete);
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "do_dishes_client");
Client client("do_dishes", true);
ROS_INFO("Waiting for action server to start.");
client.waitForServer();
ROS_INFO("Action server started, sending goal.");
learning_communication::DoDishesGoal goal;
goal.dishwasher_id = 1;
client.sendGoal(goal, &doneCallback, &activeCallback, &feedbackCallback);
ros::spin();
return 0;
}
2.3.4 编译代码
- 返回上一级,并编辑 CMakeLists.txt 文件。
- 添加以下代码:
add_executable(DoDishes_server src/DoDishes_server.cpp)
target_link_libraries(DoDishes_server ${catkin_LIBRARIES})
add_dependencies(DoDishes_server ${${PROJECT_NAME}_EXPORTED_TARGETS})
add_executable(DoDishes_client src/DoDishes_client.cpp)
target_link_libraries(DoDishes_client ${catkin_LIBRARIES})
add_dependencies(DoDishes_client ${${PROJECT_NAME}_EXPORTED_TARGETS})
cd ~/catkin_ws
catkin_make
- 警告信息忽略:
- 如果编译错误,那就是眼神不好打错字母了,重新对照一样找出错误就行了。
2.3.5 运行可执行文件
roscore
rosrun learning_communication DoDishes_client
rosrun learning_communication DoDishes_server
五、总结
- 在配置编译文件的时候很麻烦,且如果眼神不太好打错字了,到后面就很容易编译出错,我碰到好几次了,反复查找,就那么一个字母,折腾死人。
- 其中代码部分我没有太深究,这里只是做演示,有兴趣的话可以研究下这些代码,就差不多在 ROS 通信编程上有了个入门了。
六、参考资料
[1] ROS基础——话题、服务、动作编程
[2] 第2讲:ROS基础.pdf——提取码:ifcf
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)