forked from knormoyle/traquito_with_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoug.py
More file actions
executable file
·2845 lines (2376 loc) · 96.6 KB
/
Copy pathdoug.py
File metadata and controls
executable file
·2845 lines (2376 loc) · 96.6 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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# pip3 install pyserial
# pip3 install serial
# from doug. normally the radio is disabled now?
# can enable it like this
# https://groups.io/g/picoballoon/message/13652
# If you want to play with it yourself, enable the debug mode by pressing "?".
# At the bottom, in the debug json input, type in {"type":"REQ_RADIO_OUTPUT_ENABLE"}.
# can turn it off with REQ_RADIO_OUTPUT_DISABLE ??
#***********************************
# doug on
# the precedent is for wspr, that I don't have to turn tx on explicitlyy.
# > The firmware turns it on now if it's off
# > I'm assuming you don't turn the tx off after a wspr command. maybe you do? don't know,
#
# The API documentation says "This will send regardless of radio power and enable state. State will be restored once complete."
#
# This means, if the radio is off, or disabled, and you issue a send request, the radio will be turned on and enabled before the message is sent. If the radio was off or disabled before the send, that state will be restored afterward.
#
# So yes, if the power was off beforehand, it will be turned off afterward, automatically.
# The send command leaves the traquito in the same state it was before sending.
# > the gps reset doesn't turn power on?
# Correct.
#
# The API documentation doesn't talk about any relationship because there isn't any.
#
# The Hot/Warm/Cold reset is a software thing. If the GPS is physically powered off, the software command to reset is conceptually meaningless and does nothing.
#
# There is no "memory" of a reset or any other state.
# decoding AT6558 chip
# https://wiki.dfrobot.com/GPS_+_BDS_BeiDou_Dual_Module_SKU_TEL0132
# look for GPGSV and BDGSV sentences?
# can we detect this? can we get the firmware version
# hmm. what if we don't detect it right?
# default to true
API_MODE_FIRMWARE = False
# API_MODE_VERSION="2023-07-10_16:10:15"
API_MODE_VERSION="2023-07-11_10:56:46"
FLIGHT_MODE_VERSION="2023-07-11_10:54:38"
# {"type":"REP_GET_DEVICE_INFO","swVersion":"2023-07-11_10:54:38","mode":"TRACKER"}
import operator
from functools import reduce
# FIX! can get stuck in file manage new directory name dialog..can't cntrl-q out of it
import json
import serial
import sys
from termcolor import colored
import time
from time import sleep
import random
import string
import datetime
# from datetime import timedelta
from pytz import timezone
#****************************
USE_SHORT_CALLS=True
MULTIPLE_FIX = True
PREFIX = ""
FIX_PREFIX = "FIX_0"
BAND="15m"
BAND="17m"
BAND="10m"
BAND="20m"
# 20m 14095600
# traquito ? says 14095600 + 1500
# 22m 13710000
if BAND == "10m":
BASE_FREQ = 28124600
elif BAND == "15m":
BASE_FREQ = 21094600
elif BAND == "17m":
BASE_FREQ = 18104600
elif BAND == "20m":
BASE_FREQ = 14095600
else:
print("ERROR: BASE_FREQ decode missing: BAND", BAND)
sys.exit(1)
# hdsdr
# 20m 14095600
# traquito ? says 14095600 + 1500
# 22m 13710000
# each 100 correction is 1 hz
# worked..made it more negative than this limit the website has
# 500 correction does ~7 hz lower on 20M (14095.600 base freq)
# 500 correction does ~14 hz lower on 10M (28124.600 base freq)
# parts per billion (14 / 28124600) is 500 parts per billion
# (14/28124600) * 1e9 498
# this works for determining correction?
# (14/28124600) * 1e9 = 497.78
# probably has to be int
# (14/28124600) * 1e9 = 498
# this will bring us higher if we start with CORRECTION negative
# this will bring us lower if we start with CORRECTION positive
# starting with CORRECTION 0 means it won't change
# this should always be a positive number
CORRECTION_DELTA = 500
# this is dropping by 42-43 hz each time at 14010000 base freq
# 1e9 * (43 / 14095600) = 3050
# 1e9 * (42 / 14095600) = 2979
CORRECTION_DELTA = 3000
# by around 14 for 20M?
CORRECTION_DELTA = 1000
CORRECTION_DELTA = 0
CHANNEL_DELTA = 0
CHANNEL_DELTA = 5 # so it just bumps the frequency
# correction of 10000 to 11000 is not causing 10 hz change
# (correction 1000 delta)
# which is what you expect if correction is * 0.01hz
# causing 14 hz change (lower)
# for the traquito under test, wsjtx reports 14.097003 for channel 0
# the normal base freq is 14.095600 for 20M
# the audio offset is added to that base freq to get the reported freq)
# the hdsdr base freq normally matches what wsjtx is told, but can be shifted
# to match the freq shift implied by the computed 'correction' value
#************************************
# wsjtx results
# 14097004 reported - 14095600 base = 1404 offset?
# 13711403 reported - 13710000 base = 1403 offset ..add 1 below
if BAND == "10m":
# works normally
# 16 hz correction to get in the bin?
# does it have to be 2x on 10M?
# this got me to 0026 on the freq with channel 0
# FUDGE = 4
# TARGET_BASE_FREQ = 28124600 + 2 * (FUDGE + 16)
# good enough for af6im-18
# will use AF6IM 10M channel 447 with "correction":-1208
# apparently a saved correction is sticky. even if not shown
# on the web gui
# testing outside with channel 447
# I get 28.266066 on the wsjtx (around 3.5v)
# af6im qr2jyd nq23 33
# trying initially it was 008 for channel 0
if False:
# AF6IM-18
FUDGE = 1
# why did it need 2x?
TARGET_BASE_FREQ = BASE_FREQ + (2 * (FUDGE + 16))
else:
# AD6Z-19 12/16/23
# this is the fully chopped down traquito (AD6Z-18?)
# this cased ..6011
# TARGET_BASE_FREQ = BASE_FREQ
# with default channel 0 freq is 6010 ..so want 10 hz?
# DON'T 2X it and see what happens
# got ..6021 so good with FUDGE=0. why did it not need 2x??
# correction is -355
# 11/29/23 channel 427 getting 6056 (6060 is center)
# is this adjusting it to 2 * 5 (6015)
FUDGE=0
# TARGET_BASE_FREQ = BASE_FREQ + (1 * (FUDGE + 10))
TARGET_BASE_FREQ = BASE_FREQ + (2 * (FUDGE + 5))
# new 11/30/23 -640 correction
# wrong. brought us to 6065 (added 9)
TARGET_BASE_FREQ = BASE_FREQ + (2 * (FUDGE + 9))
# 11/3023 -462 correction
# now 6059 or 6060 for channel 427
TARGET_BASE_FREQ = BASE_FREQ + (1 * (FUDGE + 13))
# 12/27/23 -960 correction
TARGET_BASE_FREQ = BASE_FREQ + (1 * (FUDGE + 27))
# TARGET_BASE_FREQ = BASE_FREQ + (2 * (FUDGE + 16)) + 1
elif True and BAND == "20m":
# works normally
FUDGE = 0
TARGET_BASE_FREQ = BASE_FREQ + FUDGE
elif False and BAND == "20m":
# worked. wsjtx reported 14.097003 when setup for 20m
# hdsdr had 14.094.000 base freq
# no adjustment needed
FUDGE = 0
TARGET_BASE_FREQ = 14094000 + FUDGE
elif False and BAND == "20m":
FUDGE = -514
# this worked for the 14010000 shift
# 514 / (14095600 - 14010000)
TARGET_BASE_FREQ = 14010000 + FUDGE
elif False and BAND == "20m":
FUDGE = -2730
TARGET_BASE_FREQ = 13900000 + FUDGE
elif False and BAND == "20m":
# FUDGE = -10804
FUDGE = -10804
TARGET_BASE_FREQ = 13710000 + FUDGE
elif False and BAND == "20m":
# to play with the correction equation without fudge
FUDGE = 0
TARGET_BASE_FREQ = 13710000
else:
FUDGE = 0
TARGET_BASE_FREQ = BASE_FREQ
print("final BASE_FREQ", BASE_FREQ)
print("final TARGET_BASE_FREQ", TARGET_BASE_FREQ)
print("final FUDGE", FUDGE)
DELTA_FREQ = TARGET_BASE_FREQ - BASE_FREQ
print("final DELTA_FREQ", DELTA_FREQ)
#************************************
if True:
# should be positive number. positive makes freq lower
# every 1000 of correction shifts frequency 14
# (14/28124600) * 1e9 = 498
CORRECTION = int(-1 * ((TARGET_BASE_FREQ - BASE_FREQ)/BASE_FREQ) * 1e9)
# NEW_CORRECTION = int(-1 * ((TARGET_BASE_FREQ - BASE_FREQ) * 10))
# NEW_CORRECTION = int(-1 * ((TARGET_BASE_FREQ - BASE_FREQ)/TARGET_BASE_FREQ) * 1e9)
# DELTA = NEW_CORRECTION - CORRECTION
# print("CORRECTION", CORRECTION, "NEW_CORRECTION", NEW_CORRECTION, "DELTA", DELTA)
# sys.exit(1)
# hack needed when I went to 14.010000 base
# needed to shift down another 300 hz ?
# FIX! this is reflecting test for 20M
# with this, I got a 14.07177 print for channel 0
# CORRECTION += (300 / 7) * 500
# add 174 to the 300
# 496.6 correction actually gets you 7 hz for 20M
# this worked for
# TARGET_BASE_FREQ = 14010000
# but freq was a little high
# CORRECTION += int( (474 / 7) * 496.6 )
# this should make it 200 hz higher
# CORRECTION += int( (274 / BASE_FREQ) * 1e9 )
else:
# will cause increasing freq corrections in loop
CORRECTION = -1
# will cause decreasing freq corrections in loop
CORRECTION = +1
# FIX! is it half this when shifting 10M up?
FREQ_CORRECTION = int( round( (-1 * CORRECTION * BASE_FREQ) / 1e9) )
print("Assuming Channel 0, Initial base FREQ_CORRECTION", FREQ_CORRECTION, "with", "CORRECTION", CORRECTION)
### sys.exit(1)
# if CORRECTION < -3500 or CORRECTION > 3500:
# print("ERROR: illegal CORRECTION", CORRECTION, "should be between -3500 and 3500")
# sys.exit(1)
# no check for legal bands!
#****************************
# why are we getting stuck on 106?
# was before 11/11/23
# BYTES_NEEDED_BEFORE_READING=150
# this TEMP line has 52 chars
# two of these would be 104 plus two line ends ? 106
# {"type":"TEMP","tempC":13.635676,"tempF":56.5442168}
# BYTES_NEEDED_BEFORE_READING=106
# I suppose we could go with 53 min?
# .HackIt: in_waiting_hack 0 serReader.in_waiting 53
BYTES_NEEDED_BEFORE_READING=53
#****************************
DO_GET_DEVICE_INFO = True
# /dev/ttyACM0 by default?
ACM_DIGIT = 0
GPS_TESTING = False
DO_TX_OFF_ACK_TEST = False
# appropriate u4b usb virtual serial.
# make sure you're in the dialout group so we can access without sudo
from serial_ports import serial_ports
# to use
# ports, TTY_PORT = serial_ports()
# TTY_PORT="/dev/ttyACM0"
# TTY_PORT="/dev/ttyACM1"
# print("WARNING: if it doesn't work, change TTY_PORT. Currently:", TTY_PORT)
DELAY_AFTER_CHAR=0.03 # secs
# don't need these for traquito
controlQ=''
controlA=''
controlC=''
controlD=''
controlO=''
controlR=''
controlS=''
controlV=''
controlX=''
yes='Y'
no='N'
escape=''
enter = '\r'
backspace=''
delete='[3;5~'
print("ASSUMES you're not running putty..wants serial output port for reads!")
# this was wrong after a quick reset??
ports, TTY_PORT = serial_ports()
print("")
print("ports", ports)
print("")
#*******************
if len(sys.argv) < 2:
# leave ACM_DIGIT to whatever it is above
pass
else:
if not (sys.argv[1] in ["0", "1", "2", "3"]):
print("ERROR: ARG1 should be 0|1|2|3 for /dev/ttyACM0,1,2,3")
sys.exit(1)
ACM_DIGIT = int(sys.argv[1])
#*******************
if len(sys.argv) < 3:
# leave MULTIPLE_FIX to whatever it is above
pass
else:
if sys.argv[2] == "MULTIPLE_FIX":
MULTIPLE_FIX = True
GPS_TESTING = True
elif sys.argv[2] == "SINGLE_FIX":
MULTIPLE_FIX = False
GPS_TESTING = True
elif sys.argv[2] == "WSPR":
MULTIPLE_FIX = False
GPS_TESTING = False
else:
print("ERROR: ARG2", sys.argv[2], "should be MULTIPLE_FIX or SINGLE_FIX")
sys.exit(1)
#*******************
if len(sys.argv) < 4:
# leave DO_RESET to whatever it is above
pass
else:
# FIX! should we power GPS off/on?
if sys.argv[3] == "RESET":
DO_RESET = True
elif sys.argv[3] == "NO_RESET":
DO_RESET = False
else:
print("ERROR: ARG3 should RESET or NO_RESET")
sys.exit(1)
#*******************
if len(sys.argv) < 5:
# leave GPS_RESET_TYPE to default above
pass
else:
if sys.argv[4] == "COLD":
GPS_RESET_TYPE = "cold"
elif sys.argv[4] == "WARM":
GPS_RESET_TYPE = "warm"
elif sys.argv[4] == "HOT":
GPS_RESET_TYPE = "hot"
else:
print("ERROR: ARG4 should HOT WARM or COLD")
sys.exit(1)
#*******************
if MULTIPLE_FIX:
# 24 hours
MAX_TIMEOUT_SECS = 60 * 60 * 24
else:
# 3 minutes ..timeout for ptest.sh single fixes
MAX_TIMEOUT_SECS = 180
MAX_TIMEOUT_SECS = 50
MAX_TIMEOUT_SECS = 400
#*******************
if GPS_TESTING or DO_TX_OFF_ACK_TEST:
DO_WSPR = False
DO_RESET = True
# messes up gps fix? if off at higher freq?
# or is it just missing the ack
DO_TX_OFF = True
# automatically turns on if necessary now?
DO_TX_ON = False
DO_SLOW_CONFIG = False
DO_GPS_POWER_ON = False
DO_GPS_POWER_OFF_BATT_ON = False
DO_GPS_RESET = True
# should be hot warm or cold (only)
GPS_RESET_TYPE="cold"
else:
DO_WSPR = True
DO_RESET = True
# messes up gps fix? if off at higher freq?
DO_TX_OFF = True
# automatically turns on if necessary now?
DO_TX_ON = False
DO_SLOW_CONFIG = False
DO_GPS_POWER_ON = False
DO_GPS_POWER_OFF_BATT_ON = True
DO_GPS_RESET = False
GPS_RESET_TYPE="warm"
TTY_PORT = "/dev/ttyACM" + str(ACM_DIGIT)
# remember to rm this if you want fresh start
hw_test_filename = "hw_test_" + str(ACM_DIGIT) + ".txt"
all_lines_filename = "all_lines_" + str(ACM_DIGIT) + ".txt"
print(TTY_PORT, "and writing to", hw_test_filename)
print(TTY_PORT, "and writing to", all_lines_filename)
global fa
fa = open(all_lines_filename, "w")
global fhw
fhw = open(hw_test_filename, "w")
#*********************
# https://github.com/pyserial/pyserial/issues/216
# also http://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.threaded.LineReader (usage example at the bottom of the page)
# http://pyserial.readthedocs.io/en/latest/url_handlers.html#spy to see what its doing on the serial port
# Here's a class that serves as a wrapper to a pyserial object.
# It allows you to read lines without 100% CPU.
# It does not contain any timeout logic.
# If a timeout occurs, self.s.read(i) returns an empty string and
# you might want to throw an exception to indicate the timeout.
# to use:
# ser = serial.Serial(...)
# reader = ReadLine(ser)
# while True:
# print(reader.readline())
class ReadLine:
def __init__(self, s):
self.buf = bytearray()
self.s = s
# should add this to ser.in_waiting to decide if any buffered data is available.
@property
def in_waiting(self):
return len(self.buf)
def readline(self):
i = self.buf.find(b"\n")
# return a line if we have one in our buffer already
if i >= 0:
r = self.buf[:i+1]
self.buf = self.buf[i+1:]
return r
# otherwise
while True:
# grab no more than 2048 bytes
# why was this always reading 1 byte? does it block looking for 1 byte?
bb = max(1, min(2048, self.s.in_waiting))
data = self.s.read(bb)
ii = data.find(b"\n")
# if there's a \n just return up to the \n and save the rest
if ii >= 0:
r = self.buf + data[:ii+1]
self.buf[0:] = data[ii+1:]
return r
else:
# otherwise just save what you got in the buffer, and continue looping (wait a little first)
self.buf.extend(data)
sleep(3)
#****************
def getTime():
todayMountain = datetime.datetime.now(timezone('America/Denver'))
MountainDateHMS = todayMountain.strftime("%m-%d-%Y %H:%M:%S")
return todayMountain, MountainDateHMS
#****************
global ser
ser = None
def OpenSer():
global ser
ser = serial.Serial(TTY_PORT, baudrate=115200, timeout=10, exclusive=True)
# ser = serial.Serial(TTY_PORT, baudrate=57600, timeout=10, exclusive=True)
# losing the acks with 9600 baud
# and the device info replies?
# ser = serial.Serial(TTY_PORT, baudrate=9600, timeout=10, exclusive=True)
ser.flushInput()
# readline replacement? has internal buffering itself
global serReader
serReader = ReadLine(ser)
return ser, serReader
#****************
# globals
global startTime
global TTFF_elapsedTIme
global TTFF_firstLockDuration
global TTFF_dataArrived
# Could have a rolling fix at the very beginning?
startTime = time.time()
TTFF_firstLockDuration = None
TTFF_elapsedTime = None
TTFF_dataArrived = None
# set in GNGGA. DoTemp wants to print them on timeout
# note we don't reinit these per iteration, that should be fine though (as long as we get NMEA messages before
# any reason to print/use them. TTFF_elapsedTime is None is the main iteration boundary check
global Position_Fix_Indicator, Satellites_Used, MSL_Altitude, HDOP
Position_Fix_Indicator = None
Satellites_Used = None
MSL_Altitude = None
global GPGSV_Satellites_In_View, BDGSV_Satellites_In_View
GPGSV_Satellites_In_View = None
BDGSV_Satellites_In_View = None
# the last temp we got. Used when we print gpsFixFound info
global last_tempC
last_tempC = None
global last_tempF
last_tempF = None
global maxSNR_GPGSV
global maxSNR_BDGSV
global meanSNR_GPGSV
global meanSNR_BDGSV
maxSNR_GPGSV = None
maxSNR_BDGSV = None
meanSNR_GPGSV = None
meanSNR_BDGSV = None
#**************************
def hwprint(*args):
# hw_test_filename is global
# args is a tuple
print(" ".join(map(str, args)), file=fhw)
def allprint(*args):
todayMountain, MountainDateHMS = getTime()
# append one element to tuple
datePlusArgs = ( MountainDateHMS, ) + args
# fa was set by global
print(" ".join(map(str, datePlusArgs)), file=fa)
fa.flush()
#****************************
# (14/28124600) * 1e9 = 498
def FreqCorrection():
# if positive CORRECTION, the FREQ_CORRECTION is negative. etc
# so always multiply by -1
# FIX! is it half this when shifting 10M up?
FREQ_CORRECTION = int(round( (-1 * CORRECTION * BASE_FREQ) / 1e9 ))
CORRECTED_BASE_FREQ = BASE_FREQ + FREQ_CORRECTION
sstr = " ".join(map(str, ["Assuming Channel 0...Causing CORRECTED_BASE_FREQ", CORRECTED_BASE_FREQ, "FREQ_CORRECTION", FREQ_CORRECTION, "for CORRECTION", CORRECTION ]))
print(sstr)
hwprint(sstr)
allprint(sstr)
#**************************
def sendit(mytty, name=None, delay=None):
myttyEncoded = mytty.encode('utf-8')
if name:
print("send", myttyEncoded, name, file=sys.stderr)
else:
print("send", myttyEncoded, file=sys.stderr)
ser.write(myttyEncoded)
# see the doc https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.reset_input_buffer
ser.flush() # flush the serial buffer (doesn't discard?)
# Clear output buffer, aborting the current output and discarding all that is in the buffer.
# For some USB serial adapters, this may only flush the buffer of the OS and
# not all the data that may be present in the USB part.
# ser.reset_output_buffer() # flush the serial buffer (discards?). probably don't need this one too.
# no implicit delay?
# sleep(DELAY_AFTER_CHAR)
if delay:
sleep(int(delay))
allprint("TO_JETPACK", myttyEncoded)
return myttyEncoded
#********************************
def senditn(mytty, name=None, delay=None):
# send with a newline
myttyr = mytty + "\n"
myttyEncoded = myttyr .encode('utf-8')
if name:
print("send", myttyEncoded, name, file=sys.stderr)
else:
print("send", myttyEncoded, file=sys.stderr)
ser.write(myttyEncoded)
ser.flush() # flush the serial buffer (doesn't discard?)
# ser.reset_output_buffer() # flush the serial buffer (discards?). probably don't need this one too.
sleep(DELAY_AFTER_CHAR)
if delay:
sleep(int(delay))
allprint("TO_JETPACK", myttyEncoded)
return myttyEncoded
#**************************
# {"type":"ACK","inType":"REQ_GPS_RESET"}
# {"type":"GPS_FIX_TIME","time":"17:54:33","firstLockDuration":1354}
# fullLine is str here (originally bytes)
def DoAck(fullLine):
print("INFO: ACK", fullLine)
try:
fulljson = json.loads(fullLine)
dougType = fulljson["type"]
inType = fulljson["inType"]
ackFound = True
ackError = False
except Exception as e:
print("ERROR: DoAck exception", e)
ackFound = False
ackError = True
# does this ever happen? haven't seen it?
if "err" in fulljson and fulljson["err"] != "":
print("ERROR: DoAck 'err' in fulljson", fullLine)
ackFound = False
ackError = True
sys.exit(1)
return ackError, ackFound
#**************************
def DoRep(fullLine):
print("INFO: REP", fullLine)
fulljson = json.loads(fullLine)
# can have err?
# {"type":"REP_SET_CONFIG","ok":false,"err":"Invalid channel (0 not allowed)"}
# okay
# {"type":"REP_SET_CONFIG","ok":true,"err":""}
repError = False
repFound = False
dougType = fulljson["type"]
if dougType == "REP_WSPR_SEND":
repFound = True
elif dougType == "REP_SET_CONFIG":
repFound = True
elif dougType == "REP_GET_CONFIG":
repFound = True
elif dougType == "REP_GET_DEVICE_INFO":
repFound = True
else:
print("ERROR: unknown rep type", dougType)
repError = True
repFound = False
if "err" in fulljson and fulljson["err"] != "":
print("ERROR: DoRep 'err' in fulljson", fullLine)
repError = True
repFound = False
sys.exit(1)
return repError, repFound
#**************************
def DoGpsFixTime(fullLine):
print("INFO: GPS_FIX_TIME", fullLine)
fulljson = json.loads(fullLine)
# utc time
dougType = fulljson["type"]
# FIX! got spaces in "time"
try:
dougTime = fulljson["time"]
except Exception as e:
print("ERROR: DoGpsFixTime exception", e)
return None
firstLockDuration = None
if "firstLockDuration" in fulljson:
firstLockDuration = fulljson["firstLockDuration"]
return firstLockDuration
#**************************
# 07-08-2023 13:41:39 {"type":"TEMP","tempC":33.97269,"tempF":94.1967446}
def DoTemp(fullLine):
print("INFO: TEMP", fullLine)
fulljson = json.loads(fullLine)
# utc time
# has to be there, to get here
dougType = fulljson["type"]
tempFound = True
try:
# should be a float
tempC = float(fulljson["tempC"])
tempC_int = str(int(round(tempC,0)))+"C"
except Exception as e:
print("ERROR: DoTemp exception 2c", e)
# example error
# fullLine {"type":"TEMP","tepC":39.202208,"tempF":102.5639744}
tempFound = False
try:
tempF = float(fulljson["tempF"])
# should be a float
tempF_int = str(int(round(tempC,0)))+"C"
except Exception as e:
print("ERROR: DoTemp exception 2f", e)
tempFound = False
if not tempFound:
tempC = None
tempF = None
tempC_int = None
tempF_int = None
# good to check here..we always get TEMP
if startTime is None:
elapsedTime = 0
else:
endTime = time.time()
elapsedTime = int(endTime - startTime)
print("DoTemp elapsedTime", elapsedTime, "secs", "tempC", tempC_int)
hwprint("DoTemp elapsedTime", elapsedTime, "secs", "tempC", tempC_int)
if elapsedTime > MAX_TIMEOUT_SECS:
global PREFIX
PREFIX = FIX_PREFIX
todayMountain, MountainDateHMS = getTime()
# basically whatever info we have at the time of timeout
# identified by gpsFixTimeout so gpsFixFound grep will find in hw_test*.txt file
if tempC_int is not None:
ppp_temp = tempC_int
else:
ppp_temp = "None"
ppp = " ".join(map(str, [MountainDateHMS,
ppp_temp,
"gpsFixTimeout",
Position_Fix_Indicator, Satellites_Used, MSL_Altitude, HDOP,
GPGSV_Satellites_In_View, BDGSV_Satellites_In_View,
# these are globals
maxSNR_GPGSV, maxSNR_BDGSV,
meanSNR_GPGSV, meanSNR_BDGSV,
# TTFF_elapsedTime, TTFF_firstLockDuration]))
# so any histogram gathering doesn't barf
MAX_TIMEOUT_SECS, MAX_TIMEOUT_SECS]))
print(colored("NO" + PREFIX + ppp, "red", attrs=["bold"]))
hwprint("NO"+ PREFIX + ppp)
print("ERROR: elapsedTime", elapsedTime, "> MAX_TIMEOUT_SECS", MAX_TIMEOUT_SECS)
print("exit without error exitcode")
if DO_GPS_RESET and GPS_RESET_TYPE == "cold":
print("turn off GPS power after a timeout? (cold)")
senditn('{"type":"REQ_GPS_POWER_OFF"}')
else:
# didn't do hack here
print("turn off GPS power after a timeout? Leave backup power on")
senditn('{"type":"REQ_GPS_POWER_OFF_BATT_ON"}')
# don't wait for the ack, in case it doesn't come
# new firmware okay?
WaitForAck(waitMessage="Waiting for change gps power after timeout, before exit")
# note this doesn' work if we were testing multiple fixes in doug.py
# but that's okay to stop
sys.exit(0)
# can return None or float
return tempFound, tempC_int, tempF_int
#**************************
def DoRepGetDeviceInfo(fullLine):
print("INFO: REP GET_DEVICE_INFO", fullLine)
# we already know the type must be the rep for device info, to be called
fulljson = json.loads(fullLine)
repError = False
repFound = True
try:
swMode = fulljson["mode"]
hwprint(PREFIX + "REP_GET_DEVICE_INFO swMode", swMode)
# {"type":"REP_GET_DEVICE_INFO","swVersion":"2023-07-11_10:54:38","mode":"TRACKER"}
if swMode!="API" and swMode!="TRACKER":
print("ERROR: DoRepGetDeviceInfo api mode!='API' and !='TRACKER'..ignoring", swMode)
swMode = None
except Exception as e:
# swMode field is not in normal software. hmm. How do I know if it's missing or corrupted?
print("ERROR: DoRepGetDeviceInfo swMode exception ..ignoring", e)
repError = True
repFound = False
swMode = "API"
try:
swVersion = fulljson["swVersion"]
hwprint(PREFIX + "REP_GET_DEVICE_INFO swVersion", swVersion)
if swMode == "API" and swVersion != API_MODE_VERSION:
print("ERROR: DoRepGetDeviceInfo unexpected swVersion", swVersion, "expecting api mode version:", API_MODE_VERSION)
### sys.exit(1)
# FIX! what is the right normal sw version?
elif (swMode == "TRACKER") and swVersion != FLIGHT_MODE_VERSION:
print("ERROR: DoRepGetDeviceInfo unexpected swVersion", swVersion, "expecting flight mode version:", FLIGHT_MODE_VERSION)
### sys.exit(1)
elif swMode is None:
print("ERROR: DoRepGetDeviceInfo api mode!='API' and !='TRACKER'", swMode)
### sys.exit(1)
except Exception as e:
print("ERROR: DoRepGetDeviceInfo swVersion exception ..ignoring", e)
repError = True
repFound = False
swVersion = None
# should we retry on repError?
return repError, repFound, swVersion, swMode
#**************************
def DoRepSetConfig(fullLine):
print("INFO: REP SET_CONFIG", fullLine)
# we already know the type must be the rep for device info, to be called
fulljson = json.loads(fullLine)
repError = False
repFound = True
#****************
# can have err?
# {"type":"REP_SET_CONFIG","ok":false,"err":"Invalid channel (0 not allowed)"}
# okay
# {"type":"REP_SET_CONFIG","ok":true,"err":""}
try:
err = fulljson["err"]
hwprint(PREFIX + "REP_SET_CONFIG err:", err)
except Exception as e:
print("ERROR: DoRepSetConfig err exception ..ignoring", e)
repError = True
repFound = False
err = None
if err!="":
print("ERROR: DoRepSetConfig err not blank:", err, "...exittingi")
sys.exit(1)
#****************
# should we retry on repError?
return repError, repFound
#**************************
def DoRepGetConfig(fullLine):
# {"type":"REP_GET_CONFIG","band":"20m","channel":5,"callsign":"AD6Z","correction":0,"callsignOk":true}
# repError, repFound, band, channel, callsign, correction, callsignOk = DoRepGetConfig(fullLine)
print("INFO: REP GET_CONFIG", fullLine)
# we already know the type must be the rep for device info, to be called
fulljson = json.loads(fullLine)
repError = False
repFound = True
# band, channel, callsign, correction, callsignOk
#****************
try:
band = fulljson["band"]
hwprint(PREFIX + "REP_GET_CONFIG band", band)
except Exception as e:
print("ERROR: DoRepGetConfig band exception ..ignoring", e)
repError = True
repFound = False
band = None
#****************
try:
channel = fulljson["channel"]
hwprint(PREFIX + "REP_GET_CONFIG channel", channel)
except Exception as e:
print("ERROR: DoRepGetConfig channel exception ..ignoring", e)
repError = True
repFound = False
channel = None
#****************
try:
callsign = fulljson["callsign"]
hwprint(PREFIX + "REP_GET_CONFIG callsign", callsign)
except Exception as e:
print("ERROR: DoRepGetConfig callsign exception ..ignoring", e)
repError = True
repFound = False
callsign = None
#****************
try:
correction = fulljson["correction"]
hwprint(PREFIX + "REP_GET_CONFIG correction", correction)
except Exception as e:
print("ERROR: DoRepGetConfig correction exception ..ignoring", e)
repError = True
repFound = False
correction = None
#****************
try:
callsignOk = fulljson["callsignOk"]
hwprint(PREFIX + "REP_GET_CONFIG callsignOk", callsignOk)
except Exception as e:
print("ERROR: DoRepGetConfig callsignOk exception ..ignoring", e)
repError = True
repFound = False
callsignOk = None
#****************
# should we retry on repError?
return repError, repFound, band, channel, callsign, correction, callsignOk
#**************************
def DoGpsFix3D(fullLine):
# comes on all there of them?
# {"type":"GPS_FIX_2D","latDeg":38.02367,"lngDeg":-107.670789,"firstLockDuration":69946}
# {"type":"GPS_FIX_3D","altM":2389,"altF":7837,"speedK":0,"courseDeg":0,"firstLockDuration":71183}
# {"type":"GPS_FIX_TIME","time":"17:42:39","firstLockDuration":1357}
# error case (spK)
# INFO: GPS_FIX_3D {"type":"GPS_FIX_3D","altM":2359,"altF":7739,"spK":0,"courseDeg":212}
print("INFO: GPS_FIX_3D", fullLine)
fulljson = json.loads(fullLine)
try:
dougType = fulljson["type"]
altM = fulljson["altM"]
altF = fulljson["altF"]