ADS-B TX works well enough for dump1090 and gr-air-modes

Hooked ADS-B RX to baseband instead of debug IQ file, not tested
This commit is contained in:
furrtek
2017-07-23 12:20:32 +01:00
parent b57b41753f
commit 5a67a7080a
23 changed files with 949 additions and 437 deletions

177
firmware/common/adsb.cpp Normal file
View File

@@ -0,0 +1,177 @@
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2016 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 "adsb.hpp"
#include <math.h>
namespace adsb {
void make_frame_mode_s(ADSBFrame& frame, const uint32_t ICAO_address) {
frame.clear();
frame.push_byte((17 << 3) | 5); // DF17 and CA
frame.push_byte(ICAO_address >> 16);
frame.push_byte(ICAO_address >> 8);
frame.push_byte(ICAO_address & 0xFF);
}
void generate_frame_id(ADSBFrame& frame, const uint32_t ICAO_address, const std::string& callsign) {
std::string callsign_formatted(8, '_');
uint64_t callsign_coded = 0;
uint32_t c, s;
char ch;
make_frame_mode_s(frame, ICAO_address);
frame.push_byte(4 << 3); // TC = 4: Aircraft ID
// Translate and encode callsign
for (c = 0; c < 8; c++) {
ch = callsign[c];
for (s = 0; s < 64; s++)
if (ch == icao_id_lut[s]) break;
if (s == 64) {
ch = ' '; // Invalid character
s = 32;
}
callsign_coded <<= 6;
callsign_coded |= s;
//callsign[c] = ch;
}
// Insert callsign in frame
for (c = 0; c < 6; c++)
frame.push_byte((callsign_coded >> ((5 - c) * 8)) & 0xFF);
frame.make_CRC();
}
/*void generate_frame_emergency(ADSBFrame& frame, const uint32_t ICAO_address, const uint8_t code) {
make_frame_mode_s(frame, ICAO_address);
frame.push_byte((28 << 3) + 1); // TC = 28 (Emergency), subtype = 1 (Emergency)
frame.push_byte(code << 5);
frame.make_CRC();
}*/
void generate_frame_identity(ADSBFrame& frame, const uint32_t ICAO_address, const uint32_t code) {
frame.clear();
frame.push_byte(21 << 3); // DF = 21
frame.push_byte(0);
// 1337:
frame.push_byte(0b00011100);
frame.push_byte(0b00111101);
frame.make_CRC();
}
float cpr_mod(float a, float b) {
return a - (b * floor(a / b));
}
int cpr_NL(float lat) {
if (lat < 0)
lat = -lat; // Symmetry
for (size_t c = 0; c < 58; c++) {
if (lat < adsb_lat_lut[c])
return 59 - c;
}
return 1;
}
int cpr_N(float lat, int is_odd) {
int nl = cpr_NL(lat) - is_odd;
if (nl < 1)
nl = 1;
return nl;
}
void generate_frame_pos(ADSBFrame& frame, const uint32_t ICAO_address, const uint32_t altitude,
const float latitude, const float longitude, const uint32_t time_parity) {
uint32_t altitude_coded;
uint32_t lat, lon;
float delta_lat, yz, rlat, delta_lon, xz;
make_frame_mode_s(frame, ICAO_address);
frame.push_byte(11 << 3); // TC = 11 (Airborne position)
altitude_coded = (altitude + 1000) / 25; // Can be coded in 100ft steps also
frame.push_byte(altitude_coded >> 4); // Top-most altitude bits
// Decoding method (from dump1090):
// index int j = floor(((59 * latcprE - 60 * latcprO) / 131072) + 0.50)
// latE = DlatE * (cpr_mod(j, 60) + (latcprE / 131072))
// latO = DlatO * (cpr_mod(j, 59) + (latcprO / 131072))
// if latE >= 270 -> latE -= 360
// if latO >= 270 -> latO -= 360
// if (cpr_NL(latE) != cpr_NL(latO)) return;
// int ni = cpr_N(latE ,0);
// int m = floor((((loncprE * (cpr_NL(latE) - 1)) - (loncprO * cpr_NL(latE))) / 131072) + 0.5)
// lon = cpr_Dlon(latE, 0) * (cpr_mod(m, ni) + loncprE / 131072);
// lat = latE;
// ... or ...
// int ni = cpr_N(latO ,0);
// int m = floor((((loncprE * (cpr_NL(latO) - 1)) - (loncprO * cpr_NL(latO))) / 131072) + 0.5)
// lon = cpr_Dlon(latO, 0) * (cpr_mod(m, ni) + loncprO / 131072);
// lat = latO;
// ... and ...
// if (lon > 180) lon -= 360;
// CPR encoding
// Info: http://antena.fe.uni-lj.si/literatura/Razno/Avionika/modes/CPRencoding.pdf
delta_lat = 360.0 / ((4.0 * 15.0) - time_parity); // NZ = 15
yz = floor(131072.0 * (cpr_mod(latitude, delta_lat) / delta_lat) + 0.5);
rlat = delta_lat * ((yz / 131072.0) + floor(latitude / delta_lat));
if ((cpr_NL(rlat) - time_parity) > 0)
delta_lon = 360.0 / cpr_N(rlat, time_parity);
else
delta_lon = 360.0;
xz = floor(131072.0 * (cpr_mod(longitude, delta_lon) / delta_lon) + 0.5);
lat = cpr_mod(yz, 131072.0);
lon = cpr_mod(xz, 131072.0);
frame.push_byte((altitude_coded << 4) | ((uint32_t)time_parity << 2) | (lat >> 15));
frame.push_byte(lat >> 7);
frame.push_byte((lat << 1) | (lon >> 16));
frame.push_byte(lon >> 8);
frame.push_byte(lon);
frame.make_CRC();
}
} /* namespace adsb */

60
firmware/common/adsb.hpp Normal file
View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
* Copyright (C) 2016 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.
*/
#ifndef __ADSB_H__
#define __ADSB_H__
#include "adsb_frame.hpp"
#include <cstring>
#include <string>
namespace adsb {
const float adsb_lat_lut[58] = {
10.47047130, 14.82817437, 18.18626357, 21.02939493,
23.54504487, 25.82924707, 27.93898710, 29.91135686,
31.77209708, 33.53993436, 35.22899598, 36.85025108,
38.41241892, 39.92256684, 41.38651832, 42.80914012,
44.19454951, 45.54626723, 46.86733252, 48.16039128,
49.42776439, 50.67150166, 51.89342469, 53.09516153,
54.27817472, 55.44378444, 56.59318756, 57.72747354,
58.84763776, 59.95459277, 61.04917774, 62.13216659,
63.20427479, 64.26616523, 65.31845310, 66.36171008,
67.39646774, 68.42322022, 69.44242631, 70.45451075,
71.45986473, 72.45884545, 73.45177442, 74.43893416,
75.42056257, 76.39684391, 77.36789461, 78.33374083,
79.29428225, 80.24923213, 81.19801349, 82.13956981,
83.07199445, 83.99173563, 84.89166191, 85.75541621,
86.53536998, 87.00000000
};
void make_frame_mode_s(ADSBFrame& frame, const uint32_t ICAO_address);
void generate_frame_id(ADSBFrame& frame, const uint32_t ICAO_address, const std::string& callsign);
void generate_frame_pos(ADSBFrame& frame, const uint32_t ICAO_address, const uint32_t altitude,
const float latitude, const float longitude, const uint32_t time_parity);
void generate_frame_emergency(ADSBFrame& frame, const uint32_t ICAO_address, const uint8_t code);
void generate_frame_identity(ADSBFrame& frame, const uint32_t ICAO_address, const uint32_t code);
} /* namespace adsb */
#endif/*__ADSB_H__*/

View File

@@ -0,0 +1,27 @@
/*
* Copyright (C) 2014 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 "adsb_frame.hpp"
namespace adsb {
} /* namespace adsb */

View File

@@ -0,0 +1,120 @@
/*
* Copyright (C) 2014 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.
*/
#ifndef __ADSB_FRAME_H__
#define __ADSB_FRAME_H__
#include <cstring>
#include <string>
namespace adsb {
alignas(4) const uint8_t adsb_preamble[16] = { 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0 };
alignas(4) const char icao_id_lut[65] = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ##### ###############0123456789######";
class ADSBFrame {
public:
uint8_t get_DF() {
return (raw_data[0] >> 3);
}
uint8_t get_msg_type() {
return (raw_data[4] >> 3);
}
uint32_t get_ICAO_address() {
return (raw_data[1] << 16) + (raw_data[2] << 8) + raw_data[3];
}
void clear() {
index = 0;
memset(raw_data, 0, 14);
}
void push_byte(uint8_t byte) {
if (index >= 14)
return;
raw_data[index++] = byte;
}
uint8_t * get_raw_data() {
return raw_data;
}
std::string get_callsign() {
uint64_t callsign_coded = 0;
uint32_t c;
std::string callsign = "";
// Frame bytes to long
for (c = 5; c < 11; c++) {
callsign_coded <<= 8;
callsign_coded |= raw_data[c];
}
// Long to 6-bit characters
for (c = 0; c < 8; c++) {
callsign.append(1, icao_id_lut[(callsign_coded >> 42) & 0x3F]);
callsign_coded <<= 6;
}
return callsign;
}
void make_CRC() {
uint8_t adsb_crc[14]; // Temp buffer
uint8_t b, c, s, bitn;
const uint32_t crc_poly = 0x1205FFF;
// Clear CRC
raw_data[11] = 0x00;
raw_data[12] = 0x00;
raw_data[13] = 0x00;
// Compute CRC
memcpy(adsb_crc, raw_data, 14);
for (c = 0; c < 11; c++) {
for (b = 0; b < 8; b++) {
if ((adsb_crc[c] << b) & 0x80) {
for (s = 0; s < 25; s++) {
bitn = (c * 8) + b + s;
if ((crc_poly >> s) & 1) adsb_crc[bitn >> 3] ^= (0x80 >> (bitn & 7));
}
}
}
}
// Insert CRC in frame
memcpy(&raw_data[11], &adsb_crc[11], 3);
}
private:
static const uint8_t adsb_preamble[16];
static const char icao_id_lut[65];
alignas(4) uint8_t index { 0 };
alignas(4) uint8_t raw_data[14] { }; // 112 bits at most
};
} /* namespace adsb */
#endif/*__ADSB_FRAME_H__*/

View File

@@ -35,6 +35,7 @@
#include "ert_packet.hpp"
#include "tpms_packet.hpp"
#include "pocsag_packet.hpp"
#include "adsb_frame.hpp"
#include "jammer.hpp"
#include "dsp_fir_taps.hpp"
#include "dsp_iir.hpp"
@@ -91,6 +92,7 @@ public:
SigGenTone = 44,
POCSAGPacket = 50,
ADSBFrame = 51,
RequestSignal = 52,
FIFOData = 53,
@@ -317,6 +319,18 @@ public:
pocsag::POCSAGPacket packet;
};
class ADSBFrameMessage : public Message {
public:
constexpr ADSBFrameMessage(
const adsb::ADSBFrame& frame
) : Message { ID::ADSBFrame },
frame { frame }
{
}
adsb::ADSBFrame frame;
};
class ShutdownMessage : public Message {
public:
constexpr ShutdownMessage(

View File

@@ -26,8 +26,6 @@
#include <cstdint>
#include <cstddef>
#include "optional.hpp"
#include "baseband.hpp"
namespace pocsag {

View File

@@ -63,6 +63,7 @@ private:
char c[4];
};
constexpr image_tag_t image_tag_adsb_rx { 'P', 'A', 'D', 'R' };
constexpr image_tag_t image_tag_ais { 'P', 'A', 'I', 'S' };
constexpr image_tag_t image_tag_am_audio { 'P', 'A', 'M', 'A' };
constexpr image_tag_t image_tag_capture { 'P', 'C', 'A', 'P' };
@@ -79,7 +80,7 @@ constexpr image_tag_t image_tag_afsk { 'P', 'A', 'F', 'S' };
constexpr image_tag_t image_tag_tones { 'P', 'T', 'O', 'N' };
constexpr image_tag_t image_tag_rds { 'P', 'R', 'D', 'S' };
constexpr image_tag_t image_tag_ook { 'P', 'O', 'O', 'K' };
constexpr image_tag_t image_tag_adsb_tx { 'P', 'A', 'D', 'S' };
constexpr image_tag_t image_tag_adsb_tx { 'P', 'A', 'D', 'T' };
constexpr image_tag_t image_tag_replay { 'P', 'R', 'E', 'P' };
constexpr image_tag_t image_tag_fsktx { 'P', 'F', 'S', 'K' };
constexpr image_tag_t image_tag_mic_tx { 'P', 'M', 'T', 'X' };