文章目录
- 一、URDF
- 二、创建小车模型
- 1.创建功能包
- 2.导入依赖
- 3.创建urdf,launch文件:
- 4.可视化
- 三、添加雷达
- 1.xacro文件
- 2.集成和修改launch
- 3.添加摄像头和雷达
- 三.GAZEBO仿真
- 四、orbslam2+kitti
- 1.下载
- 2.安装编译ORB_SLAM2
- 3.运行Kitee数据集
一、URDF
URDF,即统一机器人描述格式(Unified Robot Description Format),是一种用于描述机器人模型的XML文件格式。URDF文件包含了机器人的物理和关节属性,使得机器人可以在仿真环境中被准确地表示和模拟。URDF是ROS(机器人操作系统)中广泛使用的一种格式,但它也可以独立于ROS使用。
URDF文件的主要组成部分包括:
- 机器人(Robot):定义了机器人的根元素,包含了机器人的所有链接(links)和关节(joints)。
- 链接(Link):代表机器人的一个物理部分,可以是机器人的手臂、腿部或任何其他部分。每个链接都有其几何形状、质量、尺寸和视觉/碰撞属性。
- 关节(Joint):定义了链接之间的连接方式,可以是旋转关节(revolute)、滑动关节(prismatic)或其他类型。关节定义了链接之间的相对运动。
- 材料(Material):定义了链接的视觉属性,如颜色、纹理等。
- 传感器(Sensor):可以附加到链接上,用于在仿真中模拟传感器的功能。
- 插件(Plugin):允许URDF文件包含自定义的脚本或行为。
URDF文件使得开发者能够在不同的仿真环境中重用机器人模型,同时也方便了机器人设计和仿真的交流。URDF文件可以被转换成其他格式,以适应不同的仿真软件或机器人控制系统。
二、创建小车模型
1.创建功能包
mkdir -p catkin_ws/src
cd catkin_ws/src
catkin_init_workspace
2.导入依赖
catkin_create_pkg jubot_demo urdf xacro
cd jubot_demo/
mkdir urdf
mkdir launch
mkdir meshes
mkdir config
3.创建urdf,launch文件:
<launch><!-- 将 urdf 文件内容设置进参数服务器 --><param name="robot_description" textfile="$(find jubot_demo)/urdf/box_urdf.urdf" /><!-- 启动 rviz --><!-- <node pkg="rviz" type="rviz" name="rviz" /> --><node pkg="rviz" type="rviz" name="rviz" args="-d $(find jubot_demo)/config/rviz/show_four_wheel_car.rviz" /><!-- 启动机器人状态和关节状态发布节点 --><node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher" /><node pkg="joint_state_publisher" type="joint_state_publisher" name="joint_state_publisher" /><!-- 启动图形化的控制关节运动节点 --><node pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" name="joint_state_publisher_gui" /></launch>
urdf:
<!-- <robot name="mycar"><link name="base_link"><visual><geometry><box size="0.5 0.2 0.1" /></geometry></visual></link>
</robot> --><robot name="mycar"><!-- 设置 base_footprint --><link name="base_footprint"><visual><geometry><sphere radius="0.001" /></geometry></visual></link><!-- 添加底盘 --><!-- 参数形状:圆柱 半径:10 cm 高度:8 cm 离地:1.5 cm--><link name="base_link"><visual><geometry><cylinder radius="0.1" length="0.08" /></geometry><origin xyz="0 0 0" rpy="0 0 0" /><material name="yellow"><color rgba="0.8 0.3 0.1 0.5" /></material></visual></link><joint name="base_link2base_footprint" type="fixed"><parent link="base_footprint" /><child link="base_link"/><origin xyz="0 0 0.055" /></joint><!-- 添加驱动轮 --><!-- 添加驱动轮 --><!--驱动轮是侧翻的圆柱参数半径: 3.25 cm宽度: 1.5 cm颜色: 黑色关节设置:x = 0y = 底盘的半径 + 轮胎宽度 / 2z = 离地间距 + 底盘长度 / 2 - 轮胎半径 = 1.5 + 4 - 3.25 = 2.25(cm)axis = 0 1 0--><link name="left_wheel"><visual><geometry><cylinder radius="0.0325" length="0.015" /></geometry><origin xyz="0 0 0" rpy="1.5705 0 0" /><material name="black"><color rgba="0.0 0.0 0.0 1.0" /></material></visual></link><joint name="left_wheel2base_link" type="continuous"><parent link="base_link" /><child link="left_wheel" /><origin xyz="0 0.1 -0.0225" /><axis xyz="0 1 0" /></joint><link name="right_wheel"><visual><geometry><cylinder radius="0.0325" length="0.015" /></geometry><origin xyz="0 0 0" rpy="1.5705 0 0" /><material name="black"><color rgba="0.0 0.0 0.0 1.0" /></material></visual></link><joint name="right_wheel2base_link" type="continuous"><parent link="base_link" /><child link="right_wheel" /><origin xyz="0 -0.1 -0.0225" /><axis xyz="0 1 0" /></joint><!-- 添加万向轮(支撑轮) --><!-- 添加万向轮(支撑轮) --><!--参数形状: 球体半径: 0.75 cm颜色: 黑色关节设置:x = 自定义(底盘半径 - 万向轮半径) = 0.1 - 0.0075 = 0.0925(cm)y = 0z = 底盘长度 / 2 + 离地间距 / 2 = 0.08 / 2 + 0.015 / 2 = 0.0475 axis= 1 1 1--><link name="front_wheel"><visual><geometry><sphere radius="0.0075" /></geometry><origin xyz="0 0 0" rpy="0 0 0" /><material name="black"><color rgba="0.0 0.0 0.0 1.0" /></material></visual></link><joint name="front_wheel2base_link" type="continuous"><parent link="base_link" /><child link="front_wheel" /><origin xyz="0.0925 0 -0.0475" /><axis xyz="1 1 1" /></joint><link name="back_wheel"><visual><geometry><sphere radius="0.0075" /></geometry><origin xyz="0 0 0" rpy="0 0 0" /><material name="black"><color rgba="0.0 0.0 0.0 1.0" /></material></visual></link><joint name="back_wheel2base_link" type="continuous"><parent link="base_link" /><child link="back_wheel" /><origin xyz="-0.0925 0 -0.0475" /><axis xyz="1 1 1" /></joint>
</robot>
4.可视化
先编译配置launch文件,退到主目录catkin_ws下,执行catkin_make。
成功之后,再执行 catkin_make install。
然后在主目录catkin_make下配置环境变量source devel/setup.bash。
检测环境变量是否配置成功echo $ROS_PACKAGE_PATH。
再启动launch文件,完成可视化的建立:
创建启动文件:
cd ~/catkin_ws/src/mbot_description/launch
sudo gedit display_mbot_base_urdf.launch
在打开的文件中写入
<launch><!-- 加载的参数名字叫robot_description,具体内容是urdf相关模型的路径 --><param name="robot_description" textfile="$(find mbot_description)/urdf/mbot_base.urdf" /><!-- 设置GUI参数,显示关节控制插件 --><param name="use_gui" value="true"/><!-- 运行joint_state_publisher节点,发布机器人的关节状态 --><node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" /><!-- 运行robot_state_publisher节点,发布tf --><node name="robot_state_publisher" pkg="robot_state_publisher" type="state_publisher" /><!-- 运行rviz可视化界面,保存每次打开之后的相关插件,保存到config文件夹下面 --><node name="rviz" pkg="rviz" type="rviz" args="-d $(find mbot_description)/config/mbot_urdf.rviz" required="true" />
</launch>
建立可视化
roslaunch jubot_demo box_launch.launch
得到模型:
三、添加雷达
1.xacro文件
在urdf文件夹下建一个xacro文件夹,在终端打开。
在文件夹中新建my_base.urdf.xacro文件,编写xacro文件:
vim my_base.urdf.xacro
在打开的文件中写入
<robot name="my_base" xmlns:xacro="http://www.ros.org/wiki/xacro"><xacro:property name="PI" value="3.141"/><material name="black"><color rgba="0.0 0.0 0.0 1.0" /></material><xacro:property name="base_footprint_radius" value="0.001" /> <xacro:property name="base_link_radius" value="0.1" /> <xacro:property name="base_link_length" value="0.08" /> <xacro:property name="earth_space" value="0.015" /> <link name="base_footprint"><visual><geometry><sphere radius="${base_footprint_radius}" /></geometry></visual></link><link name="base_link"><visual><geometry><cylinder radius="${base_link_radius}" length="${base_link_length}" /></geometry><origin xyz="0 0 0" rpy="0 0 0" /><material name="yellow"><color rgba="0.5 0.3 0.0 0.5" /></material></visual></link><joint name="base_link2base_footprint" type="fixed"><parent link="base_footprint" /><child link="base_link" /><origin xyz="0 0 ${earth_space + base_link_length / 2 }" /></joint><xacro:property name="wheel_radius" value="0.0325" /><xacro:property name="wheel_length" value="0.015" /><xacro:macro name="add_wheels" params="name flag"><link name="${name}_wheel"><visual><geometry><cylinder radius="${wheel_radius}" length="${wheel_length}" /></geometry><origin xyz="0.0 0.0 0.0" rpy="${PI / 2} 0.0 0.0" /><material name="black" /></visual></link><joint name="${name}_wheel2base_link" type="continuous"><parent link="base_link" /><child link="${name}_wheel" /><origin xyz="0 ${flag * base_link_radius} ${-(earth_space + base_link_length / 2 - wheel_radius) }" /><axis xyz="0 1 0" /></joint></xacro:macro><xacro:add_wheels name="left" flag="1" /><xacro:add_wheels name="right" flag="-1" /><xacro:property name="support_wheel_radius" value="0.0075" /> <!-- 支撑轮半径 --><xacro:macro name="add_support_wheel" params="name flag" ><link name="${name}_wheel"><visual><geometry><sphere radius="${support_wheel_radius}" /></geometry><origin xyz="0 0 0" rpy="0 0 0" /><material name="black" /></visual></link><joint name="${name}_wheel2base_link" type="continuous"><parent link="base_link" /><child link="${name}_wheel" /><origin xyz="${flag * (base_link_radius - support_wheel_radius)} 0 ${-(base_link_length / 2 + earth_space / 2)}" /><axis xyz="1 1 1" /></joint></xacro:macro><xacro:add_support_wheel name="front" flag="1" /><xacro:add_support_wheel name="back" flag="-1" /></robot>
2.集成和修改launch
加载robot_description时使用command属性,属性值就是调用 xacro 功能包的 xacro 程序直接解析 xacro 文件,然后启动launch文件
box_launch
<launch><!-- <param name="robot_description" textfile="$(find jubot_demo)/urdf/box_urdf.urdf" /> --><param name="robot_description" command="$(find xacro)/xacro $(find jubot_demo)/urdf/xacro/my_base.urdf.xacro" /><!-- <node pkg="rviz" type="rviz" name="rviz" /> --><node pkg="rviz" type="rviz" name="rviz" args="-d $(find jubot_demo)/config/rviz/show_four_wheel_car.rviz" /><node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher" /><node pkg="joint_state_publisher" type="joint_state_publisher" name="joint_state_publisher" /><node pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" name="joint_state_publisher_gui" /></launch>
将launch修改为
<launch><!-- <param name="robot_description" command="$(find xacro)/xacro $(find jubot_demo)/urdf/xacro/my_base.urdf.xacro" /> --><param name="robot_description" command="$(find xacro)/xacro $(find jubot_demo)/urdf/xacro/my_car_camera.urdf.xacro" /><node pkg="rviz" type="rviz" name="rviz" args="-d $(find jubot_demo)/config/rviz/show_four_wheel_car.rviz" /><node pkg="robot_state_publisher" type="robot_state_publisher" name="robot_state_publisher" /><node pkg="joint_state_publisher" type="joint_state_publisher" name="joint_state_publisher" /><node pkg="joint_state_publisher_gui" type="joint_state_publisher_gui" name="joint_state_publisher_gui" /></launch>
3.添加摄像头和雷达
在xacro文件夹中新建my_camera.urdf.xacro
<robot name="my_camera" xmlns:xacro="http://wiki.ros.org/xacro"><!-- 摄像头属性 --><xacro:property name="camera_length" value="0.01" /> <!-- 摄像头长度(x) --><xacro:property name="camera_width" value="0.025" /> <!-- 摄像头宽度(y) --><xacro:property name="camera_height" value="0.025" /> <!-- 摄像头高度(z) --><xacro:property name="camera_x" value="0.08" /> <!-- 摄像头安装的x坐标 --><xacro:property name="camera_y" value="0.0" /> <!-- 摄像头安装的y坐标 --><xacro:property name="camera_z" value="${base_link_length / 2 + camera_height / 2}" /> <link name="camera"><visual><geometry><box size="${camera_length} ${camera_width} ${camera_height}" /></geometry><origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" /><material name="black" /></visual></link><joint name="camera2base_link" type="fixed"><parent link="base_link" /><child link="camera" /><origin xyz="${camera_x} ${camera_y} ${camera_z}" /></joint>
</robot>
my_car_camera.urdf.xacro
<!-- 组合小车底盘与摄像头与雷达 -->
<robot name="my_car_camera" xmlns:xacro="http://wiki.ros.org/xacro"><xacro:include filename="my_base.urdf.xacro" /><xacro:include filename="my_camera.urdf.xacro" /><xacro:include filename="my_laser.urdf.xacro" />
</robot>
my_laser.urdf.xacro文件
<robot name="my_camera" xmlns:xacro="http://wiki.ros.org/xacro"><xacro:property name="camera_length" value="0.01" /> <xacro:property name="camera_width" value="0.025" /> <xacro:property name="camera_height" value="0.025" /> <xacro:property name="camera_x" value="0.08" /> <xacro:property name="camera_y" value="0.0" /> <xacro:property name="camera_z" value="${base_link_length / 2 + camera_height / 2}" /> <link name="camera"><visual><geometry><box size="${camera_length} ${camera_width} ${camera_height}" /></geometry><origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" /><material name="black" /></visual></link><joint name="camera2base_link" type="fixed"><parent link="base_link" /><child link="camera" /><origin xyz="${camera_x} ${camera_y} ${camera_z}" /></joint>
</robot>
三.GAZEBO仿真
修改碰撞属性和惯性矩阵,导入到gazebo中。
my_base.urdf.xacro:
<robot name="base" xmlns:xacro="http://wiki.ros.org/xacro"><xacro:macro name="sphere_inertial_matrix" params="m r"><inertial><mass value="${m}" /><inertia ixx="${2*m*r*r/5}" ixy="0" ixz="0"iyy="${2*m*r*r/5}" iyz="0" izz="${2*m*r*r/5}" /></inertial></xacro:macro><xacro:macro name="cylinder_inertial_matrix" params="m r h"><inertial><mass value="${m}" /><inertia ixx="${m*(3*r*r+h*h)/12}" ixy = "0" ixz = "0"iyy="${m*(3*r*r+h*h)/12}" iyz = "0"izz="${m*r*r/2}" /> </inertial></xacro:macro><xacro:macro name="Box_inertial_matrix" params="m l w h"><inertial><mass value="${m}" /><inertia ixx="${m*(h*h + l*l)/12}" ixy = "0" ixz = "0"iyy="${m*(w*w + l*l)/12}" iyz= "0"izz="${m*(w*w + h*h)/12}" /></inertial></xacro:macro>
</robot>
my_camera.urdf.xacro:
<robot name="my_camera" xmlns:xacro="http://wiki.ros.org/xacro"><xacro:property name="camera_length" value="0.01" /> <xacro:property name="camera_width" value="0.025" /> <xacro:property name="camera_height" value="0.025" /> <xacro:property name="camera_x" value="0.08" /> <xacro:property name="camera_y" value="0.0" /> <xacro:property name="camera_z" value="${base_link_length / 2 + camera_height / 2}" /> <xacro:property name="camera_m" value="0.01" /> <link name="camera"><visual><geometry><box size="${camera_length} ${camera_width} ${camera_height}" /></geometry><origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" /><material name="black" /></visual><collision><geometry><box size="${camera_length} ${camera_width} ${camera_height}" /></geometry><origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" /></collision><xacro:Box_inertial_matrix m="${camera_m}" l="${camera_length}" w="${camera_width}" h="${camera_height}" /></link><joint name="camera2base_link" type="fixed"><parent link="base_link" /><child link="camera" /><origin xyz="${camera_x} ${camera_y} ${camera_z}" /></joint><gazebo reference="camera"><material>Gazebo/Blue</material></gazebo>
</robot>
my_car_camera.urdf.xacro:
<robot name="my_laser" xmlns:xacro="http://wiki.ros.org/xacro"><xacro:property name="support_length" value="0.15" /> <xacro:property name="support_radius" value="0.01" /> <xacro:property name="support_x" value="0.0" /> <xacro:property name="support_y" value="0.0" /> <xacro:property name="support_z" value="${base_link_length / 2 + support_length / 2}" /> <xacro:property name="support_m" value="0.02" /> <link name="support"><visual><geometry><cylinder radius="${support_radius}" length="${support_length}" /></geometry><origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" /><material name="red"><color rgba="0.8 0.2 0.0 0.8" /></material></visual><collision><geometry><cylinder radius="${support_radius}" length="${support_length}" /></geometry><origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" /></collision><xacro:cylinder_inertial_matrix m="${support_m}" r="${support_radius}" h="${support_length}" /></link><joint name="support2base_link" type="fixed"><parent link="base_link" /><child link="support" /><origin xyz="${support_x} ${support_y} ${support_z}" /></joint><gazebo reference="support"><material>Gazebo/White</material></gazebo><xacro:property name="laser_length" value="0.05" /> <xacro:property name="laser_radius" value="0.03" /> <xacro:property name="laser_x" value="0.0" /> <xacro:property name="laser_y" value="0.0" /> <xacro:property name="laser_z" value="${support_length / 2 + laser_length / 2}" /> <xacro:property name="laser_m" value="0.1" /> <link name="laser"><visual><geometry><cylinder radius="${laser_radius}" length="${laser_length}" /></geometry><origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" /><material name="black" /></visual><collision><geometry><cylinder radius="${laser_radius}" length="${laser_length}" /></geometry><origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" /></collision><xacro:cylinder_inertial_matrix m="${laser_m}" r="${laser_radius}" h="${laser_length}" /></link><joint name="laser2support" type="fixed"><parent link="support" /><child link="laser" /><origin xyz="${laser_x} ${laser_y} ${laser_z}" /></joint><gazebo reference="laser"><material>Gazebo/Black</material></gazebo>
</robot>
my_laser.urdf.xacro:
<robot name="my_car_camera" xmlns:xacro="http://wiki.ros.org/xacro"><xacro:include filename="gazebo_head.xacro" /><xacro:include filename="gazebo_car.xacro" /><xacro:include filename="gazebo_laser.xacro" /><xacro:include filename="gazebo_camera.xacro" />
</robot>
在终端进入launch目录
vim gazebo_car.launch
在打开的文件中写入:
<launch><param name="robot_description" command="$(find xacro)/xacro $(find urdf_gazebo)/urdf/xacro/gazebo_car_union.xacro" /><include file="$(find gazebo_ros)/launch/empty_world.launch" /><node pkg="gazebo_ros" type="spawn_model" name="model" args="-urdf -model mycar -param robot_description" />
</launch>
四、orbslam2+kitti
1.下载
-
下载依赖:https://gitcode.com/raulmur/ORB_SLAM2/overview?tab=readme-ov-file&utm_source=csdn_github_accelerator&isLogin=1
-
下载Pangolin
cd Pangolin
mkdir build && cd build
cmake ..
sudo make -j8
- 下载Ceres
sudo apt-get install liblapack-dev libsuitesparse-dev libcxsparse3 libgflags-dev libgoogle-glog-dev libgtest-dev
cd ceres-solver
mkdir build && cd build
cmake ..
sudo make -j8
sudo make install
- 下载g2o
sudo apt-get install qt5-qmake qt5-default libqglviewer-dev-qt5 libsuitesparse-dev libcxsparse3 libcholmod3
cd g2o
mkdir build && cd build
cmake ..
sudo make -j8
sudo make install
- 下载DBoW3
mkdir build
cd build/
cmake ..
make
sudo make install
2.安装编译ORB_SLAM2
(1)下载ORB_SLAM2的安装包
sudo apt-get install ORB_SLAM2
(2)配置环境
cd ~/catkin_ws/src/ORB_SLAM2
sudo chmod +x build.sh
./build.sh
(3)使用ROS编译
sudo chmod +x build_ros.sh
./build_ros.sh
3.运行Kitee数据集
-
下载数据集:https://www.cvlibs.net/datasets/kitti/eval_odometry.php
-
运行ORB_SLAM2