MavlinkPassthrough.subscribe_message_async(...) , how to use it?

Hello there,

I have a custom message that I wish to send from a script on the ground station computer to an onboard computer (a Raspberry Pi) on the UAV. I’ve followed the steps to create the custom message and seem to have no issue with sending it using MavlinkPassthrough.send_message(…) as the result of this is a ‘Success’ as defined in the results enum.

My issue is on how to implement the subscribe_message_async(…) to receive the message. I assume this is the publish-subscribe pattern of sending mavlink messages. My problem probably stems from me never encountering the ‘std::function< >’ before. I’ll copy-paste the relevant parts of my code in the following section. Note that I have no errors and the code runs. This was tested in the px4_sitl gazebo_plane simulation.

CODE:

void receive_waypoint_type(MavlinkPassthrough &mavlink_passthrough)
{

std::function<void(const mavlink_message_t &)> m_callback;

m_callback = [](const mavlink_message_t &msg_raw)
{
    const mavlink_message_t* msg = &msg_raw;
    int waypoint_index = mavlink_msg_waypoint_info_get_waypoint_index(msg);
    std::cout << NORMAL_CONSOLE_TEXT << "the waypoint index is: " << waypoint_index << std::endl;

    int category_num = mavlink_msg_waypoint_info_get_category(msg);
    std::cout << NORMAL_CONSOLE_TEXT << "the category number is:  " << category_num << std::endl;

    int length_of_items_num = mavlink_msg_waypoint_info_get_total_length_of_index(msg);
    std::cout << NORMAL_CONSOLE_TEXT << "the of items sent is: " << length_of_items_num << std::endl;
};


mavlink_passthrough.subscribe_message_async(302, m_callback);

}

// IN MAIN

auto mavlink_passthrough = MavlinkPassthrough{system};
receive_waypoint_type(mavlink_passthrough);

In this same main I send out the custom message using MavlinkPassthrough.send_message(…). My issue is that the code terminates, which surprises me as I assumed subscribe_message_async would just continue.
An extra question, this is the best way to send and receive a messages using MAVSDK? I would prefer to do point-to-point communication but I can’t see how to do this with MAVSDK?

Regards,
David

Did you solve your problem? I also have the same case…

I got a bit further but remained unsuccessful. I could pass-through pre-existing messages such as the battery level. By pre-exsisting I mean they are by default published on mavlink and are visible from Qground control. I could not however pass-through my custom message.