Class: Spektrum::Log::Flight

Inherits:
Object
  • Object
show all
Defined in:
lib/spektrum/log/flight.rb

Overview

Represents a single recorded flight. Contains information about the model flown, duration of the flight, and all data records contained within.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, records) ⇒ Flight

Creates a new flight.

Parameters:

  • headers (Array<Header>)

    headers read from the file

  • records (Array<Record>)

    records read from the file



16
17
18
19
# File 'lib/spektrum/log/flight.rb', line 16

def initialize(headers, records)
  @headers = headers
  @records = records
end

Instance Attribute Details

#headersObject (readonly)

Returns the value of attribute headers.



10
11
12
# File 'lib/spektrum/log/flight.rb', line 10

def headers
  @headers
end

#recordsObject (readonly)

Returns the value of attribute records.



10
11
12
# File 'lib/spektrum/log/flight.rb', line 10

def records
  @records
end

Instance Method Details

#altimeter_recordsObject



97
98
99
# File 'lib/spektrum/log/flight.rb', line 97

def altimeter_records
  select_records AltimeterRecord
end

#altimeter_records?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/spektrum/log/flight.rb', line 93

def altimeter_records?
  any_records? AltimeterRecord
end

#basic_data_recordsObject



105
106
107
# File 'lib/spektrum/log/flight.rb', line 105

def basic_data_records
  select_records BasicDataRecord
end

#basic_data_records?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/spektrum/log/flight.rb', line 101

def basic_data_records?
  any_records? BasicDataRecord
end

#bind_typeString

Gets the binding type the flight was flown with.

Returns:

  • (String)

    binding type of the flight, ‘DSM2`, `DSMX`, etc.



39
40
41
42
43
44
45
46
47
48
# File 'lib/spektrum/log/flight.rb', line 39

def bind_type
  @bind_type ||= case @headers.first.raw_data[2].unpack('C')[0]
                 when 0x01..0x02
                   'DSM2'
                 when 0x03..0x04
                   'DSMX'
                 else
                   'Unknown'
                 end
end

#durationFloat

Gets the duration of the flight, in seconds.

Returns:

  • (Float)

    duration of the flight, in seconds



24
25
26
# File 'lib/spektrum/log/flight.rb', line 24

def duration
  @duration ||= timestamp_delta / 256.0
end

#empty?Boolean

Determines if this flight has any data. Models without telemetry transmitted, but with logging enabled will create empty flights.

Returns:

  • (Boolean)

    true if the flight has no records, false otherwise



32
33
34
# File 'lib/spektrum/log/flight.rb', line 32

def empty?
  @records.empty?
end

#flight_log_recordsObject



113
114
115
# File 'lib/spektrum/log/flight.rb', line 113

def flight_log_records
  select_records FlightLogRecord
end

#flight_log_records?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/spektrum/log/flight.rb', line 109

def flight_log_records?
  any_records? FlightLogRecord
end

#g_force_recordsObject



121
122
123
# File 'lib/spektrum/log/flight.rb', line 121

def g_force_records
  select_records GForceRecord
end

#g_force_records?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/spektrum/log/flight.rb', line 117

def g_force_records?
  any_records? GForceRecord
end

#gps1_recordsObject



129
130
131
# File 'lib/spektrum/log/flight.rb', line 129

def gps1_records
  select_records GPSRecord1
end

#gps1_records?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/spektrum/log/flight.rb', line 125

def gps1_records?
  any_records? GPSRecord1
end

#gps2_recordsObject



137
138
139
# File 'lib/spektrum/log/flight.rb', line 137

def gps2_records
  select_records GPSRecord2
end

#gps2_records?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/spektrum/log/flight.rb', line 133

def gps2_records?
  any_records? GPSRecord2
end

#model_nameString

Gets the name of the model for this flight.

Returns:

  • (String)

    model name



53
54
55
# File 'lib/spektrum/log/flight.rb', line 53

def model_name
  @model_name ||= @headers.first.raw_data[8..27].unpack('Z*')[0].strip
end

#model_numberFixnum

Gets the model’s index from the transmitter.

Returns:

  • (Fixnum)

    model number



60
61
62
# File 'lib/spektrum/log/flight.rb', line 60

def model_number
  @model_number ||= (@headers.first.raw_data[0].unpack('C')[0] + 1)
end

#model_typeString

Gets the type of model flown.

Returns:

  • (String)

    model type



67
68
69
70
71
72
73
74
75
76
# File 'lib/spektrum/log/flight.rb', line 67

def model_type
  @model_type ||= case @headers.first.raw_data[1].unpack('C')[0]
                  when 0x00
                    'Fixed Wing'
                  when 0x01
                    'Helicopter'
                  else
                    'Unknown'
                  end
end

#speed_recordsObject



145
146
147
# File 'lib/spektrum/log/flight.rb', line 145

def speed_records
  select_records SpeedRecord
end

#speed_records?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/spektrum/log/flight.rb', line 141

def speed_records?
  any_records? SpeedRecord
end

#telemetry_unitString

Gets the type of telemetry unit that sent the data.

Returns:

  • (String)

    telemetry unit



81
82
83
# File 'lib/spektrum/log/flight.rb', line 81

def telemetry_unit
  @telemetry_unit ||= derive_telemetry_unit
end

#timestamp_deltaNumber

Gets the difference between the last and the first timestamps. May be zero if no records exist.

Returns:

  • (Number)

    difference between the last and first timestamp



89
90
91
# File 'lib/spektrum/log/flight.rb', line 89

def timestamp_delta
  @timestamp_delta ||= @records.empty? ? 0.0 : (@records.last.timestamp - @records.first.timestamp)
end

#to_kml(file_options = {}, placemark_options = {}) ⇒ String

Converts the flight into a KML document containing a placemark.

Parameters:

  • file_options (Hash) (defaults to: {})

    hash containing options for file

  • placemark_options (Hash) (defaults to: {})

    hash containing options for placemark

Returns:

  • (String)

    KML document for the flight

Raises:

  • (RuntimeError)

See Also:



163
164
165
166
# File 'lib/spektrum/log/flight.rb', line 163

def to_kml(file_options = {}, placemark_options = {})
  raise RuntimeError, 'No coordinates available for KML generation' unless to_kml?
  to_kml_file(file_options, placemark_options).render
end

#to_kml?Boolean

Determines if KML methods can be called for this flight.

Returns:

  • (Boolean)

    true if KML can be generated for this flight, false otherwise



152
153
154
# File 'lib/spektrum/log/flight.rb', line 152

def to_kml?
  gps1_records?
end

#to_kml_file(file_options = {}, placemark_options = {}) ⇒ KMLFile

Converts the flight into a KMLFile containing a placemark.

Parameters:

  • file_options (Hash) (defaults to: {})

    hash containing options for file

  • placemark_options (Hash) (defaults to: {})

    hash containing options for placemark

Options Hash (file_options):

  • :name (String)

    name option of KML::Document

  • :description (String)

    name option of KML::Document

  • :style_id (String)

    id option of KML::Style

Returns:

  • (KMLFile)

    file for the flight

Raises:

  • (RuntimeError)

See Also:



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/spektrum/log/flight.rb', line 177

def to_kml_file(file_options = {}, placemark_options = {})
  raise RuntimeError, 'No coordinates available for KML generation' unless to_kml?
  options = apply_default_file_options(file_options)

  kml = KMLFile.new
  kml.objects << KML::Document.new(
    :name => options[:name],
    :description => options[:description],
    :styles => [
      KML::Style.new(
        :id => options[:style_id],
        :line_style => KML::LineStyle.new(:color => '7F00FFFF', :width => 4),
        :poly_style => KML::PolyStyle.new(:color => '7F00FF00')
      )
    ],
    :features => [ to_kml_placemark(placemark_options) ]
  )
  kml
end

#to_kml_placemark(options = {}) ⇒ KML::Placemark

Converts the flight into a KML::Placemark containing GPS coordinates.

Parameters:

  • options (Hash) (defaults to: {})

    hash containing options for placemark

Options Hash (options):

  • :altitude_mode (String)

    altitude_mode option of KML::LineString

  • :extrude (Boolean)

    extrude option of KML::LineString

  • :name (String)

    name option of KML::Placemark

  • :style_url (String)

    style_url option of KML::Placemark

  • :tessellate (Boolean)

    tessellate option of KML::LineString

Returns:

  • (KML::Placemark)

    placemark for the flight

Raises:

  • (RuntimeError)


206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/spektrum/log/flight.rb', line 206

def to_kml_placemark(options = {})
  raise RuntimeError, 'No coordinates available for KML generation' unless to_kml?
  options = apply_default_placemark_options(options)

  KML::Placemark.new(
    :name => options[:name],
    :style_url => options[:style_url],
    :geometry => KML::LineString.new(
      :altitude_mode => options[:altitude_mode],
      :extrude => options[:extrude],
      :tessellate => options[:tessellate],
      :coordinates => gps1_records.map(&:coordinate).map { |c| c.join(',') }.join(' ')
    )
  )
end