目次
1.gazebo--SpawnModel: 失敗 - モデル名 mrobot は既に存在します。
Gazebo のインストールと構成
1. ソフトウェアを受信するようにコンピューターをセットアップする
sudo sh -c 'echo "deb http://packages.osrfoundation.org/gazebo/ubuntu-stable `lsb_release -cs` main" > /etc/apt/sources.list.d/gazebo-stable.list'
2.秘密鍵を設定する
wget https://packages.osrfoundation.org/gazebo.key -O - | sudo apt-key add -
3. Gazebo をインストールする
sudo apt-get update
sudo apt-get install gazebo11
sudo apt-get install libgazebo11-dev
4.インストールが機能するかどうかを確認します=
gazebo
5. /.gazebo フォルダを開いてモデルをダウンロードします
cd ~/.gazebo
git clone https://github.com/osrf/gazebo_models
GitHub への接続に致命的な失敗があった場合は、次のコードを使用してください。
git clone https://gitclone.com/github.com/osrf/gazebo_models
ダウンロードには時間がかかる場合があり、読者は私のように他の操作を実行するために別の端末を起動することを選択でき、シーン モデルが必要になるまでにダウンロードが完了する可能性があります。
6.其他关联安装包(不定时更新)
sudo apt-get install ros-noetic-rviz
sudo apt-get install ros-noetic-robot-state-publisher
sudo apt-get install ros-noetic-gazebo-ros-pkgs ros-noetic-gazebo-ros-control
sudo apt install ros-noetic-moveit
次に、インストール パッケージ インデックスを実行します。
sudo apt-get update
次に、ロボットモデルを構築します
ロボットモデル構成
最初に mbot_description/urdf/xacro と入力します
cd ~/catkin_ws/src/mbot_description/urdf/xacro
mkdir gazebo
cd gazebo
sudo gedit mbot_base_gazebo.xacro
モデルファイルを編集
<?xml version="1.0"?>
<robot name="mbot" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!--存放下面相关定义内容-->
</robot>
<!-- PROPERTY LIST -->
<xacro:property name="M_PI" value="3.1415926"/>
<xacro:property name="base_mass" value="20" />
<xacro:property name="base_radius" value="0.20"/>
<xacro:property name="base_length" value="0.16"/>
<xacro:property name="wheel_mass" value="2" />
<xacro:property name="wheel_radius" value="0.06"/>
<xacro:property name="wheel_length" value="0.025"/>
<xacro:property name="wheel_joint_y" value="0.19"/>
<xacro:property name="wheel_joint_z" value="0.05"/>
<xacro:property name="caster_mass" value="0.5" />
<xacro:property name="caster_radius" value="0.015"/> <!-- wheel_radius - ( base_length/2 - wheel_joint_z) -->
<xacro:property name="caster_joint_x" value="0.18"/>
<!-- Defining the colors used in this robot -->
<material name="yellow">
<color rgba="1 0.4 0 1"/>
</material>
<material name="black">
<color rgba="0 0 0 0.95"/>
</material>
<material name="gray">
<color rgba="0.75 0.75 0.75 1"/>
</material>
<!-- Macro for inertia matrix -->
<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>
<!-- Macro for robot wheel -->
<xacro:macro name="wheel" params="prefix reflect">
<joint name="${prefix}_wheel_joint" type="continuous">
<origin xyz="0 ${reflect*wheel_joint_y} ${-wheel_joint_z}" rpy="0 0 0"/>
<parent link="base_link"/>
<child link="${prefix}_wheel_link"/>
<axis xyz="0 1 0"/>
</joint>
<link name="${prefix}_wheel_link">
<visual>
<origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
<geometry>
<cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
</geometry>
<material name="gray" />
</visual>
<!-- collision -->
<!-- the same with visual -->
<!--增加惯性属性和碰撞属性-->
<collision>
<origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
<geometry>
<cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
</geometry>
</collision>
<!-- inertial -->
<cylinder_inertial_matrix m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />
</link>
<!--添加gazebo标签为各link配颜色 ,gazebo与rivz颜色设置不兼容-->
<!-- Add gazebo tag to link -->
<gazebo reference="${prefix}_wheel_link">
<material>Gazebo/Gray</material>
</gazebo>
<!--joint添加传动装置,用得 transmission 标签,小车轮子用速度控制接口-->
<!-- Transmission is important to link the joints and the controller -->
<transmission name="${prefix}_wheel_joint_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="${prefix}_wheel_joint" >
<hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
</joint>
<actuator name="${prefix}_wheel_joint_motor">
<hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>
</xacro:macro>
<!-- Macro for robot caster -->
<xacro:macro name="caster" params="prefix reflect">
<joint name="${prefix}_caster_joint" type="continuous">
<origin xyz="${reflect*caster_joint_x} 0 ${-(base_length/2 + caster_radius)}" rpy="0 0 0"/>
<parent link="base_link"/>
<child link="${prefix}_caster_link"/>
<axis xyz="0 1 0"/>
</joint>
<link name="${prefix}_caster_link">
<visual>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<sphere radius="${caster_radius}" />
</geometry>
<material name="black" />
</visual>
<!-- 碰撞属性 -->
<collision>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<sphere radius="${caster_radius}" />
</geometry>
</collision>
<!-- 惯性属性 -->
<sphere_inertial_matrix m="${caster_mass}" r="${caster_radius}" />
</link>
<!--添加gazebo标签,为各link配颜色-->
<gazebo reference="${prefix}_caster_link">
<material>Gazebo/Black</material>
</gazebo>
</xacro:macro>
<xacro:macro name="mbot_base_gazebo">
<link name="base_footprint">
<visual>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<box size="0.001 0.001 0.001" />
</geometry>
</visual>
</link>
<!-- 给 base_footprint 添加标签 -->
<gazebo reference="base_footprint">
<turnGravityOff>false</turnGravityOff>
</gazebo>
<joint name="base_footprint_joint" type="fixed">
<origin xyz="0 0 ${base_length/2 + caster_radius*2}" rpy="0 0 0" />
<parent link="base_footprint"/>
<child link="base_link" />
</joint>
<!--base_link添加碰撞属性和惯性属性-->
<link name="base_link">
<visual>
<origin xyz=" 0 0 0" rpy="0 0 0" />
<geometry>
<cylinder length="${base_length}" radius="${base_radius}"/>
</geometry>
<material name="yellow" />
</visual>
<collision>
<origin xyz=" 0 0 0" rpy="0 0 0" />
<geometry>
<cylinder length="${base_length}" radius="${base_radius}"/>
</geometry>
</collision>
<cylinder_inertial_matrix m="${base_mass}" r="${base_radius}" h="${base_length}" />
</link>
<!--base_link添加gazebo标签-->
<gazebo reference="base_link">
<material>Gazebo/Blue</material>
</gazebo>
<wheel prefix="left" reflect="-1"/> <!-- 调用驱动轮子宏定义 -->
<wheel prefix="right" reflect="1"/> <!-- 调用驱动轮子宏定义 -->
<caster prefix="front" reflect="-1"/> <!--调用支撑轮子宏定义-->
<caster prefix="back" reflect="1"/> <!-- 调用支撑轮子宏定义 -->
</xacro:macro>
<!-- controller -->
<gazebo>
<plugin name="differential_drive_controller"
filename="libgazebo_ros_diff_drive.so"> <!-- gazebo提供得差速控制器插件 -->
<!-- 控制器所需参数 -->
<rosDebugLevel>Debug</rosDebugLevel>
<publishWheelTF>true</publishWheelTF>
<robotNamespace>/</robotNamespace><!-- 机器人命名空间 订阅和发布得话题 前面 会加上命名空间 /说明没有添加-->
<publishTf>1</publishTf>
<publishWheelJointState>true</publishWheelJointState>
<alwaysOn>true</alwaysOn>
<updateRate>100.0</updateRate>
<legacyMode>true</legacyMode>
<leftJoint>left_wheel_joint</leftJoint> <!-- 控制得joint在哪里,必须和上面得joint名称一致 -->
<rightJoint>right_wheel_joint</rightJoint><!-- 控制得joint在哪里,必须和上面得joint名称一致 -->
<wheelSeparation>${wheel_joint_y*2}</wheelSeparation><!-- 两个轮子得间距 -->
<wheelDiameter>${2*wheel_radius}</wheelDiameter>
<broadcastTF>1</broadcastTF>
<wheelTorque>30</wheelTorque>
<wheelAcceleration>1.8</wheelAcceleration>
<commandTopic>cmd_vel</commandTopic> <!-- 订阅得话题:速度控制指令 -->
<odometryFrame>odom</odometryFrame>
<odometryTopic>odom</odometryTopic> <!-- 发布里程计信息 -->
<robotBaseFrame>base_footprint</robotBaseFrame><!-- 设置controler所控制的机器人的坐标系是哪个坐标系 -->
</plugin>
</gazebo>
mbot_gazebo.xacro を再度編集します。
<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" /> <!-- 包含文件 -->
<mbot_base_gazebo/> <!-- 调用宏定义 -->
</robot>
起動ファイルを編集
cd ~/catkin_ws/src/mbot_description/launch/xacro
mkdir gazebo
cd gazebo
sudo gedit mbot_base_gazebo.launch
<launch>
<!-- 设置launch文件的参数 -->
<arg name="paused" default="false"/>
<arg name="use_sim_time" default="true"/>
<arg name="gui" default="true"/>
<arg name="headless" default="false"/>
<arg name="debug" default="false"/>
<!-- 运行gazebo仿真环境 -->
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="debug" value="$(arg debug)" />
<arg name="gui" value="$(arg gui)" />
<arg name="paused" value="$(arg paused)"/>
<arg name="use_sim_time" value="$(arg use_sim_time)"/>
<arg name="headless" value="$(arg headless)"/>
</include>
<!-- 加载机器人模型描述参数 -->
<param name="robot_description" command="$(find xacro)/xacro --inorder '$(find mbot_description)/urdf/xacro/gazebo/mbot_gazebo.xacro'" />
<!-- 运行joint_state_publisher节点,发布机器人的关节状态 -->
<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
<!-- 运行robot_state_publisher节点,发布tf -->
<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" output="screen" >
<param name="publish_frequency" type="double" value="50.0" />
</node>
<!-- 在gazebo中加载机器人模型-->
<node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
args="-urdf -model mrobot -param robot_description"/>
</launch>
構成が完了したら、表示モデルを実行します
roslaunch mbot_description mbot_base_gazebo.launch
知らせ
Ubuntu 20.04に対応するrosのバージョンがnoeticなので、xacroモデルファイルのマクロ定義とマクロ呼び出し方法を変更する必要があります
Ubuntu20.04のmbot_base_gazebo.xacroファイルの対応内容は以下の通り
<?xml version="1.0"?>
<robot name="mbot" xmlns:xacro="http://www.ros.org/wiki/xacro">
<!-- PROPERTY LIST -->
<xacro:property name="M_PI" value="3.1415926"/>
<xacro:property name="base_mass" value="20" />
<xacro:property name="base_radius" value="0.20"/>
<xacro:property name="base_length" value="0.16"/>
<xacro:property name="wheel_mass" value="2" />
<xacro:property name="wheel_radius" value="0.06"/>
<xacro:property name="wheel_length" value="0.025"/>
<xacro:property name="wheel_joint_y" value="0.19"/>
<xacro:property name="wheel_joint_z" value="0.05"/>
<xacro:property name="caster_mass" value="0.5" />
<xacro:property name="caster_radius" value="0.015"/> <!-- wheel_radius - ( base_length/2 - wheel_joint_z) -->
<xacro:property name="caster_joint_x" value="0.18"/>
<!-- Defining the colors used in this robot -->
<material name="yellow">
<color rgba="1 0.4 0 1"/>
</material>
<material name="black">
<color rgba="0 0 0 0.95"/>
</material>
<material name="gray">
<color rgba="0.75 0.75 0.75 1"/>
</material>
<!-- Macro for inertia matrix -->
<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>
<!-- Macro for robot wheel -->
<xacro:macro name="wheel" params="prefix reflect">
<joint name="${prefix}_wheel_joint" type="continuous">
<origin xyz="0 ${reflect*wheel_joint_y} ${-wheel_joint_z}" rpy="0 0 0"/>
<parent link="base_link"/>
<child link="${prefix}_wheel_link"/>
<axis xyz="0 1 0"/>
</joint>
<link name="${prefix}_wheel_link">
<visual>
<origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
<geometry>
<cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
</geometry>
<material name="gray" />
</visual>
<collision>
<origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
<geometry>
<cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
</geometry>
</collision>
<xacro:cylinder_inertial_matrix m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />
</link>
<gazebo reference="${prefix}_wheel_link">
<material>Gazebo/Gray</material>
</gazebo>
<!-- Transmission is important to link the joints and the controller -->
<transmission name="${prefix}_wheel_joint_trans">
<type>transmission_interface/SimpleTransmission</type>
<joint name="${prefix}_wheel_joint" >
<hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
</joint>
<actuator name="${prefix}_wheel_joint_motor">
<hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
<mechanicalReduction>1</mechanicalReduction>
</actuator>
</transmission>
</xacro:macro>
<!-- Macro for robot caster -->
<xacro:macro name="caster" params="prefix reflect">
<joint name="${prefix}_caster_joint" type="continuous">
<origin xyz="${reflect*caster_joint_x} 0 ${-(base_length/2 + caster_radius)}" rpy="0 0 0"/>
<parent link="base_link"/>
<child link="${prefix}_caster_link"/>
<axis xyz="0 1 0"/>
</joint>
<link name="${prefix}_caster_link">
<visual>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<sphere radius="${caster_radius}" />
</geometry>
<material name="black" />
</visual>
<collision>
<origin xyz="0 0 0" rpy="0 0 0"/>
<geometry>
<sphere radius="${caster_radius}" />
</geometry>
</collision>
<xacro:sphere_inertial_matrix m="${caster_mass}" r="${caster_radius}" />
</link>
<gazebo reference="${prefix}_caster_link">
<material>Gazebo/Black</material>
</gazebo>
</xacro:macro>
<xacro:macro name="mbot_base_gazebo">
<link name="base_footprint">
<visual>
<origin xyz="0 0 0" rpy="0 0 0" />
<geometry>
<box size="0.001 0.001 0.001" />
</geometry>
</visual>
</link>
<gazebo reference="base_footprint">
<turnGravityOff>false</turnGravityOff>
</gazebo>
<joint name="base_footprint_joint" type="fixed">
<origin xyz="0 0 ${base_length/2 + caster_radius*2}" rpy="0 0 0" />
<parent link="base_footprint"/>
<child link="base_link" />
</joint>
<link name="base_link">
<visual>
<origin xyz=" 0 0 0" rpy="0 0 0" />
<geometry>
<cylinder length="${base_length}" radius="${base_radius}"/>
</geometry>
<material name="yellow" />
</visual>
<collision>
<origin xyz=" 0 0 0" rpy="0 0 0" />
<geometry>
<cylinder length="${base_length}" radius="${base_radius}"/>
</geometry>
</collision>
<xacro:cylinder_inertial_matrix m="${base_mass}" r="${base_radius}" h="${base_length}" />
</link>
<gazebo reference="base_link">
<material>Gazebo/Blue</material>
</gazebo>
<xacro:wheel prefix="left" reflect="-1"/>
<xacro:wheel prefix="right" reflect="1"/>
<xacro:caster prefix="front" reflect="-1"/>
<xacro:caster prefix="back" reflect="1"/>
<!-- controller -->
<gazebo>
<plugin name="differential_drive_controller"
filename="libgazebo_ros_diff_drive.so">
<rosDebugLevel>Debug</rosDebugLevel>
<publishWheelTF>true</publishWheelTF>
<robotNamespace>/</robotNamespace>
<publishTf>1</publishTf>
<publishWheelJointState>true</publishWheelJointState>
<alwaysOn>true</alwaysOn>
<updateRate>100.0</updateRate>
<legacyMode>true</legacyMode>
<leftJoint>left_wheel_joint</leftJoint>
<rightJoint>right_wheel_joint</rightJoint>
<wheelSeparation>${wheel_joint_y*2}</wheelSeparation>
<wheelDiameter>${2*wheel_radius}</wheelDiameter>
<broadcastTF>1</broadcastTF>
<wheelTorque>30</wheelTorque>
<wheelAcceleration>1.8</wheelAcceleration>
<commandTopic>cmd_vel</commandTopic>
<odometryFrame>odom</odometryFrame>
<odometryTopic>odom</odometryTopic>
<robotBaseFrame>base_footprint</robotBaseFrame>
</plugin>
</gazebo>
</xacro:macro>
</robot>
変更が必要な箇所は
ホイールのマクロ定義構文とサポート ホイールのマクロ定義構文の前に、xacro 参照を追加します。
シリンダー慣性マトリックス、
sphere_inertial_matrix
2 つのタグを参照する場合は、xacro 参照も追加する必要があります
また、mbot_gazebo.xacro ファイルもマクロ定義を変更する必要があります。
<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" /> <!-- 包含文件 -->
<xacro:mbot_base_gazebo/> <!-- 调用宏定义 -->
</robot>
起動ファイルも変更する必要があります.変更するには3つの方法があります.一般に,
<!-- ロボット モデルの説明パラメーターを読み込む -->
<param name="robot_description" command="$(find xacro)/ xacro --inorder '$(find mbot_description)/urdf/xacro/gazebo/mbot_gazebo.xacro'" />
xacroに変更するだけです
に変更するなど、他の回避策もあります。
xacro --i
そのほか
xacro.py
<launch>
<!-- 设置launch文件的参数 -->
<arg name="paused" default="false"/>
<arg name="use_sim_time" default="true"/>
<arg name="gui" default="true"/>
<arg name="headless" default="false"/>
<arg name="debug" default="false"/>
<!-- 运行gazebo仿真环境 -->
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="debug" value="$(arg debug)" />
<arg name="gui" value="$(arg gui)" />
<arg name="paused" value="$(arg paused)"/>
<arg name="use_sim_time" value="$(arg use_sim_time)"/>
<arg name="headless" value="$(arg headless)"/>
</include>
<!-- 加载机器人模型描述参数 -->
<param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_gazebo.xacro'" />
<!-- 运行joint_state_publisher节点,发布机器人的关节状态 -->
<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
<!-- 运行robot_state_publisher节点,发布tf -->
<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" output="screen" >
<param name="publish_frequency" type="double" value="50.0" />
</node>
<!-- 在gazebo中加载机器人模型-->
<node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
args="-urdf -model mrobot -param robot_description"/>
</launch>
通常起動後はこんな感じ
シミュレーション環境を作成する
シーン モデルを追加する 2 つの方法を次に示します。
最初:直接追加
モデルを ~/.gazebo/models フォルダーに配置します - ガゼボの左側のリストで「挿入」をクリックします (その中に多くのモデルがあることがわかります。必要なモデルをリストからドラッグして配置するだけです。環境内のシミュレーションで)
https://bitbucket.org/osrf/gazebo_models/downloads/
冒頭のダウンロードが完了している場合はスキップして構いませんが、ダウンロードが完了していない場合は、ダウンロードが完了するまで待ってから先に進んでください。
目的のモデルを選択し、シーンに直接ドラッグしてビルドします
この手順では、元のロボット モデルを削除してから保存する必要があることに注意してください。
ここに「接続中」と表示されている場合は、まだ接続が完了していないことを意味します。httpを待ちます-----接続が完了したら、モデルが正常に表示されるようになり、追加するモデルを選択します。
ファイル名と場所をカスタマイズする
catkin_ws/src/mbot_descritpion/worlds に保存することをお勧めします
2 番目: 建物エディターを使用する
モデルの作成:
編集——ビルディング エディタ——環境モデルを描画——ファイル——モデル ファイルを保存するために保存(モデル ファイルの名前を自分で設定)——ビルディング エディタを終了(編集インターフェイスを終了)すると、シミュレーションが表示されます。環境はすでにガゼボショーにあります。
環境モデルの保存は最初の方法と同じで、ガゼボ インターフェイスを閉じます。
シミュレーション利用
<!-- 设置launch文件的参数 -->
<arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/><!-- 要加入的部分 -->
<arg name="paused" default="false"/>
<arg name="use_sim_time" default="true"/>
<arg name="gui" default="true"/>
<arg name="headless" default="false"/>
<arg name="debug" default="false"/>
センサーシミュレーション
cd ~/catkin_ws/src/mbot_description/urdf/xacro
mkdir sensors
cd sensors
sudo gedit camera_gazebo.xacro
<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="camera">
<xacro:macro name="usb_camera" params="prefix:=camera">
<link name="${prefix}_link">
<inertial>
<mass value="0.1" />
<origin xyz="0 0 0" />
<inertia ixx="0.01" ixy="0.0" ixz="0.0"
iyy="0.01" iyz="0.0"
izz="0.01" />
</inertial>
<visual>
<origin xyz=" 0 0 0 " rpy="0 0 0" />
<geometry>
<box size="0.01 0.04 0.04" />
</geometry>
<material name="black"/>
</visual>
<collision>
<origin xyz="0.0 0.0 0.0" rpy="0 0 0" />
<geometry>
<box size="0.01 0.04 0.04" />
</geometry>
</collision>
</link>
<gazebo reference="${prefix}_link">
<material>Gazebo/Black</material>
</gazebo>
<gazebo reference="${prefix}_link"> <!-- 这个sensor代表的link -->
<sensor type="camera" name="camera_node">
<update_rate>30.0</update_rate><!-- 摄像头发布频率 -->
<camera name="head">
<horizontal_fov>1.3962634</horizontal_fov><!-- 摄像头可视范围 -->
<image>
<width>1280</width><!-- 摄像头分辨率 -->
<height>720</height><!-- 摄像头分辨率 -->
<format>R8G8B8</format><!-- 摄像头数据格式 -->
</image>
<clip>
<near>0.02</near><!-- 最近距离 -->
<far>300</far><!-- 最远距离 -->
</clip>
<noise>
<type>gaussian</type><!-- 摄像头高斯噪声 -->
<mean>0.0</mean>
<stddev>0.007</stddev>
</noise>
</camera>
<plugin name="gazebo_camera" filename="libgazebo_ros_camera.so"><!-- 加载插件,实现摄像头功能 -->
<alwaysOn>true</alwaysOn>
<updateRate>0.0</updateRate>
<cameraName>/camera</cameraName><!-- 命名空间 -->
<imageTopicName>image_raw</imageTopicName><!-- 发布图片信息话题名称 -->
<cameraInfoTopicName>camera_info</cameraInfoTopicName><!-- 发布摄像头信息话题名称 -->
<frameName>camera_link</frameName><!-- 数据的坐标系统 -->
<hackBaseline>0.07</hackBaseline>
<distortionK1>0.0</distortionK1>
<distortionK2>0.0</distortionK2>
<distortionK3>0.0</distortionK3>
<distortionT1>0.0</distortionT1>
<distortionT2>0.0</distortionT2>
</plugin>
</sensor>
</gazebo>
</xacro:macro>
</robot>
cd ~/catkin_ws/src/mbot_description/urdf/xacro/gazebo
sudo gedit mbot_with_camera_gazebo.xacro
ファイルに次のように書くだけです
<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" />
<xacro:include filename="$(find mbot_description)/urdf/xacro/sensors/camera_gazebo.xacro" />
<xacro:property name="camera_offset_x" value="0.17" />
<xacro:property name="camera_offset_y" value="0" />
<xacro:property name="camera_offset_z" value="0.10" />
<mbot_base/>
<!-- Camera -->
<joint name="camera_joint" type="fixed">
<origin xyz="${camera_offset_x} ${camera_offset_y} ${camera_offset_z}" rpy="0 0 0" />
<parent link="base_link"/>
<child link="camera_link"/>
</joint>
<xacro:usb_camera prefix="camera"/>
<xacro:mbot_base_gazebo/>
</robot>
カメラ付きロボット起動用スタートアップファイルのコンパイル
cd ~/catkin_ws/src/mbot_description/launch/xacro/gazebo
sudo gedit view_mbot_with_camera_gazebo.launch
<launch>
<arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/>
<arg name="paused" default="false"/>
<arg name="use_sim_time" default="true"/>
<arg name="gui" default="true"/>
<arg name="headless" default="false"/>
<arg name="debug" default="false"/>
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(arg world_name)" />
<arg name="debug" value="$(arg debug)" />
<arg name="gui" value="$(arg gui)" />
<arg name="paused" value="$(arg paused)"/>
<arg name="use_sim_time" value="$(arg use_sim_time)"/>
<arg name="headless" value="$(arg headless)"/>
</include>
<param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_with_camera_gazebo.xacro'" />
<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" output="screen" >
<param name="publish_frequency" type="double" value="50.0" />
</node>
<node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
args="-urdf -model mrobot -param robot_description"/>
</launch>
次に、表示モデルを実行します
roslaunch mbot_description view_mbot_with_camera_gazebo.launch
単語が表示される場合は注意してください
[ERROR] [1666006852.591762, 106.467000]: Spawn service failed. Exiting.
[urdf_spawner-6] プロセスが停止しました [pid 35261, exit code 1, cmd /opt/ros/noetic/lib/gazebo_ros/spawn_model -urdf -model mrobot param robot_description __name:=urdf_spawner __log:=/home/q/.ros/log/89f24a6e-4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6.log].log
ファイル: /home/q/.ros/log/89f24a6e - 4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6*.log は、
ガゼボ プロセスが完全に閉じられておらず、すべてのガゼボ プロセスを閉じる必要があることを示しています
killall gzserver
その後、正常に起動できることがわかりました
qt 視覚化ツールを使用して、カメラ画面の表示を表示します
別のターミナルを起動する
rqt_image_view
カメラパラメータの選択に注意してください
キーボード制御を有効にする
roslaunch mbot_teleop mbot_teleop.launch
キーボードコントロールに注意してください
ここでエラーが報告された場合、python パッケージが見つからない
Python プラグインをダウンロードして設定する必要があります
sudo ln -s /usr/bin/python3 /usr/bin/python
次に、キーボードでノードを制御できるようにします
リーダー機能パッケージの src ディレクトリに mbot_teleop 機能パッケージがない場合は、別のキーボード制御機能パッケージをダウンロードすることもできます
cd ~/carkin_ws/src
git clone https://github.com/ros-teleop/teleop_twist_keyboard.git
//然后启用即可
rosrun teleop_twist_keyboard teleop_twist_keyboard.py
teleop_twist_keyboard.py を実行可能ファイルに変更する必要があることに注意してください
この機能パッケージを使いたくなく、mbot_teleop 機能パッケージを独自に作成する方法を知りたい場合は、Arbotix+rviz の mbot_teleop 機能パッケージの作成方法の記事を参照してください。
Rviz はカメラによって収集された情報を表示します
rosrun rviz rviz
最初の固定フレーム (固定フレーム) select base_footprint
次に追加——robotmodel——ok
次に追加——画像——OK
その後image——画像トピック——/camera/image_raw
LiDAR シミュレーション
cd ~/catkin_ws/src/mbot_description/urdf/xacro/sensors
sudo gedit lidar_gazebo.xacro
<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="laser">
<xacro:macro name="rplidar" params="prefix">
<!-- Create laser reference frame -->
<link name="${prefix}_link">
<inertial>
<mass value="0.1" />
<origin xyz="0 0 0" />
<inertia ixx="0.01" ixy="0.0" ixz="0.0"
iyy="0.01" iyz="0.0"
izz="0.01" />
</inertial>
<visual>
<origin xyz=" 0 0 0 " rpy="0 0 0" />
<geometry>
<cylinder length="0.05" radius="0.05"/>
</geometry>
<material name="black"/>
</visual>
<collision>
<origin xyz="0.0 0.0 0.0" rpy="0 0 0" />
<geometry>
<cylinder length="0.06" radius="0.05"/>
</geometry>
</collision>
</link>
<gazebo reference="${prefix}_link">
<material>Gazebo/Black</material>
</gazebo>
<gazebo reference="${prefix}_link">
<sensor type="ray" name="rplidar">
<pose>0 0 0 0 0 0</pose>
<visualize>false</visualize>
<update_rate>5.5</update_rate>
<ray>
<scan>
<horizontal>
<samples>360</samples>
<resolution>1</resolution>
<min_angle>-3</min_angle>
<max_angle>3</max_angle>
</horizontal>
</scan>
<range>
<min>0.10</min>
<max>6.0</max>
<resolution>0.01</resolution>
</range>
<noise>
<type>gaussian</type>
<mean>0.0</mean>
<stddev>0.01</stddev>
</noise>
</ray>
<plugin name="gazebo_rplidar" filename="libgazebo_ros_laser.so">
<topicName>/scan</topicName>
<frameName>laser_link</frameName>
</plugin>
</sensor>
</gazebo>
</xacro:macro>
</robot>
次に、メインの xacro ファイルを書き込みます
cd ~/catkin_ws/src/mbot_description/urdf/xacro/gazebo
sudo gedit mbot_with_laser_gazebo.xacro
内容は以下の通り
<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" />
<xacro:include filename="$(find mbot_description)/urdf/xacro/sensors/lidar_gazebo.xacro" />
<xacro:property name="lidar_offset_x" value="0" />
<xacro:property name="lidar_offset_y" value="0" />
<xacro:property name="lidar_offset_z" value="0.105" />
<!-- lidar -->
<joint name="lidar_joint" type="fixed">
<origin xyz="${lidar_offset_x} ${lidar_offset_y} ${lidar_offset_z}" rpy="0 0 0" />
<parent link="base_link"/>
<child link="laser_link"/>
</joint>
<xacro:rplidar prefix="laser"/>
<xacro:mbot_base_gazebo/>
</robot>
次に、起動スタートアップファイルを書き込みます
cd ~/catkin_ws/src/mbot_description/launch/xacro/gazebo
sudo gedit view_mbot_with_laser_gazebo.launch
<launch>
<arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/>
<arg name="paused" default="false"/>
<arg name="use_sim_time" default="true"/>
<arg name="gui" default="true"/>
<arg name="headless" default="false"/>
<arg name="debug" default="false"/>
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(arg world_name)" />
<arg name="debug" value="$(arg debug)" />
<arg name="gui" value="$(arg gui)" />
<arg name="paused" value="$(arg paused)"/>
<arg name="use_sim_time" value="$(arg use_sim_time)"/>
<arg name="headless" value="$(arg headless)"/>
</include>
<param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_with_laser_gazebo.xacro'" />
<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" output="screen" >
<param name="publish_frequency" type="double" value="50.0" />
</node>
<node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
args="-urdf -model mrobot -param robot_description"/>
</launch>
ライダーは正常に表示されます
次に、キーボード制御を有効にします
roslaunch mbot_teleop mbot_teleop.launch
rviz レーダー収集情報を表示
rosrun rviz rviz
注意すべきことは今までと同じです
最初の固定フレーム (固定フレーム) select base_footprint
次に追加——robotmodel——ok
次に、追加 - レーザースキャン - OK
次に、レーザースキャン—トピック—スキャン
写真の赤い線がレーダーで感知した障害物です. 消防車と救急車の間に車が挟まって身動きが取れないので図では説明しません. 赤線はとても薄いです.カーパックの周りの障害物をいっぱいに取ることをお勧めします。
Kinect エミュレーション
この方法は、上記のカメラとライダーによって実装された方法と似ているため、ここでは説明せず、結果を直接示します。
ガゼボディスプレイ
cd ~/catkin_ws/src/mbot_description/urdf/xacro/sensors
sudo gedit kinect_gazebo.xacro
<?xml version="1.0"?>
<robot xmlns:xacro="http://www.ros.org/wiki/xacro" name="kinect_camera">
<xacro:macro name="kinect_camera" params="prefix:=camera">
<!-- Create kinect reference frame -->
<!-- Add mesh for kinect -->
<link name="${prefix}_link">
<origin xyz="0 0 0" rpy="0 0 0"/>
<visual>
<origin xyz="0 0 0" rpy="0 0 ${M_PI/2}"/>
<geometry>
<mesh filename="package://mbot_description/meshes/kinect.dae" />
</geometry>
</visual>
<collision>
<geometry>
<box size="0.07 0.3 0.09"/>
</geometry>
</collision>
</link>
<joint name="${prefix}_optical_joint" type="fixed">
<origin xyz="0 0 0" rpy="-1.5708 0 -1.5708"/>
<parent link="${prefix}_link"/>
<child link="${prefix}_frame_optical"/>
</joint>
<link name="${prefix}_frame_optical"/>
<gazebo reference="${prefix}_link">
<sensor type="depth" name="${prefix}">
<always_on>true</always_on>
<update_rate>20.0</update_rate>
<camera>
<horizontal_fov>${60.0*M_PI/180.0}</horizontal_fov>
<image>
<format>R8G8B8</format>
<width>640</width>
<height>480</height>
</image>
<clip>
<near>0.05</near>
<far>8.0</far>
</clip>
</camera>
<plugin name="kinect_${prefix}_controller" filename="libgazebo_ros_openni_kinect.so">
<cameraName>${prefix}</cameraName>
<alwaysOn>true</alwaysOn>
<updateRate>10</updateRate>
<imageTopicName>rgb/image_raw</imageTopicName>
<depthImageTopicName>depth/image_raw</depthImageTopicName>
<pointCloudTopicName>depth/points</pointCloudTopicName>
<cameraInfoTopicName>rgb/camera_info</cameraInfoTopicName>
<depthImageCameraInfoTopicName>depth/camera_info</depthImageCameraInfoTopicName>
<frameName>${prefix}_frame_optical</frameName>
<baseline>0.1</baseline>
<distortion_k1>0.0</distortion_k1>
<distortion_k2>0.0</distortion_k2>
<distortion_k3>0.0</distortion_k3>
<distortion_t1>0.0</distortion_t1>
<distortion_t2>0.0</distortion_t2>
<pointCloudCutoff>0.4</pointCloudCutoff>
</plugin>
</sensor>
</gazebo>
</xacro:macro>
</robot>
次に、メインの xacro ファイルを書き込みます
cd ~/catkin_ws/src/mbot_desctiption/urdf/xacro/gazebo
sudo gedit mbot_with_kinect_gazebo.xacro
<?xml version="1.0"?>
<robot name="arm" xmlns:xacro="http://www.ros.org/wiki/xacro">
<xacro:include filename="$(find mbot_description)/urdf/xacro/gazebo/mbot_base_gazebo.xacro" />
<xacro:include filename="$(find mbot_description)/urdf/xacro/sensors/kinect_gazebo.xacro" />
<xacro:property name="kinect_offset_x" value="0.15" />
<xacro:property name="kinect_offset_y" value="0" />
<xacro:property name="kinect_offset_z" value="0.11" />
<mbot_base/>
<!-- kinect -->
<joint name="kinect_joint" type="fixed">
<origin xyz="${kinect_offset_x} ${kinect_offset_y} ${kinect_offset_z}" rpy="0 0 0" />
<parent link="base_link"/>
<child link="kinect_link"/>
</joint>
<xacro:kinect_camera prefix="kinect"/>
<xacro:mbot_base_gazebo/>
</robot>
次に、起動スタートアップファイルを書き込みます
cd ~/catkin_ws/src/mbot_description/launch/xacro/gazebo
sudo gedit view_mbot_with_kinect_gazebo.launch
<launch>
<!-- 设置launch文件的参数 -->
<arg name="world_name" value="$(find mbot_description)/worlds/Ambulance.world"/><!-- 设置仿真环境文件路径 -->
<arg name="paused" default="false"/>
<arg name="use_sim_time" default="true"/>
<arg name="gui" default="true"/>
<arg name="headless" default="false"/>
<arg name="debug" default="false"/>
<!-- 运行gazebo仿真环境 -->
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(arg world_name)" />
<arg name="debug" value="$(arg debug)" />
<arg name="gui" value="$(arg gui)" />
<arg name="paused" value="$(arg paused)"/>
<arg name="use_sim_time" value="$(arg use_sim_time)"/>
<arg name="headless" value="$(arg headless)"/>
</include>
<!-- 加载机器人模型描述参数 -->
<param name="robot_description" command="$(find xacro)/xacro '$(find mbot_description)/urdf/xacro/gazebo/mbot_with_kinect_gazebo.xacro'" /> <!-- 设置机器人模型文件路径 -->
<!-- 运行joint_state_publisher节点,发布机器人的关节状态 -->
<node name="joint_state_publisher" pkg="joint_state_publisher" type="joint_state_publisher" ></node>
<!-- 运行robot_state_publisher节点,发布tf -->
<node name="robot_state_publisher" pkg="robot_state_publisher" type="robot_state_publisher" output="screen" >
<param name="publish_frequency" type="double" value="50.0" />
</node>
<!-- 在gazebo中加载机器人模型-->
<node name="urdf_spawner" pkg="gazebo_ros" type="spawn_model" respawn="false" output="screen"
args="-urdf -model mrobot -param robot_description"/>
</launch>
実行後はこんな感じ
kinect が収集した情報を表示する
rosrun rviz rviz
知らせ:
最初の固定フレーム (固定フレーム) select base_footprint
次に追加——robotmodel——ok
次に追加 - pointcloud2 - OK
次に、pointcloud2——トピック——/kinect/depth/points
以上が本節の内容であり、Gazebo物理シミュレーションプラットフォームの構築が完了しました。
問題が解決しました:
1.gazebo--SpawnModel: 失敗 - モデル名 mrobot は既に存在します。
roslaunch gazebo_ros empty_world.launch
開いた後のガゼボインターフェースで、mrobotモデルの削除を選択して削除します
その後、終了します。
上記のモデルを作成して .world を保存する前に、ロボット モデルを削除してから保存する必要があります。そうしないと、モデルの名前を変更したときにエラーが発生します。
2. [エラー] [1666006852.591762, 106.467000]: スポーン サービスに失敗しました。終了します。
[urdf_spawner-6] プロセスが終了しました [pid 35261、終了コード 1、cmd /opt/ros/noetic/lib/gazebo_ros/spawn_model -urdf -model mrobot -param robot_description __name:=urdf_spawner __log:=/home/q/. ros/log/89f24a6e-4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6.log].
ログファイル: /home/q/.ros/log/89f24a6e-4e10-11ed-b861-1f202bd9bc85/urdf_spawner-6*.log
すべてのガゼボ プロセスを閉じて再起動します
killall gzserver