Skip to content

Commit 8cbc972

Browse files
committed
add terrain data to blackbox
1 parent 05e561d commit 8cbc972

4 files changed

Lines changed: 34 additions & 5 deletions

File tree

src/main/blackbox/blackbox.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,10 @@ static const blackboxSimpleFieldDefinition_t blackboxSlowFields[] = {
520520
{"escRPM", -1, UNSIGNED, PREDICT(0), ENCODING(UNSIGNED_VB)},
521521
{"escTemperature", -1, SIGNED, PREDICT(PREVIOUS), ENCODING(SIGNED_VB)},
522522
#endif
523+
#ifdef USE_TERRAIN
524+
{"terrainAGL", -1, UNSIGNED, PREDICT(0), ENCODING(SIGNED_VB)},
525+
{"terrainAMSL", -1, UNSIGNED, PREDICT(0), ENCODING(SIGNED_VB)},
526+
#endif
523527
};
524528

525529
#define BLACKBOX_FIRST_HEADER_SENDING_STATE BLACKBOX_STATE_SEND_HEADER
@@ -624,6 +628,10 @@ typedef struct blackboxSlowState_s {
624628
#ifdef USE_ESC_SENSOR
625629
uint32_t escRPM;
626630
int8_t escTemperature;
631+
#endif
632+
#ifdef USE_TERRAIN
633+
int32_t terrainAGL;
634+
int32_t terrainAMSL;
627635
#endif
628636
uint16_t rxUpdateRate;
629637
uint8_t activeWpNumber;
@@ -1409,6 +1417,10 @@ static void writeSlowFrame(void)
14091417
blackboxWriteUnsignedVB(slowHistory.escRPM);
14101418
blackboxWriteSignedVB(slowHistory.escTemperature);
14111419
#endif
1420+
#ifdef USE_TERRAIN
1421+
blackboxWriteSignedVB(slowHistory.terrainAGL);
1422+
blackboxWriteSignedVB(slowHistory.terrainAMSL);
1423+
#endif
14121424

14131425
blackboxSlowFrameIterationTimer = 0;
14141426
}
@@ -1484,6 +1496,10 @@ static void loadSlowState(blackboxSlowState_t *slow)
14841496
slow->escRPM = escSensor->rpm;
14851497
slow->escTemperature = escSensor->temperature;
14861498
#endif
1499+
#ifdef USE_TERRAIN
1500+
slow->terrainAGL = terrainGetLastDistanceCm();
1501+
slow->terrainAMSL = terrainGetLastAMSL();
1502+
#endif
14871503
}
14881504

14891505
/**

src/main/terrain/terrain.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ PG_RESET_TEMPLATE(terrainConfig_t, terrainConfig,
6565

6666
static struct {
6767
timeMs_t lastUpdate;
68-
int32_t terrainAGL;
68+
int32_t terrainAGL; // cm
69+
int32_t terrainAMSL; // cm
6970
} terrainHeight = {
7071
.terrainAGL = 0,
7172
.lastUpdate = 0,
73+
.terrainAMSL = 0,
7274
};
7375

7476
static struct {
@@ -182,21 +184,30 @@ void terrainUpdateTask(timeUs_t currentTimeUs)
182184
{
183185
float heightASL = getHeightAmslMeters(&gpsSol.llh);
184186
if(heightASL >= 0){
185-
terrainHeight.terrainAGL = MAX(0, ((int32_t)getEstimatedActualPosition(Z) + (int32_t)(terrainHomePos.homeAltitudeM * 100.0f)) - (int32_t)(heightASL * 100.0f));
187+
terrainHeight.terrainAMSL = (int32_t)(heightASL * 100.0f);
188+
terrainHeight.terrainAGL = MAX(0, ((int32_t)getEstimatedActualPosition(Z) + (int32_t)(terrainHomePos.homeAltitudeM * 100.0f)) - terrainHeight.terrainAMSL);
186189
terrainHeight.lastUpdate = millis();
187190
}
188191
}
189192
/////////////////////////////////////////////////////////////////////////////////////////////////////
190193
}
191194

195+
int32_t terrainGetLastAMSL(void){
196+
if(!terrainConfig()->terrainEnabled){
197+
return TERRAIN_STATUS_NO_DATA;
198+
}
199+
200+
return terrainHeight.lastUpdate + TERRAIN_NO_DATA_DELAY_MS < millis() ? TERRAIN_STATUS_NO_DATA : terrainHeight.terrainAMSL;
201+
}
202+
192203
int32_t terrainGetLastDistanceCm(void)
193204
{
194205
//start a terrain system after the first query to terrain height
195206
if(!terrainConfig()->terrainEnabled){
196207
return TERRAIN_STATUS_NO_DATA;
197208
}
198209

199-
return terrainHeight.lastUpdate + 1000 < millis() ? TERRAIN_STATUS_NO_DATA : terrainHeight.terrainAGL;
210+
return terrainHeight.lastUpdate + TERRAIN_NO_DATA_DELAY_MS < millis() ? TERRAIN_STATUS_NO_DATA : terrainHeight.terrainAGL;
200211
}
201212

202213
#endif

src/main/terrain/terrain.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "navigation/navigation.h"
3131

3232
#define TERRAIN_TASK_RATE_HZ 5
33-
#define TERRAIN_TASK_ 200
33+
#define TERRAIN_NO_DATA_DELAY_MS 1000
3434

3535
#define TERRAIN_STATUS_FAILURE (-1)
3636
#define TERRAIN_STATUS_WRONG_BLOC_SIZE (-2)
@@ -148,4 +148,6 @@ PG_DECLARE(terrainConfig_t, terrainConfig);
148148

149149
void terrainInit(void);
150150
void terrainUpdateTask(timeUs_t currentTimeUs);
151+
int32_t terrainGetLastAMSL(void);
151152
int32_t terrainGetLastDistanceCm(void);
153+

src/main/terrain/terrain_location.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ neVector_t gpsGetDistanceNE(const gpsLocation_t *a, const gpsLocation_t *b)
8484
{
8585
neVector_t v;
8686
v.north = (float)(b->lat - a->lat) * LOCATION_SCALING_FACTOR;
87-
v.east = (float)diffLongitude(b->lon, a->lon) * LOCATION_SCALING_FACTOR * longitudeScale((b->lat + b->lat)/2);
87+
v.east = (float)diffLongitude(b->lon, a->lon) * LOCATION_SCALING_FACTOR * longitudeScale((b->lat + a->lat)/2);
8888
return v;
8989
}
9090

0 commit comments

Comments
 (0)