当前位置:   article > 正文

机载电脑发布自定义MAVROS消息教程_mavros 增加 msg

mavros 增加 msg

        自定义MAVROS消息需要首先通过源码安装MAVROS。Ubuntu18.04下MAVROS源码安装教程见https://blog.csdn.net/real86/article/details/131891303?spm=1001.2014.3001.5501

一、创建msg消息

        在源码安装的MAVROS工作空间中创建msg消息。具体的,在~/mavros_catkin_ws/src/mavros/mavros_msgs/msg文件夹下新建TargetDetection.msg文件,该消息文件是我们要传输的消息的数据类型,在文件中写入:

  1. std_msgs/Header header
  2. int8 targetsDetected
  3. uint8 num_targets

        在本例中,targetsDetected为检测到目标框的标志位,num_targets为检测到目标框的数量。

        在~/mavros_catkin_ws/src/mavros/mavros_msgs目录下的CmakeLists.txt文件中添加TargetDetection.msg文件,具体如下:

  1. add_message_files(
  2. ...
  3. TargetDetection.msg
  4. )

        利用catkin build在~/mavros_catkin_ws目录下编译,确保在~/mavros_catkin_ws/devel/include/mavros_msgs目录下生成了TargetDetection.h头文件。

二、添加自定义的mavros消息

        在~/mavros_catkin_ws/src/mavlink/message_definition/v1.0目录下,修改common.xml文件,添加自定义的mavros消息,具体如下:

  1. <message id="227" name="TARGET_DETECTION">
  2. <description>the detection message from nx to vehicle.</description>
  3. <field type="int8_t" name="targetsDetected">int8_t</field>
  4. <field type="uint8_t" name="num_targets">uint8_t</field>
  5. </message>

        在~/mavros_catkin_ws/src/mavlink/message_definitions目录下,使用MAVLINK的GUI代码mavgenerate.py生成MAVLINK库文件,具体如下:

python -m mavgenerate

        在弹出的GUI中选择如下:

  1. XML: ~/mavros_catkin_ws/src/mavlink/message_definitions/v1.0/common.xml
  2. OUT: ~/mavros_catkin_ws/src/mavlink/message_definitions/v1.0
  3. Language: C
  4. Protocol: 1.0
  5. Validate: 勾选

        点击Generate后,会在~/mavros_catkin_ws/src/mavlink/message_definitions/v1.0目录下生成common、minimal、standard文件夹。

        为了将自定义的MAVROS消息发送出去,还需要编写插件。具体的,在~/mavros_catkin_ws/src/mavros/mavros_extras/src/plugins目录下创建target_detection.cpp文件,具体如下:

  1. #include <mavros/mavros_plugin.h>
  2. #include <pluginlib/class_list_macros.h>
  3. #include <iostream>
  4. #include <mavros_msgs/TargetDetection.h>
  5. namespace mavros {
  6. namespace extra_plugins{
  7. class TargetDetectionPlugin : public plugin::PluginBase {
  8. public:
  9. TargetDetectionPlugin() : PluginBase(),
  10. nh("~target_detection"){ };
  11. void initialize(UAS &uas_)
  12. {
  13. PluginBase::initialize(uas_);
  14. mavros2fcu_sub = nh.subscribe("send_data", 10, &TargetDetectionPlugin::TargetDetection_cb, this);
  15. };
  16. Subscriptions get_subscriptions()
  17. {
  18. return {/* RX disabled */ };
  19. }
  20. private:
  21. ros::NodeHandle nh;
  22. ros::Subscriber mavros2fcu_sub;
  23. void TargetDetection_cb(const mavros_msgs::TargetDetection::ConstPtr &req)
  24. {
  25. mavros::UAS *m_uas_ = static_cast<TargetDetectionPlugin *>(this)->m_uas;
  26. mavlink::common::msg::TARGET_DETECTION send_data = {};
  27. send_data.targetsDetected = req->targetsDetected;
  28. send_data.num_targets = req->num_targets;
  29. UAS_FCU(m_uas)->send_message_ignore_drop(send_data);
  30. }
  31. };
  32. } // namespace extra_plugins
  33. } // namespace mavros
  34. PLUGINLIB_EXPORT_CLASS(mavros::extra_plugins::TargetDetectionPlugin, mavros::plugin::PluginBase)

        将自定义的插件添加到插件列表中,用于MAVROS自启动插件。具体的,在~/mavros_catkin_ws/src/mavros/mavros_extras/mavros_plugins.xml中添加:

  1. <class name="target_detection" type="mavros::extra_plugins::TargetDetectionPlugin" base_class_type="mavros::plugin::PluginBase">
  2. <description>Send target detection.</description>
  3. </class>

        在~/mavros_catkin_ws/src/mavros/mavros_extras/CmakeLists.txt中添加编译信息:

  1. add_library(mavros_extras
  2. src/plugins/target_detection.cpp

        在~/mavros_catkin_ws目录下进行编译,在终端输入:

catkin build

三、通过ROS发布自定义的MAVROS消息

        自定义的插件target_detection.cpp会订阅数据并发布。为了验证自定义MAVROS消息的正确性,需要写一个ROS节点发布供自定义的插件订阅的数据,让自定义的插件订阅到ROS节点发布的数据并发送给飞控。首先创建发布消息的ROS功能包,具体如下:

  1. mkdir ros_test
  2. cd ros_test
  3. mkdir src
  4. cd src
  5. catkin_init_workspace
  6. cd ~/ros_test
  7. catkin_make
  8. cd src
  9. catkin_create_pkg msg_test

        在msg_test目录下创建pub_data.cpp,用于发布自定义MAVROS消息。具体如下:

  1. #include <time.h>
  2. #include <ros/ros.h>
  3. #include <mavros_msgs/TargetDetection.h>
  4. using namespace std;
  5. mavros_msgs::TargetDetection targets_msg;
  6. int main(int argc, char **argv){
  7. ros::init(argc, argv, "target_msg");
  8. ros::NodeHandle nh;
  9. targets_msg.targetsDetected = 1;
  10. targets_msg.num_targets = 6;
  11. ros::Publisher test;
  12. test = nh.advertise<mavros_msgs::TargetDetection>("/mavros/target_detection/send_data", 10);
  13. ros::Rate rate(20.0);
  14. while(ros::ok()){
  15. ros::spinOnce();
  16. rate.sleep();
  17. test.publish(targets_msg);
  18. }
  19. return 0;
  20. }

        pub_data.cpp能够发布TargetDetection类型的消息,以供自定义插件target_detection.cpp订阅。

        在msg_test目录下,修改CmakeLists.txt文件。在文件中添加:

  1. find_package(catkin REQUIRED COMPONENTS
  2. mavros
  3. roscpp
  4. std_msgs
  5. mavros_msgs
  6. )
  7. include_directories(
  8. include
  9. ${catkin_INCLUDE_DIRS}
  10. )
  11. add_executable(target_msg pub_data.cpp)
  12. target_link_libraries(target_msg ${catkin_LIBRARIES})

        在msg_test目录下,修改package.xml文件。在文件中添加:

  1. <build_depend>mavros</build_depend>
  2. <build_depend>roscpp</build_depend>
  3. <build_depend>mavros_msgs</build_depend>
  4. <build_export_depend>mavros</build_export_depend>
  5. <build_export_depend>roscpp</build_export_depend>
  6. <build_export_depend>std_msgs</build_export_depend>
  7. <exec_depend>mavros</exec_depend>
  8. <exec_depend>roscpp</exec_depend>
  9. <exec_depend>mavros_msgs</exec_depend>

        最后,在ros_test目录下编译,执行catkin_make指令。

        测试流程如下:

        1)在机载电脑的终端中启动roscore;

        2)在~/ros_test/devel目录下依次执行:

  1. source setup.bash
  2. rosrun msg_test target_msg

        开始通过ROS节点发布自定义的MAVROS消息;

        3)在新终端中执行:

rostopic echo /mavros/target_detection/send_data

        若输出如下,代表自定义的MAVROS消息发布成功。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/688576
推荐阅读
相关标签
  

闽ICP备14008679号