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

Add Example of MAVLink Proxy Using MavlinkPassthrough Plugin #2467

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

flaviol-souza
Copy link

This pull request introduces an example demonstrating how to implement a MAVLink proxy using the MAVSDK MavlinkPassthrough plugin. The proxy facilitates communication between two MAVLink components (e.g., PX4 and QGroundControl) by intercepting and forwarding messages.

Key Features

  • Message Interception and Forwarding: Captures MAVLink messages from one component and routes them to another.
  • Multi-Threaded Architecture: Ensures efficient processing of messages in parallel threads.
  • Queue Management: Handles message queuing with size limits and thread-safe operations.
  • Graceful Shutdown: Implements signal handling for controlled termination.
  • Error Handling: Provides robust logging and checks for connection integrity.

Use Cases

  • Simulating a communication intermediary for testing MAVLink behavior.
  • Debugging or monitoring MAVLink messages between systems.
  • Building advanced communication relays for MAVLink networks.

Execution Command

To run the example, use the following command:

./build/qgc_proxy_px4 udp://127.0.0.1:14550 udp://:14050

Important Note: To avoid conflicts, the PX4 SITL instance must not run on the default port 14550. Adjust the PX4 configuration to use a different port (e.g., 14050 in this example).

Copy link
Collaborator

@JonasVautherin JonasVautherin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a nice idea! I have a few questions on the implementation but overall it looks pretty good!

examples/qgc_proxy_px4/qgc_proxy_px4.cpp Outdated Show resolved Hide resolved
examples/qgc_proxy_px4/qgc_proxy_px4.cpp Outdated Show resolved Hide resolved
queue_cvs[PX4_NAME] = std::make_unique<std::condition_variable>();
queue_cvs[QGC_NAME] = std::make_unique<std::condition_variable>();

for (uint16_t msg_id = 0; msg_id < 500; ++msg_id) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that you want to subscribe 500 times? Shouldn't you subscribe just once for each? 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, and why 500? That seems somewhat arbitrary, as message IDs go up to 43000, with mostly gaps in-between.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to enable this, should we maybe have something like a subscribe_all_messages in passthrough?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a Proxy, the correct thing to do is to subscribe to all message IDs. So a subscribe_all_messages might help. Another option is to create a list of message ID constants. I even went as far as manually importing the ID constants into a list. However, I'm open to suggestions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see the problem.

I see that Mavsdk::intercept_messages_async offers that but MavlinkPassthrough does not. It would probably be a good idea to add that.

Copy link
Collaborator

@julianoes julianoes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

I would maybe call it MAVLink proxy? Or is it QGC specific?

And it would be nice to have a comment at the top of the file for anyone that finds it to know more about what it is and what it does.

Comment on lines 32 to 37
// Thread-safe logging
void log_message(const std::string& level, const std::string& message) {
static std::mutex log_mutex;
std::lock_guard<std::mutex> lock(log_mutex);
std::cout << "[" << level << "] " << message << std::endl;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should actually do this within MAVSDK, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can to do this with MAVSDK ? Is there a function to this ?

queue_cvs[PX4_NAME] = std::make_unique<std::condition_variable>();
queue_cvs[QGC_NAME] = std::make_unique<std::condition_variable>();

for (uint16_t msg_id = 0; msg_id < 500; ++msg_id) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, and why 500? That seems somewhat arbitrary, as message IDs go up to 43000, with mostly gaps in-between.

@julianoes
Copy link
Collaborator

Thanks for the changes and comment on the top.

Out of curiosity, what is the use case for queuing? Like why do you need archived messages? Are the momentary ones not enough?

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

Successfully merging this pull request may close these issues.

3 participants