forked from alanbjohnston/CubeSatSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransmit.py
More file actions
951 lines (856 loc) · 30 KB
/
transmit.py
File metadata and controls
951 lines (856 loc) · 30 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
#!/usr/bin/env python
import RPi.GPIO as GPIO
from RPi.GPIO import output
#import subprocess
import time
from time import sleep
#import os
import sys
from os import system
from os import path
from PIL import Image, ImageDraw, ImageFont, ImageColor
import serial
import random
import subprocess
def sim_failure_check():
try:
global card
global cam_fail
global sim_mode
global sim_config
cam_fail = False
file = open("/home/pi/CubeSatSim/failure_mode.txt")
fail_mode = int(file.read(2))
# print("Fail_mode: ")
# print(fail_mode)
if (fail_mode == 11):
card = "Device" # Change audio so no FM audio plays
print("Failure mode no FM audio")
sim_mode = True
elif (fail_mode == 7):
cam_fail = True
print("Failure mode camera fail")
sim_mode = True
card = "Headphones"
elif (fail_mode == -1):
print("No failure mode")
card = "Headphones"
if sim_config:
sim_mode = True
else:
print("Other failure mode")
card = "Headphones"
sim_mode = True
except:
print("No failure mode")
card = "Headphones"
if sim_config:
sim_mode = True
def battery_saver_check():
try:
global txc
f = open("/home/pi/CubeSatSim/battery_saver", "r")
f.close()
txc = False
print("Safe Mode!")
print("battery saver activated")
except:
print("battery saver not activated")
# txc = True
def blink(times):
powerPin = 16
for i in range(times):
GPIO.output(powerPin, 0) # blink two times
sleep(0.1)
GPIO.output(powerPin, 1)
sleep(0.1)
def increment_mode():
print("increment mode")
powerPin = 16
try:
file = open("/home/pi/CubeSatSim/.mode")
mode = file.read(1)
except:
# mode = "f"
if (debug_mode == 1):
print("Can't open .mode file") # , defaulting to FSK")
file.close()
print("Mode is: ")
print(mode)
if (mode == 'a'):
mode = 'f'
blink(2)
sleep(2.5)
elif (mode == 'f'):
mode = 'b'
blink(3)
sleep(2.5)
elif (mode == 'b'):
mode = 's'
blink(4)
sleep(2.5)
elif (mode == 's'):
mode = 'm'
blink(5)
sleep(2.5)
else:
mode = 'a'
blink(1)
sleep(2.5)
try:
file = open("/home/pi/CubeSatSim/.mode", "w")
count_string = str(command_count)
file.write(mode)
file.close()
print(".mode file written")
GPIO.setwarnings(False)
GPIO.output(txLed, 0)
GPIO.output(powerPin, 0)
print("sudo reboot -h now")
GPIO.setwarnings(False)
GPIO.setup(powerPin, GPIO.OUT)
GPIO.output(powerPin, 0);
# system("reboot -h now")
# release = True;
print("Changing mode now")
# system("/home/pi/CubeSatSim/config -" + mode)
system("reboot -h now")
sleep(10);
except:
print("can't write to .mode file")
def camera_photo():
global cam_fail
sim_failure_check()
system("sudo rm /home/pi/CubeSatSim/camera_out.jpg")
stored_image = False
try:
system("raspistill -o /home/pi/CubeSatSim/camera_out.jpg -w 320 -h 256") # > /dev/null 2>&1")
f = open("/home/pi/CubeSatSim/camera_out.jpg")
f.close()
print("Photo taken")
if (cam_fail == True):
system("cp /home/pi/CubeSatSim/sstv//sstv_image_2_320_x_256.jpeg /home/pi/CubeSatSim/camera_out.jpg")
print("Using stored image")
stored_image = True
except:
system("cp /home/pi/CubeSatSim/sstv//sstv_image_2_320_x_256.jpeg /home/pi/CubeSatSim/camera_out.jpg")
print("Using stored image")
stored_image = True
if (stored_image == False):
file='/home/pi/CubeSatSim/camera_out.jpg'
font1 = ImageFont.truetype('DejaVuSerif.ttf', 20)
font2 = ImageFont.truetype('DejaVuSerif-Bold.ttf', 16)
try:
filep = open("/home/pi/CubeSatSim/telem_string.txt")
telem_string = filep.readline()
except:
telem_string = ""
if (debug_mode == 1):
print("Can't read telem_string.txt")
print(telem_string)
img = Image.open(file)
draw = ImageDraw.Draw(img)
# draw.text((10, 10), callsign, font=font2, fill='white')
# draw.text((120, 10), telem_string, font=font2, fill='white')
draw.text((12, 12), callsign, font=font1, fill='black')
draw.text((10, 10), callsign, font=font1, fill='white')
draw.text((112, 12), telem_string, font=font2, fill='black') # was 122
draw.text((110, 10), telem_string, font=font2, fill='white') # was 120
img.save(file)
print("CubeSatSim v2.2 transmit.py starting...")
pd = 21
ptt = 20
txc_pin = 7
squelch = 6
green = 16
powerPin = 16
command_tx = True
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(txc_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(green, GPIO.OUT)
GPIO.output(powerPin, 1)
transmit = False
txLed = 27
txLedOn = 1
txLedOff = 0
if GPIO.input(12) == False:
print("LPF present")
transmit = True
else:
print("No LPF")
# GPIO.setup(txLed, GPIO.OUT)
# output(txLed, txLedOff)
GPIO.setmode(GPIO.BCM) # Repeat to make LED work on Pi 4
GPIO.setwarnings(False)
GPIO.setup(txLed, GPIO.OUT)
GPIO.setup(pd, GPIO.OUT)
#output(pd, 1)
output(pd, 0)
GPIO.setup(ptt, GPIO.OUT)
output (ptt, 1)
txc = False
if GPIO.input(txc_pin) == False:
print("TXC is present")
txc = True;
else:
print("TXC not present")
# txc = False # forcing it off
output(txLed, txLedOn)
sleep(1)
output(txLed, txLedOff)
battery_saver_check()
# print(txLedOn)
print(txLed)
# GPIO.setup(27, GPIO.OUT)
# GPIO.output(27, 0)
debug_mode = 0 # change to 1 to debug transmit
skip = False
if __name__ == "__main__":
mode = "y"
if (len(sys.argv)) > 1:
# print("There are arguments!")
if (('d' == sys.argv[1]) or ('-d' in sys.argv[1])):
debug_mode = 1
elif (('x' == sys.argv[1]) or ('-x' in sys.argv[1])):
mode = "x"
print("Forcing APRS mode")
elif (('s' == sys.argv[1]) or ('-s' in sys.argv[1])):
skip = True
print("Skipping delay and CW ID")
print(transmit)
uptime_time = 55 # 55 second boot time if Pi Zero
try:
f = open("/home/pi/CubeSatSim/pi_zero2", "r")
f.close()
print("Pi Zero 2 detected!")
uptime_time = 30 # 30 second boot time if Pi Zero 2
except:
print("Pi Zero 2 not detected")
try:
system("cat /proc/uptime > /home/pi/CubeSatSim/uptime")
file = open("/home/pi/CubeSatSim/uptime")
up = file.read().split(" ")[0]
print(up)
uptime = float(up)
print(uptime)
if (uptime < uptime_time):
print("Uptime < threshold seconds")
else:
print("Uptime > threshold seconds")
print("Skip CW ID")
skip = True
file.close()
except:
print("Can't open /proc/uptime")
print(skip)
if ( mode == "y"):
try:
file = open("/home/pi/CubeSatSim/.mode")
mode = file.read(1)
except:
mode = "f"
if (debug_mode == 1):
print("Can't open .mode file, defaulting to FSK")
print("Mode is: ")
print(mode)
try:
file = open("/home/pi/CubeSatSim/beacon_off")
file.close()
command_tx = False
except:
command_tx = True
if (debug_mode == 1):
print("Can't open beacon_off file, defaulting to False")
print("Command_tx: ")
print(command_tx)
try:
file = open("/home/pi/CubeSatSim/command_count.txt", "r")
string = file.read()
command_count = int(string)
except:
command_count = 0
if (debug_mode == 1):
print("Can't open command_count file, setting to 0")
file = open("/home/pi/CubeSatSim/command_count.txt", "w")
count_string = str(command_count)
file.write(count_string)
file.close()
print("Command_count: ")
print(command_count)
tx_value = '0'
rx_value = '0'
sq = '0'
tx = '434.9000'
rx = '435.0000'
txr = '144.9000'
sim_mode = False
sim_config = False
hab_mode = False
try:
file = open("/home/pi/CubeSatSim/sim.cfg")
# callsign = file.readline().split(" ")[0]
config = file.readline().split()
callsign = config[0]
if len(config) > 4:
if config[4] == 'y' or config[4] == 'yes':
sim_mode = True
sim_config = True
print("Simulated telemetry mode is configured")
else:
# query = ["timeout", "2", "i2cdetect", "-y", "3"] # Test if Solar board is present
# try:
# result = subprocess.run(query, capture_output=True, text=True, check=True)
# print(f"Command run was: {query}")
# print("Sucess!")
# print(f"Output of the command (stdout): {result}")
# except subprocess.CalledProcessError as e:
# print(f"Command failed with return code: {e.returncode}")
# print(f"Command run was: {e.cmd}")
# print(f"Output of the command (stdout): {e.stdout}")
# print(f"Error output of the command (stderr): {e.stderr}")
try:
if path.isfile("/home/pi/CubeSatSim/sim_mode_auto"):
print("Simulated telemetry mode automatically turned on!")
sim_mode = True
sim_config = True
except:
if (debug_mode == 1):
print("/home/pi/CubeSatSim/sim_mode_auto not found")
if len(config) > 5:
sq = config[5]
if (mode == 'p') or (mode == 'P'):
sq = 0 # turn off squelch for Pacsat
print(sq)
if len(config) > 6:
txf = float(config[6])
if (mode == 'e'):
txr = (txf - 290.0) # - 0.1 # Cross Band Repeater mode transmit frequency in 2m band
tx = "{:.4f}".format(txr)
else:
tx = "{:.4f}".format(txf)
print("Transmit frequency: ",tx)
if len(config) > 7:
rxf = float(config[7])
# print(rxf)
# print( "{:.4f}".format(rxf))
rx = "{:.4f}".format(rxf)
print(rx)
if len(config) > 8:
if config[8] == 'y' or config[8] == 'yes':
hab_mode = True
print("Balloon (HAB) mode is configured.")
if len(config) > 9:
rxpl = float(config[9])
# print(rxpl)
# print( "{:.0f}".format(rxpl))
rxpl_value = "{:.0f}".format(rxpl)
print(rxpl_value)
if len(config) > 10:
txpl = float(config[10])
# print(txpl)
# print( "{:.0f}".format(txpl))
txpl_value = "{:.0f}".format(txpl)
print(txpl_value)
print(config)
print
# print(callsign)
print(sq)
# if sq == '8':
# print("squelch set to 8, no command input!")
# no_command = True
# else:
no_command = False
print(no_command)
except:
callsign = "AMSAT"
if (debug_mode == 1):
print("Can't read callsign from sim.cfg file, defaulting to AMSAT")
file.close()
no_command = True
try:
f = open("/home/pi/CubeSatSim/command_control", "r")
f.close()
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(squelch, GPIO.IN, pull_up_down=GPIO.PUD_UP) ## pull up in case pin is not connected
if GPIO.input(squelch) == False:
print("squelch not set correctly, no command input!")
else:
if (mode != 'n') and (mode != 'x'):
print("command and control is activated")
no_command = False
system("sudo systemctl start command")
else:
print("Command and control not activated since Transmit Commands mode")
txc = True # Transmit commands only works with FM transceiver, so bypass Battery Saver if activated
except:
print("command and control not activated")
print(callsign)
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4
print(txLed)
print(txLedOn)
GPIO.setup(txLed, GPIO.OUT)
card = "Headphones" # default using pcm audio output of Pi Zero
# card = "Device" # using USB sound card for audio output
query = ["sudo", "systemctl", "is-active", "gpsd.socket"]
try:
result = subprocess.run(query, capture_output=True, text=True, check=True)
print(f"Command run was: {query}")
gpsd_status = result.stdout.strip()
print(f"Output of the command (stdout): {gpsd_status}")
except subprocess.CalledProcessError as e:
# print(f"Command failed with return code: {e.returncode}")
print(f"Command run was: {e.cmd}")
gpsd_status = e.stdout.strip()
print(f"Output of the command (stdout): {e.stdout}")
# print(f"Error output of the command (stderr): {e.stderr}")
if (mode != 'e'):
if (gpsd_status == "active"):
print("Stopping gpsd.socket")
system("sudo systemctl stop gpsd.socket")
print("Programming FM module!\n");
output(pd, 1)
output (ptt, 1)
try:
ser = serial.Serial("/dev/ttyAMA0", 9600)
print(ser.portstr)
# uhf_string = "AT+DMOSETGROUP=0," + rx +"," + tx + ",0,3,0,0\r\n"
uhf_string = "AT+DMOSETGROUP=0," + rx + "," + tx + "," + rxpl_value + "," + sq + "," + txpl_value + ",0\r\n"
print(uhf_string)
for i in range(6):
# ser.write(b"AT+DMOSETGROUP=0,435.0000,434.9000,0,3,0,0\r\n")
ser.write(uhf_string.encode())
sleep(0.1)
ser.close()
ser = serial.Serial("/dev/ttyAMA0", 115200) # reset back to 115200 for cubesatsim code for payload sensor data
except:
print("Error in serial write")
output(pd, 0)
if (gpsd_status == "active"):
print("Restarting gpsd.socket")
system("sudo systemctl restart gpsd.socket")
sim_failure_check()
if (hab_mode == True) and (mode == 'a'):
print("Don't transmit CW ID since APRS HAB mode is active")
else:
if (((mode == 'a') or (mode == 'b') or (mode == 'f') or (mode == 's') or (mode == 'j')) and (command_tx == True) and (skip == False)) or ((mode == 'e') and (command_tx == True)): # battery_saver_mode
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setup(txLed, GPIO.OUT)
output(txLed, txLedOn)
print("Transmit CW ID")
status = ""
if not no_command:
status = status + " C"
if sim_mode:
status = status + " S"
if (debug_mode == 1):
system("echo 'hi hi de " + callsign + status + "' > id.txt && gen_packets -M 20 /home/pi/CubeSatSim/id.txt -o /home/pi/CubeSatSim/morse.wav -r 48000 > /dev/null 2>&1 && cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3")
else:
system("echo 'hi hi de " + callsign + status + "' > id.txt && gen_packets -M 20 /home/pi/CubeSatSim/id.txt -o /home/pi/CubeSatSim/morse.wav -r 48000 > /dev/null 2>&1 && cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
output(txLed, txLedOff)
sleep(1)
else:
print("Don't transmit CW ID since command_tx is False or APRS mode or change of mode")
if (transmit):
# print 'Length: ', len(sys.argv)
# if (len(sys.argv)) > 1:
# print("There are arguments!")
if (mode == 'a') or (mode == 'x') or (mode == 'n'):
# command_control_check()
output(pd, 1)
output(ptt, 1)
if (mode == 'a'):
print("AFSK")
else:
# GPIO.output(powerPin, 0)
print("Transmit APRS Commands")
system("sudo systemctl stop command")
# while True:
# sleep(0.1)
# if (mode != 'n'):
# system("touch /home/pi/CubeSatSim/ready")
while True:
try:
f = open("/home/pi/CubeSatSim/ready")
f.close()
if (debug_mode == 1):
print("Packet ready!")
system("gen_packets -o /home/pi/CubeSatSim/telem.wav /home/pi/CubeSatSim/t.txt -r 48000 > /dev/null 2>&1")
system("cat /home/pi/CubeSatSim/t.txt")
if (command_tx == True):
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setup(txLed, GPIO.OUT)
output(txLed, txLedOn)
# output(pd, 1)
# output (ptt, 0)
# sleep(.1)
#
# battery_saver_check()
if (txc):
sim_failure_check()
# output(pd, 1)
sleep(0.1) # add delay before transmit
output (ptt, 0)
sleep(0.3) # add even more time at start
system("aplay -D plughw:CARD=" + card + ",DEV=0 /home/pi/CubeSatSim/telem.wav")
sleep(0.2) # add more time at end
output (ptt, 1)
# output(pd, 0)
else:
system("echo 'AMSAT-11>APCSS:010101/hi hi ' >> t.txt")
if (debug_mode == 1):
system("gen_packets -o /home/pi/CubeSatSim/telem.wav /home/pi/CubeSatSim/t.txt -r 48000 > /dev/null 2>&1 && cat /home/pi/CubeSatSim/telem.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3")
else:
system("gen_packets -o /home/pi/CubeSatSim/telem.wav /home/pi/CubeSatSim/t.txt -r 48000 > /dev/null 2>&1 && cat /home/pi/CubeSatSim/telem.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
sleep(0.1)
# output (ptt, 1)
# output(pd, 0)
output(txLed, txLedOff)
system("sudo rm /home/pi/CubeSatSim/ready")
f.close()
if (debug_mode == 1):
print("Ready for next packet!")
sleep(0.5)
except:
# command_control_check()
sleep(1)
elif (mode == 'm'):
# system("touch /home/pi/CubeSatSim/cwready")
print("CW")
while True:
# command_control_check()
output (pd, 1)
output (ptt, 1)
try:
f = open("/home/pi/CubeSatSim/cwready")
f.close()
system("sudo rm /home/pi/CubeSatSim/cwready")
## ch = 1
for chan in range(7):
command = "gen_packets -M 20 -o /home/pi/CubeSatSim/morse.wav /home/pi/CubeSatSim/cw" + str(chan) + ".txt -r 48000 > /dev/null 2>&1"
print(command)
system(command)
## chan = chan + 1
if (command_tx == True):
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setup(txLed, GPIO.OUT)
output(txLed, txLedOn)
if (txc):
sim_failure_check()
# output (pd, 1)
sleep(0.3)
output (ptt, 0)
system("aplay -D plughw:CARD=" + card + ",DEV=0 /home/pi/CubeSatSim/morse.wav")
sleep(0.1)
output (ptt, 1)
# output (pd, 0)
else:
if (debug_mode == 1):
system("cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3")
else:
system("cat /home/pi/CubeSatSim/morse.wav | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
output(txLed, txLedOff)
# command_control_check()
sleep(2)
f.close()
sleep(10)
except:
# command_control_check()
sleep(1)
elif (mode == 's'):
print("SSTV")
# command_control_check()
output (ptt, 1)
output(pd, 1)
try:
# from picamera import PiCamera
# from pysstv.sstv import SSTV
# camera = PiCamera()
print("Testing for camera")
system("raspistill -o /home/pi/CubeSatSim/camera_out.jpg -w 320 -h 256")
f = open("/home/pi/CubeSatSim/camera_out.jpg")
f.close()
print("Camera present")
camera_present = 1
# camera.close()
except:
print("No camera available")
print(" -> if camera plugged in, is software enabled?")
camera_present = 0
# while 1:
output(txLed, txLedOff)
# output (ptt, 1)
# output(pd, 0)
if (camera_present == 1):
try:
file = open("/home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg")
print("First SSTV stored image detected")
system("/home/pi/PiSSTVpp/pisstvpp -r 48000 -p s2 /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg")
# command_control_check()
if (command_tx == True):
print ("Sending SSTV image")
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setup(txLed, GPIO.OUT)
output(txLed, txLedOn)
# battery_saver_check()
if (txc):
sim_failure_check()
# output(pd, 1)
output (ptt, 0)
system("aplay -D plughw:CARD=" + card + ",DEV=0 /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg.wav")
output (ptt, 1)
# output(pd, 0)
else:
if (debug_mode == 1):
system("cat /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3")
else:
system("cat /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
output(txLed, txLedOff)
# sleep(1)
except:
print("image 2 did not load - copy from CubeSatSim/sstv directory")
while 1:
# command_control_check()
camera_photo()
system("/home/pi/PiSSTVpp/pisstvpp -r 48000 -p s2 /home/pi/CubeSatSim/camera_out.jpg")
system("sudo rm /home/pi/CubeSatSim/camera_out.jpg > /dev/null 2>&1")
# command_control_check()
if (command_tx == True):
print ("Sending SSTV image")
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setup(txLed, GPIO.OUT)
output(txLed, txLedOn)
# battery_saver_check()
if (txc):
# print(card)
sim_failure_check()
# output(pd, 1)
output (ptt, 0)
system("aplay -D plughw:CARD=" + card + ",DEV=0 /home/pi/CubeSatSim/camera_out.jpg.wav")
output(ptt, 1)
# output (pd, 0)
else:
if (debug_mode == 1):
system("cat /home/pi/CubeSatSim/camera_out.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3")
else:
system("cat /home/pi/CubeSatSim/camera_out.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
output(txLed, txLedOff)
# output (ptt, 1)
# output(pd, 0)
system("sudo rm /home/pi/CubeSatSim/camera_out.jpg.wav > /dev/null 2>&1")
sleep(10)
else:
try:
# command_control_check()
file = open("/home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg")
print("First SSTV stored image detected")
system("/home/pi/PiSSTVpp/pisstvpp -r 48000 -p s2 /home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg")
# command_control_check()
if (command_tx == True):
print ("Sending SSTV image")
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setup(txLed, GPIO.OUT)
output(txLed, txLedOn)
# battery_saver_check()
if (txc):
sim_failure_check()
# output(pd, 1)
output (ptt, 0)
system("aplay -D plughw:CARD=" + card + ",DEV=0 /home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg.wav")
output(ptt, 1)
# output (pd, 0)
else:
if (debug_mode == 1):
system("cat /home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3")
else:
system("cat /home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
output(txLed, txLedOff)
# output (ptt, 1)
# output(pd, 0)
sleep(1)
except:
print("image 1 did not load - copy from CubeSatSim/sstv directory")
try:
# command_control_check()
file = open("/home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg")
print("Second SSTV stored image detected")
system("/home/pi/PiSSTVpp/pisstvpp -r 48000 -p s2 /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg")
while 1:
# command_control_check()
if (command_tx == True):
print ("Sending SSTV image")
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setup(txLed, GPIO.OUT)
output(txLed, txLedOn)
# battery_saver_check()
if (txc):
sim_failure_check()
# output(pd, 1)
output (ptt, 0)
system("aplay -D plughw:CARD=" + card + ",DEV=0 /home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg.wav")
output(ptt, 1)
# output (pd, 0)
else:
if (debug_mode == 1):
system("cat /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3")
else:
system("cat /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg.wav | csdr convert_i16_f | csdr gain_ff 14000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1")
output(txLed, txLedOff)
# output (ptt, 1)
# output(pd, 0)
sleep(10)
except:
print("image 2 did not load - copy from CubeSatSim/sstv directory")
if (txc == False):
if (command_tx == True):
system("(while true; do (sleep 10 && cat /home/pi/CubeSatSim/wav/sstv.wav); done) | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 &")
while 1:
if (command_tx == True):
# command_control_check()
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setup(txLed, GPIO.OUT)
output(txLed, txLedOn)
# battery_saver_check()
if (txc):
sim_failure_check()
# output(pd, 1)
output (ptt, 0)
system("aplay -D plughw:CARD=" + card + ",DEV=0 /home/pi/CubeSatSim/sstv.wav")
output(ptt, 1)
# output (pd, 0)
else:
sleep(60)
output(txLed, txLedOff)
# output (ptt, 1)
# output(pd, 0)
sleep(10)
elif (mode == 'b') or (mode == 'j'):
# command_control_check()
if (mode == 'b'):
print("BPSK")
else:
print("FUNcube")
print("turn on FM rx")
output(pd, 1)
output(ptt, 1)
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4
GPIO.setup(txLed, GPIO.OUT)
if (command_tx == True):
# system("sudo nc -l 8080 | csdr convert_i16_f | csdr fir_interpolate_cc 2 | csdr dsb_fc | csdr bandpass_fir_fft_cc 0.002 0.06 0.01 | csdr fastagc_ff | sudo /home/pi/rpitx/sendiq -i /dev/stdin -s 96000 -f 434.9e6 -t float &")
system("sudo nc -l 8080 | csdr convert_i16_f | csdr fir_interpolate_cc 2 | csdr dsb_fc | csdr bandpass_fir_fft_cc 0.002 0.06 0.01 | csdr fastagc_ff | sudo /home/pi/rpitx/sendiq -i /dev/stdin -s 96000 -f " + tx + "e6 -t float &")
print("Turning LED on/off and listening for carrier")
image_id = random.randint(0, 255)
print("Initial image_id: " + str(image_id) + "\n")
while 1:
# print ("LED on")
output(txLed, txLedOff)
sleep(0.4)
# if (command_tx == False):
# output(txLed, txLedOn)
# sleep(0.03)
# output(txLed, txLedOff)
# command_control_check()
if (command_tx == True):
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setup(txLed, GPIO.OUT)
output(txLed, txLedOn)
# print(txLed)
# print(txLedOn)
if (mode == 'b'):
sleep(4.2)
else: # FUNcube mode image
for i in range(4):
# print("Checking image_file.bin")
try:
file = open("/home/pi/CubeSatSim/image_file.bin")
file.close()
# image_present = True
sleep(1.0)
except:
# image_present = False
# if (image_present == False):
start = time.perf_counter()
camera_photo()
system("/home/pi/ssdv/ssdv -e -n -i " + str(image_id) + " -q 3 -J /home/pi/CubeSatSim/camera_out.jpg /home/pi/CubeSatSim/image_file.bin")
print("image_id: " + str(image_id) + "\n")
image_id = ( image_id + 1 ) % 256
print("new image_id: " + str(image_id) + "\n")
elapsed_time = time.perf_counter() - start
print("Elapsed time: ")
print(elapsed_time)
if (elapsed_time < 9):
sleep(9 - time.perf_counter() + start)
# else:
sleep(0.6)
elif (mode == 'e'): # code based on https://zr6aic.blogspot.com/2016/11/creating-2m-fm-repeater-with-raspberry.html
print("Cross Band Repeater Mode")
# print("Stopping command and control")
# system("sudo systemctl stop command")
print("turn on FM rx")
output(pd, 1)
output(ptt, 1)
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4
GPIO.setup(txLed, GPIO.OUT)
# GPIO.setup(powerPin, GPIO.OUT)
GPIO.setup(squelch, GPIO.IN, pull_up_down=GPIO.PUD_UP) ## pull up in case pin is not connected
# GPIO.output(powerPin, 1) # was 0
# txf = float(tx) - 288.9
# print("Transmit frequency: ",txf)
if (command_tx != True):
print("Beacon mode off so no repeater transmission")
print("Ready to detect carrier")
while True:
if (GPIO.input(squelch) == False) and (command_tx == True):
print("Carrier detected, starting repeater")
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setup(txLed, GPIO.OUT)
output(txLed, txLedOn)
system("sudo nc -l 8011 | csdr convert_i16_f | csdr gain_ff 16000 | csdr convert_f_samplerf 20833 | sudo rpitx -i- -m RF -f " + tx + "e3 > /dev/null 2>&1 &")
sleep(0.5)
system("sudo arecord -D shared_mic -r48000 -fS16_LE -c1 | nc localhost 8011 &")
while (GPIO.input(squelch) == False):
sleep(1)
print("No carrier detected, stopping repeater")
output(txLed, txLedOff)
system("sudo rpitx -i null > /dev/null 2>&1")
system("sudo killall -9 arecord > /dev/null 2>&1")
system("sudo killall -9 nc > /dev/null 2>&1")
system("sudo killall -9 rpitx > /dev/null 2>&1")
print("Resetting audio")
system("sudo /etc/init.d/alsa-utils stop")
system("sudo /etc/init.d/alsa-utils start")
print("Finished resetting audio")
print("Ready to detect carrier")
else:
print("FSK")
print("turn on FM rx")
output(pd, 1)
output(ptt, 1)
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi 4
GPIO.setup(txLed, GPIO.OUT)
if (command_tx == True):
system("sudo nc -l 8080 | csdr convert_i16_f | csdr gain_ff 7000 | csdr convert_f_samplerf 20833 | sudo /home/pi/rpitx/rpitx -i- -m RF -f " + tx + "e3 &")
print("Turning LED on/off and listening for carrier")
while 1:
output(txLed, txLedOff)
sleep(0.4)
# if (command_tx == False):
# output(txLed, txLedOn)
# sleep(0.03)
# output(txLed, txLedOff)
# command_control_check()
if (command_tx == True):
GPIO.setmode(GPIO.BCM) # added to make Tx LED work on Pi Zero 2 and Pi 4
GPIO.setup(txLed, GPIO.OUT)
output(txLed, txLedOn)
# print(txLed)
# print(txLedOn)
sleep(4.2)
else:
print("No Low Pass Filter so no telemetry transmit. See http://cubesatsim.org/wiki for instructions on how to build the LPF.")
while 1:
sleep(5)