[flight][geofence-prefix][mad-rush] Quickly add geofence callsign prefixes for some countries

main-solar-only
Richard Meadows 2016-01-20 10:45:15 +00:00
rodzic ac894239b8
commit c6f7a8ce32
14 zmienionych plików z 6951 dodań i 286 usunięć

Wyświetl plik

@ -56,11 +56,11 @@
#endif
void encode_backlog(char* str, tracker_datapoint* dp);
void encode_backlog(char* str, tracker_datapoint* dp, char* prefix);
void aprs_set_datapoint(tracker_datapoint* dp);
void aprs_set_comment(char* comment);
void aprs_set_backlog_comment(tracker_datapoint* log_dp);
void aprs_set_backlog_comment(tracker_datapoint* log_dp, char* prefix);
uint8_t aprs_start(void);
uint8_t aprs_tick(void);

Wyświetl plik

@ -0,0 +1,27 @@
/**
* Autogenerated prefix structures. See sim/geofence
*/
#ifndef GEOFENCE_PREFIX_H
#define GEOFENCE_PREFIX_H
#include <stdbool.h>
#include "samd20.h"
/**
* struct representing all we need to know about a prefix
*/
struct prefix_t {
const int32_t** outlines;
const uint32_t outline_count;
const uint32_t* outline_lengths;
char* prefix;
};
const struct prefix_t prefixs[46];
#endif /* GEOFENCE_PREFIX_H */

Wyświetl plik

@ -32,12 +32,13 @@
/* Update */
void location_telemetry_update(int32_t lon_hn, int32_t lat_hn);
void location_aprs_update(int32_t lon_hn, int32_t lat_hn);
void location_prefix_update(int32_t lon_hn, int32_t lat_hn);
/* Decisions */
bool location_telemetry_active(void);
bool location_aprs_active(void);
int32_t location_aprs_frequency(void);
char* location_prefix(void);
#endif /* LOCATION_H */

Wyświetl plik

@ -109,7 +109,7 @@ void encode_telemetry(char* str, tracker_datapoint* dp)
* Comment string for backlog
*/
#define BACKLOG_COMMENT_LEN (7 + (2 * 4) + 2 + (4 * 2) + 1)
void encode_backlog(char* str, tracker_datapoint* dp)
void encode_backlog(char* str, tracker_datapoint* dp, char* prefix)
{
char compressed_lat[5];
char compressed_lon[5];
@ -131,7 +131,7 @@ void encode_backlog(char* str, tracker_datapoint* dp)
/* Encode backlog string */
sprintf(str,
"%02d%02d%02dz%s%s%s%s",
"%s%02d%02d%02dz%s%s%s%s", prefix,
dp->time.day, dp->time.hour, dp->time.minute,
compressed_lat, compressed_lon, compressed_altitude,
telemetry
@ -145,15 +145,15 @@ void encode_backlog(char* str, tracker_datapoint* dp)
struct tracker_datapoint* _dp = NULL;
char* _comment = NULL;
char backlog_comment[BACKLOG_COMMENT_LEN];
char backlog_comment[BACKLOG_COMMENT_LEN+100]; /* TEMP */
void aprs_set_datapoint(tracker_datapoint* dp) {
_dp = dp;
}
void aprs_set_comment(char* comment) {
_comment = comment;
}
void aprs_set_backlog_comment(tracker_datapoint* log_dp) {
encode_backlog(backlog_comment, log_dp);
void aprs_set_backlog_comment(tracker_datapoint* log_dp, char* prefix) {
encode_backlog(backlog_comment, log_dp, prefix);
_comment = backlog_comment;
}

Plik diff jest za duży Load Diff

Wyświetl plik

@ -30,10 +30,13 @@
#include "geofence_no_aprs.h"
#include "geofence_aprs_zones.h"
#include "geofence_telemetry.h"
#include "geofence_prefix.h"
int32_t current_no_aprs_outline = -1;
int32_t current_aprs_zone = -2, current_aprs_zone_outline = -2;
int32_t current_prefix = -2, current_prefix_outline = -2;
int32_t current_telemetry_outline = -1;
@ -202,6 +205,64 @@ void location_aprs_zone_update(int32_t lat_hn, int32_t lon_hn)
current_aprs_zone = -1;
}
/**
* ============================== APRS Zones ================================
*/
/**
* Returns if a latitude and longitude is in a given aprs zone outline
*
* lat_hn, lon_hn in 100 nanodeg
*/
bool latlon_in_prefix(int32_t prefix, int32_t prefix_outline,
int32_t lat_hn, int32_t lon_hn)
{
return latlon_in_polygon(
prefixs[prefix].outlines[prefix_outline],
prefixs[prefix].outline_lengths[prefix_outline],
lat_hn, lon_hn);
}
/**
* Updates the aprs location based on the current lat/lon
*
* lat_hn, lon_hn in 100 nanodeg
*/
void location_prefix_update(int32_t lat_hn, int32_t lon_hn)
{
uint32_t z, outline;
/* Were we in an aprs zone last time? */
if (current_prefix >= 0 && current_prefix_outline >= 0) {
/* Are we still in the outline? */
if (latlon_in_prefix(current_prefix,
current_prefix_outline,
lat_hn, lon_hn)) { /* Still in outline */
return;
}
}
/* Find which prefix we are in and save it */
uint32_t n_zones = sizeof(prefixs) / sizeof(struct prefix_t);
for (z = 0; z < n_zones; z++) { /* For each zone */
for (outline = 0; outline < prefixs[z].outline_count; outline++) {
if (latlon_in_prefix(z, outline, lat_hn, lon_hn)) { /* If we're in this zone */
/* Record the current zone */
current_prefix = z;
current_prefix_outline = outline;
return; /* Go home. We return the first zone we match */
}
}
}
/* We're not in a zone */
current_prefix = -1;
}
/**
* ============================== No APRS ============================
*/
@ -305,3 +366,16 @@ int32_t location_aprs_frequency(void)
return 144800000;
}
char blankk[] = "";
/**
* Returns the current prefix
*/
char* location_prefix(void) {
if (current_prefix >= 0) {
return prefixs[current_prefix].prefix;
}
return blankk;
}

Wyświetl plik

@ -172,6 +172,8 @@ void aprs_telemetry(struct tracker_datapoint* dp)
if (gps_is_locked() == GPS_NO_LOCK) return; /* Don't bother with no GPS */
char* prefix = location_prefix();
/* Set location */
aprs_set_datapoint(dp);
@ -179,9 +181,9 @@ void aprs_telemetry(struct tracker_datapoint* dp)
backlog_dp_ptr = get_backlog();
if (backlog_dp_ptr != NULL) { /* Backlog comment if we can */
aprs_set_backlog_comment(backlog_dp_ptr);
aprs_set_backlog_comment(backlog_dp_ptr, prefix);
} else {
aprs_set_comment(APRS_COMMENT);
aprs_set_comment(prefix);
}
/* Set frequency */

Wyświetl plik

@ -50,6 +50,9 @@ void telemetry_sequence(struct tracker_datapoint* dp, uint32_t n)
/* Always update geofence */
location_telemetry_update(dp->latitude, dp->longitude);
location_aprs_update(dp->latitude, dp->longitude);
kick_the_watchdog(); /* might take time */
location_prefix_update(dp->latitude, dp->longitude);
kick_the_watchdog();
/* Telemetry */
#if RF_TX_ENABLE

Wyświetl plik

@ -43,7 +43,7 @@ __verification__ void backlog_read_tc(void) {
dp_ptr = get_backlog();
if (dp_ptr != NULL) {
encode_backlog(backlog_read_tc_results.aprs_backlog_str, dp_ptr);
encode_backlog(backlog_read_tc_results.aprs_backlog_str, dp_ptr, "");
backlog_read_tc_results.returned_null = 0;
} else {
backlog_read_tc_results.returned_null = 1;

Wyświetl plik

@ -17,6 +17,7 @@ struct location_aprs_tc_params {
struct location_aprs_tc_results {
bool tx_allow;
double frequency;
char* prefix;
} location_aprs_tc_results;
/* Function */
__verification__ void location_aprs_tc(void) {
@ -27,6 +28,13 @@ __verification__ void location_aprs_tc(void) {
(int32_t)(location_aprs_tc_params.lon * 10 * 1000 * 1000)
);
location_prefix_update(
(int32_t)(location_aprs_tc_params.lat * 10 * 1000 * 1000),
(int32_t)(location_aprs_tc_params.lon * 10 * 1000 * 1000)
);
location_aprs_tc_results.tx_allow = location_aprs_active();
location_aprs_tc_results.frequency = location_aprs_frequency();
location_aprs_tc_results.prefix = location_prefix();
}

Wyświetl plik

@ -55,6 +55,7 @@ class location_aprs_tc:
# What frequency did we return?
freq = float(result['frequency']) / (1000*1000)
prefix = result['prefix']
if str(result['tx_allow']).startswith('false'): # No APRS
if int(expected_freq) is 0:
@ -66,7 +67,7 @@ class location_aprs_tc:
return False
if freq == expected_freq:
print_info("{}: {:.3f} MHz".format(name, freq))
print_info("{}[{}]: {:.3f} MHz".format(name, prefix, freq))
return True
else:
print_info("{} ({:.1f}, {:.1f}): Expected {:.9f}, Geofence {:.9f}".format(

File diff suppressed because one or more lines are too long

Wyświetl plik

@ -0,0 +1,236 @@
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Import other notebooks\n",
"import io\n",
"from IPython.nbformat import current\n",
"\n",
"def execute_notebook(nbfile):\n",
" with io.open(nbfile) as f:\n",
" nb = current.read(f, 'json')\n",
" \n",
" ip = get_ipython()\n",
" for cell in nb.worksheets[0].cells:\n",
" if cell.cell_type != 'code':\n",
" continue\n",
" ip.run_cell(cell.input)\n",
"\n",
"execute_notebook(\"country_excludes.ipynb\")\n",
"execute_notebook(\"geofence_lib.ipynb\")\n",
"execute_notebook(\"header_files.ipynb\")\n",
"\n",
"import matplotlib"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 46
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import json\n",
"json_data = open(\"countries_world.json\").read()\n",
"countries = json.loads(json_data)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def prepare_simple_grids(grids, tolerance=2*12*1000): # Approx 12km\n",
" simple = [simplify_points(points, tolerance) for points in grids]\n",
" return [s for s in simple if len(s) > 2]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"c_prefix = []\n",
"\n",
"for c in countries:\n",
" if (c['prefix']):\n",
" c_prefix.append(c)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "u'prefix'",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-17-23d6a2b1a119>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mc\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mcountries\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'prefix'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mc_prefix\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyError\u001b[0m: u'prefix'"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print len(c_prefix)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"46\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for c in c_prefix:\n",
" c['prefix_grids'] = prepare_simple_grids(c['simple_grids'], tolerance=10 * 2000) # ~10km\n",
" c['prefix_outlines'] = fix_antiprime_outlines(ea_grid_to_lonlat(c['prefix_grids']))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n"
]
}
],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Filter out zero-length outlines\n",
"for c in c_prefix:\n",
" \n",
" filtered_outlines = []\n",
" for outline in c['prefix_outlines']:\n",
" if (len(outline) > 0):\n",
" filtered_outlines.append(outline)\n",
" \n",
" c['prefix_outlines'] = filtered_outlines"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"n_outlines = 0\n",
"n_points = 0\n",
"\n",
"for c in c_prefix:\n",
" n_outlines = n_outlines + len(c['prefix_outlines'])\n",
" \n",
" for outline in c['prefix_outlines']:\n",
" n_points = n_points + len(outline)\n",
" \n",
"print \"Total number of points {}\".format(n_points)\n",
"print \"Across {} outlines\".format(n_outlines)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Total number of points 4503\n",
"Across 157 outlines\n"
]
}
],
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Draw map\n",
"m = Basemap(projection='mill')\n",
" \n",
"m.fillcontinents(color='0.96')\n",
"m.drawcoastlines()\n",
"\n",
"for c in c_prefix:\n",
" draw_outlines(m, c['prefix_outlines'], c['colour'])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"<matplotlib.figure.Figure at 0x7fcf83335d10>"
]
}
],
"prompt_number": 31
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"write_prefix(c_prefix)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 47
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}

Wyświetl plik

@ -53,7 +53,91 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 17
"prompt_number": 66
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def file_header_prefix(f):\n",
" print >>f, \"/**\"\n",
" print >>f, \" * Autogenerated prefix structures. See sim/geofence\"\n",
" print >>f, \" */\"\n",
" print >>f, \"\"\n",
" \n",
"def header_h_prefix(header, c_prefix):\n",
" print >>header, \"#ifndef GEOFENCE_PREFIX_H\"\n",
" print >>header, \"#define GEOFENCE_PREFIX_H\"\n",
" print >>header, \"\"\n",
" print >>header, \"\"\n",
" print >>header, \"#include <stdbool.h>\"\n",
" print >>header, \"\"\n",
" print >>header, \"#include \\\"samd20.h\\\"\"\n",
" print >>header, \"\"\n",
" print >>header, \"\"\n",
" print >>header, \"/**\"\n",
" print >>header, \" * struct representing all we need to know about a prefix\"\n",
" print >>header, \" */\"\n",
" print >>header, \"struct prefix_t {\"\n",
" print >>header, \" const int32_t** outlines;\"\n",
" print >>header, \" const uint32_t outline_count;\"\n",
" print >>header, \" const uint32_t* outline_lengths;\"\n",
" print >>header, \" char* prefix;\"\n",
" print >>header, \"};\"\n",
" print >>header, \"\"\n",
" print >>header, \"\"\n",
" print >>header, \"const struct prefix_t prefixs[{0}];\".format(len(c_prefix))\n",
" print >>header, \"\"\n",
" print >>header, \"#endif /* GEOFENCE_PREFIX_H */\""
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 67
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def print_outline_prefix(f, c):\n",
" \n",
" print >>f, u\"/* {name:->24} */\".format(**c).encode('utf8')\n",
" \n",
" # Print each array of points\n",
" for o in range(len(c['prefix_outlines'])): # foreach of the countries outlines\n",
" \n",
" outline = c['prefix_outlines'][o]\n",
" \n",
" print >>f, \"const int32_t p{0}_{1}[] = {{\".format(c['prefix'].lower() + \"_outline\", o+1)\n",
" for point in outline:\n",
" # We express locations in \u00b5degrees\n",
" ulon = int(round(point[0]*1e6))\n",
" ulat = int(round(point[1]*1e6))\n",
" \n",
" print >>f, \" {: 10d}, {: 9d},\".format(ulon, ulat)\n",
" print >>f, \"};\"\n",
"\n",
" \n",
" print >>f, \"\"\n",
" \n",
" # Print an array of outlines\n",
" print >>f, \"const int32_t* p{0}_outlines[] = {{\".format(c['prefix'].lower())\n",
" for o in range(len(c['prefix_outlines'])): # foreach of the countries outlines\n",
" print >>f, \" p{0}_{1},\".format(c['prefix'].lower() + \"_outline\", o+1)\n",
" print >>f, \"};\"\n",
" \n",
" # Print an array of their lengths\n",
" print >>f, \"const uint32_t p{0}_outline_lengths[] = {{\".format(c['prefix'].lower())\n",
" for outline in c['prefix_outlines']: # foreach of the countries outlines\n",
" print >>f, \" {0},\".format(len(outline))\n",
" print >>f, \"};\"\n",
" print >>f, \"\"\n",
" print >>f, \"\"\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 68
},
{
"cell_type": "code",
@ -97,7 +181,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 18
"prompt_number": 69
},
{
"cell_type": "code",
@ -123,7 +207,35 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
"prompt_number": 70
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from __future__ import unicode_literals\n",
"\n",
"def print_prefix(f, zone):\n",
" zone['outline_n'] = zone['prefix'].lower() + \"_outlines\"\n",
" zone['outline_c'] = len(zone['prefix_outlines'])\n",
" zone['outline_l'] = zone['prefix'].lower() + \"_outline_lengths\"\n",
" \n",
" zone['p'] = zone['prefix'].upper()\n",
" \n",
" print >>f, u\" \\\n",
" /* {name:->24} */\".format(**zone).encode('utf8')\n",
" print >>f, u\" {{ .outlines = p{outline_n}, .outline_count = {outline_c}, .outline_lengths = p{outline_l},\\n\\\n",
" .prefix = \\\"{p}\\\" }},\".format(**zone).encode('utf8')\n",
" \n",
"def print_prefixs(f, zones):\n",
" print >>f, \"const struct prefix_t prefixs[] = {\"\n",
" [print_prefix(f, z) for z in zones]\n",
" print >>f, \"};\""
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 71
},
{
"cell_type": "code",
@ -158,7 +270,42 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 20
"prompt_number": 72
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def write_prefix(c):\n",
" # header\n",
" header = open('../../firmware/inc/geofence_prefix.h','w')\n",
"\n",
" file_header_prefix(header)\n",
" header_h_prefix(header, c)\n",
"\n",
" # source\n",
" source = open('../../firmware/src/geofence_prefix.c', 'w')\n",
"\n",
" file_header_prefix(source)\n",
" print >>source, \"\"\n",
" print >>source, \"#include <stdbool.h>\"\n",
" print >>source, \"\"\n",
" print >>source, \"#include \\\"samd20.h\\\"\"\n",
" print >>source, \"#include \\\"geofence_aprs_zones.h\\\"\"\n",
" print >>source, \"\"\n",
" print >>source, \"\"\n",
" print >>source, \"/* Longitude, Latitude */\"\n",
" print >>source\n",
" [print_outline_prefix(source, z) for z in c]\n",
" print_prefixs(source, c)\n",
"\n",
" header.close()\n",
" source.close()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 73
},
{
"cell_type": "markdown",
@ -194,7 +341,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 21
"prompt_number": 74
},
{
"cell_type": "code",
@ -236,7 +383,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 22
"prompt_number": 75
},
{
"cell_type": "code",
@ -270,7 +417,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 23
"prompt_number": 76
},
{
"cell_type": "markdown",
@ -306,7 +453,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 24
"prompt_number": 77
},
{
"cell_type": "code",
@ -348,7 +495,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 25
"prompt_number": 78
},
{
"cell_type": "code",
@ -382,7 +529,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 26
"prompt_number": 79
},
{
"cell_type": "markdown",
@ -431,7 +578,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 27
"prompt_number": 80
},
{
"cell_type": "code",
@ -473,7 +620,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 28
"prompt_number": 81
},
{
"cell_type": "code",
@ -501,7 +648,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 29
"prompt_number": 82
},
{
"cell_type": "code",
@ -538,7 +685,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 30
"prompt_number": 83
},
{
"cell_type": "code",
@ -547,7 +694,7 @@
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 30
"prompt_number": 83
}
],
"metadata": {}