Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Simulated Target for INST #24

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# Provides a demonstration of a Simulated Target

require 'openc3'
require 'stringio'

module OpenC3
# Simulated instrument for the demo. Populates several packets and cycles
Expand All @@ -33,10 +34,12 @@ def initialize(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_file = File.open(position_filename, 'rb')
@attitude_file = File.open(attitude_filename, 'rb')
@position_file_size = File.size(position_filename)
@attitude_file_size = File.size(attitude_filename)
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

Expand Down Expand Up @@ -135,6 +138,14 @@ def set_rates
set_rate('MECH', 10)
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

Expand Down Expand Up @@ -207,8 +218,7 @@ def read(count_100hz, time)

if pos_data.nil? or pos_data.length == 0
# Assume end of file - close and reopen
@position_file.close
@position_file = File.open(File.join(@target.dir, 'data', 'position.bin'), 'rb')
@position_file.rewind
pos_data = @position_file.read(44)
@position_file_bytes_read = 44
end
Expand All @@ -231,8 +241,7 @@ def read(count_100hz, time)
end

if att_data.nil? or att_data.length == 0
@attitude_file.close
@attitude_file = File.open(File.join(@target.dir, 'data', 'attitude.bin'), 'rb')
@attitude_file.rewind
att_data = @attitude_file.read(40)
@attitude_file_bytes_read = 40
end
Expand Down
16 changes: 10 additions & 6 deletions openc3/lib/openc3/interfaces/simulated_target_interface.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ def initialize(sim_target_file)
# Initialize the simulated target object and "connect" to the target
def connect
unless @initialized
# Save the current time + 10 ms as the next expected tick time
@next_tick_time = Time.now.sys + 0.01
# Create Simulated Target Object
@sim_target = @sim_target_class.new(@target_names[0])
# Set telemetry rates
@sim_target.set_rates

@initialized = true
end

@count_100hz = 0

# Save the current time + delta as the next expected tick time
@next_tick_time = Time.now.sys + @sim_target.tick_period_seconds

@connected = true
end

Expand Down Expand Up @@ -78,20 +82,20 @@ def read
end

while true
# Calculate time to sleep to make ticks 10ms apart
# Calculate time to sleep to make ticks the right distance apart
now = Time.now.sys
delta = @next_tick_time - now
if delta > 0.0
sleep(delta) # Sleep up to 10 ms
sleep(delta) # Sleep between packets
return nil unless @connected
elsif delta < -1.0
# Fell way behind - jump next tick time
@next_tick_time = Time.now.sys
end

@pending_packets = @sim_target.read(@count_100hz, @next_tick_time)
@next_tick_time += 0.01
@count_100hz += 1
@next_tick_time += @sim_target.tick_period_seconds
@count_100hz += @sim_target.tick_increment

packet = first_pending_packet()
if packet
Expand Down
8 changes: 8 additions & 0 deletions openc3/lib/openc3/utilities/simulated_target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ def read(count_100hz, time)
raise "Error: read must be implemented by subclass"
end

def tick_period_seconds
return 0.01 # Override this method to optimize
end

def tick_increment
return 1 # Override this method to optimize
end

protected

def set_rate(packet_name, rate)
Expand Down