ROS通信模式编程

本文详细介绍了如何使用ROS进行动作编程,通过创建TurtleMove行动来控制小海龟到达目标位置。涉及创建功能包、定义action文件、编写服务器端和客户端代码,并实现位置反馈。通过这个过程,读者可以理解ROS中动作服务器和客户端的交互以及分布式通信的工作流程。
摘要由CSDN通过智能技术生成

一、创建工作区间
创建功能包:
mkdir -p ~/catkin_ws/src
1
cd catkin_ws/src/
1
catkin_create_pkg learn_action std_msgs rospy roscpp
1
编译功能包:
cd ~/catkin_ws
1
catkin_make
1
source ~/catkin_ws/devel/setup.bash
1
二、动作编程: 客户端发送一个运动目标,模拟机器人运动到目标位置的过程,带有实时位置反馈;
1.定义action文件
在learn_action文件下创建action文件

在这里插入图片描述在action文件下创建TurtleMove.action文件,并在TurtleMove.action文件内输入代码:
在这里插入图片描述

float64 turtle_target_x

float64 turtle_target_y
float64 turtle_target_theta

float64 turtle_final_x
float64 turtle_final_y
float64 turtle_final_theta

float64 present_turtle_x
float64 present_turtle_y
float64 present_turtle_theta2.
2.在learn_action的src文件夹下,创建TurtleMove_server.cpp文件和TurtleMove_client.cpp文件
在这里插入图片描述

1)TurtleMove_server.cpp文件
/*    此程序通过通过动作编程实现由client发布一个目标位置    然后控制Turtle运动到目标位置的过程 /
#include <ros/ros.h> #include <actionlib/server/simple_action_server.h>
#include “learn_action/TurtleMoveAction.h”
#include <turtlesim/Pose.h>
#include <turtlesim/Spawn.h>
#include <geometry_msgs/Twist.h>
typedef actionlib::SimpleActionServer<learn_action::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 learn_action::TurtleMoveGoalConstPtr& goal, Server
as)
{
learn_action::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_server”);
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;
}
2)TurtleMove_client.cpp文件
#include <actionlib/client/simple_action_client.h>
#include “learn_action/TurtleMoveAction.h”
#include <turtlesim/Pose.h>
#include <turtlesim/Spawn.h>
#include <geometry_msgs/Twist.h>
typedef actionlib::SimpleActionClient<learn_action::TurtleMoveAction> Client;
struct Myturtle
{
float x;
float y;
float theta;
}turtle_present_pose;
// 当action完成后会调用该回调函数一次
void doneCb(const actionlib::SimpleClientGoalState& state, const learn_action::TurtleMoveResultConstPtr& result)
{
ROS_INFO(“Yay! The TurtleMove is finished!”);
ros::shutdown();
}
// 当action激活后会调用该回调函数一次
void activeCb()
{
ROS_INFO(“Goal just went active”);
}
// 收到feedback后调用该回调函数
void feedbackCb(const learn_action::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
learn_action::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)TurtleMove_client.cpp文件
#include <actionlib/client/simple_action_client.h>
#include “learn_action/TurtleMoveAction.h”
#include <turtlesim/Pose.h>
#include <turtlesim/Spawn.h>
#include <geometry_msgs/Twist.h>
typedef actionlib::SimpleActionClient<learn_action::TurtleMoveAction> Client;
struct Myturtle
{
float x;
float y;
float theta;
}turtle_present_pose;
// 当action完成后会调用该回调函数一次
void doneCb(const actionlib::SimpleClientGoalState& state, const learn_action::TurtleMoveResultConstPtr& result)
{
ROS_INFO(“Yay! The TurtleMove is finished!”);
ros::shutdown();
}
// 当action激活后会调用该回调函数一次
void activeCb()
{
ROS_INFO(“Goal just went active”);
}
// 收到feedback后调用该回调函数
void feedbackCb(const learn_action::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
learn_action::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;
}
3)修改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>
4)修改 ~/catkin_ws/src/learn_action/CMakeLists.txt

按如下图片作修改:
在这里插入图片描述
在这里插入图片描述
最后在末尾添加:
代码如下:
add_executable(TurtleMove_client src/TurtleMove_client.cpp)
target_link_libraries(TurtleMove_client ${catkin_LIBRARIES})
add_dependencies(TurtleMove_client ${PROJECT_NAME}_gencpp)

add_executable(TurtleMove_server src/TurtleMove_server.cpp)
target_link_libraries(TurtleMove_server ${catkin_LIBRARIES})
add_dependencies(TurtleMove_server ${PROJECT_NAME}_gencpp) 在这里插入图片描述
3.编译及运行
在这里插入图片描述
在这里插入图片描述
设置环境变量:
source ~/catkin_ws/devel/setup.bash
在这里插入图片描述
新建终端1:
roscore
在这里插入图片描述
新建终端2:运行小海龟
source ~/catkin_ws/devel/setup.bash
rosrun turtlesim turtlesim_node
在这里插入图片描述
新建终端3:运行server
source ~/catkin_ws/devel/setup.bash
rosrun learn_action TurtleMove_server
在这里插入图片描述
新建终端4:运行client
source ~/catkin_ws/devel/setup.bash
rosrun learn_action TurtleMove_client
在这里插入图片描述
最后结果如图所示:小海龟成功运动起来。
在这里插入图片描述

总结
通过本次学习,我了解了ROS动作编程的过程。通过使用代码实现服务端与客服端,实时反馈小乌龟的实时位置。电,更加深入的学习了ROS的应用和操作,掌握了ROS分布式通信的流程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值