Error while data type conversion from "Unsigned char" to "Const mavlink" {aka mavlink message type}

Hello @LorenzMeier, @Jaeyoung-Lim, @JonasVautherin, @JulianOes.
I have tried the procedure to create custom mavlink message and send it from MAVROS to PX4 simualtion instance from “https://dev.px4.io/master/en/ros/mavros_custom_messages.html”.
I face the following error when i build MAVROS again after creating files for custom message. The error is is the line highted in the image below:

The error screen shot in terminal is shown below:


Now, I have searched for way to resolve this error but unable to do. Please tell me how to do this conversion from std_msgs/Char type to mavlink_message_t. Also please give some insight on mavlink_message_t (what type of data is it and any useful info).

Ya, that’s not gonna work an unsigned char can not become a mavlink_message.
That example in the docs seems to be wrong.
Try something like this:

  void send_to_pixhawk(char cmd)
    {
        mavlink_message_t msg;
        mavlink_msg_key_command_pack_chan(UAS_PACK_CHAN(uas), &msg, cmd);
        UAS_FCU(uas)->send_message(&msg);
    }

    void keyboard_cb(const std_msgs::Char::ConstPtr &req)
    {
        std::cout << "Got Char : " << req->data <<  std::endl;
        send_to_pixhawk(req->data);
    }

From:

Hello @jimdgit, @TSC21, I followed what you told, i got the following error:

My keyboard_command.cpp file is as follows:

 #include <mavros/mavros_plugin.h>
 #include <pluginlib/class_list_macros.h>
 #include <iostream>
 #include <std_msgs/Char.h>
//#include <std_msgs/String.h>
//#include <mavlink/v2.0/mavlink_msg_key_command.h>

 namespace mavros {
 namespace extra_plugins{

 class KeyboardCommandPlugin : public plugin::PluginBase {
 public:
     KeyboardCommandPlugin() : PluginBase(),
         nh("~keyboard_command")

    { };

     void initialize(UAS &uas_)
     {
         PluginBase::initialize(uas_);
         keyboard_sub = nh.subscribe("keyboard_sub", 10, &KeyboardCommandPlugin::keyboard_cb, this);
     };

     Subscriptions get_subscriptions()
     {
         return {/* RX disabled */ };
     }

 private:
     ros::NodeHandle nh;
     ros::Subscriber keyboard_sub;
     UAS *uas;

    void send_to_pixhawk(char cmd)
    {
        mavlink_message_t msg;
        mavlink_msg_key_command_pack_chan(UAS_PACK_CHAN(uas), &msg, cmd);
        UAS_FCU(uas)->send_message(&msg);
    }

    void keyboard_cb(const std_msgs::Char::ConstPtr &req)
     {
         std::cout << "Got Char : " << req->data <<  std::endl;
         send_to_pixhawk(req->data);
         //UAS_FCU(m_uas)->send_message_ignore_drop(req->data);
     }
 };
 }   // namespace extra_plugins
 }   // namespace mavros

PLUGINLIB_EXPORT_CLASS(mavros::extra_plugins::KeyboardCommandPlugin, mavros::plugin::PluginBase)

Any suggestion to solve this, is there any header file missing, mavlink_message_t is not being recognized.

try including:
mavlink.h
But look this code was bugged from the start and I can’t find the source file for it
Also you seem new to c++ so this will no doubt be a heavy lift.
If it does not work I will have no idea why, I just know you can’t cast a char to a struct.
Perhaps @JulianOes will jump in who has much more knowledge about mavlink.
Really that example should be updated or removed.

hello @jimdgit, thanks for your quick replies.
I would like to say that mavlink_message_t struct is already included via mavros_plugin.h in the file keyboard_command.cpp. I would like to include @JulianOes, @Vladimir_Ermakov o look into the code for a second. My plugin file looks like:

     #include <mavros/mavros_plugin.h>
     #include <pluginlib/class_list_macros.h>
     #include <iostream>
     #include <std_msgs/Char.h>
     
     namespace mavros {
     namespace extra_plugins{

     class KeyboardCommandPlugin : public plugin::PluginBase {
     public:
     KeyboardCommandPlugin() : PluginBase(),
         nh("~keyboard_command")

    { };

     void initialize(UAS &uas_)
     {
         PluginBase::initialize(uas_);
         keyboard_sub = nh.subscribe("keyboard_sub", 10, &KeyboardCommandPlugin::keyboard_cb, this);
     };

     Subscriptions get_subscriptions()
     {
         return {/* RX disabled */ };
     }

     private:
     ros::NodeHandle nh;
     ros::Subscriber keyboard_sub;
     //UAS *uas;

    void send_to_pixhawk(char cmd)
    {
        mavlink_message_t msg;
        mavlink_msg_key_command_pack_chan(UAS_PACK_CHAN(uas), &msg, cmd);
        UAS_FCU(uas)->send_message(&msg);
    }

    void keyboard_cb(const std_msgs::Char::ConstPtr &req)
     {
         std::cout << "Got Char : " << req->data <<  std::endl;
         send_to_pixhawk(req->data);
         //UAS_FCU(m_uas)->send_message_ignore_drop(req->data);
     }
     };
     }   // namespace extra_plugins
     }   // namespace mavros

    PLUGINLIB_EXPORT_CLASS(mavros::extra_plugins::KeyboardCommandPlugin, mavros::plugin::PluginBase)

and the error that i encounter is shown as:

here __mavlink_messageis defined in file catkin_ws/devel/include/mavlink/v2.0/mavlink_type.h which is included in mavros_plugins.h at the top of plugin file. Can someone tell me why the error mavlink_message_t not declared in this scope appearing when already its header is included?
Moreover, i would like to point out that the function UAS_PACK_CHAN() in send_to_pixhawk function in keyboard_command.cpp is present no where in mavros, mavlink or any package in ROS installtion directory /opt/ros/melodic. Can you tell me what is it doing and where is it defined in mavlink or mavros or any related package.
Any help is appreciated.

As I have tried to explain this example is obsolete.
Please choose another example from:


as your starting point.
They will at least build and if you study them, you see examples of what you need.

OKay, thanks @jimdgit, i will try some of them suggested by you. :slightly_smiling_face:

Also look at: