MAVROS二次开发(二)(三)添加自定义消息

MAVROS二次开发

二、MAVROS消息添加

1、自定义rostopic消息

  • 路径:~/catkin_ws/src/mavros/mavros_msgs/msg

  • 自定义消息文件名称:CrawlControlStatus.msg

  • 消息内容:

    uint8 clamp_state
    uint8 crawl_state
    uint8 sw_state
    
    float32 clamp_speed
    float32 crawl_speed
    
    std_msgs/Header header
    

2、自定义rosservice消息

  • 路径:~/catkin_ws/src/mavros/mavros_msgs/srv

  • 自定义消息文件名称:CrawlControlSet.srv

  • 消息内容:

    uint8 clamp_set
    uint8 crawl_set
    float32 clamp_speed
    float32 crawl_speed
    
    ---
    bool send_success
    

3、更改CMakeLists.txt文件

  • 路径:~/catkin_ws/src/mavros/mavros_msgs/CMakeLists.txt

  • 作用:编译时生成消息头文件

  • 添加内容:

    add_message_files(
      CrawlControlStatus.msg #add
    )
    add_service_files(
      CrawlControlSet.srv #add
    )
    

MAVROS二次开发

三、MAVLINK自定义消息添加

1、MAVLINK消息产生机制

MAVROS二次开发(二)(三)添加自定义消息_第1张图片

2、自定义消息xml文件

  • 文件名称:crawl_control_status.xml
  • 文件路径:~/catkin_ws/src/mavlink/message_definitions/v1.0/crawl_control_status.xml
  • 文件内容:

<mavlink>
  <version>3version>
  <messages>
    <message id="410" name="CRAWL_CONTROL_STATUS">
      <description>Crawl control system mavlink messagedescription>
      <field type="uint8_t" name="clamp_state">uint8_tfield>
      <field type="float" name="clamp_speed">floatfield>
      <field type="uint8_t" name="crawl_state">uint8_tfield>
      <field type="float" name="crawl_speed">floatfield>
      <field type="uint8_t" name="sw_state">uint8_tfield>
    message>
  messages>
mavlink>
  • 文件名称:crawl_control_set.xml
  • 文件路径:~/catkin_ws/src/mavlink/message_definitions/v1.0/crawl_control_set.xml
  • 文件内容:

<mavlink>
  <version>3version>
  <messages>
    <message id="411" name="CRAWL_CONTROL_SET">
      <description>Crawl control system set param mavlink messagedescription>
      <field type="uint8_t" name="clamp_state_set">uint8_tfield>
      <field type="float" name="clamp_speed_set">floatfield>
      <field type="uint8_t" name="crawl_state_set">uint8_tfield>
      <field type="float" name="crawl_speed_set">floatfield>
    message>
  messages>
mavlink>

3、查看CMakeLists.txt中有关消息生成的代码

  • 自定义消息的xml文件存放位置:~/catkin_ws/src/mavlink/message_definitions/v1.0/

  • MAVLINK消息生成位置:

    ~/catkin_ws/devel/include/mavlink/v1.0

    ~/catkin_ws/devel/include/mavlink/v2.0

  • ~/catkin_ws/src/mavlink/CMakeLists.txt中相关代码解释:

# mavlink generation
#mavgen.py为mavlink消息从xml转为头文件的工具
set(mavgen_path ${CMAKE_SOURCE_DIR}/pymavlink/tools/mavgen.py)
#设置读取xml文件的路径,所以我们将自定义的xml文件放到此路径下即可
set(common_xml_path ${CMAKE_SOURCE_DIR}/message_definitions/v1.0/common.xml)
#定义了mavlink v1.0消息的生成函数
macro(generateMavlink_v10 definitions)
    foreach(definitionAbsPath ${definitions})
        get_filename_component(definition ${definitionAbsPath} NAME_WE)
        message(STATUS "processing v1.0: ${definitionAbsPath}")
        add_custom_command(
            OUTPUT include/v1.0/${definition}/${definition}.h
            COMMAND /usr/bin/env PYTHONPATH="${CMAKE_SOURCE_DIR}:$ENV{PYTHONPATH}"
                ${Python_EXECUTABLE} ${mavgen_path} --lang=C --wire-protocol=1.0
                --output=include/v1.0 ${definitionAbsPath}
            DEPENDS ${definitionAbsPath} ${common_xml_path} ${mavgen_path}
            )
        add_custom_target(${definition}.xml-v1.0
            ALL DEPENDS include/v1.0/${definition}/${definition}.h
            )
    endforeach()
endmacro()
#定义了mavlink v2.0消息的生成函数
macro(generateMavlink_v20 definitions)
    foreach(definitionAbsPath ${definitions})
        get_filename_component(definition ${definitionAbsPath} NAME_WE)
        message(STATUS "processing v2.0: ${definitionAbsPath}")

        # mavgen C++ also do C gen
        add_custom_command(
            OUTPUT ${definition}-v2.0-cxx-stamp
            #OUTPUT include/v2.0/${definition}/${definition}.hpp
            COMMAND /usr/bin/env PYTHONPATH="${CMAKE_SOURCE_DIR}:$ENV{PYTHONPATH}"
                ${Python_EXECUTABLE} ${mavgen_path} --lang=C++11 --wire-protocol=2.0
                --output=include/v2.0 ${definitionAbsPath}
            COMMAND touch ${definition}-v2.0-cxx-stamp
            DEPENDS ${definitionAbsPath} ${common_xml_path} ${mavgen_path}
            )
        add_custom_target(${definition}.xml-v2.0
            ALL DEPENDS
                ${definition}-v2.0-cxx-stamp
                #include/v2.0/${definition}/${definition}.hpp
            )
    endforeach()

    # XXX NOTE: that is workaround v2.0 mavgen bug:
    #           when it generate other dialect than common.xml
    #           resulting common.h/.hpp does not have extended enums like MAV_CMD
    #
    #           So we use stamp file to force run of mavgen and add all other xml to common.xml dependencies.
    foreach(definitionAbsPath ${definitions})
        get_filename_component(definition ${definitionAbsPath} NAME_WE)
        add_dependencies(common.xml-v2.0 ${definition}.xml-v2.0)
    endforeach()
endmacro()

# build v1.0
#读取xml文件至列表
file(GLOB V10DEFINITIONS ${CMAKE_SOURCE_DIR}/message_definitions/v1.0/*.xml)

# do not generate testing dialects
#从xml文件列表中去除某些不生成头文件的xml
foreach(definition minimal test python_array_test)
    list(REMOVE_ITEM V10DEFINITIONS "${CMAKE_SOURCE_DIR}/message_definitions/v1.0/${definition}.xml")
endforeach()
list(SORT V10DEFINITIONS)

generateMavlink_v10("${V10DEFINITIONS}")

# build v2.0 if it is supported. for now it uses same definitions as v1.0
if(EXISTS "${CMAKE_SOURCE_DIR}/pymavlink/generator/C/include_v2.0")
    set(V20DEFINITIONS "${V10DEFINITIONS}")
    generateMavlink_v20("${V20DEFINITIONS}")
endif()

你可能感兴趣的:(MAVROS二次开发(二)(三)添加自定义消息)