Skip to content

Commit

Permalink
action: add API to set current speed
Browse files Browse the repository at this point in the history
This is an API that allows to temporarily set the speed of the drone,
e.g. for mission mode or in goto/reposition mode.
  • Loading branch information
julianoes committed Mar 23, 2022
1 parent c4b06fb commit 7b314f9
Show file tree
Hide file tree
Showing 12 changed files with 1,256 additions and 88 deletions.
2 changes: 1 addition & 1 deletion proto
17 changes: 14 additions & 3 deletions src/integration_tests/action_goto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,23 @@ TEST_F(SitlTest, PX4ActionGoto)

action_ret = action->takeoff();
EXPECT_EQ(action_ret, Action::Result::Success);
std::this_thread::sleep_for(std::chrono::seconds(2));
std::this_thread::sleep_for(std::chrono::seconds(4));

// Go somewhere
action->goto_location(47.398000, 8.545592, NAN, NAN);
std::this_thread::sleep_for(std::chrono::seconds(10));
action->goto_location(47.4, 8.545592, NAN, NAN);
std::this_thread::sleep_for(std::chrono::seconds(4));

LogInfo() << "Go slow for a bit.";
// Go slow for a bit
EXPECT_EQ(Action::Result::Success, action->set_current_speed(1.0f));
std::this_thread::sleep_for(std::chrono::seconds(4));

LogInfo() << "Go fast again.";
// Then faster again
EXPECT_EQ(Action::Result::Success, action->set_current_speed(5.0f));
std::this_thread::sleep_for(std::chrono::seconds(4));

LogInfo() << "Go back.";
// And back
action->goto_location(47.3977233, 8.545592, NAN, NAN);
std::this_thread::sleep_for(std::chrono::seconds(10));
Expand Down
10 changes: 10 additions & 0 deletions src/mavsdk/plugins/action/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ Action::Result Action::set_return_to_launch_altitude(float relative_altitude_m)
return _impl->set_return_to_launch_altitude(relative_altitude_m);
}

void Action::set_current_speed_async(float speed_m_s, const ResultCallback callback)
{
_impl->set_current_speed_async(speed_m_s, callback);
}

Action::Result Action::set_current_speed(float speed_m_s) const
{
return _impl->set_current_speed(speed_m_s);
}

std::ostream& operator<<(std::ostream& str, Action::Result const& result)
{
switch (result) {
Expand Down
27 changes: 27 additions & 0 deletions src/mavsdk/plugins/action/action_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,33 @@ std::pair<Action::Result, float> ActionImpl::get_return_to_launch_altitude() con
result.second);
}

void ActionImpl::set_current_speed_async(float speed_m_s, const Action::ResultCallback& callback)
{
MavlinkCommandSender::CommandLong command{};

command.command = MAV_CMD_DO_CHANGE_SPEED;
command.params.maybe_param1 = 1.0f; // ground speed
command.params.maybe_param2 = speed_m_s;
command.params.maybe_param3 = -1.0f; // no throttle set
command.params.maybe_param4 = 0.0f; // reserved
command.target_component_id = _parent->get_autopilot_id();

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

Action::Result ActionImpl::set_current_speed(float speed_m_s)
{
auto prom = std::promise<Action::Result>();
auto fut = prom.get_future();

set_current_speed_async(speed_m_s, [&prom](Action::Result result) { prom.set_value(result); });

return fut.get();
}

Action::Result ActionImpl::action_result_from_command_result(MavlinkCommandSender::Result result)
{
switch (result) {
Expand Down
3 changes: 3 additions & 0 deletions src/mavsdk/plugins/action/action_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class ActionImpl : public PluginImplBase {
void get_return_to_launch_altitude_async(
const Action::GetReturnToLaunchAltitudeCallback& callback) const;

void set_current_speed_async(float speed_m_s, const Action::ResultCallback& callback);
Action::Result set_current_speed(float speed_m_s);

Action::Result set_return_to_launch_altitude(const float relative_altitude_m) const;
std::pair<Action::Result, float> get_return_to_launch_altitude() const;

Expand Down
22 changes: 22 additions & 0 deletions src/mavsdk/plugins/action/include/plugins/action/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,28 @@ class Action : public PluginBase {
*/
Result set_return_to_launch_altitude(float relative_altitude_m) const;

/**
* @brief Set current speed.
*
* This will set the speed during a mission, reposition, and similar.
* It is ephemeral, so not stored on the drone and does not survive a reboot.
*
* This function is non-blocking. See 'set_current_speed' for the blocking counterpart.
*/
void set_current_speed_async(float speed_m_s, const ResultCallback callback);

/**
* @brief Set current speed.
*
* This will set the speed during a mission, reposition, and similar.
* It is ephemeral, so not stored on the drone and does not survive a reboot.
*
* This function is blocking. See 'set_current_speed_async' for the non-blocking counterpart.
*
* @return Result of request.
*/
Result set_current_speed(float speed_m_s) const;

/**
* @brief Copy constructor.
*/
Expand Down
1 change: 1 addition & 0 deletions src/mavsdk/plugins/action/mocks/action_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class MockAction {
MOCK_CONST_METHOD1(set_maximum_speed, Action::Result(float)){};
MOCK_CONST_METHOD0(get_return_to_launch_altitude, std::pair<Action::Result, float>()){};
MOCK_CONST_METHOD1(set_return_to_launch_altitude, Action::Result(float)){};
MOCK_CONST_METHOD1(set_current_speed, Action::Result(float)){};
};

} // namespace testing
Expand Down
42 changes: 42 additions & 0 deletions src/mavsdk_server/src/generated/action/action.grpc.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7b314f9

Please sign in to comment.