ROS动作编程

1、目标

客户端发送一个运动坐标,模拟机器人运动到目标位置的过程。包括服务端和客户端的代码实现,要求带有实时位置反馈。

2、ROS动作编程

2.1 创建一个功能包

这里就直接略过了,还是使用上篇博客中使用的功能包

2.2 在功能包路径下创建一个action文件夹

cd ~/catkin_ws/src/learning_communication
mkdir action
cd action
touch turtleMove.action

2.3 在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.4 进入上级目录中的src文件夹下,编写相关的cpp文件

cd ../src
touch turtleMove.cpp
touch turtleMoveClient.cpp

小海龟移动的“服务文件”turtleMove.cpp

#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;
    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 "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)
{
    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
}

2.5 在CMakeLists.txt添加编译选项

找到find_package,在里面添加

actionlib_msgs
actionlib

在这里插入图片描述

找到add_action_files这里,添加上,并将修改generate_messages

add_action_files(FILES turtleMove.action)
generate_messages(DEPENDENCIES std_msgs actionlib_msgs)

在这里插入图片描述

找到catkin_package,修改成如下图所示:
在这里插入图片描述

添加上如下代码:

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)

在这里插入图片描述

2.6 修改package.xml

在里面添加上如下代码:

<build_depend>actionlib</build_depend>
<build_depend>actionlib_msgs</build_depend>
<exec_depend>actionlib</exec_depend>
<exec_depend>actionlib_msgs</exec_depend>

在这里插入图片描述

2.7 运行测试文件

先进行编译:

cd ~/catkin_ws
catkin_make
source ./devel/setup.bash

开启ros:
image-20230312010843381
新建一个终端,运行小海龟:

rosrun turtlesim turtlesim_node

image-20230312011050838
再新建一个终端,运行我们的目标位置程序代码

rosrun learning_communication turtleMoveClient

image-20230312011341265

在终端 1 运行 turtleMove,当小海龟在移动时,我们可以看到相应的窗口中的信息也在不断的更新

rosrun learning_communication turtleMove

image-20230312175949180

3、总结

本次试验的难度稍微有点大,主要的难度在于代码的理解和编写,里面的很多东西刚开始都没有看明白。所以在写的时候就很懵。之后在这里要多多的注意,还有没懂的东西之后还要抓紧时间理解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值