ROS教程 Gazebo仿真(1)--ros_control两轮差速控制_wx:pjcoder的博客-程序员宅基地

技术标签: 自动驾驶  ROS  

创建机器人描述

catkin_create_pkg  robot_description  urdf  xacro  gazebo_ros gazebo_ros_control  gazebo_plugins

URDF

robot_base.xacro

<robot name="robot_base" xmlns:xacro="http://www.ros.org/wiki/xacro">

    <xacro:property name="PI" value="3.1415926"/>

    <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" /> 
    <xacro:property name="base_link_m" value="0.5" />


    <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>
      <collision>
        <geometry>
          <cylinder radius="${base_link_radius}" length="${base_link_length}" />
        </geometry>
        <origin xyz="0 0 0" rpy="0 0 0" />
      </collision>
      <xacro:cylinder_inertial_matrix m="${base_link_m}" r="${base_link_radius}" h="${base_link_length}" />

    </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>
    <gazebo reference="base_link">
        <material>Gazebo/Yellow</material>
    </gazebo>


    <xacro:property name="wheel_radius" value="0.0325" />
    <xacro:property name="wheel_length" value="0.015" />
    <xacro:property name="wheel_m" value="0.05" /> 


    <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>
        <collision>
          <geometry>
            <cylinder radius="${wheel_radius}" length="${wheel_length}" />
          </geometry>
          <origin xyz="0.0 0.0 0.0" rpy="${PI / 2} 0.0 0.0" />
        </collision>
        <xacro:cylinder_inertial_matrix m="${wheel_m}" r="${wheel_radius}" h="${wheel_length}" />

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

      <gazebo reference="${name}_wheel">
        <material>Gazebo/Red</material>
      </gazebo>

    </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:property name="support_wheel_m" value="0.03" /> 


    <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>
        <collision>
            <geometry>
                <sphere radius="${support_wheel_radius}" />
            </geometry>
            <origin xyz="0 0 0" rpy="0 0 0" />
        </collision>
        <xacro:sphere_inertial_matrix m="${support_wheel_m}" r="${support_wheel_radius}" />
      </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>
      <gazebo reference="${name}_wheel">
        <material>Gazebo/Red</material>
      </gazebo>
    </xacro:macro>

    <xacro:add_support_wheel name="front" flag="1" />
    <xacro:add_support_wheel name="back" flag="-1" />


</robot>

camera.xacro

<robot name="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>

laser.xacro

<robot name="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>

inertia.xacro 惯性矩阵


<robot name="inertia" xmlns:xacro="http://wiki.ros.org/xacro">
    <!-- 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>

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

launch

<launch>
    <!-- 将 Urdf 文件的内容加载到参数服务器 -->
    <param name="robot_description" command="$(find xacro)/xacro $(find robot_description)/urdf/robot_car.xacro" />
    <!-- 启动 gazebo -->
    <include file="$(find gazebo_ros)/launch/empty_world.launch">
   	<arg name="world_name" value="$(find robot_description)/worlds/box_house.world" />
    </include>

    <!-- 在 gazebo 中显示机器人模型 -->
    <node pkg="gazebo_ros" type="spawn_model" name="model" args="-urdf -model mycar -param robot_description"  />
</launch>

box_house.world 接: https://pan.baidu.com/s/1T17RWZF1DMeb6FhLc8a2tQ 密码: ka98

下面配置ros_control

<robot name="move" xmlns:xacro="http://wiki.ros.org/xacro">

    <xacro:macro name="joint_trans" params="joint_name">
        <transmission name="${joint_name}_trans">
            <type>transmission_interface/SimpleTransmission</type>
            <joint name="${joint_name}">
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
            </joint>
            <actuator name="${joint_name}_motor">
                <hardwareInterface>hardware_interface/VelocityJointInterface</hardwareInterface>
                <mechanicalReduction>1</mechanicalReduction>
            </actuator>
        </transmission>
    </xacro:macro>


    <xacro:joint_trans joint_name="left_wheel2base_link" />
    <xacro:joint_trans joint_name="right_wheel2base_link" />


    <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_wheel2base_link</leftJoint>
            <rightJoint>right_wheel2base_link</rightJoint> 
            <wheelSeparation>${base_link_radius * 2}</wheelSeparation> 
            <wheelDiameter>${wheel_radius * 2}</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>

</robot>

robt_car.xacro 整合ros_control

<robot name="robot_car" xmlns:xacro="http://wiki.ros.org/xacro">
    <xacro:include filename="inertia.xacro" />    
    <xacro:include filename="robot_base.xacro" />
    <xacro:include filename="camera.xacro" />
    <xacro:include filename="laser.xacro" />
    <xacro:include filename="gazebo/move.xacro" />
</robot>

要点

两轮差速配置文件中需要注意的是 这几个参数

<xacro:joint_trans joint_name="left_wheel2base_link" />
<xacro:joint_trans joint_name="right_wheel2base_link" />
<leftJoint>left_wheel2base_link</leftJoint>
<rightJoint>right_wheel2base_link</rightJoint> 
 <wheelSeparation>${base_link_radius * 2}</wheelSeparation> 
<wheelDiameter>${wheel_radius * 2}</wheelDiameter>

一定要对应好urdf中的配置 如果配置错误有些情况下没有错误提示
在这里插入图片描述

rostopic list
/clock
/cmd_vel
/gazebo/link_states
/gazebo/model_states
/gazebo/parameter_descriptions
/gazebo/parameter_updates
/gazebo/set_link_state
/gazebo/set_model_state
/joint_states
/odom
/rosout
/rosout_agg
/tf

存在/cmd_vel 和 /odom 等话题 说明配置成功
让它原地打转

rostopic pub -r 10 /cmd_vel geometry_msgs/Twist '{linear: {x: 0.2, y: 0, z: 0}, angular: {x: 0, y: 0, z: 0.5}}'

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43928944/article/details/115899503

智能推荐

OGG Director连接 OGG12c_ggsci connection error:com.goldengate.gdsc.server.-程序员宅基地

在使用Goldengate Director GDS Admin Tool创建Data Source连接 Goldengate12c时遇到下面的报错。 Test is running directly from DirectorServer.Opening GGSCI connection...Connection FAILED, Manager doesn't seems to..._ggsci connection error:com.goldengate.gdsc.server.net.ggsci access denied

Java中二维数组内存地址详解及数组元素动态初始化_java 二维数组地址-程序员宅基地

Java中二维数组内存地址详解及数组元素动态初始化 int[][] arr = new int[3][3]; /* *[[I@10f87f48 * *@:分隔符 * 10f87f48:十六进制内存地址 *I:int 数组中存储的数据类型 * [[: 几个中括号就代表几维数组 * */ System.out.println(arr); /* 二维数组存储一维数组 的时候,存储的是一维数组的内存地址 *_java 二维数组地址

mysql2 gem,mysql2 gem安装失败-程序员宅基地

My stack is as follows:CentOS 6.3Installed MySQL 5.5 (followed steps at http://www.if-not-true-then-false.com/2010/install-mysql-on-fedora-centos-red-hat-rhel/ because yum still has MySQL 5.1 and if I..._er (mysql2::error) autolab | app 359 output: /usr/local/rvm/gems/ruby-2.6.8/

HDOJ-1052 Tian Ji -- The Horse Racing(贪心算法)-程序员宅基地

题目:HDOJ-1052题目描述:就是田忌赛马,但是要注意两边存在马速度相同的情况。分析: (看了各路大佬的题解Orz,自我总结一下)先排序,这里从快到慢排序;1.田忌最快马&amp;amp;amp;gt;王最快马(贪心,毫无疑问,进行比赛后获胜)2.田忌最快马&amp;amp;amp;lt;王最快马(同样的,既然赢不了,用最慢马去消耗掉最快马)3.田忌最快马=王最快马(要比较两者最慢马,因为没考虑这个WA了很多次…)a.田忌最...

nginx proxy_cache 批量清理脚本-程序员宅基地

#!/bin/bash#Email:[email protected]#Auto Clean Nginx Proxy_Cache Shell Scripts#Aunthor:sun~shell#Date:2017-02-23echo -e "\n\n"echo -n -e "\e[35;1m请输入Nginx Proxy_cache缓存的具体路径(友情提示:可以使用Tab补全功

随便推点

Joi.validate is a not function的解决办法_validate is not a function_Y_soybean_milk的博客-程序员宅基地

TypeError: Joi.validate is not a function的解决方法问题使用joi模块中的validate方法进行表单验证时,命令窗口报错TypeError: Joi.validate is not a function// 定义对象的验证规则const schema = { username: Joi.string().min(2).max(5) };async function run() { try { // 实施验证 awa_validate is not a function

python二分法编程_Python编程二分法实现冒泡算法+快速排序代码示例-程序员宅基地

本文分享的实例主要是Python编程二分法实现冒泡算法+快速排序,具体如下。冒泡算法:#-*- coding: UTF-8 -*-#冒泡排序def func(lt):if type(lt).__name__ !='list' and type(lt).__name__ !='tuple':returnif type(lt).__name__ == 'tuple':return list(lt)fo...

6-1图-基本概念_无向图的子图可以不连通吗-程序员宅基地

一.基础知识顶点集V边集E|E|边的条数|V|顶点个数,也称图的阶可以没有边,但不能没有顶点二.基本概念1.无向图(1)边没有箭头,这些边称为无向边(简称边)(2)每条边贡献两个度,所以|E|条边贡献了2|E|个度,故所有顶点的度之和=2|E|(3)(A,B)表示AB之间的边2.有向图(1)有箭头的边,这些边称为有向边(也称弧)。A→BA叫做弧尾,B叫做弧头(2)每条边贡献了一个出度和一个入度,故|E|条边贡献了|E|个出度,|E|个入度。故所有顶点的入度之和=所有顶点的出度_无向图的子图可以不连通吗

strict-origin-when-cross-origin 403 异常解决-程序员宅基地

我们注意到 这里是请求失败了,同时出现两次请求 一次为OPTIONS方法的请求,一次为引荐来源网址政策: strict-origin-when-cross-origin。说明由于OPTIONS请求失败,导致不能得到正确的响应结果。刚刚上线了一个服务,其他客户需要在跨域情况下对于服务进行调用,几次尝试之后,终于成功调用了。本文解决 nginx + spring boot + juery 情况下的跨域处理。正常情况下上面的配置即可 完成服务的跨域配置。

WIFI-程序员宅基地

WIFI是无线通信协议,可以允许手机直接连接到无线网络。在现在3G资费还比较贵的情况下,WIFI对于手机来说是很重要的,我们可以很方便的下载软件,音乐等资源。Android手机必须要有WIFI网卡才能支持WIFI。Android应用程序有时候需要对WIFI网卡进行操作,从而操作WIFI网络。WIFI网卡有一些状态,由一系列的×××常量来表示。...