Skip to content

Commit

Permalink
Add a Takeoff class to handle multicopter takeoff
Browse files Browse the repository at this point in the history
In a deterministic way with clear states to go through.
  • Loading branch information
MaEtUgR authored and LorenzMeier committed May 22, 2019
1 parent da533a7 commit ad6eb19
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 72 deletions.
4 changes: 4 additions & 0 deletions src/modules/mc_pos_control/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################

add_subdirectory(Takeoff)

px4_add_module(
MODULE modules__mc_pos_control
MAIN mc_pos_control
Expand All @@ -46,4 +49,5 @@ px4_add_module(
ecl_geo
WeatherVane
CollisionPrevention
Takeoff
)
42 changes: 42 additions & 0 deletions src/modules/mc_pos_control/Takeoff/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
############################################################################
#
# Copyright (c) 2019 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################

px4_add_library(Takeoff
Takeoff.cpp
)
target_include_directories(Takeoff
PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}
)

px4_add_gtest(SRC TakeoffTest.cpp LINKLIBS Takeoff)
121 changes: 121 additions & 0 deletions src/modules/mc_pos_control/Takeoff/Takeoff.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/****************************************************************************
*
* Copyright (c) 2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

/**
* @file Takeoff.cpp
*/

#include "Takeoff.hpp"
#include <float.h>

void Takeoff::updateTakeoffState(const bool armed, const bool landed, const bool want_takeoff,
const float takeoff_desired_thrust, const bool skip_takeoff)
{
_spoolup_time_hysteresis.set_state_and_update(armed);

switch (_takeoff_state) {
case TakeoffState::disarmed:
if (armed) {
_takeoff_state = TakeoffState::spoolup;

} else {
break;
}

case TakeoffState::spoolup:
if (_spoolup_time_hysteresis.get_state()) {
_takeoff_state = TakeoffState::ready_for_takeoff;

} else {
break;
}

case TakeoffState::ready_for_takeoff:
if (want_takeoff) {
_takeoff_state = TakeoffState::rampup;
_takeoff_ramp_thrust = 0.0f;

} else {
break;
}

case TakeoffState::rampup:
if (_takeoff_ramp_thrust <= takeoff_desired_thrust) {
_takeoff_state = TakeoffState::flight;

} else {
break;
}

case TakeoffState::flight:
if (landed) {
_takeoff_state = TakeoffState::ready_for_takeoff;
}

break;

default:
break;
}

if (armed && skip_takeoff) {
_takeoff_state = TakeoffState::flight;
}

// TODO: need to consider free fall here
if (!armed) {
_takeoff_state = TakeoffState::disarmed;
}
}

float Takeoff::updateThrustRamp(const float takeoff_desired_thrust, const float dt)
{
if (_takeoff_state < TakeoffState::rampup) {
return 0.f;
}

if (_takeoff_state == TakeoffState::rampup) {
if (_takeoff_ramp_time > FLT_EPSILON) {
_takeoff_ramp_thrust += takeoff_desired_thrust * dt / _takeoff_ramp_time;

} else {
_takeoff_ramp_thrust = takeoff_desired_thrust;
}

if (_takeoff_ramp_thrust > takeoff_desired_thrust) {
return _takeoff_ramp_thrust;
}
}

return takeoff_desired_thrust;
}
83 changes: 83 additions & 0 deletions src/modules/mc_pos_control/Takeoff/Takeoff.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/****************************************************************************
*
* Copyright (c) 2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

/**
* @file Takeoff.hpp
*
* A class handling all takeoff states and a smooth ramp up of the motors.
*/

#pragma once

#include <systemlib/hysteresis/hysteresis.h>
#include <drivers/drv_hrt.h>

using namespace time_literals;

enum class TakeoffState {
disarmed = 0,
spoolup,
ready_for_takeoff,
rampup,
flight
};

class Takeoff
{
public:

Takeoff() = default;
~Takeoff() = default;

/**
* Update the state for the takeoff.
* @param setpoint a vehicle_local_position_setpoint_s structure
* @return true if setpoint has updated correctly
*/
void updateTakeoffState(const bool armed, const bool landed, const bool want_takeoff,
const float takeoff_desired_thrust, const bool skip_takeoff);
float updateThrustRamp(const float dt, const float takeoff_desired_thrust);

void setTakeoffRampTime(const float seconds) { _takeoff_ramp_time = seconds; }
void setSpoolupTime(const float seconds) { _spoolup_time_hysteresis.set_hysteresis_time_from(false, (hrt_abstime)(seconds * (float)1_s)); }
TakeoffState getTakeoffState() { return _takeoff_state; }

// TODO: make this private as soon as tasks also run while disarmed and updateTakeoffState gets called all the time
systemlib::Hysteresis _spoolup_time_hysteresis{false}; /**< becomes true MPC_SPOOLUP_TIME seconds after the vehicle was armed */

private:
TakeoffState _takeoff_state = TakeoffState::disarmed;

float _takeoff_ramp_time = 0.f;
float _takeoff_ramp_thrust = 0.f;
};
49 changes: 49 additions & 0 deletions src/modules/mc_pos_control/Takeoff/TakeoffTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/****************************************************************************
*
* Copyright (C) 2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

#include <gtest/gtest.h>
#include <Takeoff.hpp>

TEST(TakeoffTest, Initialization)
{
Takeoff takeoff;
EXPECT_EQ(takeoff.getTakeoffState(), TakeoffState::disarmed);
}

// TEST(TakeoffTest, Ramp)
// {
// Takeoff takeoff;
// takeoff.updateTakeoffState(true, false, true, 1.f, false);
// takeoff.updateThrustRamp(1.f, 0.1f);
// EXPECT_EQ(takeoff.getTakeoffState(), TakeoffState::disarmed);
// }
Loading

0 comments on commit ad6eb19

Please sign in to comment.