# Send custom MAVLink message

**URL:** <https://discuss.px4.io/t/send-custom-mavlink-message/1370>\
**Category:** Uncategorized\
**Created:** [October 4, 2016, 1:41pm UTC](https://discuss.px4.io/t/send-custom-mavlink-message/1370 "2016-10-04T13:41:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pietrop88](https://discuss.px4.io/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@pietrop88](https://discuss.px4.io/u/pietrop88)\
**Post date:** [October 4, 2016, 1:41pm UTC](https://discuss.px4.io/t/send-custom-mavlink-message/1370/1 "2016-10-04T13:41:57Z")

</div>

Hi,

I want to send a custom uOrb message through mavlink.  
I follow the guide: [http://dev.px4.io/custom-mavlink-message.html](http://dev.px4.io/custom-mavlink-message.html).  
The class that I added to `mavlink_messages.cpp` is this one:

```auto
class MavlinkStreamCaTrajectory : public MavlinkStream
{
public:
    const char *get_name() const
    {
        return MavlinkStreamCaTrajectory::get_name_static();
    }
    static const char *get_name_static()
    {
        return "CA_TRAJECTORY";
    }
    static uint8_t get_id_static()
    {
        return MAVLINK_MSG_ID_CA_TRAJECTORY;
    }
		uint8_t get_id()
		{
			return get_id_static();
		}
    static MavlinkStream *new_instance(Mavlink *mavlink)
    {
        return new MavlinkStreamCaTrajectory(mavlink);
    }
    unsigned get_size()
    {
        return MAVLINK_MSG_ID_CA_TRAJECTORY_LEN + MAVLINK_NUM_NON_PAYLOAD_BYTES;
    }

private:
    MavlinkOrbSubscription *_sub;
    uint64_t _ca_traj_time;

    /* do not allow top copying this class */
    MavlinkStreamCaTrajectory(MavlinkStreamCaTrajectory &);
    MavlinkStreamCaTrajectory& operator = (const MavlinkStreamCaTrajectory &);

protected:
    explicit MavlinkStreamCaTrajectory(Mavlink *mavlink) : MavlinkStream(mavlink),
        _sub(_mavlink->add_orb_subscription(ORB_ID(ca_trajectory))), // make sure you enter the name of your uorb topic here
        _ca_traj_time(0)
    {}

    void send(const hrt_abstime t)
    {
      struct ca_traj_struct_s _ca_trajectory; //make sure ca_traj_struct_s is the definition of your uorb topic

      if (_sub->update(&_ca_traj_time, &_ca_trajectory)) {
        mavlink_ca_trajectory_t _msg_ca_trajectory; //make sure mavlink_ca_trajectory_t is the definition of your custom mavlink message

        _msg_ca_trajectory.timestamp = _ca_trajectory.timestamp;
        _msg_ca_trajectory.time_start_usec = _ca_trajectory.time_start_usec;
        _msg_ca_trajectory.time_stop_usec = _ca_trajectory.time_stop_usec;
        _msg_ca_trajectory.coefficients =_ca_trajectory.coefficients;
        _msg_ca_trajectory.seq_id = _ca_trajectory.seq_id;
        mavlink_msg_ca_trajectory_send_struct(_mavlink->get_channel(), &_msg_ca_trajectory);
      }
    }
};

```

I appended the stream class

```auto
StreamListItem *streams_list[] = {
...
  new StreamListItem(&MavlinkStreamCaTrajectory::new_instance, &MavlinkStreamCaTrajectory::get_name_static,&MavlinkStreamCaTrajectory::get_id_static),nullptr
};

```

Differences respect to the guide:

1. Since the constructor of class `StreamListItem` requires the `id` i added the method `get_name_static` to `MavlinkStreamCaTrajectory` class.
2. In the `send` method of class `MavlinkStreamCaTrajectory` I changed the method  
`_mavlink->send_message(MAVLINK_MSG_ID_CA_TRAJECTORY, &_msg_ca_trajectory);`  
with:  
`mavlink_msg_ca_trajectory_send_struct(_mavlink->get_channel(), &_msg_ca_trajectory);`  
because I had a compile error.

Now I am able to compile the code but the function `send` is never used even if I send `ca_trajectory` uOrb message and I don’t recive any `ca_trajectory` mavlink message.

I think that something is missed but I don’t no what.

---

<div class="post-metadata">

**Author:** ![pietrop88](https://discuss.px4.io/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@pietrop88](https://discuss.px4.io/u/pietrop88)\
**Post date:** [October 6, 2016, 8:40am UTC](https://discuss.px4.io/t/send-custom-mavlink-message/1370/2 "2016-10-06T08:40:10Z")

</div>

Solved. See [https://github.com/PX4/Firmware/issues/5597](https://github.com/PX4/Firmware/issues/5597)

---

<div class="post-metadata">

**Author:** ![zyq1993](https://discuss.px4.io/letter_avatar_proxy/v4/letter/z/aca169/32.png) [@zyq1993](https://discuss.px4.io/u/zyq1993)\
**Post date:** [April 9, 2017, 2:55pm UTC](https://discuss.px4.io/t/send-custom-mavlink-message/1370/3 "2017-04-09T14:55:51Z")

</div>

Hello, I saw the github page [https://github.com/PX4/Firmware/issues/5597](https://github.com/PX4/Firmware/issues/5597), but I still don’t know where to put the generated directory custom\_messages in mavros package. And could you share your mavros code? Thanks a lot.

---

<div class="post-metadata">

**Author:** ![pietrop88](https://discuss.px4.io/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@pietrop88](https://discuss.px4.io/u/pietrop88)\
**Post date:** [April 9, 2017, 3:39pm UTC](https://discuss.px4.io/t/send-custom-mavlink-message/1370/4 "2017-04-09T15:39:43Z")

</div>

Are you using mavros 0.17 or 0.18?

---

<div class="post-metadata">

**Author:** ![zyq1993](https://discuss.px4.io/letter_avatar_proxy/v4/letter/z/aca169/32.png) [@zyq1993](https://discuss.px4.io/u/zyq1993)\
**Post date:** [April 10, 2017, 1:27am UTC](https://discuss.px4.io/t/send-custom-mavlink-message/1370/5 "2017-04-10T01:27:36Z")

</div>

Yes, I use mavros 0.17.

---

<div class="post-metadata">

**Author:** ![zyq1993](https://discuss.px4.io/letter_avatar_proxy/v4/letter/z/aca169/32.png) [@zyq1993](https://discuss.px4.io/u/zyq1993)\
**Post date:** [April 10, 2017, 3:04am UTC](https://discuss.px4.io/t/send-custom-mavlink-message/1370/6 "2017-04-10T03:04:53Z")

</div>

Now, I have extended the mavros, and enabled the mavlink stream in posix-configs/SITL/init/lpe/iris, but I still can’t receive my custom mavlink messages 😢

---

<div class="post-metadata">

**Author:** ![zyq1993](https://discuss.px4.io/letter_avatar_proxy/v4/letter/z/aca169/32.png) [@zyq1993](https://discuss.px4.io/u/zyq1993)\
**Post date:** [April 10, 2017, 7:07am UTC](https://discuss.px4.io/t/send-custom-mavlink-message/1370/7 "2017-04-10T07:07:40Z")

</div>

Maybe my way to extend mavros is wrong, I can confirm the custom messages were sent by px4.  
For extending mavros, I put the directory custom\_messages that generated by pymavlink into the directory mavros/include, and added the include sentence in mavros.cpp.  
Do I miss anything? Thanks a lot.

 ![](https://discuss.px4.io/uploads/default/original/1X/494bcdf17308871619043b63714567918fc65a65.PNG)

---

<div class="post-metadata">

**Author:** ![pietrop88](https://discuss.px4.io/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@pietrop88](https://discuss.px4.io/u/pietrop88)\
**Post date:** [April 10, 2017, 7:52am UTC](https://discuss.px4.io/t/send-custom-mavlink-message/1370/8 "2017-04-10T07:52:37Z")

</div>

See [this](https://github.com/mavlink/mavros/issues/166) issue.

---

<div class="post-metadata">

**Author:** ![zyq1993](https://discuss.px4.io/letter_avatar_proxy/v4/letter/z/aca169/32.png) [@zyq1993](https://discuss.px4.io/u/zyq1993)\
**Post date:** [April 10, 2017, 2:35pm UTC](https://discuss.px4.io/t/send-custom-mavlink-message/1370/9 "2017-04-10T14:35:38Z")

</div>

I want to know how you judge whether px4 has sent the custom messages? Now I’m not sure whether px4 has sent my custom messages.

---

<div class="post-metadata">

**Author:** ![pietrop88](https://discuss.px4.io/letter_avatar_proxy/v4/letter/p/bbe5ce/32.png) [@pietrop88](https://discuss.px4.io/u/pietrop88)\
**Post date:** [April 16, 2017, 7:29pm UTC](https://discuss.px4.io/t/send-custom-mavlink-message/1370/10 "2017-04-16T19:29:10Z")

</div>

I extended mavros and it worked. If you want to see the mavlink streaming, I think that the fast way is to use pymavlink or mavproxy.
