$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
$ catkin_init_workspace
编译工作空间
$ cd ~/catkin_ws/
$ catkin_make
加载源文件
$ source ~/catkin_ws/devel/setup.bash
在.bashrc文件中加入命令
$ echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
为了确定源文件是否被加载,通过下列命令查询ROS环境正在使用的工作空间
$ echo $ROS_PACKAGE_PATH
对应输出应为:
home/username/catkin_ws/src:/opt/ros/noetic/share
URDF是具有特殊定义的XML格式的文件,专门用来对机器人组件级别进行抽象的模型描述。
在ros_robotics功能包目录生成一个/urdf目录:
$ cd ~/catkin_ws/src/robotics
$ mkdir urdf
$ cd urdf
机器人的第一个组成部分是底座盒子长宽高分别为0.5,0.5,0.25。
新建文件dd_robot.urdf保存在urdf文件夹下。
<
<robot name="dd_robot">
<link name="base_link">
<visual>
<origin xyz="0 0 0 " rpy="0 0 0" />
<geometry>
<box size="0.5 0.5 0.25" />
geometry>
visual>
link>
robot>
roslaunch:能够简化启动多个ROS节点的过程;简化参数服务器设置的过程
使用时需通过roscore命令来启动节点管理器
roslaunch语法结构
$ roslaunch <package_name> <file.launch>
在ros_robotics功能包里面新建文件夹lacunch,再新建ddrobot_rviz.launch文件
<launch>
<arg name="model"/>
<arg name="gui" default="False"/>
<param name="robot_description"
textfile="$(find ros_robotics)/urdf/$(arg model)"/>
<param name="use_gui" value="$(arg gui)"/>
<node name="joint_state_publisher"
pkg="joint_state_publisher"
type="joint_state_publisher"/>
<node name="robot_state_publisher"
pkg="robot_state_publisher"
type="robot_state_publisher"/>
<node name="rviz" pkg="rviz" type="rviz"
args="-d $(find ros_robotics)/urdf.rviz"
required="true"/>
launch>
输入以下命令查看机器人模型
$ roslaunch ros_robotics ddrobot_rviz.launch model:=dd_robot.urdf
如果出现下面错误,RLException: [ddrobot_rviz.launch] is neither a launch file in package [launch] nor is [launch] a launch file name
The traceback for the exception was written to the log file
解决方法:将devel/setup.bash添加到系统
source ./devel/setup.bash
在displays添加Robotmodel和TF,并将FIxed frame改为base_link。
继续在上一段代码加
<robot name="dd_robot">
<link name="base_link">
<visual>
<origin xyz="0 0 0 " rpy="0 0 0" />
<geometry>
<box size="0.5 0.5 0.25" />
geometry>
visual>
link>
<link name="right_wheel">
<visual>
<origin xyz="0 0 0" rpy="1.570795 0 0 " />
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
visual>
link>
<joint name="joint_right_wheel" type="continuous">
<parent link="base_link"/>
<child link="right_wheel"/>
<origin xyz="0 -0.30 0" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
joint>
<link name="left_wheel">
<visual>
<origin xyz="0 0 0" rpy="1.570795 0 0" />
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
visual>
link>
<joint name="joint_left_wheel" type="continuous">
<parent link="base_link"/>
<child link="left_wheel"/>
<origin xyz="0 0.30 0" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
joint>
robot>
运行
为了方便观看
还是在上面代码改
<robot name="dd_robot">
<link name="base_link">
<visual>
<origin xyz="0 0 0 " rpy="0 0 0" />
<geometry>
<box size="0.5 0.5 0.25" />
geometry>
<material name="blue">
<color rgba="0 0.5 1 1"/>
material>
visual>
link>
<link name="right_wheel">
<visual>
<origin xyz="0 0 0" rpy="1.570795 0 0 " />
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
<material name="black">
<color rgba="0.05 0.05 0.05 1"/>
material>
visual>
link>
<joint name="joint_right_wheel" type="continuous">
<parent link="base_link"/>
<child link="right_wheel"/>
<origin xyz="0 -0.30 0" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
joint>
<link name="left_wheel">
<visual>
<origin xyz="0 0 0" rpy="1.570795 0 0" />
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
<material name="black">
<color rgba="0.05 0.05 0.05 1"/>
material>
visual>
link>
<joint name="joint_left_wheel" type="continuous">
<parent link="base_link"/>
<child link="left_wheel"/>
<origin xyz="0 0.30 0" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
joint>
robot>
继续运行,我们的车出来了
添加小脚轮来使小车平衡
在base_link加代码
<visual name="caster">
<origin xyz="0.2 0 -0.125" rpy="0 0 0"/>
<geometry>
<sphere radius="0.05"/>
geometry>
visual>
添加碰撞属性来使小车检测和识别物体的边界
同样是在元素中加
完整代码
<robot name="dd_robot">
<link name="base_link">
<visual>
<origin xyz="0 0 0 " rpy="0 0 0" />
<geometry>
<box size="0.5 0.5 0.25" />
geometry>
<material name="blue">
<color rgba="0 0.5 1 1"/>
material>
visual>
<collision>
<origin xyz="0 0 0 " rpy="0 0 0 "/>
<geometry>
<box size="0.5 0.5 0.25"/>
geometry>
collision>
<visual name="caster">
<origin xyz="0.2 0 -0.125" rpy="0 0 0"/>
<geometry>
<sphere radius="0.05"/>
geometry>
visual>
<collision>
<origin xyz="0.2 0 -0.125" rpy="0 0 0 "/>
<geometry>
<sphere radius="0.05"/>
geometry>
collision>
link>
<link name="right_wheel">
<visual>
<origin xyz="0 0 0" rpy="1.570795 0 0 " />
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
<material name="black">
<color rgba="0.05 0.05 0.05 1"/>
material>
visual>
<collision>
<origin xyz="0 0 0" rpy="1.570795 0 0 "/>
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
collision>
link>
<joint name="joint_right_wheel" type="continuous">
<parent link="base_link"/>
<child link="right_wheel"/>
<origin xyz="0 -0.30 0" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
joint>
<link name="left_wheel">
<visual>
<origin xyz="0 0 0" rpy="1.570795 0 0" />
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
<material name="black">
<color rgba="0.05 0.05 0.05 1"/>
material>
visual>
<collision>
<origin xyz="0 0 0" rpy="1.570795 0 0 "/>
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
collision>
link>
<joint name="joint_left_wheel" type="continuous">
<parent link="base_link"/>
<child link="left_wheel"/>
<origin xyz="0 0.30 0" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
joint>
robot>
接下来,我们通过GUI弹窗来控制轮子的关节组件
将gui添加到命令字段,如下:
$ roslaunch ros_robotics ddrobot_rviz.launch model:=dd_robot.urdf gui:=True
如果发现打开Rviz,发现没有GUI窗口。采用下面这个方法
$ sudo apt-get install ros-noetic-joint-state-publisher-gui
物理属性:包括惯性
之后就可以在Gazebo引擎中启动了
需要在中添加
具体代码较长
<robot name="dd_robot">
<link name="base_link">
<visual>
<origin xyz="0 0 0 " rpy="0 0 0" />
<geometry>
<box size="0.5 0.5 0.25" />
geometry>
<material name="blue">
<color rgba="0 0.5 1 1"/>
material>
visual>
<collision>
<origin xyz="0 0 0 " rpy="0 0 0 "/>
<geometry>
<box size="0.5 0.5 0.25"/>
geometry>
collision>
<inertial>
<mass value="5"/>
<inertia ixx="0.13" ixy="0.0" ixz="0.0" iyy="0.21" iyz="0.0" izz="0.13"/>
inertial>
<visual name="caster">
<origin xyz="0.2 0 -0.125" rpy="0 0 0"/>
<geometry>
<sphere radius="0.05"/>
geometry>
visual>
<collision>
<origin xyz="0.2 0 -0.125" rpy="0 0 0 "/>
<geometry>
<sphere radius="0.05"/>
geometry>
collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.0001" ixy="0.0" ixz="0.0" iyy="0.0001" iyz="0.0" izz="0.0001"/>
inertial>
link>
<link name="right_wheel">
<visual>
<origin xyz="0 0 0" rpy="1.570795 0 0 " />
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
<material name="black">
<color rgba="0.05 0.05 0.05 1"/>
material>
visual>
<collision>
<origin xyz="0 0 0" rpy="1.570795 0 0 "/>
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.005" iyz="0.0" izz="0.005"/>
inertial>
link>
<joint name="joint_right_wheel" type="continuous">
<parent link="base_link"/>
<child link="right_wheel"/>
<origin xyz="0 -0.30 0" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
joint>
<link name="left_wheel">
<visual>
<origin xyz="0 0 0" rpy="1.570795 0 0" />
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
<material name="black">
<color rgba="0.05 0.05 0.05 1"/>
material>
visual>
<collision>
<origin xyz="0 0 0" rpy="1.570795 0 0 "/>
<geometry>
<cylinder length="0.1" radius="0.2"/>
geometry>
collision>
<inertial>
<mass value="0.5"/>
<inertia ixx="0.01" ixy="0.0" ixz="0.0" iyy="0.005" iyz="0.0" izz="0.005"/>
inertial>
link>
<joint name="joint_left_wheel" type="continuous">
<parent link="base_link"/>
<child link="left_wheel"/>
<origin xyz="0 0.30 0" rpy="0 0 0"/>
<axis xyz="0 1 0"/>
joint>
robot>
我们可以使用check_urdf工具来检查语法
$ check_urdf dd_robot.urdf
打开Gazebo
$ gazebo
$ roslaunch gazebo_ros empty_world.launch
<gazebo reference="base_link">
material>Gazebo/Bluematerial>
<pose>0 0 3 0 0 0pose>
gazebo>
验证代码是否有问题
$ gzsdf -p dd_robot.gazebo
如果这个有问题换下面的
$ gz sdf -p dd_robot.gazebo
使用XML生成ddrobot_gazebo.launch
<launch>
<include file="$(find gazebo_ros)/launch/empty_world.launch">
<arg name="world_name" value="$(find ros_robotics)/worlds/ddrobot.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"/>
include>
<node name="spawn_urdf" pkg="gazebo_ros" type="spawn_model" output="screen"
args="-file $(find ros_robotics)/urdf/dd_robot.gazebo -urdf -model ddrobot" />
launch>
在功能包里面新建文件夹worlds
新建文件ddrobot.world
<sdf version="1.4">
<world name="default">
<include>
<uri>model://ground_planeuri>
include>
<include>
<uri>model://sunuri>
include>
<include>
<uri>model://construction_coneuri>
<name>construction_conename>
<pose>-3.0 0 0 0 0 0pose>
include>
<include>
<uri>model://construction_coneuri>
<name>construction_conename>
<pose>3.0 0 0 0 0 0pose>
include>
world>
sdf>
参考文献:ROS机器人开发实用案例分析(第二版)