文章目录
- ROS2仿真安装及基本测试
- Ignition安装:
- Ignition测试:
- 使用键盘控制小车:
- Rviz中可视化雷达数据:
- 麦克纳姆轮机器人仿真:
- 模型文件编写及启动
- 过程中遇到的问题:
- 1. 报错:`Expected exactly one top-level <model>, <light> or <actor> on SDF`
- 2. 在命令行发布速度,小车不动。
ROS2仿真安装及基本测试
官方教程:https://docs.ros.org/en/humble/Tutorials/Advanced/Simulators/Ignition.html
Ignition安装:
有新版本了兄弟们,适用于Ubuntu20/22
官方地址:https://gazebosim.org/docs/garden
Iginition官方地址:[https://gazebosim.org/docs/fortress] (https://gazebosim.org/docs/fortress)
(虽然fortress上面写的只支持bionic/focal,但我试了下在ubuntu22上也能用,暂时没出现问题。)
新的流程:
sudo apt-get update
sudo apt-get install lsb-release wget gnupg
sudo wget https://packages.osrfoundation.org/gazebo.gpg -O /usr/share/keyrings/pkgs-osrf-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/pkgs-osrf-archive-keyring.gpg] http://packages.osrfoundation.org/gazebo/ubuntu-stable $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/gazebo-stable.list > /dev/null
sudo apt-get update
sudo apt-get install gz-garden
Ignition测试:
ign gazebo -v 4 -r visualize_lidar.sdf
分别运行以下命令:
ros2 topic list
ign topic -l
可以发现ros2 topic不显示,这是因为ros和ignition之间需要消息连接
安装并使用ros-ign-bridge:
sudo apt-get install ros-humble-ros-ign-bridge
ros2 run ros_ign_bridge parameter_bridge /model/vehicle_blue/cmd_vel@geometry_msgs/msg/Twist]ignition.msgs.Twist
ros2 topic pub /model/vehicle_blue/cmd_vel geometry_msgs/Twist "linear: { x: 0.1 }"
想要让小车停下来ctrl c似乎没用,重新发布一个0的速度就可以了。
使用键盘控制小车:
sudo apt-get install ros-humble-teleop-twist-keyboard
由于默认的键盘发布话题为/cmd_vel,而仿真小车订阅的话题为.model/ehicle_blue/cmd_vel,需要重新映射,最终运行命令如下:
ros2 run teleop_twist_keyboard teleop_twist_keyboard --ros-args -r /cmd_vel:=/model/vehicle_blue/cmd_vel
关于ROS2命令行传参数可以参考:https://zhuanlan.zhihu.com/p/354479100
之后就可以键盘控制,可以看到小车是有惯性和碰撞的。
Rviz中可视化雷达数据:
ros2 run ros_ign_bridge parameter_bridge /lidar2@sensor_msgs/msg/LaserScan[ignition.msgs.LaserScan --ros-args -r /lidar2:=/laser_scan
rviz2
更改Global Options为:vehicle_blue/lidar_link/gpu_lidar
之后点击Add添加laser_scan,觉得不明显的话可以把Size改大一点。
麦克纳姆轮机器人仿真:
麦克纳姆轮的建模并不容易,官方的教程使用了圆球来替代所有类型的轮子,但我也找到一份开源的麦克纳姆轮机器人的文档,先克隆到本地备用。
模型文件编写及启动
git clone https://github.com/joshvillbrandt/mecanumbot-ros-pkg
下面新建一个工作空间来进行仿真工作
mkdir -p sim2_ws/src
cd sim2_ws/src
ros2 pkg create mecanumbot
将上面克隆的文件夹中的description移动进刚创建的文件夹后编译
cd ..
colcon build
这里按照之前的流程应该在bashrc中添加bash,但这次需要添加的是local_setup.bash,理由参考:https://www.guyuehome.com/38443
gedit ~/.bashrc
作如下修改:
#source ~/ros2_ws/install/setup.bash
source ~/sim2_ws/install/local_setup.bash
#export _colcon_cd_root=~/ros2_ws
export _colcon_cd_root=~/sim2_ws
使用命令行查看小车模型:
ign service -s /world/empty/create --reqtype ignition.msgs.EntityFactory --reptype ignition.msgs.Boolean --timeout 1000 --req 'sdf_filename: "/home/b408/sim2_ws/src/mecanumbot/description/mecanumbot.urdf", name: "urdf_model"'
可以把小车的urdf文件转换为sdf:
ign sdf -p mecanumbot.urdf > mecanum.sdf
创建launch文件来启动仿真:
colcon_cd mecanum
mkdir launch
cd launch
touch demo.launch.py
填入内容:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, IncludeLaunchDescription, ExecuteProcess
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch_ros.actions import Node
from launch.substitutions import LaunchConfigurationdef generate_launch_description():ld = LaunchDescription()pkg_ros_ign = get_package_share_directory('ros_ign_gazebo')pkg_mecanumbot = get_package_share_directory('mecanumbot')sdf = DeclareLaunchArgument('sdf_file', default_value=os.path.join(pkg_mecanumbot, 'description', 'mecanum_drive.sdf'))gazebo = IncludeLaunchDescription(PythonLaunchDescriptionSource(os.path.join(pkg_ros_ign, 'launch', 'ign_gazebo.launch.py')), launch_arguments={'ign_args': '-r '+os.path.join(pkg_mecanumbot, 'description', 'mecanum_drive.sdf')}.items(),)bridge = Node(package='ros_ign_bridge', executable='parameter_bridge', arguments=['/model/mecanumbot/cmd_vel@geometry_msgs/msg/Twist@ignition.msgs.Twist'])ld.add_action(gazebo)ld.add_action(bridge)return ld
下面再创建一个小车的sdf文件,包含了地板、光照、小车模型
创建mecanum_drive.sdf,并填入以下内容:
<?xml version="1.0" ?>
<sdf version="1.6"><world name="mecanum_drive"><physics name="1ms" type="ignored"><max_step_size>0.001</max_step_size><real_time_factor>1.0</real_time_factor></physics><pluginfilename="ignition-gazebo-physics-system"name="ignition::gazebo::systems::Physics"></plugin><pluginfilename="ignition-gazebo-user-commands-system"name="ignition::gazebo::systems::UserCommands"></plugin><pluginfilename="ignition-gazebo-scene-broadcaster-system"name="ignition::gazebo::systems::SceneBroadcaster"></plugin><light type="directional" name="sun"><cast_shadows>true</cast_shadows><pose>0 0 10 0 0 0</pose><diffuse>1 1 1 1</diffuse><specular>0.5 0.5 0.5 1</specular><attenuation><range>1000</range><constant>0.9</constant><linear>0.01</linear><quadratic>0.001</quadratic></attenuation><direction>-0.5 0.1 -0.9</direction></light><model name="ground_plane"><static>true</static><link name="link"><collision name="collision"><geometry><plane><normal>0 0 1</normal><size>100 100</size></plane></geometry><surface><friction><ode><mu>50</mu></ode></friction></surface></collision><visual name="visual"><geometry><plane><normal>0 0 1</normal><size>100 100</size></plane></geometry><material><ambient>0.8 0.8 0.8 1</ambient><diffuse>0.8 0.8 0.8 1</diffuse><specular>0.8 0.8 0.8 1</specular></material></visual></link></model><include><uri>/home/b408/sim2_ws/src/mecanumbot/description/mecanum.sdf</uri></include></world>
</sdf>
修改CMakeLists.txt:
install(DIRECTORYlaunchdescriptionDESTINATION share/${PROJECT_NAME}
)
但是小车一直在抖动,不知道是自动转换的sdf文件错误还是因为模型本身就有问题。
过程中遇到的问题:
1. 报错:Expected exactly one top-level <model>, <light> or <actor> on SDF
原因:不能启动两个sdf,如果想要启动多个sdf,可以像前面的sdf文件中那样使用
2. 在命令行发布速度,小车不动。
原因:pose设置错误,卡在地面里了
修改mevanum.sdf中的pose,把z轴高度提高一点:
<sdf version='1.9'><model name='mecanumbot'><pose>0 0 0.14 0 0 0</pose>
在ignition官网的community查找到了一个麦克纳姆轮的例程:
https://github.com/gazebosim/gz-sim/blob/ign-gazebo6/examples/worlds/mecanum_drive.sdf
可以参照这个来编写自己的sdf文件。想控制小车需要添加插件,如例程中展示的:
插件的具体内容可以参照:
https://github.com/gazebosim/gz-sim/blob/270da60b9387d3751d8d65eafd60f6459b450b00/src/systems/mecanum_drive/MecanumDrive.cc