Significantly improved adsb demod

Change preamble detection, reduce cpu load
This commit is contained in:
heurist1 2021-10-15 14:26:33 +01:00
parent 0fed64636a
commit be01ca6da1
8 changed files with 6632 additions and 80 deletions

View File

@ -288,9 +288,10 @@ void ADSBRxView::on_frame(const ADSBFrameMessage * message) {
// we received has the same ICAO address, i.e. belongs to
// the same aircraft:
// FIXSBT does this still cause too many refreshes
if(send_updates && details_view->get_current_entry().ICAO_address == ICAO_address) {
details_view->update(entry);
}
//if(send_updates && details_view->get_current_entry().ICAO_address == ICAO_address) {
//if (send_updates && detailed_entry_key == ICAO_address) {
// details_view->update(entry);
//}
}
} else if(msg_type == AIRBORNE_VEL && msg_sub >= VEL_GND_SUBSONIC && msg_sub <= VEL_AIR_SUPERSONIC){
entry.set_frame_velo(frame);
@ -300,9 +301,10 @@ void ADSBRxView::on_frame(const ADSBFrameMessage * message) {
// same here:
// FIXSBT does this still cause too many refreshes
if (send_updates && details_view->get_current_entry().ICAO_address == ICAO_address) {
details_view->update(entry);
}
//if (send_updates && details_view->get_current_entry().ICAO_address == ICAO_address) {
//if (send_updates && detailed_entry_key == ICAO_address) {
// details_view->update(entry);
//}
}
}
replace_entry(entry);
@ -324,15 +326,15 @@ void ADSBRxView::on_tick_second() {
entry.inc_age();
if (details_view) {
if (send_updates && (entry.key() == detailed_entry_key))
if (send_updates && (entry.key() == detailed_entry_key)) // Check if the ICAO address match
details_view->update(entry);
}
// FIXSBT following block is new, check if it is required
else
{
if ((entry.age == ADSB_DECAY_A) || (entry.age == ADSB_DECAY_B))
recent_entries_view.set_dirty();
}
//else
//{
// if ((entry.age == ADSB_DECAY_A) || (entry.age == ADSB_DECAY_B))
// recent_entries_view.set_dirty();
//}
}
// Sort the list if it is being displayed

View File

@ -167,6 +167,8 @@ public:
void update(const AircraftRecentEntry& entry);
std::string title() const override { return "Details"; };
AircraftRecentEntry get_current_entry() { return entry_copy; }
private:
AircraftRecentEntry entry_copy { 0 };

View File

@ -0,0 +1,301 @@
/*
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2017 Furrtek
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ui.hpp"
#include "ui_receiver.hpp"
#include "ui_geomap.hpp"
#include "ui_font_fixed_8x16.hpp"
#include "file.hpp"
#include "recent_entries.hpp"
#include "log_file.hpp"
#include "adsb.hpp"
#include "message.hpp"
#include "crc.hpp"
using namespace adsb;
namespace ui {
#define ADSB_DECAY_A 10 // In seconds
#define ADSB_DECAY_B 30
#define ADSB_DECAY_C 60 // Can be used for removing old entries, RecentEntries already caps to 64
#define AIRCRAFT_ID_L 1 // aircraft ID message type (lowest type id)
#define AIRCRAFT_ID_H 4 // aircraft ID message type (highest type id)
#define SURFACE_POS_L 5 // surface position (lowest type id)
#define SURFACE_POS_H 8 // surface position (highest type id)
#define AIRBORNE_POS_BARO_L 9 // airborne position (lowest type id)
#define AIRBORNE_POS_BARO_H 18 // airborne position (highest type id)
#define AIRBORNE_VEL 19 // airborne velocities
#define AIRBORNE_POS_GPS_L 20 // airborne position (lowest type id)
#define AIRBORNE_POS_GPS_H 22 // airborne position (highest type id)
#define RESERVED_L 23 // reserved for other uses
#define RESERVED_H 31 // reserved for other uses
#define VEL_GND_SUBSONIC 1
#define VEL_GND_SUPERSONIC 2
#define VEL_AIR_SUBSONIC 3
#define VEL_AIR_SUPERSONIC 4
#define O_E_FRAME_TIMEOUT 20 // timeout between odd and even frames
struct AircraftRecentEntry {
using Key = uint32_t;
static constexpr Key invalid_key = 0xffffffff;
uint32_t ICAO_address { };
uint16_t hits { 0 };
// FXSBT did I add age_state or 1.40
uint16_t age_state { 0 };
uint32_t age { 0 };
adsb_pos pos { false, 0, 0, 0 };
adsb_vel velo { false, 0, 999, 0 };
ADSBFrame frame_pos_even { };
ADSBFrame frame_pos_odd { };
std::string callsign { " " };
std::string time_string { "" };
std::string info_string { "" };
AircraftRecentEntry(
const uint32_t ICAO_address
) : ICAO_address { ICAO_address }
{
}
Key key() const {
return ICAO_address;
}
void set_callsign(std::string& new_callsign) {
callsign = new_callsign;
}
void inc_hit() {
hits++;
}
void set_frame_pos(ADSBFrame& frame, uint32_t parity) {
if (!parity)
frame_pos_even = frame;
else
frame_pos_odd = frame;
if (!frame_pos_even.empty() && !frame_pos_odd.empty()) {
// FIXSBT shouldn't need 20 s
if (abs(frame_pos_even.get_rx_timestamp() - frame_pos_odd.get_rx_timestamp()) < O_E_FRAME_TIMEOUT)
pos = decode_frame_pos(frame_pos_even, frame_pos_odd);
}
}
void set_frame_velo(ADSBFrame& frame){
velo = decode_frame_velo(frame);
}
void set_info_string(std::string& new_info_string) {
info_string = new_info_string;
}
void set_time_string(std::string& new_time_string) {
time_string = new_time_string;
}
void reset_age() {
age = 0;
}
void inc_age() {
age++;
age_state = (age < ADSB_DECAY_A) ? 0 : (age < ADSB_DECAY_B) ? 1 : 2;
}
};
using AircraftRecentEntries = RecentEntries<AircraftRecentEntry>;
class ADSBLogger {
public:
Optional<File::Error> append(const std::filesystem::path& filename) {
return log_file.append(filename);
}
void log_str(std::string& logline);
private:
LogFile log_file { };
};
class ADSBRxDetailsView : public View {
public:
ADSBRxDetailsView(NavigationView&, const AircraftRecentEntry& entry, const std::function<void(void)> on_close);
~ADSBRxDetailsView();
ADSBRxDetailsView(const ADSBRxDetailsView&) = delete;
ADSBRxDetailsView(ADSBRxDetailsView&&) = delete;
ADSBRxDetailsView& operator=(const ADSBRxDetailsView&) = delete;
ADSBRxDetailsView& operator=(ADSBRxDetailsView&&) = delete;
void focus() override;
void update(const AircraftRecentEntry& entry);
std::string title() const override { return "Details"; };
private:
AircraftRecentEntry entry_copy { 0 };
std::function<void(void)> on_close_ { };
GeoMapView* geomap_view { nullptr };
bool send_updates { false };
File db_file { };
Labels labels {
{ { 0 * 8, 1 * 16 }, "Callsign:", Color::light_grey() },
{ { 0 * 8, 2 * 16 }, "Last seen:", Color::light_grey() },
{ { 0 * 8, 3 * 16 }, "Airline:", Color::light_grey() },
{ { 0 * 8, 5 * 16 }, "Country:", Color::light_grey() },
{ { 0 * 8, 13 * 16 }, "Even position frame:", Color::light_grey() },
{ { 0 * 8, 15 * 16 }, "Odd position frame:", Color::light_grey() }
};
Text text_callsign {
{ 9 * 8, 1 * 16, 8 * 8, 16 },
"-"
};
Text text_last_seen {
{ 11 * 8, 2 * 16, 19 * 8, 16 },
"-"
};
Text text_airline {
{ 0 * 8, 4 * 16, 30 * 8, 16 },
"-"
};
Text text_country {
{ 8 * 8, 5 * 16, 22 * 8, 16 },
"-"
};
Text text_infos {
{ 0 * 8, 6 * 16, 30 * 8, 16 },
"-"
};
Text text_info2 {
{0*8, 7*16, 30*8, 16},
"-"
};
Text text_frame_pos_even {
{ 0 * 8, 14 * 16, 30 * 8, 16 },
"-"
};
Text text_frame_pos_odd {
{ 0 * 8, 16 * 16, 30 * 8, 16 },
"-"
};
Button button_see_map {
{ 8 * 8, 9 * 16, 14 * 8, 3 * 16 },
"See on map"
};
};
class ADSBRxView : public View {
public:
ADSBRxView(NavigationView& nav);
~ADSBRxView();
ADSBRxView(const ADSBRxView&) = delete;
ADSBRxView(ADSBRxView&&) = delete;
ADSBRxView& operator=(const ADSBRxView&) = delete;
ADSBRxView& operator=(ADSBRxView&&) = delete;
void focus() override;
std::string title() const override { return "ADS-B receive"; };
void replace_entry(AircraftRecentEntry & entry);
AircraftRecentEntry find_or_create_entry(uint32_t ICAO_address);
void sort_entries_by_state();
private:
std::unique_ptr<ADSBLogger> logger { };
void on_frame(const ADSBFrameMessage * message);
void on_tick_second();
const RecentEntriesColumns columns { {
{ "ICAO", 6 },
{ "Callsign", 9 },
{ "Hits", 4 },
{ "Time", 8 }
} };
AircraftRecentEntries recent { };
RecentEntriesView<RecentEntries<AircraftRecentEntry>> recent_entries_view { columns, recent };
SignalToken signal_token_tick_second { };
ADSBRxDetailsView* details_view { nullptr };
uint32_t detailed_entry_key { 0 };
bool send_updates { false };
Labels labels {
{ { 0 * 8, 0 * 8 }, "LNA: VGA: AMP:", Color::light_grey() }
};
LNAGainField field_lna {
{ 4 * 8, 0 * 16 }
};
VGAGainField field_vga {
{ 11 * 8, 0 * 16 }
};
RFAmpField field_rf_amp {
{ 18 * 8, 0 * 16 }
};
RSSI rssi {
{ 20 * 8, 4, 10 * 8, 8 },
};
MessageHandlerRegistration message_handler_frame {
Message::ID::ADSBFrame,
[this](Message* const p) {
const auto message = static_cast<const ADSBFrameMessage*>(p);
this->on_frame(message);
}
};
// FIXSBT is this used
//Dump1090Crc dump1090Crc;
};
} /* namespace ui */

View File

@ -34,68 +34,45 @@ void ADSBRXProcessor::execute(const buffer_c8_t& buffer) {
int8_t re, im;
float mag;
uint32_t c;
uint8_t level, bit, byte { };
//bool confidence;
bool first_in_window, last_in_window;
uint8_t bit, byte{};
// This is called at 2M/2048 = 977Hz
// One pulse = 500ns = 2 samples
// One bit = 2 pulses = 1us = 4 samples
if (!configured) return;
for (size_t i = 0; i < buffer.count; i++) {
// Compute sample's magnitude
re = buffer.p[i].real();
im = buffer.p[i].imag();
mag = __builtin_sqrtf((re * re) + (im * im)) * k;
// Only used for preamble detection and visualisation
level = (mag < 0.3) ? 0 : // Blank weak signals
(mag > prev_mag) ? 1 : 0;
re = buffer.p[i].real(); // make re float and scale it
im = buffer.p[i].imag(); // make re float and scale it
mag = ((re * re) + (im * im)) * (k*k);
if (decoding) {
// Decode
// 1 bit lasts 2 samples
if (sample_count & 1) {
if ((prev_mag < threshold_low) && (mag < threshold_low)) {
// Both under window, silence.
if (null_count > 3) {
const ADSBFrameMessage message(frame);
shared_memory.application_queue.push(message);
decoding = false;
} else
null_count++;
//confidence = false;
if (bit_count >= 112)
{
const ADSBFrameMessage message(frame);
shared_memory.application_queue.push(message);
decoding = false;
if (prev_mag > mag)
bit = 1;
else
bit = 0;
}
else
{
//confidence = true;
if (prev_mag > mag)
bit = 1;
else
bit = 0;
} else {
null_count = 0;
first_in_window = ((prev_mag >= threshold_low) && (prev_mag <= threshold_high));
last_in_window = ((mag >= threshold_low) && (mag <= threshold_high));
if ((first_in_window && !last_in_window) || (!first_in_window && last_in_window)) {
//confidence = true;
if (prev_mag > mag)
bit = 1;
else
bit = 0;
} else {
//confidence = false;
if (prev_mag > mag)
bit = 1;
else
bit = 0;
}
}
byte = bit | (byte << 1);
@ -104,34 +81,66 @@ void ADSBRXProcessor::execute(const buffer_c8_t& buffer) {
// Got one byte
frame.push_byte(byte);
}
}
} // Second sample of each bit
sample_count++;
} else {
// Look for preamble
// Shift
// FIXSBT make this a ring buffer
// FIXSBT store level in int16 for quick compare to preamble
for (c = 0; c < (ADSB_PREAMBLE_LENGTH - 1); c++)
{
shifter[c] = shifter[c + 1];
shifter[15] = std::make_pair(mag, level);
}
shifter[15] = mag;
// Compare
for (c = 0; c < ADSB_PREAMBLE_LENGTH; c++) {
if (shifter[c].second != adsb_preamble[c])
break;
}
if (c == ADSB_PREAMBLE_LENGTH) {
decoding = true;
sample_count = 0;
null_count = 0;
bit_count = 0;
frame.clear();
// Compute preamble pulses power to set thresholds
threshold = (shifter[0].first + shifter[2].first + shifter[7].first + shifter[9].first) / 4;
threshold_high = threshold * 1.414; // +3dB
threshold_low = threshold * 0.707; // -3dB
}
// First check of relations between the first 10 samples
// representing a valid preamble. We don't even investigate further
// if this simple test is not passed
if (shifter[0] > shifter[1] &&
shifter[1] < shifter[2] &&
shifter[2] > shifter[3] &&
shifter[3] < shifter[0] &&
shifter[4] < shifter[0] &&
shifter[5] < shifter[0] &&
shifter[6] < shifter[0] &&
shifter[7] > shifter[8] &&
shifter[8] < shifter[9] &&
shifter[9] > shifter[6])
{
// The samples between the two spikes must be < than the average
// of the high spikes level. We don't test bits too near to
// the high levels as signals can be out of phase so part of the
// energy can be in the near samples
float high = (shifter[0] + shifter[2] + shifter[7] + shifter[9]) / 12;
if (shifter[4] < high &&
shifter[5] < high)
{
// Similarly samples in the range 11-14 must be low, as it is the
// space between the preamble and real data. Again we don't test
// bits too near to high levels, see above
if (shifter[11] < high &&
shifter[12] < high &&
shifter[13] < high &&
shifter[14] < high)
{
//if (c == ADSB_PREAMBLE_LENGTH) {
decoding = true;
sample_count = 0;
bit_count = 0;
frame.clear();
// Compute preamble pulses power to set thresholds
//threshold = (shifter[0] + shifter[2] + shifter[7] + shifter[9]) / 4;
// FIXSBT other use max * 0.2
// FIXSBT threshold_high and threshold_low should be ditched
//threshold_high = threshold * 1.414f; // +3dB
//threshold_low = threshold * 0.707f; // -3dB
} // 11-14 low
} // 4 & 5 high
} // Check for preamble pattern
}
prev_mag = mag;
@ -140,7 +149,6 @@ void ADSBRXProcessor::execute(const buffer_c8_t& buffer) {
void ADSBRXProcessor::on_message(const Message* const message) {
if (message->id == Message::ID::ADSBConfigure) {
null_count = 0;
bit_count = 0;
sample_count = 0;
decoding = false;
@ -148,8 +156,10 @@ void ADSBRXProcessor::on_message(const Message* const message) {
}
}
#ifndef _WIN32
int main() {
EventDispatcher event_dispatcher { std::make_unique<ADSBRXProcessor>() };
event_dispatcher.run();
return 0;
}
#endif

View File

@ -51,8 +51,10 @@ private:
bool configured { false };
float prev_mag { 0 };
float threshold { }, threshold_low { }, threshold_high { };
size_t null_count { 0 }, bit_count { 0 }, sample_count { 0 };
std::pair<float, uint8_t> shifter[ADSB_PREAMBLE_LENGTH];
//size_t null_count{ 0 };
size_t bit_count { 0 }, sample_count { 0 };
//std::pair<float, uint8_t> shifter[ADSB_PREAMBLE_LENGTH];
float shifter[ADSB_PREAMBLE_LENGTH];
bool decoding { };
bool preamble { }, active { };
uint16_t bit_pos { 0 };

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,4 @@
[ZoneTransfer]
ZoneId=3
ReferrerUrl=https://www.bountysource.com/
HostUrl=https://github-repository-files.githubusercontent.com/251043704/4747359?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20211011%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20211011T232519Z&X-Amz-Expires=300&X-Amz-Signature=25180babeea757c128d1a995735486006e018abe6941ab5493597d194dc7b994&X-Amz-SignedHeaders=host&actor_id=21973866&key_id=0&repo_id=251043704&response-content-disposition=attachment%3Bfilename%3Dairlines.csv.txt&response-content-type=text%2Fplain

View File

@ -24,7 +24,7 @@ import struct
outfile = open("airlines.db", "w")
# Download airlines.txt from http://xdeco.org/?page_id=30
lines = [line.rstrip('\n') for line in open('../../sdcard/ADSB/airlines.txt', 'r')]
lines = [line.rstrip('\n') for line in open('../../sdcard/ADSB/airlines.csv.txt', 'r')]
n = 0
for line in lines: