Movebase定点巡航

#include <ros/ros.h>

#include <move_base_msgs/MoveBaseAction.h>

#include <actionlib/client/simple_action_client.h>

 

typedef actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction> MoveBaseClient;

 

int main(int argc, char** argv)

{

    ros::init(argc, argv, "patrol_node");

    ros::NodeHandle nh;

    MoveBaseClient ac("move_base", true);

 

    move_base_msgs::MoveBaseGoal goals[4];

 

    // 设置第一个目标点的坐标

    goals[0].target_pose.header.frame_id = "map";

    goals[0].target_pose.pose.position.x = 1.0;

    goals[0].target_pose.pose.position.y = 2.0;

    goals[0].target_pose.pose.orientation.w = 1.0;

 

    // 设置第二个目标点的坐标

    goals[1].target_pose.header.frame_id = "map";

    goals[1].target_pose.pose.position.x = 0.0;

    goals[1].target_pose.pose.position.y = 2.0;

    goals[1].target_pose.pose.orientation.w = 1.0;

 

    // 设置第三个目标点的坐标

    goals[2].target_pose.header.frame_id = "map";

    goals[2].target_pose.pose.position.x = 0.0;

    goals[2].target_pose.pose.position.y = 0.0;

    goals[2].target_pose.pose.orientation.w = 1.0;

 

    // 设置第四个目标点的坐标

    goals[3].target_pose.header.frame_id = "map";

    goals[3].target_pose.pose.position.x = 1.0;

    goals[3].target_pose.pose.position.y = 0.0;

    goals[3].target_pose.pose.orientation.w = 1.0;

    

 

    ROS_INFO("等待MoveBase服务器...");

    ac.waitForServer(ros::Duration(5));

 

    int current_goal = 0;

    while(ros::ok())

    {

        ROS_INFO("向目标 #%d 发送请求", current_goal+1);

        ac.sendGoal(goals[current_goal]);

 

        ac.waitForResult();

 

        actionlib::SimpleClientGoalState state = ac.getState();

        ROS_INFO("Goal state: %s", state.toString().c_str());

 

        if(state == actionlib::SimpleClientGoalState::SUCCEEDED)

            ROS_INFO("机器人已到达目标 #%d", current_goal+1);

        else

            ROS_INFO("机器人未能到达目标 #%d", current_goal+1);

 

        current_goal = (current_goal + 1) % 4;

 

        ros::Duration(5).sleep();

    }

 

    return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值