-
Notifications
You must be signed in to change notification settings - Fork 2
/
aprs.ino
530 lines (451 loc) · 14 KB
/
aprs.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* From Project Swift - High altitude balloon flight software */
/*=======================================================================*/
/* Copyright 2010-2012 Philip Heron <[email protected]> */
/* */
/* 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>. */
/*
* aprs symbol table: http://ak6ak.net/Info/APRS%20Symbol%20Chart.html
*/
#ifdef APRS_DATA
#ifdef SIM_DATA
//ADD YOUR OWN DATA TO THIS SECTION FOR SIMULATING FLIGHT DATA FOR TESTING PURPOSES
static const float simLats[] = {}; //comma separated array of floats to simulate latitude values for testing.
static const float simLongs[] = {}; //comma separated array of floats to simulate longitude values for testing.
static const int simAlts[] = {}; //comma separated array of ints to simulate altitude values (in meters) for testing.
static const int simTemps[] = {}; //comma separated array of floats to simulate temperature values (in °F) for testing.
static const float simHumids[] = {}; //comma separated array of floats to simulate humidity % (RH%) values for testing.
static const int simPress[] = {}; //comma separated array of floats to simulate pressure values (in millbars) for testing.
#endif
#include <util/crc16.h>
#include <avr/pgmspace.h>
#define BAUD_RATE (1200)
#define TABLE_SIZE (512)
#define PREAMBLE_BYTES (50)
#define REST_BYTES (5)
#define PLAYBACK_RATE (F_CPU / 256)
#define SAMPLES_PER_BAUD (PLAYBACK_RATE / BAUD_RATE)
#define PHASE_DELTA_1200 (((TABLE_SIZE * 1200L) << 7) / PLAYBACK_RATE)
#define PHASE_DELTA_2200 (((TABLE_SIZE * 2200L) << 7) / PLAYBACK_RATE)
#define PHASE_DELTA_XOR (PHASE_DELTA_1200 ^ PHASE_DELTA_2200)
#define APRS_DEVID "APEHAB"
//char * APRS_SYMBOL = "O";
//uint8_t APRS_SSID = 11;
char lastTransmissionTime[20];
// Our variables
boolean transmitting = false;
unsigned long NextAPRS=0;
int aprs_mode=0;
unsigned int APRSSentenceCounter;
unsigned long Seconds = 60;
volatile static uint8_t *_txbuf = 0;
volatile static uint8_t _txlen = 0;
//unsigned long TIME_SUBTRACTOR = 1463976000; // 5/23/2016 at 00:00:00
//unsigned long TIME_SUBTRACTOR = 1462075200; // 5/1/2016 at 00:00:00
#ifdef WIREBUS
extern int DS18B20_Temperatures[];
#endif
static const uint8_t PROGMEM _sine_table[] = {
#include "sine_table.h"
};
// Code
void SetupAPRS(void){
#ifdef APRS_ENABLE
pinMode(APRS_ENABLE, OUTPUT);
digitalWrite(APRS_ENABLE, 0);
delay(25);
#endif
// Fast PWM mode, non-inverting output on OC2A
TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS20);
pinMode(APRS_DATA, OUTPUT);
randomSeed(analogRead(A6));
}
boolean isTX(){
if(aprs_mode>0){
return true;
}else{
return transmitting;
}
}
void CheckAPRS(void){
if ((millis() >= NextAPRS) && (GPS.Satellites >= 4 || getLogStarted()) && (_txlen == 0)){
// APRS_SSID = 12; //set APRS_SSID to 9 for testing mode, 11 (or 12) for flight mode
// APRS_SYMBOL = "O"; //set APRS_SYMBOL to > for car icon, O for flight mode
Seconds = 60;
transmitting = true;
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.println(F("Sending APRS Packet"));
#endif
tx_aprs();
if (aprs_mode == 0){
// Normal transmission - wait another minute or whatever
if(APRS_SSID!=11 && APRS_SSID!=12){
//testing mode
Seconds = random(25,36);
}else if(GPS.AltitudeF<3800 && GPS.Speed > 6){
//really low altitude and still moving
Seconds = 12;
}else if(GPS.AltitudeF < 3800 && GPS.Speed <= 6){
//landed or not launched
Seconds = random(15,31);
}else if(GPS.AltitudeF < 10000 && GPS.AltitudeF>=3800){
//pretty low altitude
Seconds = random(15,36);
}else if(GPS.AltitudeF < 18000 && GPS.AltitudeF>=10000){
//low altitude and still moving
Seconds = random(25,61);
}else if(GPS.AltitudeF > 90000){
//real high altitude almost at burst
Seconds = random(25,61);
}else{
//higher altitude
Seconds = random(50,91);
}
if(Seconds>120 || Seconds<12){
Seconds = 30;
}
}else{
Seconds = 0;
}
if(Seconds>120 || Seconds<0){
Seconds = 60;
}
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.print(F("Next packet in "));
DEBUG_SERIAL.print(Seconds);
DEBUG_SERIAL.println(F(" seconds"));
#endif
NextAPRS = millis() + (Seconds * 1000L);
strncpy(lastTransmissionTime,GPS.Timestamp,19);
lastTransmissionTime[19] = '\0'; //guarding for null terminated strings
#ifdef RTTY_INTERVAL
if(millis() - getLastRTTY() >= (getRTTYInterval()*1000L)){
#ifdef DEBUG_SERIAL
DEBUG_SERIAL.print(F("Set last RTTY to be "));
DEBUG_SERIAL.print((getRTTYInterval()-5));
DEBUG_SERIAL.println(F(" seconds ago"));
#endif
setLastRTTY(millis() - ((getRTTYInterval()-2)*1000L));
}
#endif
#ifdef USE_WATCHDOG
wdt_reset();
#endif
}
}
void ax25_frame(const char *scallsign, const char sssid, const char *dcallsign, const char dssid, const char ttl1, const char ttl2, const char *data, ...){
static uint8_t frame[100];
uint8_t *s;
uint16_t x;
va_list va;
// #ifdef DEBUG_SERIAL
// DEBUG_SERIAL.print(F("ax25_frame(")); DEBUG_SERIAL.print(aprs_mode); DEBUG_SERIAL.println(F(")"));
// #endif
va_start(va, data);
/* Write in the callsigns and paths */
s = _ax25_callsign(frame, dcallsign, dssid);
s = _ax25_callsign(s, scallsign, sssid);
if (ttl1) s = _ax25_callsign(s, "WIDE1", ttl1);
if (ttl2) s = _ax25_callsign(s, "WIDE2", ttl2);
/* Mark the end of the callsigns */
s[-1] |= 1;
*(s++) = 0x03; /* Control, 0x03 = APRS-UI frame */
*(s++) = 0xF0; /* Protocol ID: 0xF0 = no layer 3 data */
vsnprintf((char *) s, 100 - (s - frame) - 2, data, va);
va_end(va);
/* Calculate and append the checksum */
for(x = 0xFFFF, s = frame; *s; s++)
x = _crc_ccitt_update(x, *s);
*(s++) = ~(x & 0xFF);
*(s++) = ~((x >> 8) & 0xFF);
/* Point the interrupt at the data to be transmit */
_txbuf = frame;
_txlen = s - frame;
/* Enable the timer and key the radio */
TIMSK2 |= _BV(TOIE2);
#ifdef LED_TX
allLEDoff();
if (GPS.AltitudeF < 2000){
digitalWrite(LED_TX, 1);
}
#endif
#ifdef APRS_ENABLE
digitalWrite(APRS_ENABLE, 1);
delay(25);
#endif
}
void tx_aprs(void){
char slat[5];
char slng[5];
char stlm[75];
char *ptr;
#ifdef SIM_DATA
static uint16_t seq = 0;
#else
static uint16_t seq = 0;
#endif
int32_t aprs_lat, aprs_lon, aprs_alt;
char Wide1Path, Wide2Path;
#ifdef SIM_DATA
GPS.Latitude = simLats[seq];
GPS.Longitude = simLongs[seq];
GPS.Altitude = simAlts[seq];
GPS.AltitudeF = mToF(GPS.Altitude);
#endif
// Convert the UBLOX-style coordinates to the APRS compressed format
aprs_lat = 380926 * (90.0 - GPS.Latitude);
aprs_lon = 190463 * (180.0 + GPS.Longitude);
if(GPS.Satellites>=3){
aprs_alt = GPS.Altitude * 32808 / 10000;
}else{
aprs_alt = getBMPAltitude() * 32808 / 10000;
}
if (GPS.Altitude > APRS_PATH_ALTITUDE)
{
Wide1Path = 0;
Wide2Path = APRS_HIGH_USE_WIDE2;
}
else
{
Wide1Path = 1;
Wide2Path = 1;
}
ptr = stlm;
ax25_base91enc(ptr, 2, seq);
ptr += 2;
ax25_base91enc(ptr, 2, GPS.Satellites);
ptr += 2;
#ifdef SIM_DATA
ax25_base91enc(ptr, 2, (int)simTemps[seq] + 100);
ptr += 2;
ax25_base91enc(ptr, 2, (int)(simPress[seq]) + 100);
ptr += 2;
ax25_base91enc(ptr, 2, (int)simHumids[seq] + 100);
ptr += 2;
#else
#ifdef WIREBUS
ax25_base91enc(ptr, 2, (int)DS18B20_Temperatures[1] + 100);
ptr += 2;
#ifdef DHTPIN
ax25_base91enc(ptr, 2, (int)getPressureRead() + 100);
ptr += 2;
ax25_base91enc(ptr, 2, (int)readHumid() + 100);
#else
ax25_base91enc(ptr, 2, (int)DS18B20_Temperatures[0] + 100);
ptr += 2;
ax25_base91enc(ptr, 2, (int)getHumidityRead() + 100);
#endif
ptr += 2;
#endif
#endif
ax25_base91enc(ptr, 2, (getVoltage(0)*100));
if (aprs_mode == 0){
/* Construct the compressed telemetry format */
ax25_frame(
APRS_CALLSIGN, APRS_SSID,
APRS_DEVID, 0,
Wide1Path, Wide2Path,
"!/%s%s%s /A=%06ld|%s|%s",
ax25_base91enc(slat, 4, aprs_lat),
ax25_base91enc(slng, 4, aprs_lon),
APRS_SYMBOL,
aprs_alt, stlm, APRS_COMMENT); // comment,APRS_CALLSIGN, ++APRSSentenceCounter);
#ifdef APRS_TELEM_INTERVAL
// Send the telemetry definitions every 10 packets
if(seq>0){
if(seq % (APRS_TELEM_INTERVAL) == 0){
aprs_mode = 1;
}
}
#endif
if(seq==0){
aprs_mode = 0;
delay(1000);
}
seq++;
}
#ifdef APRS_TELEM_INTERVAL
#define APRS_PARM1 ":%-9s:PARM.Satellites"
#define APRS_UNIT1 ":%-9s:UNIT.Sats"
#define APRS_EQNS1 ":%-9s:EQNS.0,1,0"
#ifdef DHTPIN
#define APRS_PARM2 ",Temperature"
#define APRS_UNIT2 ",deg.F"
#define APRS_EQNS2 ",0,1,-100"
#define APRS_PARM3 ",Pressure"
#define APRS_UNIT3 ",mbar"
#define APRS_EQNS3 ",0,1,-100"
#else
#define APRS_PARM2 ",External"
#define APRS_UNIT2 ",deg.F"
#define APRS_EQNS2 ",0,1,-100"
#define APRS_PARM3 ",Internal"
#define APRS_UNIT3 ",deg.F"
#define APRS_EQNS3 ",0,1,-100"
#endif
#define APRS_PARM4 ",Humidity"
#define APRS_UNIT4 ",pct"
#define APRS_EQNS4 ",0,1,-100"
#define APRS_PARM5 ",Battery"
#define APRS_UNIT5 ",Volts"
#define APRS_EQNS5 ",0,0.01,0"
else if (aprs_mode >= 1){
char s[20];
strncpy_P(s, PSTR(APRS_CALLSIGN), 7);
if(APRS_SSID) snprintf_P(s + strlen(s), 4, PSTR("-%i"), APRS_SSID);
if (aprs_mode == 1){
// Transmit telemetry definitions
ax25_frame(
APRS_CALLSIGN, APRS_SSID,
APRS_DEVID, 0,
0, 0,
APRS_PARM1 APRS_PARM2 APRS_PARM3 APRS_PARM4 APRS_PARM5,
s);
aprs_mode++;
}else if (aprs_mode == 2){
ax25_frame(
APRS_CALLSIGN, APRS_SSID,
APRS_DEVID, 0,
0, 0,
APRS_UNIT1 APRS_UNIT2 APRS_UNIT3 APRS_UNIT4 APRS_UNIT5,
s);
aprs_mode++;
}else if (aprs_mode == 3){
ax25_frame(
APRS_CALLSIGN, APRS_SSID,
APRS_DEVID, 0,
0, 0,
APRS_EQNS1 APRS_EQNS2 APRS_EQNS3 APRS_EQNS4 APRS_EQNS5,
s);
aprs_mode = 0;
}else{
aprs_mode = 0;
}
}
#endif
}
ISR(TIMER2_OVF_vect){
static uint16_t phase = 0;
static uint16_t step = PHASE_DELTA_1200;
static uint16_t sample = 0;
static uint8_t rest = PREAMBLE_BYTES + REST_BYTES;
static uint8_t byte;
static uint8_t bit = 7;
static int8_t bc = 0;
uint8_t value;
/* Update the PWM output */
value = pgm_read_byte(&_sine_table[(phase >> 7) & 0x1FF]);
#ifdef APRS_PRE_EMPHASIS
if (step == PHASE_DELTA_1200)
{
value = (value >> 1) + 64;
}
#endif
OCR2B = value;
phase += step;
if(++sample < SAMPLES_PER_BAUD) return;
sample = 0;
/* Zero-bit insertion */
if(bc == 5)
{
step ^= PHASE_DELTA_XOR;
bc = 0;
return;
}
/* Load the next byte */
if(++bit == 8)
{
bit = 0;
if(rest > REST_BYTES || !_txlen)
{
if(!--rest)
{
// Disable radio, Tx LED off, disable interrupt
#ifdef APRS_ENABLE
digitalWrite(APRS_ENABLE, 0);
delay(25);
#endif
#ifdef LED_TX
digitalWrite(LED_TX, 0);
#endif
transmitting = false;
TIMSK2 &= ~_BV(TOIE2);
/* Prepare state for next run */
phase = sample = 0;
step = PHASE_DELTA_1200;
rest = PREAMBLE_BYTES + REST_BYTES;
bit = 7;
bc = 0;
return;
}
/* Rest period, transmit ax.25 header */
byte = 0x7E;
bc = -1;
}
else
{
/* Read the next byte from memory */
byte = *(_txbuf++);
if(!--_txlen) rest = REST_BYTES + 2;
if(bc < 0) bc = 0;
}
}
/* Find the next bit */
if(byte & 1)
{
/* 1: Output frequency stays the same */
if(bc >= 0) bc++;
}
else
{
/* 0: Toggle the output frequency */
step ^= PHASE_DELTA_XOR;
if(bc >= 0) bc = 0;
}
byte >>= 1;
}
char *ax25_base91enc(char *s, uint8_t n, uint32_t v){
/* Creates a Base-91 representation of the value in v in the string */
/* pointed to by s, n-characters long. String length should be n+1. */
for(s += n, *s = '\0'; n; n--)
{
*(--s) = v % 91 + 33;
v /= 91;
}
return(s);
}
static uint8_t *_ax25_callsign(uint8_t *s, const char *callsign, const char ssid){
char i;
for(i = 0; i < 6; i++)
{
if(*callsign) *(s++) = *(callsign++) << 1;
else *(s++) = ' ' << 1;
}
*(s++) = ('0' + ssid) << 1;
return(s);
}
int getAPRSMode(){
return aprs_mode;
}
int getNextAPRS(){
int next_tx = ceil((NextAPRS - millis())/1000);
if(next_tx<0){
next_tx = 0;
}
return next_tx;
}
char* getLastTXtime(){
return lastTransmissionTime;
}
#endif