底盘机械臂仿真fetch_gazebo实践
Ubuntu20.04 +ros1 noetic环境下编译
1.下载和编译
mkdir fetch_ws/src
cd fetch_ws/srcgit clone https://github.com/fetchrobotics/fetch_gazebo.git
git clone https://github.com/fetchrobotics/fetch_ros.git
git clone https://github.com/fetchrobotics/fetch_msgs.git
切换到gazebo11分支
cd fetch_gazebo
git checkout gazebo11
catkin_make
出现
Could not find a package configuration file provided by “robot_controllers”
解决
sudo apt-get install ros-noetic-robot-controllers
其他编译依赖库报错
cd ~/fetch_ws
rosdep install --from-paths src --ignore-src -y
2.运行fetch_gazebo
roslaunch fetch_gazebo playground.launch
3.启动键盘控制机器人移动
rosrun teleop_twist_keyboard teleop_twist_keyboard.py
发现底盘可以移动
4.控制机械臂移动
(1)启动 MoveIt!
# 启动 MoveIt! 核心节点(运动规划、碰撞检测等)
roslaunch fetch_moveit_config move_group.launch
(2)RViz 中的交互操作
# 终端2: 启动 RViz 可视化交互界面
roslaunch fetch_moveit_config moveit_rviz.launch
打开的 RViz 界面中:
a.添加 Display(若未自动加载):
点击左下角 Add → 添加 MotionPlanning。
确保 Global Options 中的 Fixed Frame 设置为 base_link。
b.控制机械臂:
交互式标记:拖动 RViz 中的黄色立方体(末端执行器)设定目标位姿。
c.规划与执行:
点击 Plan 按钮预览运动轨迹(灰色线)。
确认无误后点击 Execute 执行运动(机械臂将实际移动)。
拖动后如图
5.c++代码控制机械臂移动
(1)创建 C++ 包**
在 ROS 工作空间中创建一个新包:
cd ~/fetch_ws/src
catkin_create_pkg arm_control roscpp moveit_core moveit_ros_planning_interface
cd ~/fetch_ws && catkin_make
(2)C++ 代码示例 :控制机械臂到目标位姿**
创建文件 src/arm_control.cpp
:
#include <ros/ros.h>
#include <moveit/move_group_interface/move_group_interface.h>
#include <moveit/planning_scene_interface/planning_scene_interface.h>int main(int argc, char** argv) {ros::init(argc, argv, "fetch_arm_control");ros::NodeHandle nh;ros::AsyncSpinner spinner(1);spinner.start();// 初始化 MoveIt! 接口moveit::planning_interface::MoveGroupInterface arm("arm");arm.setPlannerId("RRTConnectkConfigDefault"); // 设置运动规划器// 设置目标位姿 (相对于 base_link)geometry_msgs::Pose target_pose;target_pose.position.x = 0.5;target_pose.position.y = 0.2;target_pose.position.z = 0.8;target_pose.orientation.w = 1.0; // 四元数 (w=1表示无旋转)arm.setPoseTarget(target_pose);// 运动规划moveit::planning_interface::MoveGroupInterface::Plan plan;if (arm.plan(plan)) {ROS_INFO("Planning succeeded, executing...");arm.execute(plan);} else {ROS_ERROR("Planning failed!");}ros::shutdown();return 0;
}
(3)编译配置
修改 CMakeLists.txt
在 arm_control/CMakeLists.txt
中添加:
find_package(catkin REQUIRED COMPONENTSroscppmoveit_coremoveit_ros_planning_interface
)add_executable(arm_control src/arm_control.cpp)
target_link_libraries(arm_control${catkin_LIBRARIES}
)
编译运行
cd ~/fetch_ws && catkin_make
source devel/setup.bash
rosrun arm_control arm_control
发现机械臂可以运动
6.操作演示
底盘机械臂仿真fetch rviz
底盘机械臂仿真fetch
底盘机械臂仿真fetch gazebo
底盘机械臂仿真fetch