<robot name="robot_model_move" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 传动实现:用于连接控制器与关节 -->
<xacro:macro name="joint_trans" params="joint_name">
<!-- Transmission is important to link the joints and the controller -->
<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="base_l_wheel_joint" />
<xacro:joint_trans joint_name="base_r_wheel_joint" />
<!-- 控制器 -->
<gazebo>
<plugin name="differential_drive_controller" filename="libgazebo_ros_diff_drive.so">
<rosDebugLevel>Debug</rosDebugLevel>
<publishWheelTF>false</publishWheelTF>
<robotNamespace>/</robotNamespace>
<publishTf>1</publishTf>
<publishWheelJointState>false</publishWheelJointState>
<alwaysOn>true</alwaysOn>
<updateRate>100.0</updateRate>
<legacyMode>true</legacyMode>
<leftJoint>base_l_wheel_joint</leftJoint> <!-- 左轮 -->
<rightJoint>base_r_wheel_joint</rightJoint> <!-- 右轮 -->
<wheelSeparation>0.4869646685</wheelSeparation> <!-- 车轮间距 -->
<wheelDiameter>0.006</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>
小车双轮差速控制文件,需要注意的地方如下:
1.命令行输入rostopic list,查看目前的话题通信,看是否有“/cmd_vel”话题
2.开启gazebo并启动rviz时,rviz终端会一直产生一个警告:TF_REPEATED_DATA ignoring data with redundant timestamp for frame left_wheel at time 140.726000 according to authority unknown_publisher
[ WARN] [1657462827.907037520, 140.746000000]: TF_REPEATED_DATA ignoring data with redundant timestamp for frame left_wheel at time 140.726000 according to authority unknown_publisher
[ WARN] [1657462827.907093593, 140.746000000]: TF_REPEATED_DATA ignoring data with redundant timestamp for frame right_wheel at time 140.726000 according to authority unknown_publisher
[ WARN] [1657462827.907845999, 140.746000000]: TF_REPEATED_DATA ignoring data with redundant timestamp for frame left_wheel at time 140.746000 according to authority unknown_publisher
[ WARN] [1657462827.907878461, 140.746000000]: TF_REPEATED_DATA ignoring data with redundant timestamp for frame right_wheel at time 140.746000 according to authority unknown_publisher
在终端使用roswtf,可以看到如下问题:
报错提示/robot_state_publisher发布了左右轮到base_link的tf变换,但是/gazebo又发布了左右轮到base_footprint的tf变换,导致了二者的冲突。
将gazebo_control中的publishWheelTF和publishWheelJointState均设置为false就可以解决掉这个问题
3.小车在gazebo中不受控制
差速控制器中的小车车轮直径和车轮间距,一定要和核对好。
<gazebo>
<plugin name="differential_drive_controller" filename="libgazebo_ros_diff_drive.so">
<rosDebugLevel>Debug</rosDebugLevel>
<publishWheelTF>false</publishWheelTF>
<robotNamespace>/</robotNamespace>
<publishTf>1</publishTf>
<publishWheelJointState>false</publishWheelJointState>
<alwaysOn>true</alwaysOn>
<updateRate>100.0</updateRate>
<legacyMode>true</legacyMode>
<leftJoint>base_l_wheel_joint</leftJoint>
<rightJoint>base_r_wheel_joint</rightJoint>
<!-- <wheelSeparation>0.243482</wheelSeparation> -->
<wheelSeparation>0.427</wheelSeparation>车轮间距
<wheelDiameter>0.17</wheelDiameter>车轮直径
<broadcastTF>1</broadcastTF>
<wheelTorque>30</wheelTorque>
<wheelAcceleration>1.8</wheelAcceleration>
<commandTopic>cmd_vel</commandTopic>
<odometryFrame>odom</odometryFrame>
<odometryTopic>odom</odometryTopic>
<!-- <publishOdomTF>true</publishOdomTF>
<odometrySource>1</odometrySource> -->
<robotBaseFrame>base_footprint</robotBaseFrame>
</plugin>
</gazebo>
</robot>
# 该文件是控制器配置,一个机器人模型可能有多个控制器,比如: 底盘、机械臂、夹持器(机械手)....
# 因此,根 name 是 controller
controller_joint_names: ['', 'leftwheel_joint', 'rightwheel_joint', 'visual0_joint', 'visual1_joint', ]
controllers: {
# 单控制器设置
base_controller: {
#类型: 差速控制器
type: diff_controller,
#参考坐标
base_frame_id: base_link,
#两个轮子之间的间距
base_width: 0.427,
#控制频率
ticks_meter: 2000,
#PID控制参数,使机器人车轮快速达到预期速度
Kp: 12,
Kd: 12,
Ki: 0,
Ko: 50,
#加速限制
accel_limit: 1.0
}
}
4. 添加摩擦力和刚性系数
在进行小车的运动仿真过程中,前面支撑轮主要起支撑作用,可能轮子自带的属性不能转动或者我们将其使用fixed标签固定到了base_link上,所以它无法转动。
这样的后果是,在控制小车运动过程中固定的支撑轮会阻碍小车运动,造成里程计信息出错。
解决办法是把这个轮子的摩擦力设置为0,让它直接在地上滑动即可。
<gazebo reference="base_link">
<mu1 value="0.0"/>
<mu2 value="0.0"/>
<kp value="1000000.0" />
<kd value="10.0" />
</gazebo>
reference=“支撑轮所在的link”;其中mu1,mu2代表摩擦力,kp,kd代表刚性系数。