ROS学习记录:动作编程
引言:
通过本实验,我们将联系我们学过的动作编程,客户端发送一个运动目标,模拟小乌龟运动到目标位置的过程,包含服务端和客户端的代码实现,并且带有实时的位置反馈。
希望你在本次学习过后,能够有一定的收获!!!
推荐歌曲—失忆(正式版)-吕口口
冲啊!!!! ٩(͡๏̯͡๏)۶ ٩(͡๏̯͡๏)۶ ٩(͡๏̯͡๏)۶
文章目录
- ROS学习记录:动作编程
- 一、创建工作区间
- **1.创建功能包**
- **2.编译功能包**
- 二、动作编程
- 1.定义action文件
- 2.创建cpp文件
- 3.修改package.xml文件
- 4.修改 CMakeLists.txt
- 5.编译程序
一、创建工作区间
1.创建功能包
mkdir -p ~/catkin_ws/src
cd catkin_ws/src/
catkin_create_pkg learning_communication std_msgs rospy roscpp
2.编译功能包
cd ~/catkin_ws
catkin_make
source ~/catkin_ws/devel/setup.bash
二、动作编程
1.定义action文件
gedit 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.创建cpp文件
在learning_communication的src文件夹下,创建TurtleMove_server.cpp文件和TurtleMove_client.cpp文件
gedit TurtleMove_server.cpp
gedit TurtleMove_client.cpp
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;
}
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.修改 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)
5.编译程序
cd ~/catkin_ws
catkin_make
编译成功
6.运行程序
程序代码
roscorerosrun turtlesim turtlesim_nodesource ./devel/setup.bash
rosrun learning_communication TurtleMove_serversource ./devel/setup.bash
rosrun learning_communication TurtleMove_client
结果图
最后感谢大佬友情链接:
- https://blog.csdn.net/weixin_45137708/article/details/105351970