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

Fix #547 Add callback for components discovered. #556

Merged
merged 10 commits into from
Oct 1, 2018
Merged

Fix #547 Add callback for components discovered. #556

merged 10 commits into from
Oct 1, 2018

Conversation

dbowerman
Copy link
Contributor

Add the ability to register a callback so the application can be notified when individual components are added.
See example/takeoff_land for usage example.

@julianoes julianoes self-requested a review September 23, 2018 11:57
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.

Thanks for the PR! I tested it and it seems to work with the Yuneec H520. I added comments below.

example/takeoff_land/takeoff_and_land.cpp Show resolved Hide resolved
core/system_impl.cpp Show resolved Hide resolved
/**
* @brief Register a callback to be called when a component is discovered.
*
* @param callback a function of type void(uint8_t) which will be called with the COMPONENT_ID
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would prefer if we don't "leak" the mavlink component ID outside of the SDK but only the notion of autopilot/camera/gimbal. Therefore, in my opinion, it should be something like register_camera_discovered().

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there a convention on how components should be called? I mean how do you know, from MAVLink, that it is a camera?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The camera has MAV_COMP_ID_CAMERA[x], it's something known to the SDK internals.

https://mavlink.io/en/messages/common.html#MAV_COMP_ID_CAMERA

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@julianoes Can we declare an enum like ::Camera ::Gimbal ::AutoPilot ::Unknown etc (maybe this already exists and pass that to the callback? that way there'd only be one callback function instead of one function per component.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes an enum for components makes sense, I agree.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@julianoes change pushed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Cool, the comment needs to be adapted though.

/**
* @brief type for component discovery callback
*/
typedef std::function<void(ComponentType)> discover_callback_t;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, I like it.

/**
* @brief Register a callback to be called when a component is discovered.
*
* @param callback a function of type void(uint8_t) which will be called with the COMPONENT_ID
Copy link
Collaborator

Choose a reason for hiding this comment

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

Cool, the comment needs to be adapted though.

case MAV_COMP_ID_CAMERA:
case MAV_COMP_ID_CAMERA2:
case MAV_COMP_ID_CAMERA3:
case MAV_COMP_ID_CAMERA4:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Next time we'll have to refactor this switch case into a function.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, just seing it after my comment above. So that is duplicated from above, and should become something like const auto component_type = getComponentType(component_id);.

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.

Looking great! On my side, the duplicated code should be removed, and the comments fixed, and then I'll be fine with it!

Thank you @dbowerman :-)

@@ -332,6 +334,29 @@ void SystemImpl::add_new_component(uint8_t component_id)
{
auto res_pair = _components.insert(component_id);
if (res_pair.second) {
if (component_discovered_callback != nullptr) {
ComponentType component_type;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Small readability thing: I would put that in a function, e.g. getComponentType(const uint8_t component_id) (and here do const auto component_type = getComponentType(component_id). What do you think?

core/system_impl.cpp Show resolved Hide resolved
case MAV_COMP_ID_CAMERA:
case MAV_COMP_ID_CAMERA2:
case MAV_COMP_ID_CAMERA3:
case MAV_COMP_ID_CAMERA4:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, just seing it after my comment above. So that is duplicated from above, and should become something like const auto component_type = getComponentType(component_id);.

@dbowerman
Copy link
Contributor Author

@julianoes @JonasVautherin comments addressed :)

default:
return UNKNOWN;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Awesome!

@JonasVautherin
Copy link
Collaborator

Testing right now, will merge ASAP.

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.

And tested! Thanks a lot!

@JonasVautherin JonasVautherin merged commit d19a56b into mavlink:develop Oct 1, 2018
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.

4 participants