Class: OpenC3::SegmentedPolynomialConversion
- Inherits:
-
Conversion
- Object
- Conversion
- OpenC3::SegmentedPolynomialConversion
- Defined in:
- lib/openc3/conversions/segmented_polynomial_conversion.rb
Overview
Segmented polynomial conversions consist of polynomial conversions that are applied for a range of values.
Defined Under Namespace
Classes: Segment
Instance Attribute Summary collapse
-
#segments ⇒ Array<Segment>
readonly
Segments which make up this conversion.
Attributes inherited from Conversion
#converted_array_size, #converted_bit_size, #converted_type
Instance Method Summary collapse
-
#add_segment(lower_bound, *coeffs) ⇒ Object
Add a segment to the segmented polynomial.
- #as_json(*a) ⇒ Object
-
#call(value, packet, buffer) ⇒ Float
The value with the polynomial applied.
-
#initialize(segments = []) ⇒ SegmentedPolynomialConversion
constructor
Initialize the converted_type to :FLOAT and converted_bit_size to 64.
-
#to_config(read_or_write) ⇒ String
Config fragment for this conversion.
-
#to_s ⇒ String
The name of the class followed by a description of all the polynomial segments.
Constructor Details
#initialize(segments = []) ⇒ SegmentedPolynomialConversion
Initialize the converted_type to :FLOAT and converted_bit_size to 64.
94 95 96 97 98 99 100 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 94 def initialize(segments = []) super() @segments = [] segments.each { |lower_bound, coeffs| add_segment(lower_bound, *coeffs) } @converted_type = :FLOAT @converted_bit_size = 64 end |
Instance Attribute Details
Instance Method Details
#add_segment(lower_bound, *coeffs) ⇒ Object
Add a segment to the segmented polynomial. The lower bound is inclusive, but is ignored for the segment with the lowest lower_bound.
109 110 111 112 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 109 def add_segment(lower_bound, *coeffs) @segments << Segment.new(lower_bound, coeffs) @segments.sort! end |
#as_json(*a) ⇒ Object
164 165 166 167 168 169 170 171 172 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 164 def as_json(*a) params = [] @segments.each do |segment| params << [segment.lower_bound, segment.coeffs] end result = super(*a) result['params'] = [params] result end |
#call(value, packet, buffer) ⇒ Float
Returns The value with the polynomial applied.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 116 def call(value, packet, buffer) # Try to find correct segment @segments.each do |segment| return segment.calculate(value) if value >= segment.lower_bound end # Default to using segment with smallest lower_bound segment = @segments[-1] if segment return @segments[-1].calculate(value) else return nil end end |
#to_config(read_or_write) ⇒ String
Returns Config fragment for this conversion.
155 156 157 158 159 160 161 162 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 155 def to_config(read_or_write) config = '' @segments.each do |segment| config << " SEG_POLY_#{read_or_write}_CONVERSION #{segment.lower_bound} #{segment.coeffs.join(' ')}\n" end config end |
#to_s ⇒ String
Returns The name of the class followed by a description of all the polynomial segments.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 133 def to_s result = '' count = 0 @segments.each do |segment| result << "\n" if count > 0 result << "Lower Bound: #{segment.lower_bound} Polynomial: " segment.coeffs.length.times do |index| if index == 0 result << "#{segment.coeffs[index]}" elsif index == 1 result << " + #{segment.coeffs[index]}x" else result << " + #{segment.coeffs[index]}x^#{index}" end end count += 1 end result end |