文章目录
ROS动作编程
一、在工程包中创建项动作编程需要的文件
1、 创建小乌龟移动的“服务文件”turtleMove.cpp
1 、新建一个终端,这里命名为终端 1,然后进入工程包 comm 下的 src 文件中
命令:cd ~/ros/src/comm/src
2 、新建服务文件 turtleMove.cpp
命令:cd ~/ros/src/comm/src
3 、将如下 C++语言代码写入 turtleMove.cpp 文件中,保存后关闭!
命令:gedit turtleMove.cpp
/*
此程序通过通过动作编程实现由 client 发布一个目标位置
然后控制 Turtle 运动到目标位置的过程
*/
#include <ros/ros.h>
#include <actionlib/server/simple_action_server.h>
#include "comm/turtleMoveAction.h"
#include <turtlesim/Pose.h>
#include <turtlesim/Spawn.h>
#include <geometry_msgs/Twist.h>
typedef actionlib::SimpleActionServer<comm::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 comm::turtleMoveGoalConstPtr& goal, Server* as)
{
comm::turtleMoveFeedback feedback;
ROS_INFO("TurtleMove is working.");
turtle_target_pose.x=goal->turtle_target_x;
- 3 -
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);//发布控
制小乌龟运动的速度
// 定义一个服务器
- 4 -
Server server(n, "turtleMove", boost::bind(&execute, _1,
&server), false);
// 服务器开始运行
server.start();
ROS_INFO("server has started.");
ros::spin();
return 0;
}
文件创建完毕
2、创建小乌龟“发布目标位置文件”turtleMoveClient.cpp
1)、在相同的目录下,创建“发布目标位置”文件 turtleMoveClient.cpp
命令:touch turtleMoveClient.cpp
2、将如下 C++代码写入目标位置文件 turtleMoveClient.cpp 中,如下:
命令:gedit turtleMoveClient.cpp
#include <actionlib/client/simple_action_client.h>
#include "comm/turtleMoveAction.h"
#include <turtlesim/Pose.h>
#include <turtlesim/Spawn.h>
#include <geometry_msgs/Twist.h>
typedef actionlib::SimpleActionClient<comm::turtleMoveAction>
Client;
struct Myturtle
{
float x;
float y;
float theta;
}turtle_present_pose;
// 当 action 完成后会调用该回调函数一次
void doneCb(const actionlib::SimpleClientGoalState& state,
const comm::turtleMoveResultConstPtr& result)
{
- 5 -
ROS_INFO("Yay! The turtleMove is finished!");
ros::shutdown();
}
// 当 action 激活后会调用该回调函数一次
void activeCb()
{
ROS_INFO("Goal just went active");
}
// 收到 feedback 后调用该回调函数
void feedbackCb(const comm::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, "turtleMoveClient");
// 定义一个客户端
Client client("turtleMove", true);
// 等待服务器端
ROS_INFO("Waiting for action server to start.");
client.waitForServer();
ROS_INFO("Action server started, sending goal.");
// 创建一个 action 的 goal
comm::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、在功能包目录下创建 action 文件夹
1、进入工程包目录
命令:cd ~/ros/src/comm
2、创建 action 文件夹
命令:mkdir action
3、在 action 文件夹下创建 turtleMove.action 文件
- 进入 action 文件夹
命令:cd action
- 创建 turtleMove.action 文件
命令:touch turtleMove.action
- 将如下代码写入 turtleMove.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
- 7 -
float64 present_turtle_x
float64 present_turtle_y
float64 present_turtle_theta
4、修改CMake和package文件
在CMakeLists.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)
在该文件中找到find_package函数方法,修改为如下:
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
actionlib_msgs
actionlib
)
继续在该文件中找到add_action_files函数一项,默认是用#注释掉了的,这里我们找到后修改为如下代码:
add_action_files(
FILES
turtleMove.action
)
注意:以上turtleMove.action为我们在action文件夹下面创建的文件名字turtleMove.action
继续在该文件中找到generate_messages函数一项,默认也是#注释,这里修改为如下代码
generate_messages(
DEPENDENCIES
std_msgs
actionlib_msgs
)
找到catkin_package函数一项,修改为如下代码:
# INCLUDE_DIRS include
# LIBRARIES comm
# CATKIN_DEPENDS roscpp rospy std_msgs
# DEPENDS system_lib
CATKIN_DEPENDS roscpp rospy std_msgs
message_runtime
修改package.xml文件内容
将文件中build_depend一栏、替换为如下代码:
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>message_generation</build_depend>
<build_depend>actionlib</build_depend>
<build_depend>actionlib_msgs</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
将文件中exec_depend一栏、替换为如下代码:
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>message_runtime</exec_depend>
<exec_depend>actionlib</exec_depend>
<exec_depend>actionlib_msgs</exec_depend>
5、运行程序进行测试
编译文件
catkin_make
注册程序
source ./devel/setup.bash
新建一个终端,命令为终端2,启动ros
再新建一个终端,命名为终端3,运行我们的小海龟,出现小海龟窗口,我们命名为窗口3
在新建一个终端,命名为终端4,运行我们的目标位置程序代码:
进入ros工作空间
cd ~/ros
程序注册
source ./devel/setup.bash
运行turtleMoveClient.cpp,记住,输入该命令,但不忙按回车
rosrun comm turtleMoveClient
在终端1运行turtleMove,记住,输入如下命令,也不忙按回车
rosrun comm turtleMove
开始测试,终端1回车、终端4回车,出现如下画面,则我们动作编程的实验完美成功:
实验成功,我们在终端1 Ctrl+c关闭运行的程序,完成实验
总结
ros进行动作编程是进行嵌入式开发的一个核心步骤,也是帮助我们更好的了解机器学习的一项重要基础,通过动作编程,我们可以了解到机器人是如何进行我们人类行为得规划的,这也为我们未来进入人工智能打下一个良好的开端,这次实验中出现了编译失败的过程,但我并未直接解决该问题,我是通过创建一个新的工作空间来解决编译问题。
参考
https://blog.csdn.net/qq_42451251/article/details/104747355?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167878017216782428610630%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=167878017216782428610630&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_ecpm_v1~rank_v31_ecpm-1-104747355-null-null.142v73pc_search_v2,201v4add_ask,239v2insert_chatgpt&utm_term=void%20feedbackCb%28const%20comm%3A%3AturtleMoveFeedbackConstPtr%26%20feedback%29&spm=1018.2226.3001.4187