欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

gazebo模块化建模+控制器+传感器

程序员文章站 2022-06-23 20:58:16
使用xacro模块化建模定义常量: ...

使用xacro模块化建模

定义常量:

    <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>

宏定义:

<xacro:macro name="sphere_inertial_matrix" params="m r">
   <!-- Macro for robot wheel -->
    <xacro:macro name="wheel" params="prefix1 prefix2 reflect1 reflect2">
        <joint name="${prefix1}_${prefix2}_wheel_joint" type="continuous">
            <axis xyz="0 0 1"/>  
            <origin xyz="${reflect1}  ${reflect2} -0.05" rpy="0 1.57075 0"/>
            <parent link="base_link"/>
            <child link="${prefix1}_${prefix2}_wheel_link"/>
        </joint>

        <link name="${prefix1}_${prefix2}_wheel_link">
            <visual>
                <geometry>
                    <cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
                </geometry>
                <material name="gray" />
            </visual>
            <collision>
                <geometry>
                    <cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
                </geometry>
            </collision>
            <cylinder_inertial_matrix  m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />
        </link>

        <gazebo reference="${prefix1}_${prefix2}_wheel_link">
            <material>Gazebo/Gray</material>
        </gazebo>

   <!-- 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>

宏调用

<cylinder_inertial_matrix  m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />
<wheel prefix1="front"  prefix2="left" reflect1="-0.15" reflect2="0.15"/>
<wheel prefix1="front"  prefix2="right" reflect1="0.15" reflect2="0.15"/>
<wheel prefix1="back"  prefix2="left" reflect1="-0.15" reflect2="-0.15"/>
<wheel prefix1="back"  prefix2="right" reflect1="0.15" reflect2="-0.15"/>

文件包含

调用其他已定义好的模型, 可以实现模块化建模

<xacro:include filename="$(find mbot_description)/urdf/mbot_base_gazebo.xacro" />

优化物理模型

为了实现物理仿真,需要对模型添加惯性参数和碰撞属性

            <collision>
                <origin xyz="0 0 0" rpy="${M_PI/2} 0 0" />
                <geometry>
                    <cylinder radius="${wheel_radius}" length = "${wheel_length}"/>
                </geometry>
            </collision>
            <cylinder_inertial_matrix  m="${wheel_mass}" r="${wheel_radius}" h="${wheel_length}" />
        </link>

碰撞属性一般是指其碰撞的面积,为了便于计算,可以将其简化为规则形状

为link添加gazebo标签

gazebo中标签无颜色,需在此设置

        <gazebo reference="base_link">
            <material>Gazebo/Blue</material>
        </gazebo>

为jiont添加传动装置 (相当于电机)

        <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>

添加gazebo控制器插件

        <!-- Drive controller -->
    <gazebo>
    <plugin name="skid_steer_drive_controller" filename="libgazebo_ros_skid_steer_drive.so">
        <updateRate>100.0</updateRate>
        <robotNamespace>/</robotNamespace>
        <leftFrontJoint>front_left_wheel_joint</leftFrontJoint>
        <rightFrontJoint>front_right_wheel_joint</rightFrontJoint>
        <leftRearJoint>back_left_wheel_joint</leftRearJoint>
        <rightRearJoint>back_right_wheel_joint</rightRearJoint>
        <wheelSeparation>4</wheelSeparation>
        <wheelDiameter>0.1</wheelDiameter>
        <robotBaseFrame>base_link</robotBaseFrame>
        <torque>1</torque>
        <topicName>cmd_vel</topicName>
        <broadcastTF>0</broadcastTF>
    </plugin>
    </gazebo>

具体参数设置: http://gazebosim.org/tutorials?tut=ros_gzplugins#DifferentialDrive

也可以采用Ackermann转向驱动。

添加传感器仿真

可以使用gazebo的传感器模型,然后在主模型文件中添加该模型到base_link上

其他模型详见:Plugins available in gazebo_plugins

http://gazebosim.org/tutorials?tut=ros_gzplugins#OpenniKinect

<?xml version="1.0"?>
<robot name="my_car" xmlns:xacro="http://www.ros.org/wiki/xacro">

    <xacro:include filename="$(find my_car)/urdf/my_car_base.xacro" />
    <xacro:include filename="$(find my_car)/urdf/sensors/kinect_gazebo.xacro" />
    <xacro:include filename="$(find my_car)/urdf/sensors/camera_gazebo.xacro" />

    <xacro:property name="kinect_offset_x" value="0" />
    <xacro:property name="kinect_offset_y" value="0.2" />
    <xacro:property name="kinect_offset_z" value="0.11" />

    <!-- kinect -->
    <joint name="kinect_joint" type="fixed">
        <origin xyz="${kinect_offset_x} ${kinect_offset_y} ${kinect_offset_z}" rpy="0 0 1.57" />
        <parent link="base_link"/>
        <child link="kinect_link"/>
    </joint>


    <!-- Camera -->
    <xacro:kinect_camera prefix="kinect"/>
    <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" />


    <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"/>

    <my_car_base/>

</robot>

启动文件

<launch>
    <!-- 设置launch文件的参数 -->
    <arg name="world_name" value="$(find my_car)/worlds/playground.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 --inorder '$(find my_car)/urdf/my_car_base_with_rgbd.xacro'" />

      <!-- run node joint-state-pub,publish joint_state  -->
    <node name="joint_state_publisher_gui" pkg = "joint_state_publisher_gui" type = "joint_state_publisher_gui" />

     <!--run node robot_state_publisher, publish 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 my_car -param robot_description"/> 

      <!--run rviz-->
     <node name="rviz" pkg = "rviz" type = "rviz" args = "-d $(find my_car)/config/my_car_with_rgbd.rviz" />   
</launch>
: 中只需要修改path. :同上,只需修改path :同上 :同上(但可以不添加)

其他可以不用修改,下次直接使用

本文地址:https://blog.csdn.net/unspoken0714/article/details/108583708

相关标签: ros