-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflexavr.ino
More file actions
executable file
·765 lines (662 loc) · 15.2 KB
/
flexavr.ino
File metadata and controls
executable file
·765 lines (662 loc) · 15.2 KB
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/*------------------------------------------------------------------------------------------------------*\
| |
| FlexTrak Firmware |
| |
\*------------------------------------------------------------------------------------------------------*/
#include <EEPROM.h>
#include <avr/pgmspace.h>
#define VERSION F("V1.22")
#define SIG_1 'D'
#define SIG_2 'D'
//------------------------------------------------------------------------------------------------------
#define LED_OK 6
#define LED_WARN 7
#define LORA_NSS 10
#define LORA_DIO0 5
#define A0_MULTIPLIER 12.92
#define WIREBUS 4
#define APRS_ENABLE 9
#define APRS_PTT 8
#define APRS_DATA 3
#define APRS_TX A1 // 15
#define APRS_RX A2 // 16
#define CUTDOWN A3
//------------------------------------------------------------------------------------------------------
#define SENTENCE_LENGTH 120 // This is more than sufficient for the standard sentence. Extend if needed; shorten if you are tight on memory.
#define PAYLOAD_LENGTH 16
#define FIELDLIST_LENGTH 24
#define COMMAND_BUFFER_LENGTH 70
//------------------------------------------------------------------------------------------------------
//
// Globals
struct TSettings
{
// Common
char PayloadID[PAYLOAD_LENGTH];
char FieldList[FIELDLIST_LENGTH];
// GPS
unsigned int FlightModeAltitude;
// LoRa
float LORA_Frequency;
unsigned char Implicit; // 1=Implicit, 0=Explicit
unsigned char ErrorCoding;
unsigned char Bandwidth;
unsigned char SpreadingFactor;
unsigned char LowDataRateOptimize;
unsigned int LoRaCycleTime;
unsigned char LoRaSlot;
// SSDV
unsigned int LowImageCount;
unsigned int HighImageCount;
unsigned int High;
// APRS
float APRS_Frequency;
char APRS_Callsign[7]; // Max 6 characters
char APRS_SSID;
int APRS_PathAltitude;
char APRS_HighUseWide2;
int APRS_TxInterval;
char APRS_PreEmphasis;
char APRS_Random;
char APRS_TelemInterval;
// Cutdown
unsigned int CutdownAltitude;
unsigned char CutdownPeriod;
// Uplink
char UplinkCode[16];
// DS18B20
unsigned char DS18B20_Address[8];
char IncludeFieldList;
} Settings;
struct TGPS
{
int Day, Month, Year;
byte Hours, Minutes, Seconds;
unsigned long SecondsInDay; // Time in seconds since midnight
float Longitude, Latitude;
long Altitude;
byte Satellites;
int Speed;
int Direction;
byte FixType;
int Temperatures[2]; // C
byte InternalTemperature; // Index of internal temperature
int BatteryVoltage; // mV
byte FlightMode;
byte PowerMode;
float PredictedLongitude, PredictedLatitude;
byte UseHostPosition;
int LastPacketSNR;
int LastPacketRSSI;
unsigned int ReceivedCommandCount;
byte CutdownStatus;
int ExtraFields[6];
} GPS;
byte ShowGPS=1;
byte ShowLoRa=1;
byte HostPriority=0;
unsigned long HostTimeout=0;
unsigned char SSDVBuffer[256];
unsigned int SSDVBufferLength=0;
//------------------------------------------------------------------------------------------------------
void setup()
{
// Serial port
Serial.begin(38400);
Serial.println("");
Serial.print(F("PITSV3 AVR Firmware "));
Serial.println(VERSION);
Serial.print(F("Free memory = "));
Serial.println(freeRam());
if ((EEPROM.read(0) == SIG_1) && (EEPROM.read(1) == SIG_2))
{
// Store current (default) settings
LoadSettings();
}
else
{
SetDefaults();
}
SetupLEDs();
SetupCutdown();
SetupGPS();
SetupADC();
SetupLoRa();
Setupds18b20();
#ifdef APRS_DATA
SetupAPRS();
#endif
}
void SetDefaults(void)
{
// Common settings
const static char DefaultPayloadID[] PROGMEM = "CHANGEME";
strcpy_P(Settings.PayloadID, (char *)DefaultPayloadID);
const static char DefaultFieldList[] PROGMEM = "0123456789A";
strcpy_P(Settings.FieldList, (char *)DefaultFieldList);
// GPS Settings
Settings.FlightModeAltitude = 2000;
// LoRa Settings
Settings.LORA_Frequency = 434.225;
Settings.LoRaCycleTime = 0;
Settings.LoRaSlot = -1;
LoRaDefaults();
// SSDV Settings
Settings.LowImageCount = 4;
Settings.HighImageCount = 8;
Settings.High = 2000;
// APRS Settings
Settings.APRS_Frequency = 144.8;
strcpy(Settings.APRS_Callsign, "");
Settings.APRS_SSID = 0;
Settings.APRS_PathAltitude = 1500;
Settings.APRS_HighUseWide2 = 1;
Settings.APRS_TxInterval = 20; // 60;
Settings.APRS_PreEmphasis = 1;
Settings.APRS_Random = 0; // 30;
Settings.APRS_TelemInterval = 0; // 2
Serial.println(F("Placing default settings in EEPROM"));
SaveSettings();
}
void loop()
{
if (HostPriority)
{
if (CheckHost())
{
HostTimeout = millis() + 2000;
}
else if (millis() > HostTimeout)
{
HostPriority = 0;
}
}
else
{
CheckHost();
CheckGPS();
CheckLEDs();
CheckCutdown();
CheckLoRa();
CheckADC();
Checkds18b20();
#ifdef APRS_DATA
CheckAPRS();
#endif
}
}
void ShowVersion(void)
{
Serial.print(F("VER="));
Serial.println(VERSION);
}
int freeRam(void)
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
void LoadSettings(void)
{
unsigned int i;
unsigned char *ptr;
ptr = (unsigned char *)(&Settings);
for (i=0; i<sizeof(Settings); i++, ptr++)
{
*ptr = EEPROM.read(i+2);
}
}
void SaveSettings(void)
{
unsigned int i;
unsigned char *ptr;
// Signature
EEPROM.write(0, SIG_1);
EEPROM.write(1, SIG_2);
// Settings
ptr = (unsigned char *)(&Settings);
for (i=0; i<sizeof(Settings); i++, ptr++)
{
EEPROM.write(i+2, *ptr);
}
}
int CheckHost(void)
{
static char Line[COMMAND_BUFFER_LENGTH];
static unsigned int Length=0;
static unsigned int BinaryMode=0;
char Character;
int GotCharacters;
GotCharacters = 0;
while (Serial.available())
{
GotCharacters = 1;
Character = Serial.read();
if (BinaryMode)
{
Line[Length++] = Character;
if (--BinaryMode == 0)
{
ProcessCommand(Line+1);
Length = 0;
}
}
else if (Character == '~')
{
Line[0] = Character;
Length = 1;
}
else if (Character == '\r')
{
Line[Length] = '\0';
ProcessCommand(Line+1);
Length = 0;
}
else if (Length >= sizeof(Line))
{
Length = 0;
}
else if (Length > 0)
{
Line[Length++] = Character;
if (Length == 3)
{
if ((Line[1] == 'S') && (Line[2] == 'B'))
{
BinaryMode = 32;
}
}
}
}
return GotCharacters;
}
void ProcessCommand(char *Line)
{
int OK = 0;
if (Line[0] == 'G')
{
OK = ProcessGPSCommand(Line+1);
}
else if (Line[0] == 'C')
{
OK = ProcessCommonCommand(Line+1);
}
else if (Line[0] == 'L')
{
OK = ProcessLORACommand(Line+1);
}
else if (Line[0] == 'A')
{
OK = ProcessAPRSCommand(Line+1);
}
else if (Line[0] == 'S')
{
OK = ProcessSSDVCommand(Line+1);
}
else if (Line[0] == 'F')
{
OK = ProcessFieldCommand(Line+1);
}
if (OK)
{
Serial.println("*");
}
else
{
Serial.println("?");
}
}
int ProcessGPSCommand(char *Line)
{
int OK = 0;
if (Line[0] == 'P')
{
ShowGPS = atoi(Line+1);
OK = 1;
}
else if (Line[0] == 'F')
{
// Flight mode altitude
unsigned int Altitude;
Altitude = atoi(Line+1);
if (Altitude < 8000)
{
Settings.FlightModeAltitude = Altitude;
OK = 1;
}
}
return OK;
}
int ProcessCommonCommand(char *Line)
{
int OK = 0;
if (Line[0] == 'H')
{
// HostPriority mode
HostPriority = Line[1] == '1';
HostTimeout = millis() + 2000;
OK = 1;
}
else if (Line[0] == 'P')
{
// Store payload ID
if (strlen(Line+1) < PAYLOAD_LENGTH)
{
strcpy(Settings.PayloadID, Line+1);
OK = 1;
}
}
else if (Line[0] == 'F')
{
// Store Field List
if (strlen(Line+1) < FIELDLIST_LENGTH)
{
strcpy(Settings.FieldList, Line+1);
OK = 1;
}
}
else if (Line[0] == 'R')
{
// Reset to default settings
SetDefaults();
OK = 1;
}
else if (Line[0] == 'S')
{
// Save settings to flash
SaveSettings();
OK = 1;
}
else if (Line[0] == 'V')
{
// Version number
ShowVersion();
OK = 1;
}
else if (Line[0] == 'C')
{
// Cutdown Altitude
Settings.CutdownAltitude = atoi(Line+1);
OK = 1;
}
else if (Line[0] == 'T')
{
// Cutdown Time
Settings.CutdownPeriod = atoi(Line+1);
OK = 1;
}
else if (Line[0] == 'L')
{
// Enable/Disable Field List
Settings.IncludeFieldList = Line[1] == '1';
OK = 1;
}
return OK;
}
int ProcessLORACommand(char *Line)
{
int OK = 0;
if (Line[0] == 'F')
{
// New frequency
double Frequency;
Frequency = atof(Line+1);
if ((Frequency >= 400) && (Frequency < 1000))
{
Settings.LORA_Frequency = Frequency;
OK = 1;
}
}
else if (Line[0] == 'S')
{
// Spreading Factor
int SpreadingFactor = atoi(Line+1);
if ((SpreadingFactor >= 6) && (SpreadingFactor <= 12))
{
Settings.SpreadingFactor = SpreadingFactor << 4;
OK = 1;
}
}
else if (Line[0] == 'I')
{
int Implicit = atoi(Line+1);
Settings.Implicit = Implicit ? 1 : 0;
OK = 1;
}
else if (Line[0] == 'E')
{
// Error Coding
int ErrorCoding = atoi(Line+1);
if ((ErrorCoding >= 5) && (ErrorCoding <= 8))
{
Settings.ErrorCoding = (ErrorCoding - 4) << 1;
OK = 1;
}
}
else if (Line[0] == 'B')
{
// Bandwidth
int Bandwidth = atoi(Line+1);
if ((Bandwidth >= 0) && (Bandwidth <= 9))
{
Settings.Bandwidth = Bandwidth << 4;
OK = 1;
}
}
else if (Line[0] == 'L')
{
int LowDataRateOptimize = atoi(Line+1);
Settings.LowDataRateOptimize = LowDataRateOptimize ? 0x08 : 0;
OK = 1;
}
else if (Line[0] == 'T')
{
int LoRaCycleTime = atoi(Line+1);
if ((LoRaCycleTime >= 0) && (LoRaCycleTime <= 60))
{
Settings.LoRaCycleTime = LoRaCycleTime;
OK = 1;
}
}
else if (Line[0] == 'O')
{
int LoRaSlot = atoi(Line+1);
if ((LoRaSlot >= -1) && (LoRaSlot < 60))
{
Settings.LoRaSlot = LoRaSlot;
OK = 1;
}
}
else if (Line[0] == 'U')
{
strncpy(Settings.UplinkCode, Line+1, sizeof(Settings.UplinkCode));
OK = 1;
}
return OK;
}
int ProcessAPRSCommand(char *Line)
{
int OK = 0;
if (Line[0] == 'P')
{
// Store payload ID
if (strlen(Line+1) <= 6)
{
strcpy(Settings.APRS_Callsign, Line+1);
OK = 1;
}
}
else if (Line[0] == 'F')
{
// New frequency
double Frequency;
Frequency = atof(Line+1);
if ((Frequency >= 134) && (Frequency <= 174))
{
Settings.APRS_Frequency = Frequency;
// SetAPRSFrequency();
OK = 1;
}
}
else if (Line[0] == 'S')
{
// SSID
int SSID = atoi(Line+1);
if ((SSID >= 0) && (SSID <= 14))
{
Settings.APRS_SSID = SSID;
OK = 1;
}
}
else if (Line[0] == 'A')
{
// Path altitude
Settings.APRS_PathAltitude = atoi(Line+1);
OK = 1;
}
else if (Line[0] == 'W')
{
// Use Wide2
Settings.APRS_HighUseWide2 = atoi(Line+1);
OK = 1;
}
else if (Line[0] == 'I')
{
// Tx Interval in seconds
Settings.APRS_TxInterval = atoi(Line+1);
OK = 1;
}
else if (Line[0] == 'R')
{
// Random period
Settings.APRS_Random = atoi(Line+1);
OK = 1;
}
else if (Line[0] == 'M')
{
Settings.APRS_PreEmphasis = atoi(Line+1);
OK = 1;
}
else if (Line[0] == 'T')
{
Settings.APRS_TelemInterval = atoi(Line+1);
OK = 1;
}
return OK;
}
int ProcessSSDVCommand(char *Line)
{
int OK = 0;
if (Line[0] == 'C')
{
// Clear SSDV buffer
SSDVBufferLength = 0;
OK = 1;
}
else if (Line[0] == 'P')
{
// Hex SSDV Packet
unsigned char Upper=1;
unsigned char Value;
while (*(++Line))
{
if (Upper)
{
Value = HexToByte(*Line) << 4;
}
else
{
Value += HexToByte(*Line);
SSDVBuffer[SSDVBufferLength++] = Value;
}
Upper = 1 - Upper;
}
OK = 1;
Serial.print(F("SSDV="));
Serial.println(SSDVBufferLength);
}
else if (Line[0] == 'B')
{
// Binary SSDV Packet
int i;
for (i=0; i<32; i++)
{
SSDVBuffer[SSDVBufferLength++] = *(++Line);
}
OK = 1;
Serial.print(F("SSDV="));
Serial.println(SSDVBufferLength);
}
else if (Line[0] == 'S')
{
// SSDV Status
Serial.print(F("SSDV="));
Serial.println(SSDVBufferLength);
OK = 1;
}
else if (Line[0] == 'I')
{
// SSDV Image Count
sscanf(Line+1, "%d,%d,%d", &Settings.LowImageCount, Settings.HighImageCount, &Settings.High);
// Settings.ImageCount = atoi(Line+1);
OK = 1;
}
return OK;
}
int ProcessFieldCommand(char *Line)
{
int OK = 0;
if (Line[0] == 'A')
{
GPS.PredictedLatitude = atof(Line+1);
OK = 1;
}
else if (Line[0] == 'O')
{
GPS.PredictedLongitude = atof(Line+1);
OK = 1;
}
else if (Line[0] == 'T')
{
GPS.Latitude = atof(Line+1);
OK = 1;
}
else if (Line[0] == 'G')
{
GPS.Longitude = atof(Line+1);
OK = 1;
}
else if (Line[0] == 'U')
{
GPS.Altitude = atoi(Line+1);
GPS.UseHostPosition = 5;
OK = 1;
}
else if ((Line[0] >= '0') && (Line[0] <= '5'))
{
GPS.ExtraFields[Line[0] - '0'] = atoi(Line+1);
OK = 1;
}
return OK;
}
unsigned char HexToByte(char ch)
{
int num=0;
if (ch>='0' && ch<='9')
{
num=ch-'0';
}
else if (ch>='A' && ch<='F')
{
num = ch + 10 - 'A';
}
else if (ch>='a' && ch<='f')
{
num = ch + 10 - 'a';
}
else
{
num=0;
}
return num;
}