Class: OpenC3::SegmentedPolynomialConversion

Inherits:
Conversion show all
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

Attributes inherited from Conversion

#converted_array_size, #converted_bit_size, #converted_type

Instance Method Summary collapse

Constructor Details

#initialize(segments = []) ⇒ SegmentedPolynomialConversion

Initialize the converted_type to :FLOAT and converted_bit_size to 64.

Parameters:

  • segments (Array) (defaults to: [])

    Array of segments typically generated by as_json Format similar to the following: [[15, [3, 2]], [10, [1, 2]]] Where each entry is an array with the first value as the lower_bound and the other entry is an array of the coefficients for that segment.



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

#segmentsArray<Segment> (readonly)

Returns Segments which make up this conversion.

Returns:

  • (Array<Segment>)

    Segments which make up this conversion



30
31
32
# File 'lib/openc3/conversions/segmented_polynomial_conversion.rb', line 30

def segments
  @segments
end

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.

Parameters:

  • lower_bound (Integer)

    The value at which point this polynomial conversion should apply. All values >= to this value will be converted using the given coefficients.

  • coeffs (Array<Integer>)

    The polynomial coefficients



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.

Parameters:

  • value (Object)

    The value to convert

  • packet (Packet)

    The packet which contains the value. This can be useful to reach into the packet and use other values in the conversion.

  • buffer (String)

    The packet buffer

Returns:

  • (Float)

    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.

Parameters:

  • read_or_write (String)

    Either 'READ' or 'WRITE'

Returns:

  • (String)

    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_sString

Returns The name of the class followed by a description of all the polynomial segments.

Returns:

  • (String)

    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