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

gimbal/info: fixups #2286

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/mavsdk/plugins/gimbal/gimbal_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ void GimbalImpl::init()

void GimbalImpl::deinit()
{
std::lock_guard<std::mutex> lock(_mutex);

_gimbal_protocol.reset(nullptr);
_system_impl->unregister_all_mavlink_message_handlers(this);
}
Expand All @@ -61,7 +63,7 @@ void GimbalImpl::receive_protocol_timeout()
{
// We did not receive a GIMBAL_MANAGER_INFORMATION in time, so we have to
// assume Version2 is not available.
LogDebug() << "Falling back to Gimbal Version 1";
LogWarn() << "Falling back to Gimbal Version 1";
_gimbal_protocol.reset(new GimbalProtocolV1(*_system_impl));
_protocol_cookie = nullptr;
}
Expand All @@ -85,6 +87,10 @@ void GimbalImpl::process_gimbal_manager_information(const mavlink_message_t& mes
_system_impl->call_user_callback([=]() {
_gimbal_protocol.reset(new GimbalProtocolV2(
*_system_impl, gimbal_manager_information, message.sysid, message.compid));
_gimbal_protocol->attitude_async(
[this](auto attitude) { receive_attitude_update(attitude); });
_gimbal_protocol->control_async(
[this](auto control_status) { receive_control_status_update(control_status); });
});
}
}
Expand Down Expand Up @@ -275,6 +281,22 @@ void GimbalImpl::receive_command_result(
}
}

void GimbalImpl::receive_attitude_update(Gimbal::Attitude attitude)
{
std::lock_guard<std::mutex> lock(_mutex);

_attitude_subscriptions.queue(
attitude, [this](const auto& func) { _system_impl->call_user_callback(func); });
}

void GimbalImpl::receive_control_status_update(Gimbal::ControlStatus control_status)
{
std::lock_guard<std::mutex> lock(_mutex);

_control_subscriptions.queue(
control_status, [this](const auto& func) { _system_impl->call_user_callback(func); });
}

Gimbal::Result
GimbalImpl::gimbal_result_from_command_result(MavlinkCommandSender::Result command_result)
{
Expand Down
3 changes: 3 additions & 0 deletions src/mavsdk/plugins/gimbal/gimbal_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class GimbalImpl : public PluginImplBase {
const GimbalImpl& operator=(const GimbalImpl&) = delete;

private:
void receive_attitude_update(Gimbal::Attitude attitude);
void receive_control_status_update(Gimbal::ControlStatus control_status);

std::unique_ptr<GimbalProtocolBase> _gimbal_protocol{nullptr};

void* _protocol_cookie{nullptr};
Expand Down
14 changes: 7 additions & 7 deletions src/mavsdk/plugins/info/info_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void InfoImpl::enable()

if (!_flight_info_subscriptions.empty()) {
// We're hoping to get flight information regularly to update flight time.
_system_impl->set_msg_rate(MAVLINK_MSG_ID_FLIGHT_INFORMATION, 1.0);
_system_impl->set_msg_rate_async(MAVLINK_MSG_ID_FLIGHT_INFORMATION, 1.0, nullptr);
}
}

Expand Down Expand Up @@ -321,20 +321,20 @@ void InfoImpl::wait_for_identification() const
Info::FlightInformationHandle
InfoImpl::subscribe_flight_information(const Info::FlightInformationCallback& callback)
{
std::lock_guard<std::mutex> lock(_mutex);

// Make sure we get the message regularly.
_system_impl->set_msg_rate(MAVLINK_MSG_ID_FLIGHT_INFORMATION, 1.0);
_system_impl->set_msg_rate_async(MAVLINK_MSG_ID_FLIGHT_INFORMATION, 1.0, nullptr);

std::lock_guard<std::mutex> lock(_mutex);

return _flight_info_subscriptions.subscribe(callback);
}

void InfoImpl::unsubscribe_flight_information(Info::FlightInformationHandle handle)
{
std::lock_guard<std::mutex> lock(_mutex);

// Reset message to default
_system_impl->set_msg_rate(MAVLINK_MSG_ID_FLIGHT_INFORMATION, 0.0);
_system_impl->set_msg_rate_async(MAVLINK_MSG_ID_FLIGHT_INFORMATION, 0.0, nullptr);

std::lock_guard<std::mutex> lock(_mutex);

_flight_info_subscriptions.unsubscribe(handle);
}
Expand Down
Loading