打开终端,输入以下命令,启动建图:
cd ~/robot_shell
./4-gmapping.sh
新建一个命令行终端,输入rviz,打开rviz,修改 Fixed Frame 为 map ,并订阅地图topic,topic为/map:
可以切换到脚本最后一个终端,用键盘控制机器人运动,补全地图。
新建一个终端,输入以下命令即可保存地图:
cd ~/robot_shell
./5-save_map.sh
这样我们就保存了刚才建立的地图。
首先,确保你的卓尔-ARM放置在建图起点,然后打开终端,输入以下命令,启动自主导航:
cd ~/robot_shell
./6-navigation.sh
然后打开新的终端输入rviz,在rviz中,选择OpenConfig
在弹框中,选择下面的文件:
即可加载导航相关配置:
在上方2D Nav Goal按钮选择相应的按键即可实现自主导航。
自主导航可通过movebase发送路点实现,代码如下:
#include <ros/ros.h>
#include <move_base_msgs/MoveBaseAction.h>
#include <actionlib/client/simple_action_client.h>
#include <iostream>
using namespace std;
typedef actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction> MoveBaseClient;
int main(int argc, char **argv)
{
ros::init(argc, argv, "send_goals_node");
MoveBaseClient ac("move_base", true);
ac.waitForServer();
move_base_msgs::MoveBaseGoal goal1;
move_base_msgs::MoveBaseGoal goal2;
move_base_msgs::MoveBaseGoal goal3;
//待发送的 目标点 在 map 坐标系下的坐标位置
goal1.target_pose.pose.position.x = 1.798;
goal1.target_pose.pose.position.y = -0.012;
goal1.target_pose.pose.orientation.z = 0.006;
goal1.target_pose.pose.orientation.w = 1.0;
goal1.target_pose.header.frame_id = "map";
goal1.target_pose.header.stamp = ros::Time::now();
goal2.target_pose.pose.position.x = 2.398;
goal2.target_pose.pose.position.y = -0.012;
goal2.target_pose.pose.orientation.z = 0.006;
goal2.target_pose.pose.orientation.w = 1.0;
goal2.target_pose.header.frame_id = "map";
goal2.target_pose.header.stamp = ros::Time::now();
goal3.target_pose.pose.position.x = 0;
goal3.target_pose.pose.position.y = 0;
goal3.target_pose.pose.orientation.z = 0;
goal3.target_pose.pose.orientation.w = 1;
goal3.target_pose.header.frame_id = "map";
goal3.target_pose.header.stamp = ros::Time::now();
ac.sendGoal(goal1);
ROS_INFO("Send Goal 1 !!!");
ac.waitForResult();
if (ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)//如果到达目标点,就执行括号中的任务
{
ROS_INFO("OK1!!!!!");
}
else
{
ROS_WARN("The Goal Planning Failed for some reason");
}
ac.sendGoal(goal2);
ROS_INFO("Send Goal 2 !!!");
ac.waitForResult();
if (ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
{
ROS_INFO("OK2!!!!!");
}
else
{
ROS_WARN("The Goal Planning Failed for some reason");
}
ac.sendGoal(goal3);
ROS_INFO("Send Goal 3 !!!");
ac.waitForResult();
if (ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED)
{
ROS_INFO("OK3!!!!!");
}
else
{
ROS_WARN("The Goal Planning Failed for some reason");
}
return 0;
}
在上述代码中,我们通过movebase发送了三个路点,分别是(1.798, -0.012),(2.398, -0.012),(0, 0)。机器人会依次到达这三个路点。