-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathflightterminal.py
960 lines (864 loc) · 34.2 KB
/
flightterminal.py
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
#!/usr/bin/env python
# note: The data gathering portion of this should be centralized
# At the begining of each loop all needed data should be collected
# and then referenced as a local variable. I'll rewrite this when I
# get the chance.
# Notes on screen layout:
# Standard window block is ideally 25y by 15x chr
# Text display window is 25y by 30x chr
# Each gauge window is 25y by 15x including boarders
# Resource window is 10y by 15x
'''To do list:
Update the gauge drawing routine so that is only draws a gauge if a
tank for that resource exists
Add multithreading support so that the telemetry update timeouts dont
interfere with the rest of the program
Actually fix the serial communications with the arduino so that they use
proper headers and footers that dont interfere with the actual data'''
import math
import time
import curses
import copy
import telemachus_plugin as tele
import config
try:
import serial
except:
print 'PySerial does not seem to be installed'
try:
ser = serial.Serial(
port=config.arduinoSerialPort(),
#port='COM3',
#baudrate=115200, # Causes the arduino buffer to fill up
baudrate=9600, # Seems to be working well
# parity=serial.PARITY_ODD,
# stopbits=serial.STOPBITS_TWO,
# bytesize=serial.SEVENBITS
)
arduinoConnected = True
print 'Serial device connected at ' + config.arduinoSerialPort()
except:
arduinoConnected = False
print 'Unable to connect to arduino at ' + config.arduinoSerialPort()
def getFlightData(dIN):
# Try to update the dictionary with live data. If any of it fails for any
# reason, return the original dictionary and set the 'Radio Contact' key
# to false.
d = {'Zero': 0}
try:
d['MET'] = float(tele.read_missiontime())
d['ASL'] = int(tele.read_asl())
d['Height From Terrain'] = int(tele.read_heightFromTerrain())
d['Body'] = str(tele.read_body())
d['Ap'] = int(tele.read_apoapsis())
d['Pe'] = int(tele.read_periapsis())
d['Time to Ap'] = float(tele.read_time_to_ap())
d['Time to Pe'] = float(tele.read_time_to_pe())
d['Eccentricity'] = float(tele.read_eccentricity())
d['Inclination'] = float(tele.read_inclination())
d['Orbital Period'] = float(tele.read_orbitalperiod())
d['Vertical Speed'] = float(tele.read_verticalspeed())
d['Surface Speed'] = float(tele.read_surfacespeed())
d['Pitch'] = float(tele.read_facing('pitch'))
d['Yaw'] = float(tele.read_facing('yaw'))
d['Roll'] = float(tele.read_facing('roll'))
d['Throttle'] = float(tele.read_throttle())
d['Brake Status'] = int(tele.brake(2))
d['Gear Status'] = int(tele.gear(2))
d['SAS Status'] = int(tele.sas(2))
d['RCS Status'] = int(tele.rcs(2))
d['Light Status'] = int(tele.light(2))
d['ElectricCharge'] = float(tele.read_resource('ElectricCharge'))
d['Max ElectricCharge'] = float(tele.read_resource_max(
'ElectricCharge'))
d['LiquidFuel'] = float(tele.read_resource('LiquidFuel'))
d['Max LiquidFuel'] = float(tele.read_resource_max('LiquidFuel'))
d['Oxidizer'] = float(tele.read_resource('Oxidizer'))
d['Max Oxidizer'] = float(tele.read_resource_max('Oxidizer'))
d['SolidFuel'] = float(tele.read_resource('SolidFuel'))
d['Max SolidFuel'] = float(tele.read_resource_max('SolidFuel'))
d['MonoPropellant'] = float(tele.read_resource('MonoPropellant'))
d['Max MonoPropellant'] = float(tele.read_resource_max('MonoPropellant'))
# If you are playing with realism mods, uncomment these as needed
d['Oxygen'] = float(tele.read_resource('Oxygen'))
d['Max Oxygen'] = float(tele.read_resource_max('Oxygen'))
d['LiquidOxygen'] = float(tele.read_resource('LiquidOxygen'))
d['Max LiquidOxygen'] = float(tele.read_resource_max('LiquidOxygen'))
d['LiquidH2'] = float(tele.read_resource('LiquidH2'))
d['Max LiquidH2'] = float(tele.read_resource_max('LiquidH2'))
d['MMH'] = float(tele.read_resource('MMH'))
d['Max MMH'] = float(tele.read_resource_max('MMH'))
d['N2O4'] = float(tele.read_resource('N2O4'))
d['Max N2O4'] = float(tele.read_resource_max('N2O4'))
d['Previous Radio Contact'] = dIN['Radio Contact']
d['Radio Contact'] = True
#Clean up the data types
if d['SAS Status'] == 1:
d['SAS Status'] = True
elif d['SAS Status'] == 0:
d['SAS Status'] = False
else:
d['SAS Status'] = 'Error'
if d['RCS Status'] == 1:
d['RCS Status'] = True
else:
d['RCS Status'] = False
if d['Light Status'] == 1:
d['Light Status'] = True
else:
d['Light Status'] = False
if d['Gear Status'] == 1:
d['Gear Status'] = True
elif d['Gear Status'] == 0:
d['Gear Status'] = False
if d['Brake Status'] == 1:
d['Brake Status'] = True
elif d['Brake Status'] == 0:
d['Brake Status'] = False
return d
except:
dIN['Previous Radio Contact'] = dIN['Radio Contact']
dIN['Radio Contact'] = False
del d
return dIN
def drawPrimaryStatusWindow(yCord, xCord):
primaryStatusW = myscreen.subwin(25, 30, yCord, xCord)
primaryStatusW.border()
yL = 0 # local variable for the Y line
#xL = 0 # local variable for the X line
primaryStatusW.addstr(yL, 1, "## Earth Time: ##", curses.A_STANDOUT)
yL += 1
primaryStatusW.addstr(yL, 2, time.strftime("%a, %d %b %Y %H:%M:%S"), curses.color_pair(1))
yL += 1
yL += 1
primaryStatusW.addstr(yL, 1, "## Mission Time: ##", curses.A_STANDOUT)
yL += 1
primaryStatusW.addstr(yL, 4, str(round(fd['MET'], 1))
.zfill(21), curses.color_pair(1))
yL += 1
yL += 1
primaryStatusW.addstr(yL, 1, "Body: ")
primaryStatusW.addstr(yL, 20, str(fd['Body']).rjust(8))
yL += 1
primaryStatusW.addstr(yL, 1, "ASL:")
primaryStatusW.addstr(yL, 20, str(fd['ASL'])
.zfill(filldigits))
yL += 1
yL += 1
primaryStatusW.addstr(yL, 1, "## Orbital Information ##", curses.A_STANDOUT)
yL += 1
primaryStatusW.addstr(yL, 1, "Apoapsis:")
primaryStatusW.addstr(yL, 20, str(fd['Ap'])
.zfill(filldigits))
yL += 1
primaryStatusW.addstr(yL, 1, "Periapsis:")
primaryStatusW.addstr(yL, 20, str(fd['Pe'])
.zfill(filldigits))
yL += 1
primaryStatusW.addstr(yL, 1, "Eccentricity:")
primaryStatusW.addstr(yL, 20, str(round(fd['Eccentricity'], 6))
.zfill(filldigits))
yL += 1
primaryStatusW.addstr(yL, 1, "Inclination:")
primaryStatusW.addstr(yL, 20, str(round(fd['Inclination'], 6))
.zfill(filldigits))
yL += 1
primaryStatusW.addstr(yL, 1, "Orbital Period:")
primaryStatusW.addstr(yL, 20, str(round(fd['Orbital Period'], 1))
.zfill(filldigits))
yL += 1
primaryStatusW.addstr(yL, 1, "Time to Ap:")
primaryStatusW.addstr(yL, 20, str(round(fd['Time to Ap'], 1))
.zfill(filldigits))
yL += 1
primaryStatusW.addstr(yL, 1, "Time to Pe:")
primaryStatusW.addstr(yL, 20, str(round(fd['Time to Pe'], 1))
.zfill(filldigits))
yL += 1
yL += 1
primaryStatusW.addstr(yL, 1, "## Flight Configuration ##",
curses.A_STANDOUT)
yL += 1
primaryStatusW.addstr(yL, 1, "SAS Status:")
primaryStatusW.addstr(yL, 20, str(fd['SAS Status']).ljust(5))
yL += 1
primaryStatusW.addstr(yL, 1, "RCS Status:")
primaryStatusW.addstr(yL, 20, str(fd['RCS Status']).ljust(5))
yL += 1
primaryStatusW.addstr(yL, 1, "Ext. Lights:")
primaryStatusW.addstr(yL, 20, str(fd['Light Status']).ljust(5))
yL += 1
primaryStatusW.addstr(yL, 1, "Landing Gear:")
primaryStatusW.addstr(yL, 20, str(fd['Gear Status']).ljust(5))
yL += 1
primaryStatusW.addstr(yL, 1, "Brake Status:")
primaryStatusW.addstr(yL, 20, str(fd['Brake Status']).ljust(5))
yL += 1
return yL
def drawResourceWindow(yCord, xCord):
resourceW = myscreen.subwin(10, 30, yCord, xCord)
resourceW.border()
yL = 0 # local variable for the Y line
#xL = 0 # local variable for the X line
resourceW.addstr(yL, 1, "## Resources ##",
curses.A_STANDOUT)
yL += 1
if fd['ElectricCharge'] != -1:
resourceW.addstr(yL, 1, "Electricity:")
resourceW.addstr(yL, 20, str(int(fd['ElectricCharge']))
.zfill(filldigits))
yL += 1
if fd['Oxygen'] != -1:
resourceW.addstr(yL, 1, "Oxygen:")
resourceW.addstr(yL, 20, str(int(fd['Oxygen']))
.zfill(filldigits))
yL += 1
if fd['MonoPropellant'] != -1:
resourceW.addstr(yL, 1, "MonoPropellant:")
resourceW.addstr(yL, 20, str(int(fd['MonoPropellant']))
.zfill(filldigits))
yL += 1
if fd['LiquidFuel'] != -1:
resourceW.addstr(yL, 1, "Liquid Fuel:")
resourceW.addstr(yL, 20, str(int(fd['LiquidFuel']))
.zfill(filldigits))
yL += 1
if fd['Oxidizer'] != -1:
resourceW.addstr(yL, 1, "Oxidizer:")
resourceW.addstr(yL, 20, str(int(fd['Oxidizer']))
.zfill(filldigits))
yL += 1
if fd['SolidFuel'] != -1:
resourceW.addstr(yL, 1, "Solid Fuel:")
resourceW.addstr(yL, 20, str(int(fd['SolidFuel']))
.zfill(filldigits))
yL += 1
if fd['LiquidH2'] != -1:
resourceW.addstr(yL, 1, "Liquid Hydrogen:")
resourceW.addstr(yL, 20, str(int(fd['LiquidH2']))
.zfill(filldigits))
yL += 1
if fd['LiquidOxygen'] != -1:
resourceW.addstr(yL, 1, "Liquid Oxygen:")
resourceW.addstr(yL, 20, str(int(fd['LiquidOxygen']))
.zfill(filldigits))
yL += 1
if fd['MMH'] != -1:
resourceW.addstr(yL, 1, "MMH:")
resourceW.addstr(yL, 20, str(int(fd['MMH']))
.zfill(filldigits))
yL += 1
if fd['N2O4'] != -1:
resourceW.addstr(yL, 1, "N2O4:")
resourceW.addstr(yL, 20, str(int(fd['N2O4']))
.zfill(filldigits))
yL += 1
return yL
def drawArduinoWindow(yCord, xCord):
arduinoW = myscreen.subwin(10, 60, yCord, xCord)
arduinoW.border()
yL = 0 # local variable for the Y line
#fNorm = curses.A_NORMAL # Formatting for unselected options
arduinoW.addstr(yL, 30 - 12, "## Arduino Readouts ##",
curses.A_STANDOUT)
yL += 1
arduinoW.addstr(yL, 1, "Serial Out:")
yL += 1
try:
arduinoW.addstr(yL, 1, str(memA))
except:
arduinoW.addstr(yL, 1,
'Memory contains bytes that can not be printed ')
yL += 1
yL += 1
arduinoW.addstr(yL, 1, "Serial In:")
# arduinoW.addstr(yL, 10, str(int(str(memB))))
yL += 1
arduinoW.addstr(yL, 1, "".join([str(x) for x in memB]))
yL += 1
yL += 1
arduinoW.addstr(yL, 1, "Program Loop Time:")
yL += 1
arduinoW.addstr(yL, 1, str(round(loopTime, 5)))
yL += 1
def drawMainMenu(yCord, xCord):
menuDepth = 15
mainMenu = myscreen.subwin(menuDepth, 25, yCord, xCord)
mainMenu.clear()
mainMenu.attron(curses.color_pair(3))
mainMenu.border()
mainMenu.attroff(curses.color_pair(3))
#mainMenu.bkgd(32, curses.color_pair(2)) #Fill the background with spaces
yL = 1 # local variable for the Y line
FSO = curses.A_STANDOUT # Formatting for selected option
mainMenu.addstr(0, 12 - 5, " Main Menu ",
curses.A_BOLD)
if ps['Flight Transceiver Active'] is False:
if ps['Main Menu Selection'] == yL:
mainMenu.addstr(yL, 1, str(yL) + ":Activate Flight Data", FSO)
if chrin == ord('\n'):
ps['Flight Transceiver Active'] = True
else:
mainMenu.addstr(yL, 1, str(yL) + ":Activate Flight Data")
elif ps['Flight Transceiver Active'] is True:
if ps['Main Menu Selection'] == yL:
mainMenu.addstr(yL, 1, str(yL) + ":Dectivate Flight Data", FSO)
if chrin == ord('\n'):
ps['Flight Transceiver Active'] = False
else:
mainMenu.addstr(yL, 1, str(yL) + ":Dectivate Flight Data")
yL += 1
###### Set Display Options
if ps['Display Mode'] is not 'Standard':
if ps['Main Menu Selection'] == yL:
mainMenu.addstr(yL, 1, str(yL) + ":Set Disp. to Standard", FSO)
if chrin == ord('\n'):
ps['Display Mode'] = 'Standard'
myscreen.clear()
else:
mainMenu.addstr(yL, 1, str(yL) + ":Set Disp. to Standard")
elif ps['Display Mode'] is 'Standard':
if ps['Main Menu Selection'] == yL:
mainMenu.addstr(yL, 1, str(yL) + ":Dectivate Display", FSO)
if chrin == ord('\n'):
ps['Display Mode'] = 'Off'
myscreen.clear()
else:
mainMenu.addstr(yL, 1, str(yL) + ":Dectivate Display")
yL += 1
if ps['Display Mode'] is not 'Landing':
if ps['Main Menu Selection'] == yL:
mainMenu.addstr(yL, 1, str(yL) + ":Set Disp. to Landing", FSO)
if chrin == ord('\n'):
ps['Display Mode'] = 'Landing'
else:
mainMenu.addstr(yL, 1, str(yL) + ":Set Disp. to Landing")
elif ps['Display Mode'] is 'Landing':
if ps['Main Menu Selection'] == yL:
mainMenu.addstr(yL, 1, str(yL) + ":Dectivate Display", FSO)
if chrin == ord('\n'):
ps['Display Mode'] = 'Off'
myscreen.clear()
else:
mainMenu.addstr(yL, 1, str(yL) + ":Dectivate Display")
yL += 1
###### Arduino
if arduinoConnected is True:
if ps['Main Menu Selection'] == yL and ps['Arduino Active'] is False:
mainMenu.addstr(yL, 1, str(yL) + ":Activate Arduino", FSO)
if chrin == ord('\n'):
ps['Arduino Active'] = True
elif ps['Main Menu Selection'] != yL and ps['Arduino Active'] is False:
mainMenu.addstr(yL, 1, str(yL) + ":Activate Arduino")
elif ps['Main Menu Selection'] == yL and ps['Arduino Active'] is True:
mainMenu.addstr(yL, 1, str(yL) + ":Deactivate Arduino", FSO)
if chrin == ord('\n'):
ps['Arduino Active'] = False
elif ps['Main Menu Selection'] != yL and ps['Arduino Active'] is True:
mainMenu.addstr(yL, 1, str(yL) + ":Deactivate Arduino")
else:
pass
elif arduinoConnected is False:
mainMenu.attron(curses.color_pair(2))
if ps['Main Menu Selection'] == yL:
mainMenu.addstr(yL, 1, str(yL) + ":Arduino not available", FSO)
else:
mainMenu.addstr(yL, 1, str(yL) + ":Arduino not available")
mainMenu.attroff(curses.color_pair(2))
if ps['Arduino Active'] is True:
yL += 1
if ps['Main Menu Selection'] == yL:
mainMenu.addstr(yL, 1, str(yL) + ":Arduino - Flight Mode", FSO)
if chrin == ord('\n'):
arduino['Display Mode'] = 'Flight Mode'
else:
mainMenu.addstr(yL, 1, str(yL) + ":Arduino - Flight Mode")
yL += 1
if ps['Main Menu Selection'] == yL:
mainMenu.addstr(yL, 1, str(yL) + ":Arduino - Clock Mode", FSO)
if chrin == ord('\n'):
arduino['Display Mode'] = 'Clock'
else:
mainMenu.addstr(yL, 1, str(yL) + ":Arduino - Clock Mode")
yL += 1
if ps['Main Menu Selection'] == yL:
mainMenu.addstr(yL, 1, str(yL) + ":Arduino - Lamp Test", FSO)
if chrin == ord('\n'):
arduino['Display Mode'] = 'Lamp Test'
else:
mainMenu.addstr(yL, 1, str(yL) + ":Arduino - Lamp Test")
yL += 1
yL += 1
if ps['Main Menu Selection'] == yL:
mainMenu.addstr(yL, 1, str(yL) + ":Force Screen Redraw", FSO)
if chrin == ord('\n'):
myscreen.clear()
else:
mainMenu.addstr(yL, 1, str(yL) + ":Force Screen Redraw")
yL += 1
if ps['Main Menu Selection'] == yL: # Exit doesn't actually work yet
mainMenu.addstr(yL, 1, str(yL) + ":Exit", FSO)
if chrin == ord('\n'):
pass
else:
mainMenu.addstr(yL, 1, str(yL) + ":Exit")
mainMenu.addstr(menuDepth - 4, 2, "Press Enter to Select")
mainMenu.addstr(menuDepth - 3, 1, "Program Loop Time:")
mainMenu.addstr(menuDepth - 2, 1, str(round(loopTime, 5)))
#### Keyboard input for moving the hightlight ###
if chrin == ord('1'):
ps['Main Menu Selection'] = 1
elif chrin == ord('2'):
ps['Main Menu Selection'] = 2
elif chrin == ord('3'):
ps['Main Menu Selection'] = 3
elif chrin == ord('4'):
ps['Main Menu Selection'] = 4
elif chrin == ord('5'):
ps['Main Menu Selection'] = 5
elif chrin == ord('6'):
ps['Main Menu Selection'] = 6
elif chrin == 258:
if ps['Main Menu Selection'] < yL:
ps['Main Menu Selection'] += 1
else:
ps['Main Menu Selection'] = 1
elif chrin == 259:
if ps['Main Menu Selection'] > 1:
ps['Main Menu Selection'] -= 1
else:
ps['Main Menu Selection'] = yL
elif chrin == ord('\n'):
ps['Main Menu is Open'] = False
ps['Slection Made'] = True
mainMenu.clear()
elif chrin == 27:
ps['Main Menu is Open'] = False
ps['Slection Made'] = False
mainMenu.clear()
elif chrin == 96:
ps['Main Menu is Open'] = False
ps['Slection Made'] = False
mainMenu.clear()
def formatRCC(inputln): # Formating Reading Color Critical
if int(inputln) < 0:
inputln = str(int(inputln)).zfill(filldigits)
return inputln, curses.color_pair(2)
else:
return str(int(inputln)).zfill(filldigits)
def drawVGauge(gLabel, resource, yCord, xCord):
# Draws a gauge from 0-100% that is 30 rows x 15 Coll with the top left
# corner at (yCord, xCord)
percentVal = round((fd[resource] /
fd[str('Max ' + resource)]) * 100, 1)
if percentVal >= 100:
percentVal = int(100)
gauge = myscreen.subwin(25, 15, yCord, xCord)
gauge.clear()
gauge.border()
gauge.addstr(0, 7 - len(gLabel) / 2, gLabel)
barHeight = min(20, int(percentVal / 5))
gauge.addstr(22, 7 - len(gLabel) / 2, gLabel)
if barHeight > 0:
for n in range(-1, 2): # Make a vline 3 colums wide
if barHeight > 0:
gauge.attron(curses.color_pair(3))
gauge.vline(22 - barHeight, 7 + n, curses.ACS_CKBOARD,
barHeight)
gauge.attroff(curses.color_pair(3))
if barHeight >= 5:
gauge.attron(curses.color_pair(2))
gauge.vline(22 - barHeight, 7 + n, curses.ACS_CKBOARD,
barHeight - 5)
gauge.attroff(curses.color_pair(2))
if barHeight > 10:
gauge.attron(curses.color_pair(1))
gauge.vline(22 - barHeight, 7 + n, curses.ACS_CKBOARD,
barHeight - 10)
gauge.attroff(curses.color_pair(1))
gauge.addstr(23, 5, str(percentVal).zfill(3) + '%')
elif percentVal < 5 and percentVal > 0:
gauge.attron(curses.color_pair(3))
gauge.addstr(23, 6, 'Low')
gauge.attroff(curses.color_pair(3))
elif percentVal == 0:
gauge.attron(curses.color_pair(3))
gauge.addstr(23, 5, 'Empty')
gauge.attroff(curses.color_pair(3))
else:
gauge.addstr(23, 1, 'Not Available')
def drawStandardGaugeList():
#myscreen.vline(20, 35, curses.ACS_CKBOARD, 4)
#drawVGauge("Test Gauge", 42, 1, 35)
if fd['ElectricCharge'] != -1:
drawVGauge("Electricity", 'ElectricCharge', 1, 31)
if fd['Oxygen'] != -1:
drawVGauge("Oxygen", 'Oxygen', 1, 61)
if fd['MonoPropellant'] != -1:
drawVGauge("MonoProp.", 'MonoPropellant', 1, 46)
if fd['LiquidFuel'] != -1:
drawVGauge("LiquidFuel", 'LiquidFuel', 1, 61)
if fd['Oxidizer'] != -1:
drawVGauge("Oxidizer", 'Oxidizer', 1, 61)
if fd['LiquidH2'] != -1:
drawVGauge("Liquid H2", 'LiquidH2', 1, 76)
if fd['LiquidOxygen'] != -1:
drawVGauge("Liquid Oxygen", 'LiquidOxygen', 1, 91)
if fd['MMH'] != -1:
drawVGauge("MMH", 'MMH', 1, 76)
if fd['N2O4'] != -1:
drawVGauge("N2O4", 'N2O4', 1, 91)
def drawLandingStatusWindow(yCord, xCord):
landingStatusW = myscreen.subwin(25, 30, yCord, xCord)
landingStatusW.border()
yL = 0 # local variable for the Y line
#xL = 0 # local variable for the X line
landingStatusW.addstr(yL, 1, "## Earth Time: ##", curses.A_STANDOUT)
yL += 1
landingStatusW.addstr(yL, 2, time.strftime("%a, %d %b %Y %H:%M:%S"), curses.color_pair(1))
yL += 1
yL += 1
landingStatusW.addstr(yL, 1, "## Mission Time: ##", curses.A_STANDOUT)
yL += 1
landingStatusW.addstr(yL, 4, str(round(fd['MET'], 1))
.zfill(21), curses.color_pair(1))
yL += 1
yL += 1
landingStatusW.addstr(yL, 1, "ASL:")
landingStatusW.addstr(yL, 20, str(fd['ASL'])
.zfill(filldigits))
yL += 1
yL += 1
landingStatusW.addstr(yL, 1, "Radar Alt:")
landingStatusW.addstr(yL, 20, str(int(fd['Height From Terrain']))
.zfill(filldigits))
yL += 1
yL += 1
landingStatusW.addstr(yL, 1, "Surface Speed:")
landingStatusW.addstr(yL, 20, str(round(fd['Surface Speed']))
.zfill(filldigits))
yL += 1
yL += 1
landingStatusW.addstr(yL, 1, "Vertical Speed:")
landingStatusW.addstr(yL, 20, str(round(fd['Vertical Speed']))
.zfill(filldigits))
yL += 1
yL += 1
landingStatusW.addstr(yL, 1, "Throttle:")
landingStatusW.addstr(yL, 22, str(int(fd['Throttle'] * 100)) + ' %')
yL += 1
yL += 1
yL += 1
yL += 1
landingStatusW.addstr(yL, 1, "Pitch:")
landingStatusW.addstr(yL, 22, str(int(math.degrees(fd['Pitch'])))
.zfill(4))
yL += 1
yL += 1
landingStatusW.addstr(yL, 1, "Yaw:")
landingStatusW.addstr(yL, 22, str(int(math.degrees(fd['Yaw'])))
.zfill(4))
yL += 1
yL += 1
landingStatusW.addstr(yL, 1, "Roll:")
landingStatusW.addstr(yL, 22, str(int(math.degrees(fd['Roll'])))
.zfill(4))
yL += 1
yL += 1
return yL
### Arduino Utilities
def push_to_arduino(inputline):
#ser.flushOutput()
# Send data to the Arduino and end w/ a newline
ser.write(inputline + '\n')
#ser.write("255, 255, 255 \n")
#time.sleep(.2)
def formatForArduino(mode):
if mode == 'Lamp Test': # Light Test
arduino['7r0 Data'] = '88888888'
arduino['7r1 Data'] = '88888888'
arduino['7r2 Data'] = '88888888'
arduino['7r3 Data'] = '88888888'
arduino['7r4 Data'] = '88888888'
arduino['g0 Data'] = chr(255)
arduino['g1 Data'] = chr(255)
arduino['g2 Data'] = chr(255)
arduino['g3 Data'] = chr(255)
arduino['g4 Data'] = chr(255)
arduino['g5 Data'] = chr(255)
arduino['g6 Data'] = chr(255)
arduino['g7 Data'] = chr(255)
elif mode == 'Clock':
'''There's something weird going on here that causes the code to
take a full 300ms or so to loop. This requires further research.'''
arduino['7r0 Data'] = ' '
arduino['7r1 Data'] = str(time.strftime("%H %M %S"))
arduino['7r2 Data'] = str(time.strftime(" %m %d "))
arduino['7r3 Data'] = str(time.strftime(" %Y "))
arduino['7r4 Data'] = ' '
arduino['g0 Data'] = '0'
arduino['g1 Data'] = '0'
arduino['g2 Data'] = '0'
arduino['g3 Data'] = '0'
arduino['g4 Data'] = '0'
arduino['g5 Data'] = '0'
arduino['g6 Data'] = '0'
arduino['g7 Data'] = '0'
else:
arduino['7r0 Data'] = str(int(round(fd["MET"]))).zfill(8)
arduino['7r1 Data'] = str(int(round(fd["ASL"] / 100))).zfill(8)
arduino['7r2 Data'] = str(int(round(fd["Ap"] / 100))).zfill(8)
arduino['7r3 Data'] = str(int(round(fd["Pe"] / 100))).zfill(8)
arduino['7r4 Data'] = str(int(round(fd["Time to Ap"]))).zfill(8)
arduino['g0 Data'] = 'A'
arduino['g1 Data'] = 'A'
arduino['g2 Data'] = 'A'
arduino['g3 Data'] = 'A'
arduino['g4 Data'] = 'A'
arduino['g5 Data'] = 'A'
arduino['g6 Data'] = 'A'
arduino['g7 Data'] = 'A'
def buttonHandler():
# Reads memB for which buttons are pressed, then sends
# calls to telemachus as needed.
global memB
global memBOLD
if memB[1] == '1' and memB[1] != memBOLD[1]:
if (memB[7] == '1'): # Check the safety
tele.stage()
if memB[0] == '1' and memB[0] != memBOLD[0]:
if memB[7] == '1': # Check the safety
tele.abort()
if int(memB[2]) == 1 and memB[2] != memBOLD[2]:
# Toggle gear based on what we did last time
if ps['Gear Status'] is True:
# Telemachus does not yet read gear status
tele.gear(0)
ps['Gear Status'] = False
elif ps['Gear Status'] is False:
tele.gear(1)
ps['Gear Status'] = True
if int(memB[3]) == 1 and memB[3] != memBOLD[3]:
# Toggle Light based on the Telemachus reading
if fd['Light Status'] is True:
tele.light(0)
elif fd['Light Status'] is False:
tele.light(1)
if int(memB[4]) == 1 and memB[4] != memBOLD[4]:
# Toggle brake based on what we did last time
if ps['Brake Status'] is True:
# Telemachus does not yet read brake status
tele.brake(0)
ps['Brake Status'] = False
elif ps['Brake Status'] is False:
tele.brake(1)
ps['Brake Status'] = True
if int(memB[5]) == 1 and memB[5] != memBOLD[5]:
# Toggle RCS based on the Telemachus reading
if fd['RCS Status'] is True:
tele.rcs(0)
elif fd['RCS Status'] is False:
tele.rcs(1)
if int(memB[6]) == 1 and memB[6] != memBOLD[6]:
# Toggle SAS based on the Telemachus reading
if fd['SAS Status'] is True:
tele.sas(0)
elif fd['SAS Status'] is False:
tele.sas(1)
def clamp(num, minn, maxn):
if num < minn:
return minn
elif num > maxn:
return maxn
else:
return num
#### Main Program ############################################################
myscreen = curses.initscr()
if curses.can_change_color:
curses.start_color()
curses.use_default_colors()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
curses.noecho()
curses.cbreak()
myscreen.keypad(1)
chrin = -1
filldigits = 8
arduinoActive = 0
menuOpen = 0
# Flight Data Memory and other variables
fd = { # Primary data storage
'MET': -1, 'ASL': -1, 'Ap': -1, 'Pe': -1, 'Time to Ap': -1, 'Time to Pe': -1,
'Eccentricity': -1, 'Inclination': -1, 'Orbital Period': -1,
'Vertical Speed': -1, 'Surface Speed': -1, 'Pitch': -1, 'Roll': -1, 'Yaw': -1,
'Height From Terrain': -1,
'Throttle': -1, 'SAS Status': -1, 'RCS Status': -1, 'Light Status': -1,
'Brake Status': -1, 'Gear Status': -1,
'ElectricCharge': -1, 'Max ElectricCharge': -1,
'LiquidFuel': -1, 'Max LiquidFuel': -1,
'Oxidizer': -1, 'Max Oxidizer': -1,
'SolidFuel': -1, 'Max SolidFuel': -1,
'MonoPropellant': -1, 'Max MonoPropellant': -1,
'Oxygen': -1, 'Max Oxygen': -1, # Realisim Resources
'LiquidH2': -1, 'Max LiquidH2': -1,
'LiquidOxygen': -1, 'Max LiquidOxygen': -1,
'MMH': -1, 'Max MMH': -1,
'N2O4': -1, 'Max N2O4': -1,
'Radio Contact': False, 'Previous Radio Contact': False}
ps = { # Program Settings
'Main Menu is Open': False, 'Main Menu Selection': 1, 'Slection Made': False,
'Display Mode': 'Standard',
'Flight Transceiver Active': True,
'Terminal Max Y': 25, 'Terminal Max X': 40,
'Arduino Sleep Marker': 0, 'Arduino Active': False, 'Button Sleep Marker': 0,
'flightData Sleep Marker': 0, 'Gear Status': False, 'Brake Status': False}
arduino = { # Arduino Configurtion, using rows
'Display Mode': 'Lamp Test',
'7r0 Name': 'MET', '7r0 Data': str().zfill(8),
'7r1 Name': 'ASL', '7r1 Data': str().zfill(8),
'7r2 Name': 'Ap', '7r2 Data': str().zfill(8),
'7r3 Name': 'Pe', '7r3 Data': str().zfill(8),
'7r4 Name': 'Time to Ap', '7r4 Data': str().zfill(8),
'g0 Name': 'MET', 'g0 Data': str().zfill(1)
}
arduinoSleepMarker = 0
buttonSleepMarker = 0
flightDataSleepMarker = config.pollInterval()
memB = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # Current serial input
memBOLD = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # Old serial input
n = 0
ps['Terminal Max Y'], ps['Terminal Max X'] = myscreen.getmaxyx()
print ps['Terminal Max Y']
### Flight Computer Section ##################################################
while chrin != 48 and __name__ == '__main__':
# Loop until the user presses the Zero key
loopStartTime = time.time()
if (ps['Terminal Max Y'], ps['Terminal Max X']) != myscreen.getmaxyx():
myscreen.clear()
if ps['Flight Transceiver Active'] is True:
if flightDataSleepMarker >= config.pollInterval() and (
fd['Radio Contact'] is True):
fd = getFlightData(fd)
flightDataSleepMarker = 0
#### Expand this into an actual error handling bit.
# Do a single test once per second that only polls one item
# Alternativel, do actually multithreading w/ the URL stuff in the other
# thread. That makes far more sense.
elif flightDataSleepMarker >= config.pollInterval() + 2:
fd = getFlightData(fd)
flightDataSleepMarker = 0
myscreen.border()
ps['Terminal Max Y'], ps['Terminal Max X'] = myscreen.getmaxyx()
yLine = 2 # Starting Row for flight info
myscreen.nodelay(1)
myscreen.addstr(0, ps['Terminal Max X'] / 2 - 12, "Persigehl Flight Terminal")
if ps['Display Mode'] is 'Standard':
drawStandardGaugeList()
if fd['Radio Contact'] is True and fd['Previous Radio Contact'] is True:
drawPrimaryStatusWindow(1, 1)
drawResourceWindow(26, 1)
elif fd['Radio Contact'] is False:
myscreen.addstr(ps['Terminal Max Y'] - 2, ps['Terminal Max X'] / 2 - 12,
"### Radio Contact Lost ###", curses.A_STANDOUT)
elif fd['Radio Contact'] is True and fd['Previous Radio Contact'] is False:
myscreen.clear()
myscreen.border()
drawPrimaryStatusWindow(1, 1)
drawResourceWindow(26, 1)
elif ps['Display Mode'] is 'Landing':
myscreen.clear()
myscreen.border()
drawLandingStatusWindow(1, (ps['Terminal Max X'] / 2 - 10))
### Arduino Section ##########################################################
if ps['Arduino Active'] is True:
climbgauge = int(fd['Vertical Speed'])
'''This will need to be rewritten to only allow a subset of characters
so that it will no longer be possible to get header characters mixed
up in the trasmission (ie: '[' or ']') Perhaps limit the ranges to
only 128 bits, which is more than the gauges I have can really
support anyways.'''
if climbgauge > 0:
climbgauge = clamp((int(4 * math.sqrt(climbgauge)) + 127), 0, 254)
elif climbgauge < 0:
climbgauge = clamp((0 - int(4 * math.sqrt(
abs(climbgauge))) + 127), 0, 254)
else:
climbgauge = 127 # Neutral
formatForArduino(arduino['Display Mode'])
memA = (
str(arduino['7r0 Data']) +
str(arduino['7r1 Data']) +
str(arduino['7r2 Data']) +
str(arduino['7r3 Data']) +
str(arduino['7r4 Data']) +
str(arduino['g0 Data']) +
str(arduino['g1 Data']) +
str(arduino['g2 Data']) +
str(arduino['g3 Data']) +
str(arduino['g4 Data']) +
str(arduino['g5 Data']) +
str(arduino['g6 Data']) +
str(arduino['g7 Data'])
)
if arduinoSleepMarker > 0.25:
try:
push_to_arduino(memA)
except:
pass
finally:
arduinoSleepMarker = 0
if ser.inWaiting > 9:
try:
serCharIn = str(ser.read(1))
if serCharIn == '[':
while n < 12:
serCharIn = str(ser.read(1))
if serCharIn == ']':
n = 0
ser.flushInput()
break
else:
memB[n] = serCharIn
n += 1
if n == 11:
ser.flushInput()
except:
ser.flushInput()
if buttonSleepMarker > 0.1:
buttonHandler()
button_sleep_marker = 0
memBOLD = list(memB)
drawArduinoWindow(26, 31)
if chrin == 96 and ps['Main Menu is Open'] is False:
# Display the pop up menu if '`' is pressed (the key to the left of 1)
ps['Main Menu is Open'] = True
chrin = 0
if ps['Main Menu is Open'] is True:
drawMainMenu(1, ps['Terminal Max X'] - 26)
### Main Loop Cleanup ########################################################
myscreen.addstr(ps['Terminal Max Y'] - 1, 2, " Press ` to toggle Main Menu ")
myscreen.addstr(ps['Terminal Max Y'] - 1, ps['Terminal Max X'] - 19, " Press 0 to Exit ")
myscreen.refresh()
chrin = myscreen.getch()
loopTimeOffset = config.pollInterval() + loopStartTime - time.time()
# This can be used to slow the entire program down to cycle at a
# given interval. This is a failsafe to prevent 100% utilization
if loopTimeOffset > 0:
time.sleep(loopTimeOffset)
# Combined Bandwidth used based on interval:
# 33ms = around 395 Packets/S, 345kbs
# 25ms = around 500 Packets/S, 410kbs
loopEndTime = time.time()
loopTime = loopEndTime - loopStartTime
arduinoSleepMarker += loopTime
buttonSleepMarker += loopTime
flightDataSleepMarker += loopTime
curses.nocbreak()
myscreen.keypad(0)
curses.echo()
curses.endwin()
print 'FlightTerminal ended by user'
print 'Screen dimensions on close were:'
print ('Y: ' + str(ps['Terminal Max Y']) + " " + 'X: ' +
str(ps['Terminal Max X']))