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

Winch and Gripper plugins #1958

Merged
merged 5 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion proto
2 changes: 2 additions & 0 deletions src/mavsdk/core/system_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@ std::string SystemImpl::component_name(uint8_t component_id)
return "Gimbal";
case MAV_COMP_ID_MISSIONPLANNER:
return "Ground station";
case MAV_COMP_ID_WINCH:
return "Winch";
default:
return "Unsupported component";
}
Expand Down
15 changes: 15 additions & 0 deletions src/mavsdk/plugins/gripper/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
target_sources(mavsdk
PRIVATE
gripper.cpp
gripper_impl.cpp
)

target_include_directories(mavsdk PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include/mavsdk>
)

install(FILES
include/plugins/gripper/gripper.h
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/mavsdk/plugins/gripper
)
75 changes: 75 additions & 0 deletions src/mavsdk/plugins/gripper/gripper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// WARNING: THIS FILE IS AUTOGENERATED! As such, it should not be edited.
// Edits need to be made to the proto files
// (see https://github.com/mavlink/MAVSDK-Proto/blob/master/protos/gripper/gripper.proto)

#include <iomanip>

#include "gripper_impl.h"
#include "plugins/gripper/gripper.h"

namespace mavsdk {

Gripper::Gripper(System& system) : PluginBase(), _impl{std::make_unique<GripperImpl>(system)} {}

Gripper::Gripper(std::shared_ptr<System> system) :
PluginBase(),
_impl{std::make_unique<GripperImpl>(system)}
{}

Gripper::~Gripper() {}

void Gripper::grab_async(uint32_t instance, const ResultCallback callback)
{
_impl->grab_async(instance, callback);
}

Gripper::Result Gripper::grab(uint32_t instance) const
{
return _impl->grab(instance);
}

void Gripper::release_async(uint32_t instance, const ResultCallback callback)
{
_impl->release_async(instance, callback);
}

Gripper::Result Gripper::release(uint32_t instance) const
{
return _impl->release(instance);
}

std::ostream& operator<<(std::ostream& str, Gripper::Result const& result)
{
switch (result) {
case Gripper::Result::Unknown:
return str << "Unknown";
case Gripper::Result::Success:
return str << "Success";
case Gripper::Result::NoSystem:
return str << "No System";
case Gripper::Result::Busy:
return str << "Busy";
case Gripper::Result::Timeout:
return str << "Timeout";
case Gripper::Result::Unsupported:
return str << "Unsupported";
case Gripper::Result::Failed:
return str << "Failed";
default:
return str << "Unknown";
}
}

std::ostream& operator<<(std::ostream& str, Gripper::GripperAction const& gripper_action)
{
switch (gripper_action) {
case Gripper::GripperAction::Release:
return str << "Release";
case Gripper::GripperAction::Grab:
return str << "Grab";
default:
return str << "Unknown";
}
}

} // namespace mavsdk
118 changes: 118 additions & 0 deletions src/mavsdk/plugins/gripper/gripper_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include "gripper_impl.h"

namespace mavsdk {

GripperImpl::GripperImpl(System& system) : PluginImplBase(system)
{
_parent->register_plugin(this);
}

GripperImpl::GripperImpl(std::shared_ptr<System> system) : PluginImplBase(std::move(system))
{
_parent->register_plugin(this);
}

GripperImpl::~GripperImpl()
{
_parent->unregister_plugin(this);
}

void GripperImpl::init() {}

void GripperImpl::deinit() {}

void GripperImpl::enable() {}

void GripperImpl::disable() {}

void GripperImpl::grab_async(uint32_t instance, const Gripper::ResultCallback callback)
{
MavlinkCommandSender::CommandLong command{};

command.command = MAV_CMD_DO_GRIPPER;
command.params.maybe_param1 = instance;
JonasVautherin marked this conversation as resolved.
Show resolved Hide resolved
command.params.maybe_param2 = static_cast<float>(Gripper::GripperAction::Grab);

command.target_component_id = MAV_COMPONENT::MAV_COMP_ID_WINCH; // TODO
JonasVautherin marked this conversation as resolved.
Show resolved Hide resolved

_parent->send_command_async(
command, [this, callback](MavlinkCommandSender::Result result, float) {
command_result_callback(result, callback);
});
}

Gripper::Result GripperImpl::grab(uint32_t instance)
{
auto prom = std::promise<Gripper::Result>();
auto fut = prom.get_future();

grab_async(instance, [&prom](Gripper::Result result) { prom.set_value(result); });

return fut.get();
}

void GripperImpl::release_async(uint32_t instance, const Gripper::ResultCallback callback)
{
MavlinkCommandSender::CommandLong command{};

command.command = MAV_CMD_DO_GRIPPER;
command.params.maybe_param1 = instance;
JonasVautherin marked this conversation as resolved.
Show resolved Hide resolved
command.params.maybe_param2 = static_cast<float>(Gripper::GripperAction::Release);

command.target_component_id = MAV_COMPONENT::MAV_COMP_ID_WINCH; // TODO

_parent->send_command_async(
command, [this, callback](MavlinkCommandSender::Result result, float) {
command_result_callback(result, callback);
});
}

Gripper::Result GripperImpl::release(uint32_t instance)
{
auto prom = std::promise<Gripper::Result>();
auto fut = prom.get_future();

release_async(instance, [&prom](Gripper::Result result) { prom.set_value(result); });

return fut.get();
}

Gripper::Result GripperImpl::gripper_result_from_command_result(MavlinkCommandSender::Result result)
{
switch (result) {
case MavlinkCommandSender::Result::Success:
return Gripper::Result::Success;
case MavlinkCommandSender::Result::NoSystem:
return Gripper::Result::NoSystem;
case MavlinkCommandSender::Result::ConnectionError:
// Fallthrough
case MavlinkCommandSender::Result::Timeout:
return Gripper::Result::Timeout;
case MavlinkCommandSender::Result::Busy:
return Gripper::Result::Busy;
case MavlinkCommandSender::Result::Denied:
// Fallthrough
case MavlinkCommandSender::Result::TemporarilyRejected:
// Fallthrough
case MavlinkCommandSender::Result::Failed:
return Gripper::Result::Failed;
case MavlinkCommandSender::Result::Unsupported:
return Gripper::Result::Unsupported;
default:
return Gripper::Result::Unknown;
}
}

void GripperImpl::command_result_callback(
MavlinkCommandSender::Result command_result, const Gripper::ResultCallback& callback) const
{
Gripper::Result action_result = gripper_result_from_command_result(command_result);

if (callback) {
auto temp_callback = callback;
_parent->call_user_callback(
[temp_callback, action_result]() { temp_callback(action_result); });
}
}

} // namespace mavsdk
35 changes: 35 additions & 0 deletions src/mavsdk/plugins/gripper/gripper_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include "plugins/gripper/gripper.h"

#include "plugin_impl_base.h"

namespace mavsdk {

class GripperImpl : public PluginImplBase {
public:
explicit GripperImpl(System& system);
explicit GripperImpl(std::shared_ptr<System> system);

~GripperImpl() override;

void init() override;
void deinit() override;

void enable() override;
void disable() override;

void grab_async(uint32_t instance, const Gripper::ResultCallback callback);
Gripper::Result grab(uint32_t instance);

void release_async(uint32_t instance, const Gripper::ResultCallback callback);
Gripper::Result release(uint32_t instance);

private:
static Gripper::Result gripper_result_from_command_result(MavlinkCommandSender::Result result);

void command_result_callback(
MavlinkCommandSender::Result command_result, const Gripper::ResultCallback& callback) const;
};

} // namespace mavsdk
Loading