找回密码
 立即注册
首页 业界区 安全 ROS1(20.04 noetic) + PX4 + AirSim

ROS1(20.04 noetic) + PX4 + AirSim

愆蟠唉 昨天 13:51
博客地址:https://www.cnblogs.com/zylyehuo/
参考视频: 保姆级教程~手把手教你实现 ROS2 + PX4 + AirSim 联动
博主使用的版本配置如下:


  • ROS: 20.04 noetic 【echo $ROS_DISTRO】
  • PX4: v1.13.2 【git describe --tags】
  • AirSim: 1.7.0 【找到\Unreal\Plugins\AirSim这个目录,进入后,打开AirSim.uplugin文件查看版本】
1.jpeg

最终效果

2.png

安装 ROS


  • 20.04 noetic
参考链接: 小鱼的一键安装系列
  1. wget http://fishros.com/install -O fishros && . fishros
复制代码
PX4 开发环境搭建


  • MAVROS + QGC + PX4
参考链接: PX4 仿真环境开发整理
参考链接: Linux系统 ubuntu20.04 无人机 PX4 开发环境搭建
遇到报错参考链接: make px4_sitl_default gazebo出错
遇到报错参考链接: Gazebo终端显示[Err] [InsertModelWidget.cc:403] Missing model.config for model
遇到报错参考链接: px4 运行mavros仿真
AirSim 仿真平台搭建


  • UE4 + AirSim
参考链接: 在Ubuntu系统中搭建Unreal4和AirSim环境
使用Airsim的ros包

参考链接: ubuntu20.04下载和使用Airsim的ros包
遇到报错参考链接: 解决Ubuntu系统下ROS包找不到问题:环境配置与路径检查详解
AirSim 配置文件修改

settings.json
  1. {
  2.     "SettingsVersion": 1.2,
  3.     "SimMode": "Multirotor",
  4.     "ClockType": "SteppableClock",
  5.     "Vehicles": {
  6.         "PX4": {
  7.             "VehicleType": "PX4Multirotor",
  8.             "UseSerial": false,
  9.             "LockStep": true,
  10.             "UseTcp": true,
  11.             "TcpPort": 4560,
  12.             "ControlPortLocal": 14030,
  13.             "ControlPortRemote": 14280,
  14.             "ControlIp": "remote",
  15.             "LocalHostIp": "192.168.1.5",
  16.             "Cameras": {
  17.                 "CameraDepth": {
  18.                     "CaptureSettings": [
  19.                         {
  20.                         "ImageType": 2,
  21.                         "Width": 800,
  22.                         "Height": 600,
  23.                         "FOV_Degrees": 120,
  24.                         "AutoExposureSpeed": 100,
  25.                         "AutoExposureBias": 0,
  26.                         "AutoExposureMaxBrightness": 0.64,
  27.                         "AutoExposureMinBrightness": 0.03,
  28.                         "MotionBlurAmount": 0,
  29.                         "TargetGamma": 1.0,
  30.                         "ProjectionMode": "",
  31.                         "OrthoWidth": 5.12
  32.                         }
  33.                     ],
  34.                     "NoiseSettings": [
  35.                         {
  36.                         "Enabled": false,
  37.                         "ImageType": 0,
  38.                         "RandContrib": 0.2,
  39.                         "RandSpeed": 100000.0,
  40.                         "RandSize": 500.0,
  41.                         "RandDensity": 2,
  42.                         "HorzWaveContrib":0.03,
  43.                         "HorzWaveStrength": 0.08,
  44.                         "HorzWaveVertSize": 1.0,
  45.                         "HorzWaveScreenSize": 1.0,
  46.                         "HorzNoiseLinesContrib": 1.0,
  47.                         "HorzNoiseLinesDensityY": 0.01,
  48.                         "HorzNoiseLinesDensityXY": 0.5,
  49.                         "HorzDistortionContrib": 1.0,
  50.                         "HorzDistortionStrength": 0.002
  51.                         }
  52.                     ],
  53.                     "Gimbal": {
  54.                         "Stabilization": 0,
  55.                         "Pitch": 0, "Roll": 0, "Yaw": 0
  56.                     },
  57.                     "X": 0, "Y": 0, "Z": -1,
  58.                     "Pitch": 0, "Roll": 0, "Yaw": 0,
  59.                     "UnrealEngine": {
  60.                         "PixelFormatOverride": [
  61.                             {
  62. <?xml version="1.0"?>
  63. <launch>
  64.   
  65.   
  66.   
  67.   
  68.   
  69.   
  70.   <include file="$(find mavros)/launch/px4.launch">
  71.    
  72.    
  73.    
  74.   </include>
  75.   
  76.   <node pkg="offboard_py" type="offb_node.py" name="offb_node_py" required="true" output="screen"/>
  77. </launch>"ImageType": 0,
  78. <?xml version="1.0"?>
  79. <launch>
  80.   
  81.   
  82.   
  83.   
  84.   
  85.   
  86.   <include file="$(find mavros)/launch/px4.launch">
  87.    
  88.    
  89.    
  90.   </include>
  91.   
  92.   <node pkg="offboard_py" type="offb_node.py" name="offb_node_py" required="true" output="screen"/>
  93. </launch>"PixelFormat": 0
  94.                             }
  95.                         ]
  96.                     }
  97.                 },
  98.                 "CameraImage": {
  99.                     "CaptureSettings": [
  100.                         {
  101.                         "ImageType": 0,
  102.                         "Width": 800,
  103.                         "Height": 600,
  104.                         "FOV_Degrees": 120,
  105.                         "AutoExposureSpeed": 100,
  106.                         "AutoExposureBias": 0,
  107.                         "AutoExposureMaxBrightness": 0.64,
  108.                         "AutoExposureMinBrightness": 0.03,
  109.                         "MotionBlurAmount": 0,
  110.                         "TargetGamma": 1.0,
  111.                         "ProjectionMode": "",
  112.                         "OrthoWidth": 5.12
  113.                         }
  114.                     ],
  115.                     "X": 0, "Y": 0, "Z": -1,
  116.                     "Pitch": 0, "Roll": 0, "Yaw": 0
  117.                 }
  118.             },
  119.             "Sensors":{
  120.                 "Magnetometer": {
  121.                     "SensorType": 4,
  122.                     "Enabled": true
  123.                 },
  124.                 "Barometer":{
  125.                     "SensorType": 1,
  126.                     "Enabled": true,
  127.                     "PressureFactorSigma": 0.0001825
  128.                 },
  129.                 "Imu": {
  130.                     "SensorType": 2,
  131.                     "Enabled" : true,
  132.                     "AngularRandomWalk": 0.3,
  133.                     "GyroBiasStabilityTau": 500,
  134.                     "GyroBiasStability": 4.6,
  135.                     "VelocityRandomWalk": 0.24,
  136.                     "AccelBiasStabilityTau": 800,
  137.                     "AccelBiasStability": 36
  138.                 },
  139.                 "Gps": {
  140.                     "SensorType": 3,
  141.                     "Enabled" : true,
  142.                     "EphTimeConstant": 0.9,
  143.                     "EpvTimeConstant": 0.9,
  144.                     "EphInitial": 25,
  145.                     "EpvInitial": 25,
  146.                     "EphFinal": 0.1,
  147.                     "EpvFinal": 0.1,
  148.                     "EphMin3d": 3,
  149.                     "EphMin2d": 4,
  150.                     "UpdateLatency": 0.2,
  151.                     "UpdateFrequency": 50,
  152.                     "StartupDelay": 1
  153.                 },
  154.                 "Lidar": {
  155.                     "SensorType": 6,
  156.                     "Enabled": true,
  157.                     "NumberOfChannels": 16,
  158.                     "RotationsPerSecond": 10,
  159.                     "PointsPerSecond": 100000,
  160.                     "X": 0, "Y": 0, "Z": -1,
  161.                     "Roll": 0, "Pitch": 0, "Yaw": 0,
  162.                     "VerticalFOVUpper": 10,
  163.                     "VerticalFOVLower": -10,
  164.                     "HorizontalFOVStart": -20,
  165.                     "HorizontalFOVEnd": 20,
  166.                     "DrawDebugPoints": true,
  167.                     "DataFrame": "SensorLocalFrame"
  168.                 },
  169.                 "DistanceDown": {
  170.                     "SensorType": 5,
  171.                     "Enabled" : true,
  172.                     "MinDistance": 0.1,
  173.                     "MaxDistance": 40,
  174.                     "X": 0, "Y": 0, "Z": -0.5,
  175.                     "Yaw": 0, "Pitch": -90, "Roll": 0,
  176.                     "DrawDebugPoints": true,
  177.                     "ExternalController": true
  178.                 }
  179.             },
  180.             "Parameters": {
  181.                 "NAV_RCL_ACT": 0,
  182.                 "NAV_DLL_ACT": 0,
  183.                 "COM_OBS_AVOID": 0
  184.             }
  185.         }
  186.     }
  187. }
复制代码
安装 Micro-XRCE-DDS-Agent

参考链接: Installing the Agent standalone


  • 安装指令
  1. $ git clone https://github.com/eProsima/Micro-XRCE-DDS-Client.git
  2. $ cd Micro-XRCE-DDS-Client
  3. $ mkdir build && cd build
  4. $ cmake ..
  5. $ make -j8
  6. $ sudo make install
  7. $ sudo ldconfig /usr/local/lib/
  8. $ MicroXRCEAgent --help
复制代码
开发流程

参考链接: MAVROS Offboard control example (Python)
  1. yehuo@zyl:~$ mkdir px4_airsim_ros1_ws
  2. yehuo@zyl:~$ cd px4_airsim_ros1_ws
  3. yehuo@zyl:~/px4_airsim_ros1_ws$ mkdir src
  4. yehuo@zyl:~/px4_airsim_ros1_ws$ cd src
  5. yehuo@zyl:~/px4_airsim_ros1_ws/src$ catkin_create_pkg offboard_py rospy
  6. yehuo@zyl:~/px4_airsim_ros1_ws/src$ cd ..
  7. yehuo@zyl:~/px4_airsim_ros1_ws$ catkin build
  8. yehuo@zyl:~/px4_airsim_ros1_ws$ source devel/setup.bash
  9. yehuo@zyl:~/px4_airsim_ros1_ws$ roscd offboard_py
  10. yehuo@zyl:~/px4_airsim_ros1_ws/src/offboard_py$ mkdir scripts
  11. yehuo@zyl:~/px4_airsim_ros1_ws/src/offboard_py$ cd scripts
  12. yehuo@zyl:~/px4_airsim_ros1_ws/src/offboard_py/scripts$ touch offb_node.py
  13. yehuo@zyl:~/px4_airsim_ros1_ws/src/offboard_py/scripts$ chmod +x offb_node.py
复制代码
offb_node.py
  1. """
  2. * File: offb_node.py
  3. * Stack and tested in Gazebo 9 SITL
  4. """
  5. #! /usr/bin/env python
  6. import rospy
  7. from geometry_msgs.msg import PoseStamped
  8. from mavros_msgs.msg import State
  9. from mavros_msgs.srv import CommandBool, CommandBoolRequest, SetMode, SetModeRequest
  10. current_state = State()
  11. def state_cb(msg):
  12.     global current_state
  13.     current_state = msg
  14. if __name__ == "__main__":
  15.     rospy.init_node("offb_node_py")
  16.     state_sub = rospy.Subscriber("mavros/state", State, callback = state_cb)
  17.     local_pos_pub = rospy.Publisher("mavros/setpoint_position/local", PoseStamped, queue_size=10)
  18.     rospy.wait_for_service("/mavros/cmd/arming")
  19.     arming_client = rospy.ServiceProxy("mavros/cmd/arming", CommandBool)
  20.     rospy.wait_for_service("/mavros/set_mode")
  21.     set_mode_client = rospy.ServiceProxy("mavros/set_mode", SetMode)
  22.     # Setpoint publishing MUST be faster than 2Hz
  23.     rate = rospy.Rate(20)
  24.     # Wait for Flight Controller connection
  25.     while(not rospy.is_shutdown() and not current_state.connected):
  26.         rate.sleep()
  27.     pose = PoseStamped()
  28.     pose.pose.position.x = 0
  29.     pose.pose.position.y = 0
  30.     pose.pose.position.z = 2
  31.     # Send a few setpoints before starting
  32.     for i in range(100):
  33.         if(rospy.is_shutdown()):
  34.             break
  35.         local_pos_pub.publish(pose)
  36.         rate.sleep()
  37.     offb_set_mode = SetModeRequest()
  38.     offb_set_mode.custom_mode = 'OFFBOARD'
  39.     arm_cmd = CommandBoolRequest()
  40.     arm_cmd.value = True
  41.     last_req = rospy.Time.now()
  42.     while(not rospy.is_shutdown()):
  43.         if(current_state.mode != "OFFBOARD" and (rospy.Time.now() - last_req) > rospy.Duration(5.0)):
  44.             if(set_mode_client.call(offb_set_mode).mode_sent == True):
  45.                 rospy.loginfo("OFFBOARD enabled")
  46.             last_req = rospy.Time.now()
  47.         else:
  48.             if(not current_state.armed and (rospy.Time.now() - last_req) > rospy.Duration(5.0)):
  49.                 if(arming_client.call(arm_cmd).success == True):
  50.                     rospy.loginfo("Vehicle armed")
  51.                 last_req = rospy.Time.now()
  52.         local_pos_pub.publish(pose)
  53.         rate.sleep()
复制代码
  1. yehuo@zyl:~/px4_airsim_ros1_ws/src/offboard_py/scripts$ roscd offboard_py
  2. yehuo@zyl:~/px4_airsim_ros1_ws/src/offboard_py$ mkdir launch
  3. yehuo@zyl:~/px4_airsim_ros1_ws/src/offboard_py$ cd launch
  4. yehuo@zyl:~/px4_airsim_ros1_ws/src/offboard_py/launch$ touch start_offb.launch
复制代码
start_offb.launch
  1. <?xml version="1.0"?>
  2. <launch>
  3.   
  4.   
  5.   
  6.   
  7.   
  8.   
  9.   <include file="$(find mavros)/launch/px4.launch">
  10.    
  11.    
  12.    
  13.   </include>
  14.   
  15.   <node pkg="offboard_py" type="offb_node.py" name="offb_node_py" required="true" output="screen"/>
  16. </launch>
复制代码
  1. yehuo@zyl:~/px4_airsim_ros1_ws/src/offboard_py/launch$ roslaunch offboard_py start_offb.launch
复制代码
3.png


流程框架整理


  • step1: 启动 AirSim
  • step2: 启动 PX4
  1. yehuo@zyl:~/下载/PX4_Firmware$ make px4_sitl_default none_iris
复制代码

  • step3: 启动 DDS
  1. yehuo@zyl:~$ MicroXRCEAgent udp4 -p 8888
复制代码

  • step4: 启动 MAVROS (Python版本)
  1. yehuo@zyl:~$ roscore
复制代码
  1. yehuo@zyl:~$ roslaunch offboard_py start_offb.launch
复制代码

  • step5: 查看话题是否成功获取
  1. yehuo@zyl:~$ rostopic list
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册