MAVROS part:
1、In mavros/mavros_msgs/msg create TestMessage.msg
std_msgs/Header header
float32[10] f_32
int32[10] i_32
In mavros/mavros_msgs/msg/CMakeLists.txt add
add_message_files(
...
TestMessage.msg
...
)
2、In mavlink/message_definitions/v1.0/common.xml add
<message id="12921" name="TEST_MSG_MAV_DATA">
<description>defined by test message</description>
<field type="uint64_t" name="time_usec" units="us">Timestamp</field>
<field type="float[10]" name="f_32">float32_array</field>
<field type="int32_t[10]" name="i_32">float32_array</field>
</message>
3、after catkin build, devel/include/mavros_msgs can find TestMessage.h; in devel/include/mavlink/v2.0/common can find mavlink_msg_test_msg_mav_data.h mavlink_msg_test_msg_mav_data.hpp
4、then in mavros/mavros_extras/src/plugins create test_message.cpp
#include <mavros/mavros_plugin.h>
#include <pluginlib/class_list_macros.h>
#include <iostream>
#include <mavros_msgs/TestMessage.h>
namespace mavros {
namespace extra_plugins {
class testMessagePlugin : public plugin::PluginBase {
public:
testMessagePlugin() : PluginBase(),
nh("~TestMessage")
{ };
void initialize(UAS &uas_)
{
PluginBase::initialize(uas_);
send_sub = nh.subscribe("send_data", 10, &testMessagePlugin::testMessage_cb, this);
};
Subscriptions get_subscriptions() override
{
return { };
}
private:
ros::NodeHandle nh;
ros::Subscriber send_sub;
void testMessage_cb(const mavros_msgs::TestMessage::ConstPtr& req)
{
mavros::UAS *m_usa_ = static_cast<testMessagePlugin*>(this)->m_uas;
mavlink::common::msg::TEST_MSG_MAV_DATA send_data = {};
for(int i=0; i<10; ++i)
{
send_data.f_32[i] = req->f_32[i];
send_data.i_32[i] = req->i_32[i];
}
std::cout << "Mavros Got Data:" << req->f_32[0] << req->f_32[1] << req->f_32[2] << std::endl;
UAS_FCU(m_uas)->send_message_ignore_drop(send_data);
}
};
}
}
PLUGINLIB_EXPORT_CLASS(mavros::extra_plugins::testMessagePlugin, mavros::plugin::PluginBase)
5、In mavros/mavros_extras/CMakeLists.txt add
add_library(mavros_extras
…
src/plugins/test_message.cpp
…
)
In mavros/mavros_extras/mavros_plugins.xml add
<class name="test_message" type="mavros::extra_plugins::testMessagePlugin" base_class_type="mavros::plugin::PluginBase">
<description>defined by test message</description>
</class>
the catkin build
After all these work
I run
make px4_sitl_default gazebo
roslaunch mavros px4.launch fcu_url:=“udp://:14540@127.0.0.1:14557”
and I run an ros pkg pub topic /mavros/TestMessage/send_data, then I can see Mavros Got Data: in mavros, px4 mavlink get in px4_sitl_defalut gazebo
so I think I success to send custom message from MAVROS to PX4-Autopolit and the port is ok