ROS第一个程序:机器人直行和旋转

系统版本:Ubuntu14.04,ROS indigo
机器人底盘:kobuki
摄像头:Asus Xtion

一、功能介绍

  编译两个简单程序,一个是机器人直行,另一个是原地旋转。

二、实验步骤

1.创建程序包

  catkin_create_pkg robot_move roscpp geometry_msgs tf

2.在robot_move/src/里创建goforward.cpp,并粘贴如下代码:

#include <ros/ros.h>
#include <signal.h>
#include <geometry_msgs/Twist.h>

ros::Publisher cmdVelPub;

void shutdown(int sig)
{
  cmdVelPub.publish(geometry_msgs::Twist());//使机器人停止运动
  ROS_INFO("goforward cpp ended!");
  ros::shutdown();
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "GoForward");//初始化ROS,它允许ROS通过命令行进行名称重映射
  std::string topic = "/cmd_vel";
  ros::NodeHandle node;//为这个进程的节点创建一个句柄
  cmdVelPub = node.advertise<geometry_msgs::Twist>(topic, 1);//告诉master将要在/cmd_vel topic上发布一个geometry_msgs/Twist的消息
  ros::Rate loopRate(10);//The desired rate to run at in Hz,ros::Rate对象可以允许你指定自循环的频率
  // Override the default ros sigint handler. This must be set after the first NodeHandle is creat
  signal(SIGINT, shutdown);
  ROS_INFO("goforward cpp start...");
  geometry_msgs::Twist speed; // 控制信号载体 Twist message
  while (ros::ok())
  {
    speed.linear.x = 0.1; // 设置线速度为0.1m/s,正为前进,负为后退
    speed.angular.z = 0; // 设置角速度为0rad/s,正为左转,负为右转
    cmdVelPub.publish(speed); // 将刚才设置的指令发送给机器人
    loopRate.sleep();//休眠直到一个频率周期的时间
  }

  return 0;
}


3.在robot_move/src/里创建goincircles.cpp,将goforward.cpp的源代码复制到goincircles.cpp

将speed.linear.x设置为0,speed.angular.z设置为0.4,其它不变。

4.修改robot_move目录下的CMakeLists.txt

在CMakeLists.txt文件末尾加入几条语句:

add_executable(goforward src/goforward.cpp)
target_link_libraries(goforward ${catkin_LIBRARIES})

add_executable(goincircles src/goincircles.cpp)
target_link_libraries(goincircles ${catkin_LIBRARIES})

整个CMakeLists.txt文件如下:

cmake_minimum_required(VERSION 2.8.3)
project(robot_move)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  geometry_msgs
  tf
  roscpp
)

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES robot_move
  CATKIN_DEPENDS geometry_msgs roscpp
#  DEPENDS system_lib
)

###########
## Build ##
###########

## Specify additional locations of header files
## Your package locations should be listed before other locations
# include_directories(include)
include_directories(
  ${catkin_INCLUDE_DIRS}
)

add_executable(goforward src/goforward.cpp)
target_link_libraries(goforward ${catkin_LIBRARIES})

add_executable(goincircles src/goincircles.cpp)
target_link_libraries(goincircles ${catkin_LIBRARIES})

5.编译程序

在catkin_ws目录下,执行一下命令编译:

catkin_make –force-cmake -G”Eclipse CDT4 - Unix Makefiles” -DCMAKE_BUILD_TYPE=Debug -DCMAKE_ECLIPSE_MAKE_ARGUMENTS=-j8

得到goforward和goincircles两个执行程序。
为何不直接执行catkin_make编译?因为执行命令也可以让Eclipse识别该工程,方便在Eclipse编写调试代码。执行过一次,以后只需catkin_make编译,Eclipse也能识别。

6.测试程序

6.1 启动roscore

   roscore

6.2 启动机器人

6.2.1若是运行仿真机器人
   roslaunch aicroboxi_bringup fake_aicroboxi.launch
6.2.2若是运行真实的机器人平台
   roslaunch aicroboxi_bringup minimal.launch

6.3 启动 rviz 图形化显示程序,如图1

   roslaunch aicroboxi_rviz view_mobile.launch

RVIZ1

图1

6.4 启动goforward程序,如图2

   rosrun robot_move goforward

RVIZ2

图2

6.5 Ctrl+C结束goforward程序,再测试goincircles程序,如图3

   rosrun robot_move goincircles

RVIZ3

图3

源代码:https://github.com/KeoChi/robot_move
个人学习笔记,欢迎交流学习。

猜你喜欢

转载自blog.csdn.net/haorenhaoren123/article/details/51438666