-
Notifications
You must be signed in to change notification settings - Fork 86
/
main.c
2240 lines (1917 loc) · 77.4 KB
/
main.c
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
/*
* Transmits CubeSat Telemetry at 434.9MHz in AFSK, FSK, BPSK, or CW format
* Or transmits SSTV stored images or Pi camera iamges.
*
* Copyright Alan B. Johnston
*
* Portions Copyright (C) 2018 Jonathan Brandenburg
*
* 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
* (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/>.
*/
#include "main.h"
//#define HAB // uncomment to change APRS icon from Satellite to Balloon and only BAT telemetry
int main(int argc, char * argv[]) {
printf("\n\nCubeSatSim v2.0 starting...\n\n");
wiringPiSetup();
// Open configuration file with callsign and reset count
FILE * config_file = fopen("/home/pi/CubeSatSim/sim.cfg", "r");
if (config_file == NULL) {
printf("Creating config file.");
config_file = fopen("/home/pi/CubeSatSim/sim.cfg", "w");
fprintf(config_file, "%s %d", " ", 100);
fclose(config_file);
config_file = fopen("/home/pi/CubeSatSim/sim.cfg", "r");
}
// char * cfg_buf[100];
fscanf(config_file, "%s %d %f %f %s %d %s %s %s %d %d",
call, & reset_count, & lat_file, & long_file, sim_yes, & squelch, tx, rx, hab_yes, & rx_pl, & tx_pl);
fclose(config_file);
fprintf(stderr,"Config file /home/pi/CubeSatSim/sim.cfg contains %s %d %f %f %s %d %s %s %s %d %d\n",
call, reset_count, lat_file, long_file, sim_yes, squelch, tx, rx, hab_yes, rx_pl, tx_pl);
fprintf(stderr, "Transmit on %s MHz Receive on %s MHz\n", tx, rx);
FILE * uptime_file = fopen("/proc/uptime", "r");
fscanf(uptime_file, "%f", & uptime_sec);
printf("Uptime sec: %f \n", uptime_sec);
fclose(uptime_file);
// program_radio(); // do in transmit instead
if (uptime_sec < 30.0) {
reset_count = (reset_count + 1) % 0xffff; // only increment uptime if just rebooted
fprintf(stderr,"INFO: Reset Count: %d Uptime since Reset: %ld \n", reset_count, uptime_sec);
}
if ((fabs(lat_file) > 0) && (fabs(lat_file) < 90.0) && (fabs(long_file) > 0) && (fabs(long_file) < 180.0)) {
fprintf(stderr, "Valid latitude and longitude in config file\n");
// convert to APRS DDMM.MM format
// latitude = toAprsFormat(lat_file);
// longitude = toAprsFormat(long_file);
latitude = lat_file;
longitude = long_file;
fprintf(stderr, "Lat/Long %f %f\n", latitude, longitude);
fprintf(stderr, "Lat/Long in APRS DDMM.MM format: %07.2f/%08.2f\n", toAprsFormat(latitude), toAprsFormat(longitude));
newGpsTime = millis();
} else { // set default
// latitude = toAprsFormat(latitude);
// longitude = toAprsFormat(longitude);
newGpsTime = millis();
}
if (strcmp(sim_yes, "yes") == 0) {
sim_mode = TRUE;
fprintf(stderr, "Sim mode is turned ON by configuration\n");
sim_config = TRUE;
}
if (strcmp(hab_yes, "yes") == 0) {
hab_mode = TRUE;
fprintf(stderr, "HAB mode is ON\n");
}
FILE * command_file = fopen("/home/pi/CubeSatSim/command_control", "r");
if (command_file == NULL) {
fprintf(stderr,"Command and control is OFF\n");
c2cStatus = DISABLED;
} else {
command_file = fopen("/home/pi/CubeSatSim/command_control_direwolf", "r");
if (command_file == NULL) {
fprintf(stderr,"Command and control Carrier (squelch) is ON\n");
c2cStatus = CARRIER;
} else {
fprintf(stderr,"Command and control DTMF or APRS is ON\n");
c2cStatus = DTMF_APRS;
}
}
printf("c2cStatus: %d \n", c2cStatus);
char resbuffer[1000];
// const char testStr[] = "cat /proc/cpuinfo | grep 'Revision' | awk '{print $3}' | sed 's/^1000//' | grep '9000'";
const char testStr[] = "cat /proc/cpuinfo | grep 'Revision' | awk '{print $3}' | sed 's/^1000//'";
const char test2Str[] = "cat /proc/cpuinfo | grep 'Revision' | awk '{print $3}' | sed 's/^1000//' | grep '902120'";
FILE *file_test = sopen(testStr); // see if Pi Zero 2
fgets(resbuffer, 1000, file_test);
fprintf(stderr, "Pi Zero test result: %s\n", resbuffer);
fclose(file_test);
// fprintf(stderr, "hex: %x %x %x %x \n", resbuffer[0], resbuffer[1], resbuffer[2], resbuffer[3]);
if ((resbuffer[0] != '9') || (resbuffer[1] != '0') || (resbuffer[2] != '0') || (resbuffer[3] != '0'))
{
// voltageThreshold = 3.7;
// if ((resbuffer[0] != '9') || (resbuffer[1] != '0') || (resbuffer[2] != '2') || (resbuffer[3] != '1'))
FILE *file2_test = sopen(test2Str); // see if Pi Zero 2
fgets(resbuffer, 1000, file2_test);
fprintf(stderr, "Pi Zero 2 test result: %s\n", resbuffer);
fclose(file2_test);
if (strlen(resbuffer) > 5)
fprintf(stderr, "Pi Zero 2 detected\n");
else
fprintf(stderr, "Not a Pi Zero or Pi Zero 2\n");
pi_zero_2_offset = 500;
if (uptime_sec < 30.0) {
FILE * transmit_stop = popen("sudo systemctl start transmit", "r");
pclose(transmit_stop);
fprintf(stderr, "Sleep 5 sec\n");
sleep(5); // try sleep at start to help boot
}
}
else {
fprintf(stderr,"Pi Zero detected\n");
if ((c2cStatus == DISABLED) || (c2cStatus == CARRIER)) {
pi_zero_2_offset = 500;
}
if (uptime_sec < 30.0) {
FILE * transmit_stop = popen("sudo systemctl start transmit", "r");
pclose(transmit_stop);
fprintf(stderr,"Sleep 10 sec\n");
sleep(10);
}
}
// FILE * transmit_stop = popen("sudo systemctl stop transmit", "r");
// FILE * transmit_stop = popen("sudo systemctl restart transmit", "r");
// FILE * cc_start = popen("/home/pi/CubeSatSim/command &", "r");
// pclose(cc_start);
// FILE * file_deletes = popen("sudo rm /home/pi/CubeSatSim/ready /home/pi/CubeSatSim/cwready > /dev/null", "r");
// pclose(file_deletes);
printf("Test bus 1\n");
fflush(stdout);
i2c_bus1 = (test_i2c_bus(1) != -1) ? 1 : OFF;
printf("Test bus 3\n");
fflush(stdout);
i2c_bus3 = (test_i2c_bus(3) != -1) ? 3 : OFF;
printf("Finished testing\n");
fflush(stdout);
// sleep(2);
//#ifdef HAB
if (hab_mode)
fprintf(stderr, "HAB mode enabled - in APRS balloon icon and no battery saver or low voltage shutdown\n");
//#endif
// FILE * transmit_restart = popen("sudo systemctl restart transmit", "r");
// pclose(transmit_restart);
mode = BPSK;
frameCnt = 1;
if (argc > 1) {
// strcpy(src_addr, argv[1]);
if ( * argv[1] == 'b') {
mode = BPSK;
printf("Mode is BPSK\n");
} else if ( * argv[1] == 'a') {
mode = AFSK;
printf("Mode is AFSK\n");
} else if ( * argv[1] == 'm') {
mode = CW;
printf("Mode is CW\n");
} else {
printf("Mode is BPSK\n");
}
if (argc > 2) {
// printf("String is %s %s\n", *argv[2], argv[2]);
loop = atoi(argv[2]);
loop_count = loop;
}
printf("Looping %d times \n", loop);
if (argc > 3) {
if ( * argv[3] == 'n') {
cw_id = OFF;
printf("No CW id\n");
}
}
} else {
FILE * mode_file = fopen("/home/pi/CubeSatSim/.mode", "r");
if (mode_file != NULL) {
char mode_string;
mode_string = fgetc(mode_file);
fclose(mode_file);
printf("Mode file /home/pi/CubeSatSim/.mode contains %c\n", mode_string);
if ( mode_string == 'f') {
mode = FSK;
printf("Mode is FSK\n");
} else if ( mode_string == 'a') {
mode = AFSK;
printf("Mode is AFSK\n");
} else if ( mode_string == 's') {
mode = SSTV;
printf("Mode is SSTV\n");
} else if ( mode_string == 'm') {
mode = CW;
printf("Mode is CW\n");
} else if ( mode_string == 'e') {
mode = REPEATER;
printf("Mode is Repeater\n");
} else if ( mode_string == 'n') {
mode = TXCOMMAND;
printf("Mode is Transmit Command\n");
} else {
printf("Mode is BPSK\n");
}
}
}
// Open telemetry file with STEM Payload Data
telem_file = fopen("/home/pi/CubeSatSim/telem.txt", "a");
if (telem_file == NULL)
printf("Error opening telem file\n");
fclose(telem_file);
printf("Opened telem file\n");
battery_saver_mode = battery_saver_check();
/**/
if (battery_saver_mode == ON) {
SafeMode = 1;
fprintf(stderr, "Safe Mode! Battery_saver_mode is ON\n\n");
}
else {
fprintf(stderr, "\nBattery_saver_mode is OFF\n\n");
SafeMode = 0;
}
/**/
fflush(stderr);
if (mode == AFSK)
{
// Check for SPI and AX-5043 Digital Transceiver Board
FILE * file = popen("sudo raspi-config nonint get_spi", "r");
// printf("getc: %c \n", fgetc(file));
if (fgetc(file) == 48) {
printf("SPI is enabled!\n");
FILE * file2 = popen("ls /dev/spidev0.* 2>&1", "r");
printf("Result getc: %c \n", getc(file2));
if (fgetc(file2) != 'l') {
printf("SPI devices present!\n");
// }
setSpiChannel(SPI_CHANNEL);
setSpiSpeed(SPI_SPEED);
initializeSpi();
ax25_init( & hax25, (uint8_t * ) dest_addr, 11, (uint8_t * ) call, 11, AX25_PREAMBLE_LEN, AX25_POSTAMBLE_LEN);
if (init_rf()) {
printf("AX5043 successfully initialized!\n");
ax5043 = TRUE;
cw_id = OFF;
// mode = AFSK;
// cycle = OFF;
printf("Mode AFSK with AX5043\n");
transmit = TRUE;
// sleep(10); // just in case CW ID is sent
} else
printf("AX5043 not present!\n");
pclose(file2);
}
}
pclose(file);
}
txLed = 2;
txLedOn = HIGH;
txLedOff = LOW;
vB5 = TRUE;
onLed = 27;
onLedOn = HIGH;
onLedOff = LOW;
pinMode(26, INPUT);
pullUpDnControl(26, PUD_UP);
if (digitalRead(26) != HIGH) {
printf("LPF present\n");
transmit = TRUE;
}
config_file = fopen("sim.cfg", "w");
fprintf(config_file, "%s %d %8.4f %8.4f %s %d %s %s %s %d %d", call, reset_count, lat_file, long_file, sim_yes, squelch, tx, rx, hab_yes, rx_pl, tx_pl);
// fprintf(config_file, "%s %d", call, reset_count);
fclose(config_file);
config_file = fopen("sim.cfg", "r");
if (vB4) {
map[BAT] = BAT2;
map[BAT2] = BAT;
snprintf(busStr, 10, "%d %d", i2c_bus1, test_i2c_bus(0));
} else if (vB5) {
map[MINUS_X] = MINUS_Y;
map[PLUS_Z] = MINUS_X;
map[MINUS_Y] = PLUS_Z;
if (access("/dev/i2c-11", W_OK | R_OK) >= 0) { // Test if I2C Bus 11 is present
printf("/dev/i2c-11 is present\n\n");
snprintf(busStr, 10, "%d %d", test_i2c_bus(1), test_i2c_bus(11));
} else {
snprintf(busStr, 10, "%d %d", i2c_bus1, i2c_bus3);
}
} else {
map[BAT2] = MINUS_Z;
map[BAT] = BAT2;
map[PLUS_Z] = BAT;
map[MINUS_Z] = PLUS_Z;
snprintf(busStr, 10, "%d %d", i2c_bus1, test_i2c_bus(0));
voltageThreshold = 8.0;
}
// check for camera
// char cmdbuffer1[1000];
FILE * file4 = popen("vcgencmd get_camera", "r");
fgets(cmdbuffer, 1000, file4);
char camera_present[] = "supported=1 detected=1";
// printf("strstr: %s \n", strstr( & cmdbuffer1, camera_present));
camera = (strstr( (const char *)& cmdbuffer, camera_present) != NULL) ? ON : OFF;
printf("Camera result:%s camera: %d \n", & cmdbuffer, camera);
pclose(file4);
#ifdef DEBUG_LOGGING
printf("INFO: I2C bus status 0: %d 1: %d 3: %d camera: %d\n", i2c_bus0, i2c_bus1, i2c_bus3, camera);
#endif
FILE * file5 = popen("sudo rm /home/pi/CubeSatSim/camera_out.jpg > /dev/null 2>&1", "r");
//file5 = popen("sudo rm /home/pi/CubeSatSim/camera_out.jpg.wav > /dev/null 2>&1", "r");
pclose(file5);
if (!ax5043) // don't test for payload if AX5043 is present
{
payload = OFF;
fprintf(stderr,"Opening serial\n");
if ((uart_fd = serialOpen("/dev/ttyAMA0", 115200)) >= 0) { // was 9600
fprintf(stderr,"Serial opened to Pico\n");
// payload = ON;
payload = get_payload_serial(FALSE);
fprintf(stderr,"Get_payload_status: %d \n", payload); // not debug
} else {
fprintf(stderr, "Unable to open UART: %s\n -> Did you configure /boot/config.txt and /boot/cmdline.txt?\n", strerror(errno));
}
}
if ((i2c_bus3 == OFF) || (sim_mode == TRUE)) {
sim_mode = TRUE;
fprintf(stderr, "Simulated telemetry mode!\n");
srand((unsigned int)time(0));
axis[0] = rnd_float(-0.2, 0.2);
if (axis[0] == 0)
axis[0] = rnd_float(-0.2, 0.2);
axis[1] = rnd_float(-0.2, 0.2);
axis[2] = (rnd_float(-0.2, 0.2) > 0) ? 1.0 : -1.0;
angle[0] = (float) atan(axis[1] / axis[2]);
angle[1] = (float) atan(axis[2] / axis[0]);
angle[2] = (float) atan(axis[1] / axis[0]);
volts_max[0] = rnd_float(4.5, 5.5) * (float) sin(angle[1]);
volts_max[1] = rnd_float(4.5, 5.5) * (float) cos(angle[0]);
volts_max[2] = rnd_float(4.5, 5.5) * (float) cos(angle[1] - angle[0]);
float amps_avg = rnd_float(150, 300);
amps_max[0] = (amps_avg + rnd_float(-25.0, 25.0)) * (float) sin(angle[1]);
amps_max[1] = (amps_avg + rnd_float(-25.0, 25.0)) * (float) cos(angle[0]);
amps_max[2] = (amps_avg + rnd_float(-25.0, 25.0)) * (float) cos(angle[1] - angle[0]);
batt = rnd_float(3.8, 4.3);
speed = rnd_float(1.0, 2.5);
eclipse = (rnd_float(-1, +4) > 0) ? 1.0 : 0.0;
period = rnd_float(150, 300);
tempS = rnd_float(20, 55);
temp_max = rnd_float(50, 70);
temp_min = rnd_float(10, 20);
#ifdef DEBUG_LOGGING
for (int i = 0; i < 3; i++)
printf("axis: %f angle: %f v: %f i: %f \n", axis[i], angle[i], volts_max[i], amps_max[i]);
printf("batt: %f speed: %f eclipse_time: %f eclipse: %f period: %f temp: %f max: %f min: %f\n", batt, speed, eclipse_time, eclipse, period, tempS, temp_max, temp_min);
#endif
time_start = (long int) millis();
eclipse_time = (long int)(millis() / 1000.0);
if (eclipse == 0.0)
eclipse_time -= period / 2; // if starting in eclipse, shorten interval
}
// tx_freq_hz -= tx_channel * 50000;
if (transmit == FALSE) {
fprintf(stderr, "\nNo CubeSatSim Low Pass Filter detected. No transmissions after the CW ID.\n");
fprintf(stderr, " See http://cubesatsim.org/wiki for info about building a CubeSatSim\n\n");
}
if (mode == FSK) {
bitRate = 200;
rsFrames = 1;
payloads = 1;
rsFrameLen = 64;
headerLen = 6;
dataLen = 58;
syncBits = 10;
syncWord = 0b0011111010;
parityLen = 32;
amplitude = 32767 / 3;
samples = S_RATE / bitRate;
bufLen = (frameCnt * (syncBits + 10 * (headerLen + rsFrames * (rsFrameLen + parityLen))) * samples);
samplePeriod = (int) (((float)((syncBits + 10 * (headerLen + rsFrames * (rsFrameLen + parityLen)))) / (float) bitRate) * 1000 - 500);
sleepTime = 0.1f;
frameTime = ((float)((float)bufLen / (samples * frameCnt * bitRate))) * 1000; // frame time in ms
printf("\n FSK Mode, %d bits per frame, %d bits per second, %d ms per frame, %d ms sample period\n",
bufLen / (samples * frameCnt), bitRate, frameTime, samplePeriod);
} else if (mode == BPSK) {
bitRate = 1200;
rsFrames = 3;
payloads = 6;
rsFrameLen = 159;
headerLen = 8;
dataLen = 78;
syncBits = 31;
syncWord = 0b1000111110011010010000101011101;
parityLen = 32;
amplitude = 32767;
samples = S_RATE / bitRate;
bufLen = (frameCnt * (syncBits + 10 * (headerLen + rsFrames * (rsFrameLen + parityLen))) * samples);
samplePeriod = ((float)((syncBits + 10 * (headerLen + rsFrames * (rsFrameLen + parityLen))))/(float)bitRate) * 1000 - 1800;
// samplePeriod = 3000;
// sleepTime = 3.0;
//samplePeriod = 2200; // reduce dut to python and sensor querying delays
sleepTime = 2.2f;
frameTime = ((float)((float)bufLen / (samples * frameCnt * bitRate))) * 1000; // frame time in ms
printf("\n BPSK Mode, bufLen: %d, %d bits per frame, %d bits per second, %d ms per frame %d ms sample period\n",
bufLen, bufLen / (samples * frameCnt), bitRate, frameTime, samplePeriod);
sin_samples = S_RATE/freq_Hz;
// printf("Sin map: ");
for (int j = 0; j < sin_samples; j++) {
sin_map[j] = (short int)(amplitude * sin((float)(2 * M_PI * j / sin_samples)));
// printf(" %d", sin_map[j]);
}
printf("\n");
}
memset(voltage, 0, sizeof(voltage));
memset(current, 0, sizeof(current));
memset(sensor, 0, sizeof(sensor));
memset(other, 0, sizeof(other));
if (((mode == FSK) || (mode == BPSK))) // && !sim_mode)
get_tlm_fox(); // fill transmit buffer with reset count 0 packets that will be ignored
firstTime = 1;
// if (!sim_mode) // always read sensors, even in sim mode
{
strcpy(pythonStr, pythonCmd);
strcat(pythonStr, busStr);
strcat(pythonConfigStr, pythonStr);
strcat(pythonConfigStr, " c");
fprintf(stderr, "pythonConfigStr: %s\n", pythonConfigStr);
file1 = sopen(pythonConfigStr); // python sensor polling function
fgets(cmdbuffer, 1000, file1);
fprintf(stderr, "pythonStr result: %s\n", cmdbuffer);
}
for (int i = 0; i < 9; i++) {
voltage_min[i] = 1000.0;
current_min[i] = 1000.0;
voltage_max[i] = -1000.0;
current_max[i] = -1000.0;
}
for (int i = 0; i < SENSOR_FIELDS; i++) {
sensor_min[i] = 1000.0;
sensor_max[i] = -1000.0;
// printf("Sensor min and max initialized!");
}
for (int i = 0; i < 3; i++) {
other_min[i] = 1000.0;
other_max[i] = -1000.0;
}
loopTime = millis();
while (loop-- != 0) {
fflush(stdout);
fflush(stderr);
// frames_sent++;
sensor_payload[0] = 0;
memset(voltage, 0, sizeof(voltage));
memset(current, 0, sizeof(current));
memset(sensor, 0, sizeof(sensor));
memset(other, 0, sizeof(other));
FILE * uptime_file = fopen("/proc/uptime", "r");
fscanf(uptime_file, "%f", & uptime_sec);
uptime = (int) (uptime_sec + 0.5);
// printf("Uptime sec: %f \n", uptime_sec);
// #ifdef DEBUG_LOGGING
// printf("INFO: Reset Count: %d Uptime since Reset: %ld \n", reset_count, uptime);
// #endif
fclose(uptime_file);
{
int count1;
char * token;
fputc('\n', file1);
fgets(cmdbuffer, 1000, file1);
// fprintf(stderr, "Python read Result: %s\n", cmdbuffer);
// serialPuts(uart_fd, cmdbuffer); // write INA data to Pico over serial
const char space[2] = " ";
token = strtok(cmdbuffer, space);
for (count1 = 0; count1 < 8; count1++) {
if (token != NULL) {
voltage[count1] = (float) atof(token);
#ifdef DEBUG_LOGGING
// printf("voltage: %f ", voltage[count1]);
#endif
token = strtok(NULL, space);
if (token != NULL) {
current[count1] = (float) atof(token);
if ((current[count1] < 0) && (current[count1] > -0.5))
current[count1] *= (-1.0f);
#ifdef DEBUG_LOGGING
// printf("current: %f\n", current[count1]);
#endif
token = strtok(NULL, space);
}
}
if (voltage[map[BAT]] == 0.0)
batteryVoltage = 4.5;
else {
batteryVoltage = voltage[map[BAT]];
if (sim_mode && !sim_config) { // if Voltage sensor on Battery board is present, exit simulated telemetry mode
sim_mode = FALSE;
fprintf(stderr, "Turning off sim_mode since battery sensor is present\n");
}
}
batteryCurrent = current[map[BAT]];
}
// if (payload == ON) { // moved to here
if (!ax5043) {
// if ((payload == ON) && (mode != BPSK)) { // moved to here
// STEMBoardFailure = 0;
payload = get_payload_serial(FALSE);
printf("get_payload_status: %d \n", payload); // not debug
fflush(stdout);
// printf("String: %s\n", buffer2);
fflush(stdout);
strcpy(sensor_payload, buffer2);
// printf(" Response from STEM Payload board: %s\n", sensor_payload);
telem_file = fopen("/home/pi/CubeSatSim/telem.txt", "a");
// printf("Writing payload string\n");
time_t timeStamp;
time(&timeStamp); // get timestamp
// printf("Timestamp: %s\n", ctime(&timeStamp));
char timeStampNoNl[31], bat_string[31];
snprintf(timeStampNoNl, 30, "%.24s", ctime(&timeStamp));
// printf("TimeStamp: %s\n", timeStampNoNl);
if (c2cStatus == DISABLED)
snprintf(bat_string, 30, "BAT %4.2f %5.1f", batteryVoltage, batteryCurrent);
else
snprintf(bat_string, 30, "BAT %4.2f %5.1f C", batteryVoltage, batteryCurrent);
fprintf(telem_file, "%s %s %s\n", timeStampNoNl, bat_string, sensor_payload); // write telemetry string to telem.txt file
fclose(telem_file);
if ((sensor_payload[0] == 'O') && (sensor_payload[1] == 'K')) // only process if valid payload response
{
int count1;
char * token;
const char space[2] = " ";
token = strtok(sensor_payload, space);
// printf("token: %s\n", token);
for (count1 = 0; count1 < SENSOR_FIELDS; count1++) {
if (token != NULL) {
sensor[count1] = (float) atof(token);
// #ifdef DEBUG_LOGGING
// printf("sensor: %f ", sensor[count1]); // print sensor data
// #endif
token = strtok(NULL, space);
}
}
printf("\n");
// if (sensor[GPS1] != 0) {
if ((sensor[GPS1] > -90.0) && (sensor[GPS1] < 90.0) && (sensor[GPS1] != 0.0)) {
if (sensor[GPS1] != latitude) {
latitude = sensor[GPS1];
printf("Latitude updated to %f \n", latitude);
newGpsTime = millis();
}
}
// if (sensor[GPS2] != 0) {
if ((sensor[GPS2] > -180.0) && (sensor[GPS2] < 180.0) && (sensor[GPS2] != 0.0)) {
if (sensor[GPS2] != longitude) {
longitude = sensor[GPS2];
printf("Longitude updated to %f \n", longitude);
newGpsTime = millis();
}
}
}
else
; //payload = OFF; // turn off since STEM Payload is not responding
}
if ((millis() - newGpsTime) > 60000) {
longitude += rnd_float(-0.05, 0.05) / 100.0; // was .05
latitude += rnd_float(-0.05, 0.05) / 100.0;
// printf("GPS Location with Rnd: %f, %f \n", latitude, longitude);
// printf("GPS Location with Rnd: APRS %07.2f, %08.2f \n", toAprsFormat(latitude), toAprsFormat(longitude));
newGpsTime = millis();
}
if ((sensor_payload[0] == 'O') && (sensor_payload[1] == 'K')) {
for (int count1 = 0; count1 < SENSOR_FIELDS; count1++) {
if (sensor[count1] < sensor_min[count1])
sensor_min[count1] = sensor[count1];
if (sensor[count1] > sensor_max[count1])
sensor_max[count1] = sensor[count1];
// printf("Smin %f Smax %f \n", sensor_min[count1], sensor_max[count1]);
}
}
if (sim_mode) { // simulated telemetry
double time = ((long int)millis() - time_start) / 1000.0;
if ((time - eclipse_time) > period) {
eclipse = (eclipse == 1) ? 0 : 1;
eclipse_time = time;
printf("\n\nSwitching eclipse mode! \n\n");
}
double Xi = eclipse * amps_max[0] * (float) sin(2.0 * 3.14 * time / (46.0 * speed)) + rnd_float(-2, 2);
double Yi = eclipse * amps_max[1] * (float) sin((2.0 * 3.14 * time / (46.0 * speed)) + (3.14 / 2.0)) + rnd_float(-2, 2);
double Zi = eclipse * amps_max[2] * (float) sin((2.0 * 3.14 * time / (46.0 * speed)) + 3.14 + angle[2]) + rnd_float(-2, 2);
double Xv = eclipse * volts_max[0] * (float) sin(2.0 * 3.14 * time / (46.0 * speed)) + rnd_float(-0.2, 0.2);
double Yv = eclipse * volts_max[1] * (float) sin((2.0 * 3.14 * time / (46.0 * speed)) + (3.14 / 2.0)) + rnd_float(-0.2, 0.2);
double Zv = 2.0 * eclipse * volts_max[2] * (float) sin((2.0 * 3.14 * time / (46.0 * speed)) + 3.14 + angle[2]) + rnd_float(-0.2, 0.2);
// printf("Yi: %f Zi: %f %f %f Zv: %f \n", Yi, Zi, amps_max[2], angle[2], Zv);
current[map[PLUS_X]] = (Xi >= 0) ? Xi : 0;
current[map[MINUS_X]] = (Xi >= 0) ? 0 : ((-1.0f) * Xi);
current[map[PLUS_Y]] = (Yi >= 0) ? Yi : 0;
current[map[MINUS_Y]] = (Yi >= 0) ? 0 : ((-1.0f) * Yi);
current[map[PLUS_Z]] = (Zi >= 0) ? Zi : 0;
current[map[MINUS_Z]] = (Zi >= 0) ? 0 : ((-1.0f) * Zi);
voltage[map[PLUS_X]] = (Xv >= 1) ? Xv : rnd_float(0.9, 1.1);
voltage[map[MINUS_X]] = (Xv <= -1) ? ((-1.0f) * Xv) : rnd_float(0.9, 1.1);
voltage[map[PLUS_Y]] = (Yv >= 1) ? Yv : rnd_float(0.9, 1.1);
voltage[map[MINUS_Y]] = (Yv <= -1) ? ((-1.0f) * Yv) : rnd_float(0.9, 1.1);
voltage[map[PLUS_Z]] = (Zv >= 1) ? Zv : rnd_float(0.9, 1.1);
voltage[map[MINUS_Z]] = (Zv <= -1) ? ((-1.0f) * Zv) : rnd_float(0.9, 1.1);
// printf("temp: %f Time: %f Eclipse: %d : %f %f | %f %f | %f %f\n",tempS, time, eclipse, voltage[map[PLUS_X]], voltage[map[MINUS_X]], voltage[map[PLUS_Y]], voltage[map[MINUS_Y]], current[map[PLUS_Z]], current[map[MINUS_Z]]);
tempS += (eclipse > 0) ? ((temp_max - tempS) / 50.0f) : ((temp_min - tempS) / 50.0f);
tempS += +rnd_float(-1.0, 1.0);
// IHUcpuTemp = (int)((tempS + rnd_float(-1.0, 1.0)) * 10 + 0.5);
other[IHU_TEMP] = tempS;
voltage[map[BAT2]] = 0.0; // rnd_float(5.0, 5.005);
current[map[BAT2]] = 0.0; // rnd_float(158, 171);
// float charging = current[map[PLUS_X]] + current[map[MINUS_X]] + current[map[PLUS_Y]] + current[map[MINUS_Y]] + current[map[PLUS_Z]] + current[map[MINUS_Z]];
float charging = eclipse * (fabs(amps_max[0] * 0.707) + fabs(amps_max[1] * 0.707) + rnd_float(-4.0, 4.0));
current[map[BAT]] = ((current[map[BAT2]] * voltage[map[BAT2]]) / batt) - charging;
// printf("charging: %f bat curr: %f bus curr: %f bat volt: %f bus volt: %f \n",charging, current[map[BAT]], current[map[BAT2]], batt, voltage[map[BAT2]]);
batt -= (batt > 3.5) ? current[map[BAT]] / 30000 : current[map[BAT]] / 3000;
if (batt < 3.0) {
batt = 3.0;
SafeMode = 1;
printf("Safe Mode!\n");
} else
SafeMode= 0;
if (batt > 4.5)
batt = 4.5;
voltage[map[BAT]] = batt + rnd_float(-0.01, 0.01);
// end of simulated telemetry
}
else {
}
FILE * cpuTempSensor = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
if (cpuTempSensor) {
// double cpuTemp;
fscanf(cpuTempSensor, "%lf", & cpuTemp);
cpuTemp /= 1000;
#ifdef DEBUG_LOGGING
// printf("CPU Temp Read: %6.1f\n", cpuTemp);
#endif
other[IHU_TEMP] = (double)cpuTemp;
// IHUcpuTemp = (int)((cpuTemp * 10.0) + 0.5);
}
fclose(cpuTempSensor);
}
#ifdef DEBUG_LOGGING
// fprintf(stderr, "INFO: Battery voltage: %5.2f V Threshold %5.2f V Current: %6.1f mA Threshold: %6.1f mA\n", batteryVoltage, voltageThreshold, batteryCurrent, currentThreshold);
#endif
if ((batteryCurrent > currentThreshold) && (batteryVoltage < (voltageThreshold + 0.15)) && !sim_mode && !hab_mode)
{
fprintf(stderr,"Battery voltage low\n");
if (battery_saver_mode == OFF) {
fprintf(stderr,"Switch to battery saver\n");
battery_saver(ON);
fprintf(stderr, "Safe Mode!\n");
SafeMode = 1;
}
} else if ((battery_saver_mode == ON) && (batteryCurrent < 0) && !sim_mode && !hab_mode)
{
fprintf(stderr,"Battery is being charged - switch battery saver off\n");
if (battery_saver_mode == ON) {
battery_saver(OFF);
fprintf(stderr, "Safe Mode off!\n");
SafeMode = 0;
}
}
if ((batteryCurrent > currentThreshold) && (batteryVoltage < voltageThreshold) && !sim_mode && !hab_mode) // currentThreshold ensures that this won't happen when running on DC power.
{
fprintf(stderr, "Battery voltage too low: %f V - shutting down!\n", batteryVoltage);
digitalWrite(txLed, txLedOff);
digitalWrite(onLed, onLedOff);
FILE * file6;
file6 = popen("echo 'shutdown due to low battery voltage!' | wall", "r");
pclose(file6);
sleep(1);
digitalWrite(onLed, onLedOn);
sleep(1);
digitalWrite(onLed, onLedOff);
sleep(1);
digitalWrite(onLed, onLedOn);
sleep(1);
digitalWrite(onLed, onLedOff);
file6 = popen("sudo shutdown -h now > /dev/null 2>&1", "r");
pclose(file6);
sleep(10);
}
//#endif
FILE * fp = fopen("/home/pi/CubeSatSim/telem_string.txt", "w");
if (fp != NULL) {
// printf("Writing telem_string.txt\n");
if (batteryVoltage != 4.5)
if (c2cStatus == DISABLED)
fprintf(fp, "BAT %4.2fV %4.0fmA\n", batteryVoltage, batteryCurrent);
else
fprintf(fp, "BAT %4.2fV %4.0fmA C\n", batteryVoltage, batteryCurrent); // show command and control is on
else
fprintf(fp, "\n"); // don't show voltage and current if it isn't a sensor value
fclose(fp);
} else
printf("Error writing to telem_string.txt\n");
/**/
// sleep(1); // Delay 1 second
ctr = 0;
#ifdef DEBUG_LOGGING
// fprintf(stderr, "INFO: Getting TLM Data\n");
#endif
FILE * command_file = fopen("/home/pi/CubeSatSim/command_control", "r");
if (command_file == NULL) {
if (c2cStatus != DISABLED) {
fprintf(stderr,"Command and control is OFF\n");
c2cStatus = DISABLED;
}
} else {
command_file = fopen("/home/pi/CubeSatSim/command_control_direwolf", "r");
if (command_file == NULL) {
if (c2cStatus != CARRIER) {
fprintf(stderr,"Command and control Carrier (squelch) is ON\n");
c2cStatus = CARRIER;
}
} else {
if (c2cStatus != DTMF_APRS) {
fprintf(stderr,"Command and control DTMF or APRS is ON\n");
c2cStatus = DTMF_APRS;
}
}
}
// printf("c2cStatus: %d \n", c2cStatus);
if ((mode == AFSK) || (mode == CW)) {
get_tlm();
sleep(25);
// fprintf(stderr, "INFO: Sleeping for 25 sec\n");
int rand_sleep = (int)rnd_float(0.0, 5.0);
sleep(rand_sleep);
// fprintf(stderr, "INFO: Sleeping for extra %d sec\n", rand_sleep);
} else if ((mode == FSK) || (mode == BPSK)) {// FSK or BPSK
get_tlm_fox();
} else { // SSTV
// fprintf(stderr, "Sleeping\n");
sleep(30);
}
#ifdef DEBUG_LOGGING
// fprintf(stderr, "INFO: Getting ready to send\n");
#endif
}
if (mode == BPSK) {
// digitalWrite(txLed, txLedOn);
#ifdef DEBUG_LOGGING
// printf("Tx LED On 1\n");
#endif
// printf("Sleeping to allow BPSK transmission to finish.\n");
sleep((unsigned int)(loop_count * 5));
// printf("Done sleeping\n");
// digitalWrite(txLed, txLedOff);
#ifdef DEBUG_LOGGING
// printf("Tx LED Off\n");
#endif
} else if (mode == FSK) {
// printf("Sleeping to allow FSK transmission to finish.\n");
sleep((unsigned int)loop_count);
// printf("Done sleeping\n");
}
return 0;
}
// Returns lower digit of a number which must be less than 99
//
int lower_digit(int number) {
int digit = 0;
if (number < 100)
digit = number - ((int)(number / 10) * 10);
else
fprintf(stderr, "ERROR: Not a digit in lower_digit!\n");
return digit;
}
// Returns upper digit of a number which must be less than 99
//
int upper_digit(int number) {
int digit = 0;
if (number < 100)
digit = (int)(number / 10);
else
fprintf(stderr, "ERROR: Not a digit in upper_digit!\n");
return digit;
}
static int init_rf() {
int ret;
fprintf(stderr, "Initializing AX5043\n");
ret = ax5043_init( & hax5043, XTAL_FREQ_HZ, VCO_INTERNAL);
if (ret != PQWS_SUCCESS) {
fprintf(stderr,
"ERROR: Failed to initialize AX5043 with error code %d\n", ret);
// exit(EXIT_FAILURE);
return (0);
}
return (1);
}
void get_tlm(void) {
FILE * txResult;
for (int j = 0; j < frameCnt; j++) {
fflush(stdout);
fflush(stderr);
int tlm[7][5];
memset(tlm, 0, sizeof tlm);
tlm[1][A] = (int)(voltage[map[BAT2]] / 15.0 + 0.5) % 100; // Current of 5V supply to Pi
tlm[1][B] = (int)(99.5 - current[map[PLUS_X]] / 10.0) % 100; // +X current [4]
tlm[1][C] = (int)(99.5 - current[map[MINUS_X]] / 10.0) % 100; // X- current [10]
tlm[1][D] = (int)(99.5 - current[map[PLUS_Y]] / 10.0) % 100; // +Y current [7]
tlm[2][A] = (int)(99.5 - current[map[MINUS_Y]] / 10.0) % 100; // -Y current [10]
tlm[2][B] = (int)(99.5 - current[map[PLUS_Z]] / 10.0) % 100; // +Z current [10] // was 70/2m transponder power, AO-7 didn't have a Z panel
tlm[2][C] = (int)(99.5 - current[map[MINUS_Z]] / 10.0) % 100; // -Z current (was timestamp)
tlm[2][D] = (int)(50.5 + current[map[BAT]] / 10.0) % 100; // NiMH Battery current
// tlm[3][A] = abs((int)((voltage[map[BAT]] * 10.0) - 65.5) % 100);
if (voltage[map[BAT]] > 4.6)
tlm[3][A] = (int)((voltage[map[BAT]] * 10.0) - 65.5) % 100; // 7.0 - 10.0 V for old 9V battery
else
tlm[3][A] = (int)((voltage[map[BAT]] * 10.0) + 44.5) % 100; // 0 - 4.5 V for new 3 cell battery
tlm[3][B] = (int)(voltage[map[BAT2]] * 10.0) % 100; // 5V supply to Pi
tlm[4][A] = (int)((95.8 - other[IHU_TEMP]) / 1.48 + 0.5) % 100; // was [B] but didn't display in online TLM spreadsheet
tlm[6][B] = 0;
tlm[6][D] = 49 + rand() % 3;
/*
#ifdef DEBUG_LOGGING
// Display tlm
int k, j;
for (k = 1; k < 7; k++) {
for (j = 1; j < 5; j++) {
printf(" %2d ", tlm[k][j]);
}
printf("\n");
}
#endif
*/
char str[1000];
char tlm_str[1000];
char header_str[] = "\x03\xf0"; // hi hi ";
char header_str3[] = "echo '";
char header_str2[] = "-11>APCSS:";
char header_str2b[30]; // for APRS coordinates
char header_lat[10];
char header_long[10];
char header_str4[] = "hi hi de ";
// char footer_str1[] = "\' > t.txt && echo \'";
char footer_str1[] = "\' > t.txt";
// char footer_str[] = "-11>APCSS:010101/hi hi ' >> t.txt && touch /home/pi/CubeSatSim/ready"; // transmit is done by transmit.py