系统版本:Ubuntu14.04,ROS indigo
机器人底盘:kobuki
摄像头:Asus Xtion
编译两个简单程序,一个是机器人直行,另一个是原地旋转。
catkin_create_pkg robot_move roscpp geometry_msgs tf
#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;
}
将speed.linear.x设置为0,speed.angular.z设置为0.4,其它不变。
在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})
在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也能识别。
roscore
roslaunch aicroboxi_bringup fake_aicroboxi.launch
roslaunch aicroboxi_bringup minimal.launch
roslaunch aicroboxi_rviz view_mobile.launch
rosrun robot_move goforward
rosrun robot_move goincircles
源代码:https://github.com/KeoChi/robot_move
个人学习笔记,欢迎交流学习。