-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswfoscsim_logger.py
166 lines (143 loc) · 5.94 KB
/
swfoscsim_logger.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
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 38400)
ser_logger = serial.Serial('/dev/ttyUSB5', 9600)
# Set up byte array for housekeeping packet
hk_apid = 0x4e4
hk_pktlen = 68 - 7
hk_seqcnt = 0
hk_packet = bytearray(( (hk_apid//256) | 0x08, hk_apid & 0xff,
0, 0, # seqcnt
hk_pktlen//256, hk_pktlen & 0xff,
0,0,0, # day
0,0,0,0, # ms of day
0,0, # usec
0, # spare
0,0,0,0, # wd 0: cmd_acc (8), scb0 status (24)
0,0,0,0, # wd 1: cmd_rej (8), scb1 status (24)
0,0,0,0, # wd 2: acc fc (8), misc (24)
0,0,0,0, # wd 3: rej fc (8), misc (24)
0,0,0,0, # wd 4
0,0,0,0, # wd 5
0,0,0,0, # wd 6
0,0,0,0, # wd 7
0,0,0,0, # wd 8
0,0,0,0, # wd 9
0,0,0,0, # wd 10
0,0,0,0, # wd 11
0,0,0,0)) # wd 12
# Set up byte array for mag vector packet
vec_apid = 0x4e5
vec_seqcnt = 0
vec_packet = bytearray(( (vec_apid//256) | 0x08, vec_apid & 0xff,
0,0, # seqcnt
0,0, # packet length field
0,0,0, # day
0,0,0,0, # ms of day
0,0, # usec
0, # spare
0,0,0,0, # flags
0,0,0,0,0,0,0,0,0, # SCB-0 X
0,0,0,0,0,0,0,0,0, # SCB-0 Y
0,0,0,0,0,0,0,0,0, # SCB-0 Z
0,0,0,0,0,0,0,0,0, # SCB-1 X
0,0,0,0,0,0,0,0,0, # SCB-1 Y
0,0,0,0,0,0,0,0,0)) # SCB-1 Z
mag_cadence = 1
#mag_cadence = 0
hk_cadence = 8
tmsg_days=0
tmsg_msecs=0
tmsg_usecs=0
time_tag = 0
last_hk = 0
last_mag = 0
hk_seqcnt = 0
vec_seqcnt = 0
time_msg_apid = 1
while True:
offset = 0
multipacket = True
data = ser.read(ser.inWaiting())
if data :
print(len(data),ser.inWaiting())
start=time.monotonic()
print([hex(x) for x in data])
while multipacket:
# Decode primary header
apid = (data[offset+0] * 256 + data[offset+1]) & 0x7ff
seq_cnt = (data[offset+2] * 256 + data[offset+3]) & 0x3fff
plen = data[offset+4] * 256 + data[offset+5]
oldoffset = offset
offset = offset + plen + 7
if len(data) < offset:
print(">>> incomplete packet received. Looking for remainder")
print(plen,len(data),offset)
while not ser.inWaiting():
pass
print(time.monotonic()-start)
print(ser.inWaiting())
data = data + ser.read(ser.inWaiting())
if data :
print(len(data),ser.inWaiting())
print([hex(x) for x in data])
logger_string = ''.join('{0:02x}'.format(x) for x in data[oldoffset:oldoffset+plen+7])
logger_string = "TC" + logger_string + "\n"
logger_bytestring = bytes(logger_string,"UTF-8")
ser_logger.write(logger_bytestring)
if len(data) != offset:
print(">>> multiple packets received. Looking for newpacket")
print(plen,len(data),offset)
else:
multipacket = False
if apid == 1: # Time message
tmsg_days = (data[oldoffset+6] * 256 + data[oldoffset+7]) * 256 + data[oldoffset+8]
tmsg_msecs = ((data[oldoffset+9] * 256 + data[oldoffset+10]) * 256 + data[oldoffset+11]) * 256 + data[oldoffset+12]
tmsg_usecs = data[oldoffset+13] * 256 + data[oldoffset+14]
print("Received Time Message: days = %06x msecs = %08x usecs = %04x" % (tmsg_days, tmsg_msecs, tmsg_usecs))
if (hk_cadence > 0) and ((time.monotonic() - last_hk) > hk_cadence) :
last_hk=time.monotonic()
# Update sequence count in primary header
hk_seqcnt = (hk_seqcnt + 1) & 0x3fff # 14 bits
hk_packet[2] = 0xC0 | (hk_seqcnt >> 8)
hk_packet[3] = hk_seqcnt & 0xff
# Update time code in secondary header
hk_packet[6] = (tmsg_days >> 16) & 0xff
hk_packet[7] = (tmsg_days >> 8) & 0xff
hk_packet[8] = tmsg_days & 0xff
hk_packet[9] = (tmsg_msecs >> 24) & 0xff
hk_packet[10] = (tmsg_msecs >> 16) & 0xff
hk_packet[11] = (tmsg_msecs >> 8) & 0xff
hk_packet[12] = tmsg_msecs & 0xff
# Update telemetry items
for i in range(hk_pktlen + 7 - 16):
hk_packet[16 + i] = i
ser.write(hk_packet[0:hk_pktlen+7])
if (mag_cadence > 0) and ((time.monotonic() - last_mag) > mag_cadence) :
last_mag=time.monotonic()
# Mag vector packet: once per second
vec_seqcnt = (vec_seqcnt + 1) & 0x3fff # 14 bits
# packet length:
# 16 bytes header, 4 bytes flags, 6*9 bytes components
plen = 16 + 4 + 6*9 - 7
# Update primary header
vec_packet[2] = 0xC0 | (vec_seqcnt >> 8)
vec_packet[3] = vec_seqcnt & 0xff
vec_packet[4] = (plen >> 8) & 0xff
vec_packet[5] = plen & 0xff
# Update time code in secondary header
vec_packet[6] = (tmsg_days >> 16) & 0xff
vec_packet[7] = (tmsg_days >> 8) & 0xff
vec_packet[8] = tmsg_days & 0xff
vec_packet[9] = (tmsg_msecs >> 24) & 0xff
vec_packet[10] = (tmsg_msecs >> 16) & 0xff
vec_packet[11] = (tmsg_msecs >> 8) & 0xff
vec_packet[12] = tmsg_msecs & 0xff
# Update telemetry items
vec_packet[16] = 0xaa # flags
vec_packet[17] = 0xbb # flags
vec_packet[18] = 0xcc # flags
vec_packet[19] = 0xdd # flags
for i in range(6*9):
vec_packet[20+i] = 0x00 + i
ser.write(vec_packet[0:plen+7])