Skip to content

Commit d53dada

Browse files
committed
LP-598 Followed Laurent's suggestions that makes it possible to remove duplicated code
- Update assessAltitude() to return the altitude in auto takeoff as well as auto land code. Code that duplicated the calculations has been removed. - Make pretty & update headers
1 parent ec9b867 commit d53dada

5 files changed

Lines changed: 31 additions & 36 deletions

File tree

flight/modules/PathFollower/inc/vtolautotakeofffsm.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
* @addtogroup LibrePilotModules LibrePilot Modules
44
* @{
55
* @addtogroup PathFollower FSM
6-
* @brief Executes landing sequence via an FSM
6+
* @brief Executes auto takeoff sequence via an FSM
77
* @{
88
*
9-
* @file vtollandfsm.h
10-
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
9+
* @file vtolautotakeofffsm.h
10+
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016-2018
1111
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
12-
* @brief Executes FSM for landing sequence
12+
* @brief Executes FSM for auto takeoff sequence
1313
*
1414
* @see The GNU Public License (GPL) Version 3
1515
*
@@ -144,7 +144,7 @@ class VtolAutoTakeoffFSM : public PathFollowerFSM {
144144
int32_t runAlways();
145145

146146
void updateVtolAutoTakeoffFSMStatus();
147-
void assessAltitude(void);
147+
float assessAltitude(void);
148148

149149
void setStateTimeout(int32_t count);
150150

flight/modules/PathFollower/inc/vtollandfsm.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
* @{
88
*
99
* @file vtollandfsm.h
10-
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
10+
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2018
11+
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
1112
* @brief Executes FSM for landing sequence
1213
*
1314
* @see The GNU Public License (GPL) Version 3
@@ -133,7 +134,7 @@ class VtolLandFSM : public PathFollowerFSM {
133134
int32_t runState();
134135
int32_t runAlways();
135136
void updateVtolLandFSMStatus();
136-
void assessAltitude(void);
137+
float assessAltitude(void);
137138

138139
void setStateTimeout(int32_t count);
139140

flight/modules/PathFollower/vtolautotakeoffcontroller.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
22
******************************************************************************
33
*
4-
* @file vtollandcontroller.cpp
5-
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
4+
* @file vtolautotakeoffcontroller.cpp
5+
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016-2018
66
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
7-
* @brief Vtol landing controller loop
7+
* @brief Vtol auto takeoff controller loop
88
* @see The GNU Public License (GPL) Version 3
99
* @addtogroup LibrePilot LibrePilotModules Modules PathFollower Navigation
1010
*

flight/modules/PathFollower/vtolautotakeofffsm.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
******************************************************************************
33
*
44
* @file vtolautotakeofffsm.cpp
5-
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
5+
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2018
6+
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
67
* @brief This autotakeoff state machine is a helper state machine to the
78
* VtolAutoTakeoffController.
89
* @see The GNU Public License (GPL) Version 3
@@ -60,11 +61,11 @@ extern "C" {
6061

6162

6263
// Private constants
63-
#define TIMER_COUNT_PER_SECOND (1000 / vtolPathFollowerSettings->UpdatePeriod)
64-
#define TIMEOUT_SLOWSTART (2 * TIMER_COUNT_PER_SECOND)
65-
#define TIMEOUT_THRUSTUP (1 * TIMER_COUNT_PER_SECOND)
66-
#define TIMEOUT_THRUSTDOWN (5 * TIMER_COUNT_PER_SECOND)
67-
#define AUTOTAKEOFFING_SLOWDOWN_HEIGHT -5.0f
64+
#define TIMER_COUNT_PER_SECOND (1000 / vtolPathFollowerSettings->UpdatePeriod)
65+
#define TIMEOUT_SLOWSTART (2 * TIMER_COUNT_PER_SECOND)
66+
#define TIMEOUT_THRUSTUP (1 * TIMER_COUNT_PER_SECOND)
67+
#define TIMEOUT_THRUSTDOWN (5 * TIMER_COUNT_PER_SECOND)
68+
#define AUTOTAKEOFF_SLOWDOWN_HEIGHT -5.0f
6869

6970
VtolAutoTakeoffFSM::PathFollowerFSM_AutoTakeoffStateHandler_T VtolAutoTakeoffFSM::sAutoTakeoffStateTable[AUTOTAKEOFF_STATE_SIZE] = {
7071
[AUTOTAKEOFF_STATE_INACTIVE] = { .setup = &VtolAutoTakeoffFSM::setup_inactive, .run = 0 },
@@ -246,14 +247,8 @@ void VtolAutoTakeoffFSM::setState(StatusVtolAutoTakeoffStateOptions newState, St
246247
mAutoTakeoffData->currentState = newState;
247248

248249
if (newState != STATUSVTOLAUTOTAKEOFF_STATE_INACTIVE) {
249-
PositionStateData positionState;
250-
PositionStateGet(&positionState);
251-
float altitudeAboveGround = 0.0f;
252-
if (mAutoTakeoffData->takeOffLocation.Status == TAKEOFFLOCATION_STATUS_VALID) {
253-
altitudeAboveGround = mAutoTakeoffData->takeOffLocation.Down - positionState.Down;
254-
}
255-
mAutoTakeoffData->fsmAutoTakeoffStatus.AltitudeAtState[newState] = altitudeAboveGround;
256-
assessAltitude();
250+
float altitudeAboveTakeoff = assessAltitude();
251+
mAutoTakeoffData->fsmAutoTakeoffStatus.AltitudeAtState[newState] = altitudeAboveTakeoff;
257252
}
258253

259254
// Restart state timer counter
@@ -290,7 +285,7 @@ void VtolAutoTakeoffFSM::updateVtolAutoTakeoffFSMStatus()
290285
}
291286

292287

293-
void VtolAutoTakeoffFSM::assessAltitude(void)
288+
float VtolAutoTakeoffFSM::assessAltitude(void)
294289
{
295290
float positionDown;
296291

@@ -300,11 +295,13 @@ void VtolAutoTakeoffFSM::assessAltitude(void)
300295
takeOffDown = mAutoTakeoffData->takeOffLocation.Down;
301296
}
302297
float positionDownRelativeToTakeoff = positionDown - takeOffDown;
303-
if (positionDownRelativeToTakeoff < AUTOTAKEOFFING_SLOWDOWN_HEIGHT) {
298+
if (positionDownRelativeToTakeoff < AUTOTAKEOFF_SLOWDOWN_HEIGHT) {
304299
mAutoTakeoffData->flLowAltitude = false;
305300
} else {
306301
mAutoTakeoffData->flLowAltitude = true;
307302
}
303+
// Return the altitude above takeoff, which is the negation of positionDownRelativeToTakeoff
304+
return -positionDownRelativeToTakeoff;
308305
}
309306

310307
// Action the required state from plans.c

flight/modules/PathFollower/vtollandfsm.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
******************************************************************************
33
*
44
* @file vtollandfsm.cpp
5-
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
5+
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2018
6+
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2015.
67
* @brief This landing state machine is a helper state machine to the
78
* VtolLandController.
89
* @see The GNU Public License (GPL) Version 3
@@ -275,14 +276,8 @@ void VtolLandFSM::setState(StatusVtolLandStateOptions newState, StatusVtolLandSt
275276
mLandData->currentState = newState;
276277

277278
if (newState != STATUSVTOLLAND_STATE_INACTIVE) {
278-
PositionStateData positionState;
279-
PositionStateGet(&positionState);
280-
float takeOffDown = 0.0f;
281-
if (mLandData->takeOffLocation.Status == TAKEOFFLOCATION_STATUS_VALID) {
282-
takeOffDown = mLandData->takeOffLocation.Down;
283-
}
284-
mLandData->fsmLandStatus.AltitudeAtState[newState] = positionState.Down - takeOffDown;
285-
assessAltitude();
279+
float altitudeAboveTakeoff = assessAltitude();
280+
mLandData->fsmLandStatus.AltitudeAtState[newState] = altitudeAboveTakeoff;
286281
}
287282

288283
// Restart state timer counter
@@ -332,7 +327,7 @@ float VtolLandFSM::BoundVelocityDown(float velocity_down)
332327
}
333328
}
334329

335-
void VtolLandFSM::assessAltitude(void)
330+
float VtolLandFSM::assessAltitude(void)
336331
{
337332
float positionDown;
338333

@@ -347,6 +342,8 @@ void VtolLandFSM::assessAltitude(void)
347342
} else {
348343
mLandData->flLowAltitude = true;
349344
}
345+
// Return the altitude above takeoff, which is the negation of positionDownRelativeToTakeoff
346+
return -positionDownRelativeToTakeoff;
350347
}
351348

352349

0 commit comments

Comments
 (0)