1、gazebo闪退黑屏
gazebo闪退黑屏有可能是model包不全
两种方法:
第一种:
cd ~/.gazebo/
mkdir -p models
cd ~/.gazebo/models/
wget http://file.ncnynl.com/ros/gazebo_models.txt
#输出:
gazebo_models.txt
#继续运行:
wget -i gazebo_models.txt
ls model.tar.g* | xargs -n1 tar xzvf
我这边的下载速度非常非常非常慢,大概下了一个多小时才一半左右,单纯网络问题。
第二种:
去gitee或者GitHub clone一下
git clone https://gitee.com/l1yb/gazebo_models.git
注意:建议文件名和子目录依然沿用第一个方法的名称
快速结束gazebo
运行ros节点执行ctrl+c后进程会转而执行rosspin()后面的程序,但是如果在一定时间内程序没有执行完毕,进程会强制退出,并抛出”escalating to SIGTERM“错误,为了解决此问题,可以修改ros配置文件
sudo gedit /opt/ros/melodic/lib/python2.7/dist-packages/roslaunch/nodeprocess.py
将该文件中的_TIMEOUT_SIGINT = 15.0 #seconds”中的15秒改为期望运行的最大时间。
2、xacro 实现gazebo
在xacro文件中,需要添加两个标签<collision>和<inertial>
2.1 <collision>标签是碰撞属性
如果机器人link是标准的几何体形状,和link的 visual 属性设置一致即可。
2.2 <inertial>标签是惯性矩阵
惯性矩阵的设置需要结合link的质量与外形参数动态生成,标准的球体、圆柱与立方体的惯性矩阵公式如下(已经封装为 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>
可以写在collision便标签之后,例如:
<xacro:cylinder_inertial_matrix m="${wheel_m}" r="${wheel_radius}" h="${wheel_length}" />
需要注意的是,原则上,除了 base_footprint 外,机器人的每个刚体部分都需要设置惯性矩阵,且惯性矩阵必须经计算得出,如果随意定义刚体部分的惯性矩阵,那么可能会导致机器人在 Gazebo 中出现抖动,移动等现象。
2.3<gazebo>颜色设置
在 gazebo 中显示 link 的颜色,必须要使用指定的标签:
<gazebo reference="link节点名称">
<material>Gazebo/Blue</material>
</gazebo>
PS:material 标签中,设置的值区分大小写,颜色可以设置为 Red Blue Green Black .....
3、集成launch文件
launch 文件实现:
<launch>
<!-- 将 Urdf 文件的内容加载到参数服务器 -->
<param name="robot_description" textfile="$(find demo02_urdf_gazebo)/urdf/urdf01_helloworld.urdf" />
<!-- 启动 gazebo -->
<include file="$(find gazebo_ros)/launch/empty_world.launch" />
<!-- 在 gazebo 中显示机器人模型 -->
<node pkg="gazebo_ros" type="spawn_model" name="model" args="-urdf -model mycar -param robot_description" />
</launch>
代码解释:
<include file="$(find gazebo_ros)/launch/empty_world.launch" />
<!-- 启动 Gazebo 的仿真环境,当前环境为空环境 -->
<node pkg="gazebo_ros" type="spawn_model" name="model" args="-urdf -model mycar -param robot_description" />
<!--
在 Gazebo 中加载一个机器人模型,该功能由 gazebo_ros 下的 spawn_model 提供:
-urdf 加载的是 urdf 文件
-model mycar 模型名称是 mycar
-param robot_description 从参数 robot_description 中载入模型
-x 模型载入的 x 坐标
-y 模型载入的 y 坐标
-z 模型载入的 z 坐标
-->
然后运行时出现:
按照终端提示,找到文件
sudo gedit /usr/lib/python2.7/xml/etree/ElementTree.py
在import sys下边添加(没有就写一个)
reload(sys)
sys.setdefaultencoding("utf-8")
4、仿真环境搭建
4.1 launch文件
<launch>
<!-- 将 Urdf 文件的内容加载到参数服务器 -->
<param name="robot_description" command="$(find xacro)/xacro $(find demo02_urdf_gazebo)/urdf/xacro/my_base_camera_laser.urdf.xacro" />
<!-- 启动 gazebo -->
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(find demo02_urdf_gazebo)/worlds/hello.world" />
</include>
<!-- 在 gazebo 中显示机器人模型 -->
<node pkg="gazebo_ros" type="spawn_model" name="model" args="-urdf -model mycar -param robot_description" />
</launch>
核心代码: 启动 empty_world 后,再根据arg
加载自定义的仿真环境
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(find demo02_urdf_gazebo)/worlds/hello.world" />
</include>
4.2 自己构建仿真环境
4.2.1 insert插入模型库
在第一部分安装的模型库地址添加到模型库里即可。
(卡的一批,等我用双系统再试一下,虚拟机死机了)
4.2.2自己绘制-------building editor
小结:今天记录了解决gazebo闪退黑屏的问题,还有gazebo如何实现xacro文件,以及他的launch文件的编写,搭建了仿真环境,试图自己搭建环境的时候虚拟机卡死了,小车还没有动起来,溜了溜了。明天见