• gazebo中vins-fusion在仿真小车上的部署


     软件要求:Ubuntu 20.04  ros的noetic版本,我是在虚拟机vitrualbox上运行的

       这几天在学ROS,跟着赵虚左老师过了一遍之后,感觉还是有很多不懂的地方,xtdrone上仿真跟着文档走了一遍,好像没学到什么东西,所以我决定想办法自己搭建一个仿真平台,至少实现定位和路径规划的功能。 

       定位的话,这里我想用vins-fusion来做,奈何网上的资料太少了,完全不知道该从何下手,经过几天的查找资料,我目前算是解决了这个问题。

    VINS-Fusion的安装

       这里跟着这个教程走就行vins-fusion环境配置、安装与测试-CSDN博客,最后在数据集上测试通过代表安装完成,vins-fusion可以在自己的工作空间下面安装。

    Gazebo的搭建

       这里我们跟着赵虚左老师的视频【Autolabor初级教程】ROS机器人入门_哔哩哔哩_bilibili,最后会得到一个类似这样的机器人

     

    我们将原camera的位置往左移一点,然后在注释掉原carema,在原camera的位置加上IMU,它会发布名称叫imu/data的消息

    1. <robot name="my_sensors" xmlns:xacro="http://wiki.ros.org/xacro">
    2. <gazebo reference="camera">
    3. <material>Gazebo/Bule</material>
    4. <gravity>true</gravity>
    5. <sensor name="imu_sensor" type="imu">
    6. <always_on>true</always_on>
    7. <update_rate>100</update_rate>
    8. <visualize>true</visualize>
    9. <topic>__default_topic__</topic>
    10. <plugin filename="libgazebo_ros_imu_sensor.so" name="imu_plugin">
    11. <topicName>imu/data</topicName>
    12. <bodyName>imu_base</bodyName>
    13. <updateRateHZ>100.0</updateRateHZ>
    14. <gaussianNoise>0.01</gaussianNoise>
    15. <xyzOffset>0 0 0</xyzOffset>
    16. <rpyOffset>0 0 0</rpyOffset>
    17. <frameName>imu_base</frameName>
    18. </plugin>
    19. <pose>0 0 0 0 0 0</pose>
    20. </sensor>
    21. </gazebo>
    22. </robot>

    由于vins-fusion除了imu还需要双目相机,所以图上那个比较大的方块就是我加上的双目相机,代码如下:

    1. <?xml version="1.0"?>
    2. <!-- 摄像头相关的 xacro 文件 -->
    3. <robot xmlns:xacro="http://wiki.ros.org/xacro">
    4. <!-- 摄像头属性 -->
    5. <xacro:property name="camera_length" value="0.025" /> <!-- 摄像头长度(x) -->
    6. <xacro:property name="camera_width" value="0.04" /> <!-- 摄像头宽度(y) -->
    7. <xacro:property name="camera_height" value="0.04" /> <!-- 摄像头高度(z) -->
    8. <xacro:property name="camera_x" value="0.06" /> <!-- 摄像头安装的x坐标 -->
    9. <xacro:property name="camera_y" value="0.06" /> <!-- 摄像头安装的y坐标 -->
    10. <xacro:property name="camera_z" value="${base_link_length / 2 + camera_height / 2}" /> <!-- 摄像头安装的z坐标:底盘高度 / 2 + 摄像头高度 / 2 -->
    11. <xacro:property name="camera_m" value="0.01" /> <!-- 摄像头质量 -->
    12. <!-- 摄像头关节以及link -->
    13. <link name="double_camera">
    14. <visual>
    15. <geometry>
    16. <box size="${camera_length} ${camera_width} ${camera_height}" />
    17. </geometry>
    18. <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
    19. <material name="black" />
    20. </visual>
    21. <collision>
    22. <geometry>
    23. <box size="${camera_length} ${camera_width} ${camera_height}" />
    24. </geometry>
    25. <origin xyz="0.0 0.0 0.0" rpy="0.0 0.0 0.0" />
    26. </collision>
    27. <xacro:Box_inertial_matrix m="${camera_m}" l="${camera_length}" w="${camera_width}" h="${camera_height}" />
    28. </link>
    29. <joint name="double_camera2base_link" type="fixed">
    30. <parent link="base_link" />
    31. <child link="double_camera" />
    32. <origin xyz="${camera_x} ${camera_y} ${camera_z}" />
    33. </joint>
    34. <!-- camera left joints and links -->
    35. <joint name="left_joint" type="fixed">
    36. <origin xyz="0 0 0" rpy="0 0 0" />
    37. <parent link="double_camera" />
    38. <child link="stereo_left_frame" />
    39. </joint>
    40. <link name="stereo_left_frame"/>
    41. <joint name="left_optical_joint" type="fixed">
    42. <origin xyz="0 0 0" rpy="${-pi/2} 0 ${-pi/2}" />
    43. <parent link="stereo_left_frame" />
    44. <child link="stereo_left_optical_frame" />
    45. </joint>
    46. <link name="stereo_left_optical_frame"/>
    47. <!-- camera right joints and links -->
    48. <joint name="right_joint" type="fixed">
    49. <origin xyz="0 -0.07 0" rpy="0 0 0" />
    50. <parent link="double_camera" />
    51. <child link="stereo_right_frame" />
    52. </joint>
    53. <link name="stereo_right_frame"/>
    54. <joint name="right_optical_joint" type="fixed">
    55. <origin xyz="0 0 0" rpy="${-pi/2} 0 ${-pi/2}" />
    56. <parent link="stereo_right_frame" />
    57. <child link="stereo_right_optical_frame" />
    58. </joint>
    59. <link name="stereo_right_optical_frame"/>
    60. <!-- stereo camera -->
    61. <gazebo reference="double_camera">
    62. <sensor type="multicamera" name="stereocamera">
    63. <material>Gazebo/Blue</material>
    64. <always_on>true</always_on>
    65. <update_rate>30</update_rate>
    66. <visualize>1</visualize>
    67. <camera name="left">
    68. <pose>0 0 0 0 0 0</pose>
    69. <horizontal_fov>1.047</horizontal_fov>
    70. <image>
    71. <width>640</width>
    72. <height>360</height>
    73. <!-- format>L_UINT8</format -->
    74. <format>R8G8B8</format>
    75. </image>
    76. <clip>
    77. <near>0.1</near>
    78. <far>100</far>
    79. </clip>
    80. </camera>
    81. <camera name="right">
    82. <pose>0 -0.07 0 0 0 0</pose>
    83. <horizontal_fov>1.047</horizontal_fov>
    84. <image>
    85. <width>640</width>
    86. <height>360</height>
    87. <!-- format>L_UINT8</format -->
    88. <format>R8G8B8</format>
    89. </image>
    90. <clip>
    91. <near>0.1</near>
    92. <far>100</far>
    93. </clip>
    94. </camera>
    95. <plugin name="stereo_camera_controller" filename="libgazebo_ros_multicamera.so">
    96. <cameraName>stereocamera</cameraName>
    97. <alwaysOn>true</alwaysOn>
    98. <updateRate>30</updateRate>
    99. <cameraName>stereocamera</cameraName>
    100. <imageTopicName>image_raw</imageTopicName>
    101. <cameraInfoTopicName>camera_info</cameraInfoTopicName>
    102. <frameName>camera_link_optical</frameName>
    103. <baseline>0.07</baseline>
    104. <distortion_k1>0.0</distortion_k1>
    105. <distortion_k2>0.0</distortion_k2>
    106. <distortion_k3>0.0</distortion_k3>
    107. <distortion_t1>0.0</distortion_t1>
    108. <distortion_t2>0.0</distortion_t2>
    109. </plugin>
    110. </sensor>
    111. </gazebo>
    112. </robot>

    OK,现在我们已经安装好了IMU和双目相机。

    VINS-Fusion参数更改

    在/VINS-Fusion/config/vi_car/vi_car.yaml(因为我们用的是小车,无人机去改config里面另外的文件),我们需要修改它的imu和image的topic,从这个

    1. imu_topic: "/imu0"
    2. image0_topic: "/cam0/image_raw"
    3. image1_topic: "/cam1/image_raw"
    4. output_path: "/home/tong/output/"

    改成你自己的topic

    1. imu_topic: "/imu/data"
    2. image0_topic: "/stereocamera/right/image_raw"
    3. image1_topic: "/stereocamera/left/image_raw"
    4. output_path: "/home/tong/output/"

    OK,现在启动VINS-Fusion(记得rosrun你刚才修改的vi_car文件)

    1. roslaunch vins vins_rviz.launch
    2. rosrun vins vins_node ~/你自己的workspace/src/VINS-Fusion/config/vi_car/vi_car.yaml

    可以看到目前是已经跑通的状态,但是现在移动小车就会发现,轨迹很不准确,这是因为相机的内参和外参还没改 。

    相机的内参和外参更改

    在我们刚才修改的vi_car.yaml文件中,我们会找到这个:

    1. cam0_calib: "cam0_mei.yaml"
    2. cam1_calib: "cam1_mei.yaml"

    说明我们要修改这两个文件,这两个文件正好就在vi_car.yaml的目录下。

    打开仿真环境,使用rostopic list

    可以看到有这些

    通过 /stereocamera/left/camera_info 和 /stereocamera/right/camera_info 话题查看相机内参, 由于是仿真环境, 所以左右目外参理论上是一样的 

     通过这个内参更改那两个文件,这是这个文件的含义:

    1. #######################################################################
    2. # Calibration Parameters #
    3. #######################################################################
    4. # These are fixed during camera calibration. Their values will be the #
    5. # same in all messages until the camera is recalibrated. Note that #
    6. # self-calibrating systems may "recalibrate" frequently. #
    7. # #
    8. # The internal parameters can be used to warp a raw (distorted) image #
    9. # to: #
    10. # 1. An undistorted image (requires D and K) #
    11. # 2. A rectified image (requires D, K, R) #
    12. # The projection matrix P projects 3D points into the rectified image.#
    13. #######################################################################
    14. # The image dimensions with which the camera was calibrated. Normally
    15. # this will be the full camera resolution in pixels.
    16. # 高 ,单位:像素
    17. uint32 height
    18. # 宽 ,单位:像素
    19. uint32 width
    20. # The distortion parameters, size depending on the distortion model.
    21. # For "plumb_bob", the 5 parameters are: (k1, k2, t1, t2, k3).
    22. # 畸变参数
    23. float64[] D
    24. # Intrinsic camera matrix for the raw (distorted) images.
    25. # 未做去畸变处理图像的内参
    26. # [fx 0 cx]
    27. # K = [ 0 fy cy]
    28. # [ 0 0 1]
    29. # Projects 3D points in the camera coordinate frame to 2D pixel
    30. # coordinates using the focal lengths (fx, fy) and principal point
    31. # (cx, cy).
    32. float64[9] K # 3x3 row-major matrix
    33. # Rectification matrix (stereo cameras only)
    34. # 仅用于立体相机,通常是多目相机
    35. # 用于极线对齐
    36. # A rotation matrix aligning the camera coordinate system to the ideal
    37. # stereo image plane so that epipolar lines in both stereo images are
    38. # parallel.
    39. float64[9] R # 3x3 row-major matrix
    40. # Projection/camera matrix
    41. # 投影矩阵:去畸变,修正后世界坐标系下的三维坐标点投影到像素坐标系下的二维点
    42. # [fx' 0 cx' Tx]
    43. # P = [ 0 fy' cy' Ty]
    44. # [ 0 0 1 0]
    45. # By convention, this matrix specifies the intrinsic (camera) matrix
    46. # of the processed (rectified) image. That is, the left 3x3 portion
    47. # is the normal camera intrinsic matrix for the rectified image.
    48. # It projects 3D points in the camera coordinate frame to 2D pixel
    49. # coordinates using the focal lengths (fx', fy') and principal point
    50. # (cx', cy') - these may differ from the values in K.
    51. # 单目相机,tx=ty=0
    52. # For monocular cameras, Tx = Ty = 0. Normally, monocular cameras will
    53. # also have R = the identity and P[1:3,1:3] = K.
    54. # 双目相机
    55. # For a stereo pair, the fourth column [Tx Ty 0]' is related to the
    56. # position of the optical center of the second camera in the first
    57. # camera's frame. We assume Tz = 0 so both cameras are in the same
    58. # stereo image plane. The first camera always has Tx = Ty = 0. For
    59. # the right (second) camera of a horizontal stereo pair, Ty = 0 and
    60. # Tx = -fx' * B, where B is the baseline between the cameras.
    61. # Given a 3D point [X Y Z]', the projection (x, y) of the point onto
    62. # the rectified image is given by:
    63. # [u v w]' = P * [X Y Z 1]'
    64. # x = u / w
    65. # y = v / w
    66. # This holds for both images of a stereo pair.
    67. float64[12] P # 3x4 row-major matrix

    外参的通过ROS的TF坐标变换来获取,打开rviz,添加tf,如图所示

    通过以下命令查看左, 右目相机与IMU的外参

     这里我参考gazebo仿真跑VINS-Fusion双目视觉惯性SLAM_gazebo双目相机xzcro-CSDN博客,之所以用camera,是因为我当时把imu放到原camera的位置了,我也没改名字,外参的话,Translation的偏移量,下面第一个Q是四元数,可以搜一下如何使用四元数得到相机的外参矩阵,在vi_car中修改,这样的话,我们的VINS-Fusion算是彻底跑通了。

  • 相关阅读:
    去掉表格里某一列单元格的所有后缀
    【Azure Developer - 密钥保管库 】使用 Python Azure SDK 实现从 Azure Key Vault Certificate 中下载证书(PEM文件)
    【python】python面向对象——类的使用
    leetcode 1207. Unique Number of Occurrences(出现次数的唯一性)
    Flir Blackfly S USB3 工业相机:计数器和定时器的使用方法
    Doris单机安装
    PHP基础学习第十九篇(了解MySQL数据库、MySQL的连接和创建数据库、MySQL创建数据表)
    golang程序能不能用LD_PRELOAD进行hook?
    Python常用基础语法知识点大全
    【csapp lab】lab2_bomblab
  • 原文地址:https://blog.csdn.net/MOMO_114514/article/details/137399597