Class: OpenC3::PolynomialConversion

Inherits:
Conversion
  • Object
show all
Defined in:
lib/openc3/conversions/polynomial_conversion.rb,
ext/openc3/ext/polynomial_conversion/polynomial_conversion.c

Overview

Performs a polynomial conversion on the value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*coeffs) ⇒ PolynomialConversion

Initializes the conversion with the given polynomial coefficients. Sets the converted_type to :FLOAT and the converted_bit_size to 64.

Parameters:

  • coeffs (Array<Float>)

    The polynomial coefficients



36
37
38
39
40
41
# File 'lib/openc3/conversions/polynomial_conversion.rb', line 36

def initialize(*coeffs)
  super()
  @coeffs = coeffs.map { |coeff| coeff.to_f }
  @converted_type = :FLOAT
  @converted_bit_size = 64
end

Instance Attribute Details

#coeffsArray<Float>

Returns The polynomial coefficients.

Returns:



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

def coeffs
  @coeffs
end

Instance Method Details

#as_json(*a) ⇒ Object



84
85
86
87
88
# File 'lib/openc3/conversions/polynomial_conversion.rb', line 84

def as_json(*a)
  result = super(*a)
  result['params'] = @coeffs
  result
end

#call(value, myself, buffer) ⇒ Object

Calling this method performs a polynomial conversion on the given value.

conversion.call(1, packet) #=> 2.5


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/openc3/conversions/polynomial_conversion.rb', line 46

def call(value, myself, buffer)
  value = value.to_f

  # Handle C0
  result = @coeffs[0]

  # Handle Coefficients raised to a power
  raised_to_power = 1.0
  @coeffs[1..-1].each do |coeff|
    raised_to_power *= value
    result += (coeff * raised_to_power)
  end

  return result
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



80
81
82
# File 'lib/openc3/conversions/polynomial_conversion.rb', line 80

def to_config(read_or_write)
  "    POLY_#{read_or_write}_CONVERSION #{@coeffs.join(' ')}\n"
end

#to_sString

Returns Class followed by the list of coefficients.

Returns:

  • (String)

    Class followed by the list of coefficients



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/openc3/conversions/polynomial_conversion.rb', line 64

def to_s
  result = ""
  @coeffs.length.times do |index|
    if index == 0
      result << "#{@coeffs[index]}"
    elsif index == 1
      result << " + #{@coeffs[index]}x"
    else
      result << " + #{@coeffs[index]}x^#{index}"
    end
  end
  result
end