我们在第三章和第四章中详细介绍了如何使用URDF以及Navigation 2,而第五章开始我们将学习如何将前面所学的结合起来,来形成一个Unity与ROS完整且系统的框架
这一部分作为我们第三讲的内容,我们在之前的基础上通过使用ROS2命令操作URDF模型增加激光传感器。具体的代码如下:toio_style.urdf
这就构成了我们最基础的二轮激光雷达小车的结构,通过运行,来将urdf模型在rviz2中打开
ros2 launch urdf_tutorial display.launch.py model:=toio_style.urdf
然后按照第三章讲到的导入到的几个步骤
docker run -v ~/ros2_ws:/home/ubuntu/colcon_ws:cached -p 6080:80 -p 10000:10000 -p 5005:5005 --shm-size=1024m tiryoh/ros2-desktop-vnc:galactic
cd ~/colcon_ws/src
git clone -b main-ros2 https://github.com/Unity-Technologies/ROS-TCP-Endpoint
~/colcon_ws/src
。并安装nav2相关的安装包以及turtlebot3的安装包sudo apt install ros-foxy-navigation2
sudo apt install ros-foxy-nav2-bringup
sudo apt install ros-foxy-turtlebot3*
source ~/.bashrc
~/.bashrc
中export TURTLEBOT3_MODEL=waffle
export GAZEBO_MODEL_PATH=$GAZEBO_MODEL_PATH:/opt/ros/foxy/share/turtlebot3_gazebo/models
source ~/.bashrc
cd ~/colcon_ws
colcon build
source ~/colcon_ws/install/setup.bash
https://github.com/Unity-Technologies/ROS-TCP-Connector.git?path=/com.unity.robotics.ros-tcp-connectorhttps://github.com/Unity-Technologies/URDF-Importer.git?path=/com.unity.robotics.urdf-importerhttps://github.com/Unity-Technologies/ROS-TCP-Connector.git?path=/com.unity.robotics.visualizations
・AGVController.cs
・TimeStamp.cs
・Clock.cs
・ROSClockPublisher.cs
・TransformExtensions.cs
・TransformTreeNode.cs
・ROSTransformTreePublisher.cs
・LaserScanSensor.cs
在配置完上述的内容后就可以导入URDF文件了,在Unity的Assets中导入toio_style.urdf。并选择“Import Robot Select form URDF file”。
在Hierarchy窗口中选择“toio_style→base_footprint→base_link→scan_base”
在Inspector窗口中添加LaserScanSensor脚本,设置如下
在ROS侧,运行启动Nav2、SLAM Toolbox和rviz2的launch文件
ros2 launch unity_slam_example unity_slam_example.py
具体代码如下
import os
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from ament_index_python.packages import get_package_share_directory
from launch_ros.actions import Nodedef generate_launch_description():package_name = 'unity_slam_example'package_dir = get_package_share_directory(package_name)return LaunchDescription({IncludeLaunchDescription(PythonLaunchDescriptionSource(os.path.join(get_package_share_directory('ros_tcp_endpoint'), 'launch', 'endpoint.py')),),Node(package='rviz2',executable='rviz2',output='screen',arguments=['-d', os.path.join(package_dir, 'nav2_unity.rviz')],parameters=[{'use_sim_time':True}]),IncludeLaunchDescription(PythonLaunchDescriptionSource(os.path.join(get_package_share_directory('nav2_bringup'), 'launch', 'navigation_launch.py')),launch_arguments={'use_sim_time': 'true'}.items()),IncludeLaunchDescription(PythonLaunchDescriptionSource(os.path.join(get_package_share_directory('slam_toolbox'), 'launch', 'online_async_launch.py')),launch_arguments={'use_sim_time': 'true'}.items())})
然后在Unity端,用“toio_style→AGVController”在“Mode”中指定“Keyboard”,用Play按钮执行