ros机器人的直行

#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 ;}

修改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})
  • 1
  • 2
  • 3
  • 4
  • 5

整个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})

启动机器人

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

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

   roslaunch aicroboxi_rviz view_mobile.launch
  • 1

ros机器人的直行_第1张图片



你可能感兴趣的:(ros操作)