-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathsim_inst.rb
More file actions
448 lines (401 loc) · 15.3 KB
/
sim_inst.rb
File metadata and controls
448 lines (401 loc) · 15.3 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
# encoding: ascii-8bit
# Copyright 2022 Ball Aerospace & Technologies Corp.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt
#
# 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 Affero General Public License for more details.
# Modified by OpenC3, Inc.
# All changes Copyright 2025, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
# Provides a demonstration of a Simulated Target
require 'openc3'
require 'stringio'
require 'base64'
require 'openc3/accessors/binary_accessor'
module OpenC3
# Simulated instrument for the demo. Populates several packets and cycles
# the telemetry to simulate an active target.
class SimInst < SimulatedTarget
SOLAR_PANEL_DFLTS = [-179.0, 179.0, -179.0, 179.0, -95.0] unless defined? SOLAR_PANEL_DFLTS
def initialize(target_name)
super(target_name)
@target = System.targets[target_name]
position_filename = File.join(@target.dir, 'data', 'position.bin')
attitude_filename = File.join(@target.dir, 'data', 'attitude.bin')
position_data = File.read(position_filename, mode: "rb")
attitude_data = File.read(attitude_filename, mode: "rb")
@position_file = StringIO.new(position_data)
@position_file_size = position_data.length
@attitude_file = StringIO.new(attitude_data)
@attitude_file_size = attitude_data.length
@position_file_bytes_read = 0
@attitude_file_bytes_read = 0
@images = []
data = File.read(File.join(@target.dir, 'public', 'spiral.jpg'), mode: "rb")
@images << Base64.encode64(data)
data = File.read(File.join(@target.dir, 'public', 'sun.jpg'), mode: "rb")
@images << Base64.encode64(data)
data = File.read(File.join(@target.dir, 'public', 'ganymede.jpg'), mode: "rb")
@images << Base64.encode64(data)
@cur_image = 0
@pos_packet = Structure.new(:BIG_ENDIAN)
@pos_packet.append_item('DAY', 16, :UINT)
@pos_packet.append_item('MSOD', 32, :UINT)
@pos_packet.append_item('USOMS', 16, :UINT)
@pos_packet.append_item('POSX', 32, :FLOAT)
@pos_packet.append_item('POSY', 32, :FLOAT)
@pos_packet.append_item('POSZ', 32, :FLOAT)
@pos_packet.append_item('SPARE1', 16, :UINT)
@pos_packet.append_item('SPARE2', 32, :UINT)
@pos_packet.append_item('SPARE3', 16, :UINT)
@pos_packet.append_item('VELX', 32, :FLOAT)
@pos_packet.append_item('VELY', 32, :FLOAT)
@pos_packet.append_item('VELZ', 32, :FLOAT)
@pos_packet.append_item('SPARE4', 32, :UINT)
@pos_packet.enable_method_missing
@att_packet = Structure.new(:BIG_ENDIAN)
@att_packet.append_item('DAY', 16, :UINT)
@att_packet.append_item('MSOD', 32, :UINT)
@att_packet.append_item('USOMS', 16, :UINT)
@att_packet.append_item('Q1', 32, :FLOAT)
@att_packet.append_item('Q2', 32, :FLOAT)
@att_packet.append_item('Q3', 32, :FLOAT)
@att_packet.append_item('Q4', 32, :FLOAT)
@att_packet.append_item('BIASX', 32, :FLOAT)
@att_packet.append_item('BIASY', 32, :FLOAT)
@att_packet.append_item('BIASZ', 32, :FLOAT)
@att_packet.append_item('SPARE', 32, :FLOAT)
@att_packet.enable_method_missing
packet = @tlm_packets['HEALTH_STATUS']
packet.enable_method_missing
packet.CcsdsSeqFlags = 'NOGROUP'
packet.CcsdsLength = packet.buffer.length - 7
packet.temp1 = 50.0
packet.temp2 = -20.0
packet.temp3 = 85.0
packet.temp4 = 0.0
packet.duration = 10.0
packet.collect_type = 'NORMAL'
packet = @tlm_packets['ADCS']
packet.enable_method_missing
packet.CcsdsSeqFlags = 'NOGROUP'
packet.CcsdsLength = packet.buffer.length - 7
packet = @tlm_packets['PARAMS']
packet.enable_method_missing
packet.CcsdsSeqFlags = 'NOGROUP'
packet.CcsdsLength = packet.buffer.length - 7
packet.value1 = 0
packet.value2 = 1
packet.value3 = 2
packet.value4 = 1
packet.value5 = 0
packet.write('P_2.2,2', BinaryAccessor::MIN_INT64)
packet.write('P-3+3=3', BinaryAccessor::MAX_INT64)
packet.write('P4!@#$%^&*?', 0)
packet.write('P</5|\>', 1740684371613049856)
packet.write('P(:6;)', BinaryAccessor::MAX_UINT64)
packet = @tlm_packets['IMAGE']
packet.enable_method_missing
packet.CcsdsSeqFlags = 'NOGROUP'
packet.CcsdsLength = packet.buffer.length - 7
packet = @tlm_packets['MECH']
packet.enable_method_missing
packet.CcsdsSeqFlags = 'NOGROUP'
packet.CcsdsLength = packet.buffer.length - 7
packet = @tlm_packets['HIDDEN']
packet.enable_method_missing
packet.CcsdsSeqFlags = 'NOGROUP'
packet.CcsdsLength = packet.buffer.length - 7
@solar_panel_positions = SOLAR_PANEL_DFLTS.dup
@solar_panel_thread = nil
@solar_panel_thread_cancel = false
@track_stars = []
@track_stars[0] = 1237
@track_stars[1] = 1329
@track_stars[2] = 1333
@track_stars[3] = 1139
@track_stars[4] = 1161
@track_stars[5] = 682
@track_stars[6] = 717
@track_stars[7] = 814
@track_stars[8] = 583
@track_stars[9] = 622
@bad_temp2 = false
@last_temp2 = 0
@quiet = false
@time_offset = 0
@ip_address = 0
end
def set_rates
set_rate('ADCS', 10)
set_rate('HEALTH_STATUS', 100)
set_rate('PARAMS', 100)
set_rate('IMAGE', 100)
set_rate('MECH', 10)
set_rate('HIDDEN', 500)
set_rate('JSONTLM', 500)
set_rate('CBORTLM', 500)
set_rate('XMLTLM', 500)
set_rate('HTMLTLM', 500)
set_rate('HYBRIDTLM', 500)
end
def tick_period_seconds
return 0.1 # Override this method to optimize
end
def tick_increment
return 10 # Override this method to optimize
end
def write(packet)
name = packet.packet_name.upcase
hs_packet = @tlm_packets['HEALTH_STATUS']
params_packet = @tlm_packets['PARAMS']
json_packet = @tlm_packets['JSONTLM']
cbor_packet = @tlm_packets['CBORTLM']
xml_packet = @tlm_packets['XMLTLM']
html_packet = @tlm_packets['HTMLTLM']
hybrid_packet = @tlm_packets['HYBRIDTLM']
case name
when 'COLLECT'
hs_packet.cmd_acpt_cnt += 1
hs_packet.collects += 1
hs_packet.duration = packet.read('duration')
hs_packet.collect_type = packet.read("type")
when 'ABORT', 'FLTCMD', 'ARYCMD'
hs_packet.cmd_acpt_cnt += 1
when 'CLEAR'
hs_packet.cmd_acpt_cnt = 0
hs_packet.collects = 0
when 'SETPARAMS'
hs_packet.cmd_acpt_cnt += 1
params_packet.value1 = packet.read('value1')
params_packet.value2 = packet.read('value2')
params_packet.value3 = packet.read('value3')
params_packet.value4 = packet.read('value4')
params_packet.value5 = packet.read('value5')
params_packet.write('P4!@#$%^&*?', packet.read('bigint'))
when 'ASCIICMD'
hs_packet.cmd_acpt_cnt += 1
hs_packet.asciicmd = packet.read('string')
when 'SLRPNLDEPLOY'
hs_packet.cmd_acpt_cnt += 1
return if @solar_panel_thread and @solar_panel_thread.alive?
@solar_panel_thread = Thread.new do
@solar_panel_thread_cancel = false
(0..@solar_panel_positions.size - 1).to_a.reverse_each do |i|
while (@solar_panel_positions[i] > 0.1) or (@solar_panel_positions[i] < - 0.1)
if @solar_panel_positions[i] > 3.0
@solar_panel_positions[i] -= 3.0
elsif @solar_panel_positions[i] < -3.0
@solar_panel_positions[i] += 3.0
else
@solar_panel_positions[i] = 0.0
end
sleep(0.10)
break if @solar_panel_thread_cancel
end
if @solar_panel_thread_cancel
@solar_panel_thread_cancel = false
break
end
end
end
when 'SLRPNLRESET'
hs_packet.cmd_acpt_cnt += 1
OpenC3.kill_thread(self, @solar_panel_thread)
@solar_panel_positions = SOLAR_PANEL_DFLTS.dup
when 'MEMLOAD'
hs_packet.cmd_acpt_cnt += 1
hs_packet.blocktest = packet.read('data')
when 'QUIET'
hs_packet.cmd_acpt_cnt += 1
if packet.read('state') == 'TRUE'
@quiet = true
else
@quiet = false
end
when 'TIME_OFFSET'
hs_packet.cmd_acpt_cnt += 1
@time_offset = packet.read('seconds')
@ip_address = packet.read('ip_address')
when 'HIDDEN'
# Deliberately do not increment cmd_acpt_cnt
@tlm_packets['HIDDEN'].count = packet.read('count')
when 'JSONCMD'
json_packet.buffer = packet.buffer
when 'CBORCMD'
cbor_packet.buffer = packet.buffer
when 'XMLCMD'
xml_packet.buffer = packet.buffer
when 'HTMLCMD'
html_packet.buffer = packet.buffer
when 'HYBRIDCMD'
hybrid_packet.buffer = packet.buffer
end
end
def graceful_kill
@solar_panel_thread_cancel = true
end
def read(count_100hz, time)
pending_packets = get_pending_packets(count_100hz)
pending_packets.each do |packet|
case packet.packet_name
when 'ADCS'
# Read 44 Bytes for Position Data
pos_data = nil
begin
pos_data = @position_file.read(44)
@position_file_bytes_read += 44
rescue
# Do Nothing
end
if pos_data.nil? or pos_data.length == 0
# Assume end of file - close and reopen
@position_file.rewind
pos_data = @position_file.read(44)
@position_file_bytes_read = 44
end
@pos_packet.buffer = pos_data
packet.posx = @pos_packet.posx
packet.posy = @pos_packet.posy
packet.posz = @pos_packet.posz
packet.velx = @pos_packet.velx
packet.vely = @pos_packet.vely
packet.velz = @pos_packet.velz
# Read 40 Bytes for Attitude Data
att_data = nil
begin
att_data = @attitude_file.read(40)
@attitude_file_bytes_read += 40
rescue
# Do Nothing
end
if att_data.nil? or att_data.length == 0
@attitude_file.rewind
att_data = @attitude_file.read(40)
@attitude_file_bytes_read = 40
end
@att_packet.buffer = att_data
packet.q1 = @att_packet.q1
packet.q2 = @att_packet.q2
packet.q3 = @att_packet.q3
packet.q4 = @att_packet.q4
packet.biasx = @att_packet.biasx
packet.biasy = @att_packet.biasy
packet.biasy = @att_packet.biasz
packet.star1id = @track_stars[((count_100hz / 100) + 0) % 10]
packet.star2id = @track_stars[((count_100hz / 100) + 1) % 10]
packet.star3id = @track_stars[((count_100hz / 100) + 2) % 10]
packet.star4id = @track_stars[((count_100hz / 100) + 3) % 10]
packet.star5id = @track_stars[((count_100hz / 100) + 4) % 10]
packet.posprogress = (@position_file_bytes_read.to_f / @position_file_size.to_f) * 100.0
packet.attprogress = (@attitude_file_bytes_read.to_f / @attitude_file_size.to_f) * 100.0
packet.timesec = time.tv_sec - @time_offset
packet.timeus = time.tv_usec
packet.ccsdsseqcnt += 1
when 'HEALTH_STATUS'
if @quiet
@bad_temp2 = false
cycle_tlm_item(packet, 'temp1', -15.0, 15.0, 5.0)
cycle_tlm_item(packet, 'temp2', -50.0, 25.0, -1.0)
cycle_tlm_item(packet, 'temp3', 0.0, 50.0, 2.0)
else
cycle_tlm_item(packet, 'temp1', -95.0, 95.0, 5.0)
if @bad_temp2
packet.write('temp2', @last_temp2)
@bad_temp2 = false
end
@last_temp2 = cycle_tlm_item(packet, 'temp2', -50.0, 50.0, -1.0)
if (packet.temp2.abs - 30).abs < 2
packet.write('temp2', Float::NAN)
@bad_temp2 = true
elsif (packet.temp2.abs - 20).abs < 2
packet.write('temp2', -Float::INFINITY)
@bad_temp2 = true
elsif (packet.temp2.abs - 10).abs < 2
packet.write('temp2', Float::INFINITY)
@bad_temp2 = true
end
cycle_tlm_item(packet, 'temp3', -30.0, 80.0, 2.0)
end
cycle_tlm_item(packet, 'temp4', 0.0, 20.0, -0.1)
cycle_tlm_item(packet, 'bracket[0]', 0, 255, 10)
packet.timesec = time.tv_sec - @time_offset
packet.timeus = time.tv_usec
packet.ccsdsseqcnt += 1
ary = []
10.times do |index|
ary << index
end
packet.ary = ary
if @quiet
packet.ground1status = 'CONNECTED'
packet.ground2status = 'CONNECTED'
else
if count_100hz % 1000 == 0
if packet.ground1status == 'CONNECTED'
packet.ground1status = 'UNAVAILABLE'
else
packet.ground1status = 'CONNECTED'
end
end
if count_100hz % 500 == 0
if packet.ground2status == 'CONNECTED'
packet.ground2status = 'UNAVAILABLE'
else
packet.ground2status = 'CONNECTED'
end
end
end
when 'PARAMS'
packet.timesec = time.tv_sec - @time_offset
packet.timeus = time.tv_usec
packet.ccsdsseqcnt += 1
packet.ip_address = @ip_address
when 'IMAGE'
packet.timesec = time.tv_sec - @time_offset
packet.timeus = time.tv_usec
packet.ccsdsseqcnt += 1
if packet.ccsdsseqcnt % 20 == 0
@cur_image += 1
@cur_image = 0 if @cur_image == @images.length
end
packet.image = @images[@cur_image]
# Create an Array and then initialize
# using a sample of all possible hex values (0..15)
# finally pack it into binary using the Character 'C' specifier
packet.block = Array.new(1000) { Array(0..15).sample }.pack("C*")
when 'MECH'
packet.timesec = time.tv_sec - @time_offset
packet.timeus = time.tv_usec
packet.ccsdsseqcnt += 1
packet.slrpnl1 = @solar_panel_positions[0]
packet.slrpnl2 = @solar_panel_positions[1]
packet.slrpnl3 = @solar_panel_positions[2]
packet.slrpnl4 = @solar_panel_positions[3]
packet.slrpnl5 = @solar_panel_positions[4]
packet.current = 0.5
when 'HIDDEN'
packet.timesec = time.tv_sec - @time_offset
packet.timeus = time.tv_usec
packet.ccsdsseqcnt += 1
end
end
# Every 10s throw an unknown packet at the server just to demo that
data = Array.new(10) { rand(0..255) }.pack("C*")
if count_100hz % 1000 == 900
pending_packets << Packet.new(nil, nil, :BIG_ENDIAN, nil, data)
end
pending_packets
end
end
end