How to setup a navigation goal for a mobile robot

asked 2022-02-16 16:31:26 -0500

sincoy gravatar image

Hello ROS developers,

I would like to move my robot via terminal command 1 meter forwards and backwards. Afterwards I would like to rotate my robot in 360 degrees.

You can find my cpp code above:

#include "ros/ros.h"
#include <move_base/move_base.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, "nav_goal");
  if (argc != 4 )
  {
    ROS_INFO("rosrun navigation nav_goal pos_x pos_y orien_z");
    return 1;
  } 

  //tell the action client that we want to spin a thread by default
  MoveBaseClient ac("move_base", true);
  //wait for the action server to come up
  while(!ac.waitForServer(ros::Duration(5.0))){
    ROS_INFO("Waiting for the move_base action server to come up");
  }
        move_base_msgs::MoveBaseGoal goal;

  //we'll send a goal to the robot to move 1 meter forward
        goal.target_pose.header.frame_id = "map";
        goal.target_pose.header.stamp = ros::Time::now();
        goal.target_pose.pose.position.x = atoll(argv[1]);
        goal.target_pose.pose.position.y = atoll(argv[2]);
        goal.target_pose.pose.position.z = 0.0;
        goal.target_pose.pose.orientation.x=0.0;
        goal.target_pose.pose.orientation.y=0.0;
        goal.target_pose.pose.orientation.z = atoll(argv[3]);
        goal.target_pose.pose.orientation.w = 1.0;

    ROS_INFO("Sending goal");
    ac.sendGoal(goal);
    ac.waitForResult();

    if(ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
    ROS_INFO("Hooray, the base moved successfully");
    else
    ROS_INFO("The base failed to move forward 1 meter for some reason");

  return 0;
}

I would really appreciate your help! Thank you very much!

edit retag flag offensive close merge delete