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 action features #377

Merged
merged 7 commits into from
May 3, 2018
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
163 changes: 140 additions & 23 deletions backend/src/plugins/action/action_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,178 @@ namespace dronecore {
namespace backend {

template <typename Action = Action>
class ActionServiceImpl final : public dronecore::rpc::action::ActionService::Service
class ActionServiceImpl final : public rpc::action::ActionService::Service
{
public:
ActionServiceImpl(const Action &action)
ActionServiceImpl(Action &action)
: _action(action) {}

grpc::Status Arm(grpc::ServerContext * /* context */,
const dronecore::rpc::action::ArmRequest * /* request */,
dronecore::rpc::action::ArmResponse *response)
const rpc::action::ArmRequest * /* request */,
rpc::action::ArmResponse *response) override
{
auto action_result = _action.arm();
auto rpc_result = static_cast<dronecore::rpc::action::ActionResult::Result>(action_result);

auto *rpc_action_result = new dronecore::rpc::action::ActionResult();
if (response != nullptr) {
fillResponseWithResult(response, action_result);
}

return grpc::Status::OK;
}

template <typename ResponseType>
void fillResponseWithResult(ResponseType *response, ActionResult &action_result) const
{
auto rpc_result = static_cast<rpc::action::ActionResult::Result>(action_result);

auto *rpc_action_result = new rpc::action::ActionResult();
rpc_action_result->set_result(rpc_result);
rpc_action_result->set_result_str(dronecore::action_result_str(action_result));
rpc_action_result->set_result_str(action_result_str(action_result));

response->set_allocated_action_result(rpc_action_result);
}

grpc::Status Disarm(grpc::ServerContext * /* context */,
const rpc::action::DisarmRequest * /* request */,
rpc::action::DisarmResponse *response) override
{
auto action_result = _action.disarm();

if (response != nullptr) {
fillResponseWithResult(response, action_result);
}

return grpc::Status::OK;
}

grpc::Status Takeoff(grpc::ServerContext * /* context */,
const dronecore::rpc::action::TakeoffRequest * /* request */,
dronecore::rpc::action::TakeoffResponse *response)
const rpc::action::TakeoffRequest * /* request */,
rpc::action::TakeoffResponse *response) override
{
auto action_result = _action.takeoff();
auto rpc_result = static_cast<dronecore::rpc::action::ActionResult::Result>(action_result);

auto *rpc_action_result = new dronecore::rpc::action::ActionResult();
rpc_action_result->set_result(rpc_result);
rpc_action_result->set_result_str(dronecore::action_result_str(action_result));

response->set_allocated_action_result(rpc_action_result);
if (response != nullptr) {
fillResponseWithResult(response, action_result);
}

return grpc::Status::OK;
}

grpc::Status Land(grpc::ServerContext * /* context */,
const dronecore::rpc::action::LandRequest * /* request */,
dronecore::rpc::action::LandResponse *response)
const rpc::action::LandRequest * /* request */,
rpc::action::LandResponse *response) override
{
auto action_result = _action.land();
auto rpc_result = static_cast<dronecore::rpc::action::ActionResult::Result>(action_result);

auto *rpc_action_result = new dronecore::rpc::action::ActionResult();
rpc_action_result->set_result(rpc_result);
rpc_action_result->set_result_str(dronecore::action_result_str(action_result));
if (response != nullptr) {
fillResponseWithResult(response, action_result);
}

response->set_allocated_action_result(rpc_action_result);
return grpc::Status::OK;
}

grpc::Status Kill(grpc::ServerContext * /* context */,
const rpc::action::KillRequest * /* request */,
rpc::action::KillResponse *response) override
{
auto action_result = _action.kill();

if (response != nullptr) {
fillResponseWithResult(response, action_result);
}

return grpc::Status::OK;
}

grpc::Status ReturnToLaunch(grpc::ServerContext * /* context */,
const rpc::action::ReturnToLaunchRequest * /* request */,
rpc::action::ReturnToLaunchResponse *response) override
{
auto action_result = _action.return_to_launch();

if (response != nullptr) {
fillResponseWithResult(response, action_result);
}

return grpc::Status::OK;
}

grpc::Status TransitionToFixedWings(grpc::ServerContext * /* context */,
Copy link
Collaborator

Choose a reason for hiding this comment

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

TransitionToFixedWings > TransitionToFixedWing` even though there are two wings.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Haha thanks! I never know, I guess I'm slowly adding this typo everywhere :/. I'll be careful from now on!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, I cannot change it here because it is in the proto file. So the best I can do is promise you that I will update the proto file :D.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

More than a promise: it is here: mavlink/MAVSDK-Proto#18 :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

const rpc::action::TransitionToFixedWingsRequest * /* request */,
rpc::action::TransitionToFixedWingsResponse *response) override
{
auto action_result = _action.transition_to_fixedwing();

if (response != nullptr) {
fillResponseWithResult(response, action_result);
}

return grpc::Status::OK;
}

grpc::Status TransitionToMulticopter(grpc::ServerContext * /* context */,
const rpc::action::TransitionToMulticopterRequest * /* request */,
rpc::action::TransitionToMulticopterResponse *response) override
{
auto action_result = _action.transition_to_multicopter();

if (response != nullptr) {
fillResponseWithResult(response, action_result);
}

return grpc::Status::OK;
}

grpc::Status GetTakeoffAltitude(grpc::ServerContext * /* context */,
const rpc::action::GetTakeoffAltitudeRequest * /* request */,
rpc::action::GetTakeoffAltitudeResponse *response) override
{
if (response != nullptr) {
auto takeoff_altitude = _action.get_takeoff_altitude_m();
response->set_altitude_m(takeoff_altitude);
}

return grpc::Status::OK;
}

grpc::Status SetTakeoffAltitude(grpc::ServerContext * /* context */,
const rpc::action::SetTakeoffAltitudeRequest *request,
rpc::action::SetTakeoffAltitudeResponse * /* response */) override
{
if (request != nullptr) {
const auto requested_altitude = request->altitude_m();
_action.set_takeoff_altitude(requested_altitude);
}

return grpc::Status::OK;
}

grpc::Status GetMaximumSpeed(grpc::ServerContext * /* context */,
const rpc::action::GetMaximumSpeedRequest * /* request */,
rpc::action::GetMaximumSpeedResponse *response) override
{
if (response != nullptr) {
auto max_speed = _action.get_max_speed_m_s();
response->set_speed_m_s(max_speed);
}

return grpc::Status::OK;
}

grpc::Status SetMaximumSpeed(grpc::ServerContext * /* context */,
const rpc::action::SetMaximumSpeedRequest *request,
rpc::action::SetMaximumSpeedResponse * /* response */) override
{
if (request != nullptr) {
const auto requested_speed = request->speed_m_s();
_action.set_max_speed(requested_speed);
}

return grpc::Status::OK;
}

private:
const Action &_action;
Action &_action;
};

} // namespace backend
Expand Down
Loading