Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there documentation or example on how to use the C++11 generated libraries? #392

Open
aarondewindt opened this issue Sep 18, 2021 · 7 comments

Comments

@aarondewindt
Copy link

I have an incoming and outgoing stream of bytes which I need to serialize and deserialize to/from mavlink::Message objects. How do I do this? I can't find the documentation for the C++11 libraries or some example. So does anyone know of some example code doing this?

@aarondewindt
Copy link
Author

So to be more specific. I want to deserialize the stream into messages of their own type, but return a std::shared_pointer<mavlink::Message>.

@hamishwillee
Copy link
Collaborator

@aarondewindt The only examples and docs we have are for C and Python. @julianoes Can you advise?

@julianoes
Copy link
Contributor

What about MAVROS? Does that help?
e.g. https://github.com/mavlink/mavros/blob/ros2/mavros/src/plugins/manual_control.cpp

@vooon
Copy link
Member

vooon commented Sep 20, 2021

@julianoes i think plugins code didn't help here, because mavlink transcoding is hidden by the api.

@aarondewindt C++11 uses C library to finalize serialization and message parsing.
You should implement mavlink::mavlink_get_msg_entry(), as i gives extra crc byte.

https://github.com/mavlink/mavros/blob/87f2b114206057595ef4ed72e75057aed05e71cb/libmavconn/src/mavlink_helpers.cpp.em#L68-L78

To serialize mavlink::Message you need to:

  1. Copy your message to mavlink_message_t trough mavlink::MsgMap by calling Message::serialize();
  2. Set headers and crc by mavlink_finalize_message_buffer();
  3. Copy mavlink_message_t to bytes buffer (which then you could send)

https://github.com/mavlink/mavros/blob/87f2b114206057595ef4ed72e75057aed05e71cb/libmavconn/include/mavconn/msgbuffer.hpp#L60-L78

To parse you should call mavlink::mavlink_frame_char_buffer() and then use MsgMap with resulting mavlink_message_t to be able to Message::deserialize().

https://github.com/mavlink/mavros/blob/87f2b114206057595ef4ed72e75057aed05e71cb/libmavconn/src/interface.cpp#L101-L125

https://github.com/mavlink/mavros/blob/87f2b114206057595ef4ed72e75057aed05e71cb/mavros/include/mavros/plugin.hpp#L161-L174

@julianoes
Copy link
Contributor

@vooon sorry, you're right. I did not read the question correctly 🤦‍♂️.

@aarondewindt
Copy link
Author

@vooon Thaaanks, that help quite a bit. I figured I had to use the C library and mavlink::MsgMap but implemented it using mavlink_parse_char instead, I haven't had a chance to test it though. I'll post an short update once I get it to work. Maybe with a gist as a simple example for future reference.

@hamishwillee
Copy link
Collaborator

A simple example or advice we can add to the docs would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants