文章目录
1 话题、服务模式的ROS程序设计
1.1 创建工作空间
创建工作空间
mkdir -p ~/comm_ws/src
cd ~/comm_ws/src
catkin_init_workspace
编译工作空间,返回上一目录进行编译
cd ..
catkin_make
进入src目录,创建功能包
catkin_create_pkg learning_communication std_msgs rospy roscpp
编译功能包
cd ..
catkin_make
source ~/comm_ws/devel/setup.bash
1.2 话题编程
1.2.1 步骤
- 创建发布者
- 初始化ROS节点
- 向ROS Master注册节点信息,包括发布的话题名和话题中的消息类型
- 按照一定频率循环发布消息
- 创建订阅者
- 初始化ROS节点
- 订阅需要的话题
- 循环等待话题消息,接收到消息后进行回调函数
- 回调函数中完成消息处理
- 添加编译选项
- 设置需要编译的代码和生成的可执行文件
- 设置链接库
- 设置依赖
- 运行可执行程序
1.2.2 实践
- 添加文件
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.c_str());
}
int main(int argc,char **argv)
{
//初始化ROS节点
ros::init(argc,argv,"listener");
//创建节点句柄
ros::NodeHandle n;
//创建一个Subscriber,订阅名为chatter的topic,注册回调函数chatterCallback
ros::Subscriber sub=n.subscribe("chatter",1000,chatterCallback);
//循环等待回调函数
ros::spin();
return 0;
}
talker.cpp
#include<sstream>
#include"ros/ros.h"
#include"std_msgs/String.h"
int main(int argc,char **argv)
{
//ROS节点初始化
ros::init(argc,argv,"talker");
//创建节点句柄
ros::NodeHandle n;
//创建一个Publisher,发布名为chatter的topic,消息类型为std_msgs::String
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类型的消息
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;
}
- CMakeLists.txt文件
进入工程包目录下,打开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 ~/comm_ws/
catkin_make
- 运行可执行文件
启动ros
roscore
打开两个新的终端分别输入:
cd ~/comm_ws/
source devel/setup.bash
rosrun learning_communication talker
cd ~/comm_ws/
source devel/setup.bash
rosrun learning_communication listener
5. 自定义话题消息
定义msg文件
mkdir ~/comm_ws/src/learning_communication/msg
cd ~/comm_ws/src/learning_communication/msg
vim Person.msg
在package.xml
中添加功能包依赖
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
修改CMakeLists.txt
编译
查看自定义消息
rosmsg show Person
2 动作编程
练习ROS动作编程:客户端发送一个运动坐标,模拟机器人运动到目标位置的过程。包括服务端和客户端的代码实现,要求带有实时位置反馈。
2.1 创建文件
创建小乌龟移动的“服务文件”turtleMove.cpp
/*
此程序通过通过动作编程实现由client发布一个目标位置
然后控制Turtle运动到目标位置的过程
*/
#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include "learning_communication/TurtleMoveAction.h"
#include <turtlesim/Pose.h>
#include <turtlesim/Spawn.h>
#include <geometry_msgs/Twist.h>
typedef actionlib::SimpleActionServer<learning_communication::TurtleMoveAction> Server;
struct Myturtle
{
float x;
float y;
float theta;
}turtle_original_pose,turtle_target_pose;
ros::Publisher turtle_vel;
void posecallback(const turtlesim::PoseConstPtr& msg)
{
ROS_INFO("Turtle1_position:(%f,%f,%f)",msg->x,msg->y,msg->theta);
turtle_original_pose.x=msg->x;
turtle_original_pose.y=msg->y;
turtle_original_pose.theta=msg->theta;
}
// 收到action的goal后调用该回调函数
void execute(const learning_communication::TurtleMoveGoalConstPtr& goal, Server* as)
{
learning_communication::TurtleMoveFeedback feedback;
ROS_INFO("TurtleMove is working.");
turtle_target_pose.x=goal->turtle_target_x;
turtle_target_pose.y=goal->turtle_target_y;
turtle_target_pose.theta=goal->turtle_target_theta;
geometry_msgs::Twist vel_msgs;
float break_flag;
while(1)
{
ros::Rate r(10);
vel_msgs.angular.z = 4.0 * (atan2(turtle_target_pose.y-turtle_original_pose.y,
turtle_target_pose.x-turtle_original_pose.x)-turtle_original_pose.theta);
vel_msgs.linear.x = 0.5 * sqrt(pow(turtle_target_pose.x-turtle_original_pose.x, 2) +
pow(turtle_target_pose.y-turtle_original_pose.y, 2));
break_flag=sqrt(pow(turtle_target_pose.x-turtle_original_pose.x, 2) +
pow(turtle_target_pose.y-turtle_original_pose.y, 2));
turtle_vel.publish(vel_msgs);
feedback.present_turtle_x=turtle_original_pose.x;
feedback.present_turtle_y=turtle_original_pose.y;
feedback.present_turtle_theta=turtle_original_pose.theta;
as->publishFeedback(feedback);
ROS_INFO("break_flag=%f",break_flag);
if(break_flag<0.1) break;
r.sleep();
}
// 当action完成后,向客户端返回结果
ROS_INFO("TurtleMove is finished.");
as->setSucceeded();
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "TurtleMove");
ros::NodeHandle n,turtle_node;
ros::Subscriber sub = turtle_node.subscribe("turtle1/pose",10,&posecallback); //订阅小乌龟的位置信息
turtle_vel = turtle_node.advertise<geometry_msgs::Twist>("turtle1/cmd_vel",10);//发布控制小乌龟运动的速度
// 定义一个服务器
Server server(n, "TurtleMove", boost::bind(&execute, _1, &server), false);
// 服务器开始运行
server.start();
ROS_INFO("server has started.");
ros::spin();
return 0;
}
创建小乌龟“发布目标位置文件”turtleMoveClient.cpp
#include <actionlib/client/simple_action_client.h>
#include "learning_communication/TurtleMoveAction.h"
#include <turtlesim/Pose.h>
#include <turtlesim/Spawn.h>
#include <geometry_msgs/Twist.h>
typedef actionlib::SimpleActionClient<learning_communication::TurtleMoveAction> Client;
struct Myturtle
{
float x;
float y;
float theta;
}turtle_present_pose;
// 当action完成后会调用该回调函数一次
void doneCb(const actionlib::SimpleClientGoalState& state,
const learning_communication::TurtleMoveResultConstPtr& result)
{
ROS_INFO("Yay! The TurtleMove is finished!");
ros::shutdown();
}
// 当action激活后会调用该回调函数一次
void activeCb()
{
ROS_INFO("Goal just went active");
}
// 收到feedback后调用该回调函数
void feedbackCb(const learning_communication::TurtleMoveFeedbackConstPtr& feedback)
{
ROS_INFO(" present_pose : %f %f %f", feedback->present_turtle_x,
feedback->present_turtle_y,feedback->present_turtle_theta);
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "TurtleMove_client");
// 定义一个客户端
Client client("TurtleMove", true);
// 等待服务器端
ROS_INFO("Waiting for action server to start.");
client.waitForServer();
ROS_INFO("Action server started, sending goal.");
// 创建一个action的goal
learning_communication::TurtleMoveGoal goal;
goal.turtle_target_x = 1;
goal.turtle_target_y = 1;
goal.turtle_target_theta = 0;
// 发送action的goal给服务器端,并且设置回调函数
client.sendGoal(goal, &doneCb, &activeCb, &feedbackCb);
ros::spin();
return 0;
}
2.2 创建action文件夹
在功能包目录下创建action文件夹,并在此文件夹下创建TurtleMove.action
文件
添加如下内容:
# Define the goal
float64 turtle_target_x # Specify Turtle's target position
float64 turtle_target_y
float64 turtle_target_theta
---
# Define the result
float64 turtle_final_x
float64 turtle_final_y
float64 turtle_final_theta
---
# Define a feedback message
float64 present_turtle_x
float64 present_turtle_y
float64 present_turtle_theta
2.3 修改文件
修改CMakeList.txt
文件
在文件尾添加如下内容
add_executable(turtleMoveClient src/turtleMoveClient.cpp)
target_link_libraries(turtleMoveClient ${
catkin_LIBRARIES})
add_dependencies(turtleMoveClient ${
PROJECT_NAME}_gencpp)
add_executable(turtleMove src/turtleMove.cpp)
target_link_libraries(turtleMove ${
catkin_LIBRARIES})
add_dependencies(turtleMove ${
PROJECT_NAME}_gencpp)
其他修改内容如下:
修改package.xml
文件
<build_depend>message_generation</build_depend>
<build_depend>actionlib</build_depend>
<build_depend>actionlib_msgs</build_depend>
<exec_depend>message_runtime</exec_depend>
<exec_depend>actionlib</exec_depend>
<exec_depend>actionlib_msgs</exec_depend>
2.4 编译运行
编译
在四个终端按顺序分别运行下面命令
roscore
rosrun turtlesim turtlesim_node
rosrun learning_communication turtleMove
rosrun learning_communication turtleMoveClient
3 分布式通信
在两台电脑上演示ROS的分布式通信
3.1 主机
开启ros
roscore
新建一个终端,输入
export ROS_IP=xxx.xxx.xxx #本机IP
export ROS_MASTER_URI=http://xxx.xxx.xxx:11311/ #主机IP
rosrun turtlesim turtlesim_node
source ~/.bashrc
3.2 从机
终端中输入
export ROS_IP=X.X.X.X #本机ip
export ROS_MASTER_URI=http://x.x.x.x:11311 #主机IP
source ~/.bashrc
rosrun turtlesim turtle_teleop_key
主机:
从机:
即可实现从机上控制小海龟行走
总结
通过本次实验,学习了解了ROS相关控制机制,学习了话题、服务、动作的相关知识,同时也学习了解到了分布式通信,实现了从机控制主机的小海龟的行走。
参考
https://blog.csdn.net/qq_43279579/article/details/114764633
https://blog.csdn.net/xiongmingkang/article/details/81203329