1.启动gazebo并加载空地图
roslaunch mbot_gazebo nav_gmapping_view_mbot_gazebolaserandcamera_room.launch
2.命令行发送cmd_vel指令
rostopic list 查看话题类型
rostopic pub -r 10 /cmd_vel 按两下 Tab键,命令即补全
rostopic pub -r 10 /cmd_vel geometry_msgs/Twist "linear:x: 0.0y: 0.0z: 0.0
angular:x: 0.0y: 0.0z: 0.0"
先让机器人转圈,输入这个命令
rostopic pub -r 10 /cmd_vel geometry_msgs/Twist "linear:x: 2.0y: 0.0z: 0.0
angular:x: 0.0y: 0.0z:-0.5"
让机器人直行,输入
rostopic pub -r 10 /cmd_vel geometry_msgs/Twist "linear:x: 2.0y: 0.0z: 0.0
angular:x: 0.0y: 0.0z: 0.0"
4./cmd_vel geometry_msgs/Twist使用示例
新建一个节点不断发送/cmd_vel控制机器人运动的节点
mkdir -p cmdveltest/src
cd cmdveltest/src
catkin_create_pkg cmdveltest geometry_msgs
mkdir src
cd src
touch cmdveltest.cpp
cd cmdveltest/src
catkin_make
在CMakeLists.txt
加入
add_executable(cmdveltest src/cmdveltest.cpp)
target_link_libraries(cmdveltest ${catkin_LIBRARIES})
在src/cmdveltest.cpp内如如下
#include <iostream>
#include <geometry_msgs/Twist.h>
#include <ros/ros.h>
#include <ros/console.h>
#include<unistd.h>
using namespace std;
int main(int argc,char** argv)
{ros::init(argc, argv, "cmdveltest");ros::NodeHandle cmdh;ros::Publisher cmdpub= cmdh.advertise<geometry_msgs::Twist>("cmd_vel", 1, true);ros::Rate r(60);while(1){geometry_msgs::Twist twist;geometry_msgs::Vector3 linear;linear.x=0.1;linear.y=0;linear.z=0;geometry_msgs::Vector3 angular;angular.x=0;angular.y=0;//直行//angular.z=0;//转圈angular.z=-0.5;twist.linear=linear;twist.angular=angular;cmdpub.publish(twist);cout<<"hello"<<endl;// ros::spinOnce();//r.sleep();sleep(1);}return 0;
}
编译后启动 rosrun cmdveltest cmdveltest 发现机器人跑的飞起
启动命令过程
1.启动主程序
nvidia@tegra-ubuntu:~/catkin_ws/src/mbot_gazebo/launch$ roslaunch mbot_gazebo nav_gmapping_view_mbot_gazebolaserandcamera_room.launch
2.启动rqt_graph查看节点
nvidia@tegra-ubuntu:~/catkin_ws/src/cmdveltest$ rqt_graph
3.查看话题类型
nvidia@tegra-ubuntu:~/catkin_ws/src/cmdveltest$ rostopic list
4.查看cmd_vel命令格式
nvidia@tegra-ubuntu:~/catkin_ws/src/mbot_gazebo/launch$ rostopic pub -r 10 /cmd_vel
5.启动cmd_vel程序
rosrun cmdveltest cmdveltest
可以看到在rviz和gazebo软件中机器人在转圈