目录
环境配置与流程
1、重新建立文件夹,初始化工作空间
$ mkdir scout_ws && cd scout_ws
$ mkdir src .devcontainer
$ cd ../.devcontainer
2、在 .devcontainer 中新建 devcontainer.json 和 Dockerfile 两个文件:
$ gedit devcontainer.json # 也可以自行更换其他的编辑器
3、在 devcontainer.json 保存如下内容:
{
"dockerFile": "Dockerfile",
"build": {
"args": {
"WORKSPACE": "${containerWorkspaceFolder}"
}
},
"remoteUser": "ros",
"runArgs": [
"--network=host",
"--cap-add=SYS_PTRACE",
"--security-opt=seccomp:unconfined",
"--security-opt=apparmor:unconfined",
"--volume=/tmp/.X11-unix:/tmp/.X11-unix"
],
"containerEnv": { "DISPLAY": "${localEnv:DISPLAY}" },
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.profiles.linux": {
"bash": {
"path": "bash"
},
},
"terminal.integrated.defaultProfile.linux": "bash"
},
"extensions": [
"dotjoshjohnson.xml",
"zachflower.uncrustify",
"ms-azuretools.vscode-docker",
"ms-iot.vscode-ros",
"ms-python.python",
"ms-vscode.cpptools",
"redhat.vscode-yaml",
"smilerobotics.urdf",
"streetsidesoftware.code-spell-checker",
"twxs.cmake",
"yzhang.markdown-all-in-one"
]
}
4、新建dockerfile
$ gedit Dockerfile
5、加入内容
FROM althack/ros:melodic-gazebo
# Set up auto-source of workspace for ros user
ARG WORKSPACE
RUN echo "if [ -f ${WORKSPACE}/install/setup.bash ]; then source ${WORKSPACE}/install/setup.bash; fi" >> /home/ros/.bashrc
6、使用 VSC 在 docker 中打开文件夹
$ cd scout_ws
$ code .
7、更新 apt 和 rosdep database
$ sudo apt update
$ rosdep update
8、文件放置
将提供地址里找到文件并放置
地址:https://github.com/agilexrobotics/ugv_gazebo_sim/tree/master/scout
文件:
scout_control 、scout_description 、scout_gazebo_sim
三个文件放到 src目录之中。
9、安装ROS库
$ sudo apt-get -y install --no-install-recommends \
ros-melodic-ros-control \
ros-melodic-ros-controllers \
ros-melodic-gazebo-ros \
ros-melodic-gazebo-ros-control \
ros-melodic-joint-state-publisher-gui \
ros-melodic-teleop-twist-keyboard
10、初始化工作空间并安装依赖:
$ cd scout_ws/src
$ catkin_init_workspace
$ cd ..
$ rosdep install --from-paths src --ignore-src -r -y
11、编译与运行
$ catkin_make
$ source devel/setup.bash
12、车型展示
$ roslaunch scout_description display_mini_models.launch # 在 Rviz 中查看 scout mini 车型
这里scout_description 包主要为 scout 系列车型仿真模型,display_xxx_.launch 为不同车型在 Rviz 中的展示
13、查看车型
$ roslaunch scout_gazebo_sim scout_mini_empty_world.launch # 在 gazebo 中使用空地图仿真 scout mini
scout_gazebo_sim 包为 scout 系列车型在 gazebo 中的仿真,scout_xxx_.launch 为不同车型在 gazebo 中仿真展示
14、使用 xacro 为小车加上一个简易摄像头:
<?xml version="1.0"?>
<!--
Author: AnthonySuen
Date: 2020-4-8
-->
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="universal_sensor_adder">
<!-- 用于生成一个新的传感器节点,
x_offset y_offset z_offset r p y对应于传感器和 base_link 的相对位置
sensor_config 为传感器的配置信息
sensor_plugin_config 为 gazebo 相关插件的配置内容 -->
<xacro:macro name="add_sensor" params="sensor_name type x_offset y_offset z_offset r p y **sensor_config **sensor_plugin_config">
<link name="sensor_${sensor_name}">
<!-- 配置传感器视觉信息 -->
<visual>
<geometry>
<box size="0.03 0.05 0.05"/>
</geometry>
<material name="red">
<color rgba="1.0 0.0 0.0 1.0"/>
</material>
</visual>
<!-- 配置碰撞体积, 为gazebo仿真使用 -->
<collision>
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/>
<geometry>
<box size="0.03 0.05 0.05"/>
</geometry>
</collision>
<!-- 配置惯性矩阵, 用于 gazebo 仿真,这里我假设传感器很轻很小 -->
<inertial>
<origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0"/>
<mass value="1e-5"/>
<inertia ixx="1e-6" ixy="1e-6" ixz="1e-6" iyy="1e-6" iyz="1e-6" izz="1e-6"/>
</inertial>
</link>
<!-- 配置关节信息, 这里我默认传感器是固定在车上的 -->
<joint name="sensor_${sensor_name}_joint" type="fixed">
<parent link="base_link"/>
<child link="sensor_${sensor_name}"/>
<origin xyz="${x_offset} ${y_offset} ${z_offset}" rpy="${r} ${p} ${y}"/>
</joint>
<!-- gazebo 配置仿真配置部分 -->
<gazebo reference="sensor_${sensor_name}"> <!-- 此部分要与 传感器 link 名称保持一致 -->
<sensor name="${sensor_name}" type="${type}">
<!-- 这里会插入传感器自身配置信息和相关插件的配置内容 -->
<xacro:insert_block name="sensor_config"/>
<xacro:insert_block name="sensor_plugin_config"/>
</sensor>
</gazebo>
</xacro:macro>
</robot>
15、编辑 empty.urdf 文件并修改
<?xml version="1.0"?>
<!--
Author: AnthonySuen
Date: 2020-4-8
-->
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="sensors">
<!-- 加载我们之前编写的文件,之后可以使用其中相关内容 -->
<xacro:include filename="$(find scout_description)/urdf/universal_sensor_adder.xacro" />
<!-- 使用我们编写的函数,填入相关参数 -->
<xacro:add_sensor sensor_name="camera" type="camera" x_offset="0.1" y_offset="0.0" z_offset="0.085" r="0.0" p="0.0" y="0.0">
<!-- 这部分会替换 <xacro:insert_block name="sensor_config"/> 中的内容 -->
<sensor_config>
<update_rate>30</update_rate>
<camera name="general_camera">
<image width="640" height="480" hfov="1.5708" format="RGB8" near="0.01" far="50.0"/>
</camera>
</sensor_config>
<!-- 这部分会替换 <xacro:insert_block name="sensor_plugin_config"/> 中的内容 -->
<sensor_plugin_config>
<plugin name="general_camera_controller" filename="libgazebo_ros_camera.so">
<alwaysOn>true</alwaysOn>
<updateRate>36.0</updateRate>
<cameraName>sensor_camera</cameraName>
<imageTopicName>image_raw</imageTopicName>
<cameraInfoTopicName>camera_info</cameraInfoTopicName>
<frameName>sensor_camera</frameName> <!-- 需要与 传感器 link 名称保持一致 -->
<hackBaseline>0.1</hackBaseline>
<distortionK1>0.0</distortionK1>
<distortionK2>0.0</distortionK2>
<distortionK3>0.0</distortionK3>
<distortionT1>0.0</distortionT1>
<distortionT2>0.0</distortionT2>
</plugin>
</sensor_plugin_config>
</xacro:add_sensor>
</robot>
16、运行 Rivz
$ roslaunch scout_description display_mini_models.launch
17、在 gazebo 中仿真并用 Rviz 接收
$ roslaunch scout_gazebo_sim scout_mini_playpen.launch