1
0
Fork 0
mirror of https://github.com/betaflight/betaflight.git synced 2025-07-16 12:55:19 +03:00

Code allowing the use of sonar without requiring a barometer.

This code has been flight tested on a Naze32 acro, with no barometer onboard.  It also works when the target doesn't have BARO defined.
This commit is contained in:
Ben Hitchcock 2014-09-05 11:28:55 +08:00
parent f22953b37a
commit 66fce423bb
8 changed files with 72 additions and 19 deletions

View file

@ -40,6 +40,7 @@ typedef enum {
HEADFREE_MODE = (1 << 6), HEADFREE_MODE = (1 << 6),
AUTOTUNE_MODE = (1 << 7), AUTOTUNE_MODE = (1 << 7),
PASSTHRU_MODE = (1 << 8), PASSTHRU_MODE = (1 << 8),
SONAR_MODE = (1 << 9),
} flightModeFlags_e; } flightModeFlags_e;
extern uint16_t flightModeFlags; extern uint16_t flightModeFlags;

View file

@ -58,7 +58,7 @@
#include "config/config_profile.h" #include "config/config_profile.h"
#include "config/config_master.h" #include "config/config_master.h"
#ifdef BARO #if defined(BARO) || defined(SONAR)
static int16_t initialThrottleHold; static int16_t initialThrottleHold;
static void multirotorAltHold(void) static void multirotorAltHold(void)
@ -79,7 +79,7 @@ static void multirotorAltHold(void)
rcCommand[THROTTLE] = constrain(initialThrottleHold + BaroPID, masterConfig.escAndServoConfig.minthrottle, masterConfig.escAndServoConfig.maxthrottle); rcCommand[THROTTLE] = constrain(initialThrottleHold + BaroPID, masterConfig.escAndServoConfig.minthrottle, masterConfig.escAndServoConfig.maxthrottle);
} }
} else { } else {
// slow alt changes for apfags // slow alt changes, mostly used for aerial photography
if (abs(rcCommand[THROTTLE] - initialThrottleHold) > currentProfile->alt_hold_deadband) { if (abs(rcCommand[THROTTLE] - initialThrottleHold) > currentProfile->alt_hold_deadband) {
// set velocity proportional to stick movement +100 throttle gives ~ +50 cm/s // set velocity proportional to stick movement +100 throttle gives ~ +50 cm/s
setVelocity = (rcCommand[THROTTLE] - initialThrottleHold) / 2; setVelocity = (rcCommand[THROTTLE] - initialThrottleHold) / 2;
@ -128,4 +128,20 @@ void updateAltHoldState(void)
} }
} }
void updateSonarAltHoldState(void)
{
// Sonar alt hold activate
if (rcOptions[BOXSONAR]) {
if (!FLIGHT_MODE(SONAR_MODE)) {
ENABLE_FLIGHT_MODE(SONAR_MODE);
AltHold = EstAlt;
initialThrottleHold = rcCommand[THROTTLE];
errorVelocityI = 0;
BaroPID = 0; // TODO: Change naming of BaroPID to "AltPID" as this is used by both sonar and baro
}
} else {
DISABLE_FLIGHT_MODE(SONAR_MODE);
}
}
#endif #endif

View file

@ -18,4 +18,5 @@
void updateAltHold(void); void updateAltHold(void);
void updateAltHoldState(void); void updateAltHoldState(void);
void updateSonarAltHoldState(void);

View file

@ -372,7 +372,8 @@ int16_t calculateThrottleAngleCorrection(uint8_t throttle_correction_value)
return lrintf(throttle_correction_value * sinf(angle / (900.0f * M_PI / 2.0f))); return lrintf(throttle_correction_value * sinf(angle / (900.0f * M_PI / 2.0f)));
} }
#ifdef BARO #if defined(BARO) || defined(SONAR)
// 40hz update rate (20hz LPF on acc) // 40hz update rate (20hz LPF on acc)
#define BARO_UPDATE_FREQUENCY_40HZ (1000 * 25) #define BARO_UPDATE_FREQUENCY_40HZ (1000 * 25)
@ -453,6 +454,7 @@ void calculateEstimatedAltitude(uint32_t currentTime)
previousTime = currentTime; previousTime = currentTime;
#ifdef BARO
if (!isBaroCalibrationComplete()) { if (!isBaroCalibrationComplete()) {
performBaroCalibrationCycle(); performBaroCalibrationCycle();
vel = 0; vel = 0;
@ -460,6 +462,10 @@ void calculateEstimatedAltitude(uint32_t currentTime)
} }
BaroAlt = baroCalculateAltitude(); BaroAlt = baroCalculateAltitude();
#else
BaroAlt = 0;
#endif
#ifdef SONAR #ifdef SONAR
tiltAngle = calculateTiltAngle(&inclination); tiltAngle = calculateTiltAngle(&inclination);
@ -496,9 +502,11 @@ void calculateEstimatedAltitude(uint32_t currentTime)
accSum_reset(); accSum_reset();
#ifdef BARO
if (!isBaroCalibrationComplete()) { if (!isBaroCalibrationComplete()) {
return; return;
} }
#endif
if (sonarAlt > 0 && sonarAlt < 200) { if (sonarAlt > 0 && sonarAlt < 200) {
// the sonar has the best range // the sonar has the best range

View file

@ -22,7 +22,7 @@ enum {
BOXANGLE, BOXANGLE,
BOXHORIZON, BOXHORIZON,
BOXBARO, BOXBARO,
// BOXVARIO, BOXSONAR,
BOXMAG, BOXMAG,
BOXHEADFREE, BOXHEADFREE,
BOXHEADADJ, BOXHEADADJ,

View file

@ -149,7 +149,7 @@ static const box_t const boxes[] = {
{ BOXANGLE, "ANGLE;", 1 }, { BOXANGLE, "ANGLE;", 1 },
{ BOXHORIZON, "HORIZON;", 2 }, { BOXHORIZON, "HORIZON;", 2 },
{ BOXBARO, "BARO;", 3 }, { BOXBARO, "BARO;", 3 },
//{ BOXVARIO, "VARIO;", 4 }, { BOXSONAR, "SONAR;", 4 },
{ BOXMAG, "MAG;", 5 }, { BOXMAG, "MAG;", 5 },
{ BOXHEADFREE, "HEADFREE;", 6 }, { BOXHEADFREE, "HEADFREE;", 6 },
{ BOXHEADADJ, "HEADADJ;", 7 }, { BOXHEADADJ, "HEADADJ;", 7 },
@ -421,6 +421,10 @@ void mspInit(serialConfig_t *serialConfig)
activeBoxIds[activeBoxIdCount++] = BOXBARO; activeBoxIds[activeBoxIdCount++] = BOXBARO;
} }
if (feature(FEATURE_SONAR)){
activeBoxIds[activeBoxIdCount++] = BOXSONAR;
}
if (sensors(SENSOR_ACC) || sensors(SENSOR_MAG)) { if (sensors(SENSOR_ACC) || sensors(SENSOR_MAG)) {
activeBoxIds[activeBoxIdCount++] = BOXMAG; activeBoxIds[activeBoxIdCount++] = BOXMAG;
activeBoxIds[activeBoxIdCount++] = BOXHEADFREE; activeBoxIds[activeBoxIdCount++] = BOXHEADFREE;
@ -491,6 +495,7 @@ static bool processOutCommand(uint8_t cmdMSP)
tmp = IS_ENABLED(FLIGHT_MODE(ANGLE_MODE)) << BOXANGLE | tmp = IS_ENABLED(FLIGHT_MODE(ANGLE_MODE)) << BOXANGLE |
IS_ENABLED(FLIGHT_MODE(HORIZON_MODE)) << BOXHORIZON | IS_ENABLED(FLIGHT_MODE(HORIZON_MODE)) << BOXHORIZON |
IS_ENABLED(FLIGHT_MODE(BARO_MODE)) << BOXBARO | IS_ENABLED(FLIGHT_MODE(BARO_MODE)) << BOXBARO |
IS_ENABLED(FLIGHT_MODE(SONAR_MODE)) << BOXSONAR |
IS_ENABLED(FLIGHT_MODE(MAG_MODE)) << BOXMAG | IS_ENABLED(FLIGHT_MODE(MAG_MODE)) << BOXMAG |
IS_ENABLED(FLIGHT_MODE(HEADFREE_MODE)) << BOXHEADFREE | IS_ENABLED(FLIGHT_MODE(HEADFREE_MODE)) << BOXHEADFREE |
rcOptions[BOXHEADADJ] << BOXHEADADJ | rcOptions[BOXHEADADJ] << BOXHEADADJ |

View file

@ -375,10 +375,12 @@ typedef enum {
#endif #endif
#ifdef BARO #ifdef BARO
UPDATE_BARO_TASK, UPDATE_BARO_TASK,
CALCULATE_ALTITUDE_TASK,
#endif #endif
#ifdef SONAR #ifdef SONAR
UPDATE_SONAR_TASK, UPDATE_SONAR_TASK,
#endif
#if defined(BARO) || defined(SONAR)
CALCULATE_ALTITUDE_TASK,
#endif #endif
UPDATE_GPS_TASK UPDATE_GPS_TASK
} periodicTasks; } periodicTasks;
@ -405,9 +407,20 @@ void executePeriodicTasks(void)
baroUpdate(currentTime); baroUpdate(currentTime);
} }
break; break;
#endif
#if defined(BARO) || defined(SONAR)
case CALCULATE_ALTITUDE_TASK: case CALCULATE_ALTITUDE_TASK:
#if defined(BARO) && !defined(SONAR)
if (sensors(SENSOR_BARO) && isBaroReady()) { if (sensors(SENSOR_BARO) && isBaroReady()) {
#endif
#if defined(BARO) && defined(SONAR)
if ((sensors(SENSOR_BARO) && isBaroReady()) || sensors(SENSOR_SONAR)) {
#endif
#if !defined(BARO) && defined(SONAR)
if (sensors(SENSOR_SONAR)) {
#endif
calculateEstimatedAltitude(currentTime); calculateEstimatedAltitude(currentTime);
} }
break; break;
@ -548,7 +561,7 @@ void processRx(void)
void loop(void) void loop(void)
{ {
static uint32_t loopTime; static uint32_t loopTime;
#ifdef BARO #if defined(BARO) || defined(SONAR)
static bool haveProcessedAnnexCodeOnce = false; static bool haveProcessedAnnexCodeOnce = false;
#endif #endif
@ -565,6 +578,16 @@ void loop(void)
} }
} }
#endif #endif
#ifdef SONAR
// the 'annexCode' initialses rcCommand, updateAltHoldState depends on valid rcCommand data.
if (haveProcessedAnnexCodeOnce) {
if (sensors(SENSOR_SONAR)) {
updateSonarAltHoldState();
}
}
#endif
} else { } else {
// not processing rx this iteration // not processing rx this iteration
executePeriodicTasks(); executePeriodicTasks();
@ -582,7 +605,7 @@ void loop(void)
previousTime = currentTime; previousTime = currentTime;
annexCode(); annexCode();
#ifdef BARO #if defined(BARO) || defined(SONAR)
haveProcessedAnnexCodeOnce = true; haveProcessedAnnexCodeOnce = true;
#endif #endif
@ -596,13 +619,12 @@ void loop(void)
} }
#endif #endif
#ifdef BARO #if defined(BARO) || defined(SONAR)
if (sensors(SENSOR_BARO)) { if (sensors(SENSOR_BARO) || sensors(SENSOR_SONAR)) {
if (FLIGHT_MODE(BARO_MODE)) { if (FLIGHT_MODE(BARO_MODE) || FLIGHT_MODE(SONAR_MODE)) {
updateAltHold(); updateAltHold();
} }
} }
#endif #endif
if (currentProfile->throttle_correction_value && (FLIGHT_MODE(ANGLE_MODE) || FLIGHT_MODE(HORIZON_MODE))) { if (currentProfile->throttle_correction_value && (FLIGHT_MODE(ANGLE_MODE) || FLIGHT_MODE(HORIZON_MODE))) {

View file

@ -363,8 +363,8 @@ static void detectBaro()
return; return;
} }
#endif #endif
sensorsClear(SENSOR_BARO);
#endif #endif
sensorsClear(SENSOR_BARO);
} }
void reconfigureAlignment(sensorAlignmentConfig_t *sensorAlignmentConfig) void reconfigureAlignment(sensorAlignmentConfig_t *sensorAlignmentConfig)