文章目录
运动控制实现流程
roslaunch urdf02_gazebo demo03_env.launch
启动gazebo仿真环境 demo03_env.launch
launch文件
<launch>
<!-- 1. 需要再参数服务器中载入 urdf -->
<param name = "robot_description" command = "$(find xacro)/xacro $(find urdf02_gazebo)/urdf/car.urdf.xacro" />
<!-- 2. 启动Gazebo仿真环境 roslaunch gazebo_ros empty_world.launch-->
<include file = "$(find gazebo_ros)/launch/empty_world.launch">
<arg name = "world_name" value = "$(find urdf02_gazebo)/worlds/box_house.world" />
</include>
<!-- 3. 在Gazebo中添加机器人模型 -->
<!-- rosrun gazebo_ros spawn_model -urdf -model mycar -param robot_description , 这里mycar是robot name-->
<node pkg = "gazebo_ros" type = "spawn_model" name = "spawn_model" args = "-urdf -model mycar -param robot_description"/>
</launch>
添加传动装置及控制器 move.xacro
<robot name="my_car_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="left2link" />
<xacro:joint_trans joint_name="right2link" />
<!-- 控制器 -->
<gazebo>
<plugin name="differential_drive_controller" filename="libgazebo_ros_diff_drive.so">
<rosDebugLevel>Debug</rosDebugLevel>
<publishWheelTF>true</publishWheelTF>
<robotNamespace>/</robotNamespace>
<publishTf>1</publishTf>
<publishWheelJointState>true</publishWheelJointState>
<alwaysOn>true</alwaysOn>
<updateRate>100.0</updateRate>
<legacyMode>true</legacyMode>
<leftJoint>left2ink</leftJoint> <!-- 左轮 -->
<rightJoint>right2link</rightJoint> <!-- 右轮 -->
<wheelSeparation>${base_radius * 2}</wheelSeparation> <!-- 车轮间距 -->
<wheelDiameter>${wheel_radius * 2}</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>
添加xacro集成文件 car.urdf.xacro
<robot name="mycar" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 包含惯性矩阵文件 -->
<xacro:include filename = "head.xacro" />
<!-- 包含底盘、摄像头、雷达的xacro文件 -->
<xacro:include filename = "demo02_car_base.urdf.xacro" />
<xacro:include filename = "demo03_car_camera.urdf.xacro" />
<xacro:include filename = "demo04_car_laser.urdf.xacro" />
<!-- 运动控制 -->
<xacro:include filename = "gazebo/move.xacro" />
</robot>
Rviz 查看里程计消息
里程计: 机器人相对出发点坐标系的位姿状态(X 坐标 Y 坐标 Z坐标以及朝向)
启动Rviz demo04_sensor.launch
<launch>
<!-- 启动rviz -->
<node pkg = "rviz" type = "rviz" name = "rviz" args = "-d $(find urdf01_rviz)/config/show_mycar.rviz"/>
<!-- 添加关节状态发布节点 -->
<node pkg = "joint_state_publisher" type = "joint_state_publisher" name = "joint_state_publisher" />
<!-- 添加机器人状态发布节点 -->
<node pkg = "robot_state_publisher" type = "robot_state_publisher" name = "robot_state_publisher" />
</launch>
source ./devel/setup.bash
roslaunch urdf02_gazebo demo03_env.launch
roslaunch urdf02_gazebo demo04_sensor.launch
添加组件
雷达 laser.xacro
<robot name="my_sensors" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 雷达 -->
<gazebo reference="laser"><!-- reference 名字要与demo04_car_laser里面雷达名字相同-->
<sensor type="ray" name="rplidar">
<pose>0 0 0 0 0 0</pose><!-- x y z 欧拉角 -->
<visualize>true</visualize>
<update_rate>5.5</update_rate>
<ray>
<scan>
<horizontal>
<samples>360</samples> <!--雷达旋转一周采集360个点-->
<resolution>1</resolution><!--每一个线会有一根测距,2表示每两根线会有一根测距-->>
<min_angle>-3</min_angle><!-- 单位是弧度 向右转3个弧度 顺时针-->
<max_angle>3</max_angle>
</horizontal>
</scan>
<range>
<min>0.10</min><!--雷达采样距离-->
<max>30.0</max>
<resolution>0.01</resolution><!--精确度-->
</range>
<noise>
<type>gaussian</type><!--高斯噪音 用于仿真 模拟误差 -->
<mean>0.0</mean>
<stddev>0.01</stddev>
</noise>
</ray>
<plugin name="gazebo_rplidar" filename="libgazebo_ros_laser.so">
<topicName>/scan</topicName>
<frameName>laser</frameName><!--名字要与demo04_car_laser里面雷达名字相同-->
</plugin>
</sensor>
</gazebo>
</robot>
摄像头 camera.xacro
<robot name="my_sensors" xmlns:xacro="http://wiki.ros.org/xacro">
<!-- 被引用的link -->
<gazebo reference="camera">
<!-- 类型设置为 camara -->
<sensor type="camera" name="camera_node">
<update_rate>30.0</update_rate> <!-- 更新频率 -->
<!-- 摄像头基本信息设置 -->
<camera name="head">
<horizontal_fov>1.3962634</horizontal_fov>
<image>
<width>1280</width>
<height>720</height>
<format>R8G8B8</format>
</image>
<clip>
<near>0.02</near>
<far>300</far>
</clip>
<noise>
<type>gaussian</type>
<mean>0.0</mean>
<stddev>0.007</stddev>
</noise>
</camera>
<!-- 核心插件 -->
<plugin name="gazebo_camera" filename="libgazebo_ros_camera.so">
<alwaysOn>true</alwaysOn>
<updateRate>0.0</updateRate>
<cameraName>/camera</cameraName>
<imageTopicName>image_raw</imageTopicName>
<cameraInfoTopicName>camera_info</cameraInfoTopicName>
<frameName>camera</frameName>
<hackBaseline>0.07</hackBaseline>
<distortionK1>0.0</distortionK1>
<distortionK2>0.0</distortionK2>
<distortionK3>0.0</distortionK3>
<distortionT1>0.0</distortionT1>
<distortionT2>0.0</distortionT2>
</plugin>
</sensor>
</gazebo>
</robot>
rostopic pub -r 10 /cmd_vel 设置参数让机器人转动