Class: Px4LogReader::MessageDescriptor

Inherits:
Object
  • Object
show all
Defined in:
lib/px4_log_reader/message_descriptor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ MessageDescriptor

Returns a new instance of MessageDescriptor.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/px4_log_reader/message_descriptor.rb', line 44

def initialize(attrs={})
  if attrs.keys.uniq.sort == [:name,:type,:length,:format,:fields].uniq.sort
    @name = attrs[:name]
    @type = attrs[:type]
    @length = attrs[:length]
    @format = attrs[:format]
    @format_specifier = MessageDescriptor.build_format_specifier( @format )
    fields = attrs[:fields]

    @field_list = MessageDescriptor.build_field_list( fields )
  elsif attrs.size > 0
    raise "Missing attributes"
  else
    @name = nil
    @type = nil
    @length = nil
    @format = nil

    @field_list = {}
  end
end

Instance Attribute Details

#field_listObject (readonly)

Returns the value of attribute field_list.



41
42
43
# File 'lib/px4_log_reader/message_descriptor.rb', line 41

def field_list
  @field_list
end

#formatObject (readonly)

Returns the value of attribute format.



40
41
42
# File 'lib/px4_log_reader/message_descriptor.rb', line 40

def format
  @format
end

#format_specifierObject (readonly)

Returns the value of attribute format_specifier.



42
43
44
# File 'lib/px4_log_reader/message_descriptor.rb', line 42

def format_specifier
  @format_specifier
end

#lengthObject (readonly)

Returns the value of attribute length.



39
40
41
# File 'lib/px4_log_reader/message_descriptor.rb', line 39

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/px4_log_reader/message_descriptor.rb', line 37

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



38
39
40
# File 'lib/px4_log_reader/message_descriptor.rb', line 38

def type
  @type
end

Class Method Details

.build_field_list(fields) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/px4_log_reader/message_descriptor.rb', line 105

def build_field_list( fields )
  field_list = {}
  fields.each_with_index do |field,index|
    field_list[field] = index
  end
  field_list
end

.build_format_specifier(px4_format_string) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/px4_log_reader/message_descriptor.rb', line 113

def build_format_specifier( px4_format_string )
  format_specifier = ''

  px4_format_string.unpack('C*').each do |field_format|
    case field_format
    when 0x66 # 'f'
      format_specifier << 'F'
    when 0x71, 0x51 # 'q', 'Q'
      format_specifier << 'l!'
    when 0x69, 0x4C, 0x65 # 'i', 'L', 'e'
      format_specifier << 'l'
    when 0x49, 0x45 # 'I', 'E'
      format_specifier << 'L'
    when 0x62 # 'b'
      format_specifier << 'c'
    when 0x42, 0x4D # 'B', 'M'
      format_specifier << 'C'
    when 0x68, 0x63 # 'h', 'c'
      format_specifier << 's'
    when 0x48, 0x43 # 'H', 'C'
      format_specifier << 'S'
    when 0x6E # 'n'
      format_specifier << 'A4'
    when 0x4E # 'N'
      format_specifier << 'A16'
    when 0x5A # 'Z'
      format_specifier << 'A64'
    else
      raise InvalidDescriptorError.new(
        "Invalid format specifier: '#{ '0x%02X' % field_format }' in #{px4_format_string}")
    end
  end

  return format_specifier
end

Instance Method Details

#from_message(message) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/px4_log_reader/message_descriptor.rb', line 66

def from_message( message )

      if message.descriptor.type != 0x80

raise InvalidDescriptorError.new(
  'Invalid descriptor type for format specifier message' )

      elsif message.fields.count != 5

raise InvalidDescriptorError.new(
  "Invalid field count for format specifier message: expected 5 fields, found #{message.fields.count}" )

      end

      @type, @length, @name, @format, fields_string = message.fields

      @format_specifier = MessageDescriptor.build_format_specifier( @format )

      unless fields_string.empty?

fields = fields_string.split(',')

if fields.length != @format.length
  raise InvalidDescriptorError.new(
    "Field count must match format length: expected #{@format.length}; found #{fields.length}")
else
  @field_list = MessageDescriptor.build_field_list( fields )
end

      end
      
end

#pack_message(fields) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/px4_log_reader/message_descriptor.rb', line 175

def pack_message( fields )

  if fields.count != @format.length
    raise "Descriptor format length must match message field count"
  end

  corrected_fields = fields.dup
  @format.unpack('C*').each_with_index do |field_format,index|
    case field_format
    when 0x4C # 'L'
      corrected_fields[index] /= 1.0E-7
    when 0x63, 0x43, 0x45 # 'c', 'C', 'E'
      corrected_fields[index] /= 1.0E-2
    else
    end
  end

  return corrected_fields.pack( @format_specifier )
end

#to_messageObject



99
100
101
# File 'lib/px4_log_reader/message_descriptor.rb', line 99

def to_message
  return LogMessage.new( FORMAT_MESSAGE, [ @type, @length, @name, @format, @fields_string ] )
end

#to_sObject



195
196
197
198
199
200
201
202
# File 'lib/px4_log_reader/message_descriptor.rb', line 195

def to_s
  puts "#{@name}( #{@type} ):"
  puts "   length = #{@length}"
  puts "   format = #{@format}"
  @field_list.each do |name,index|
    puts "   field#{index} = #{name}"
  end
end

#unpack_message(message_data) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/px4_log_reader/message_descriptor.rb', line 150

def unpack_message( message_data )

  if @format_specifier.nil?
    @format_specifier = MessageDescriptor.build_format_specifier( @format )
  end

  fields = message_data.unpack( @format_specifier )

  @format.unpack('C*').each_with_index do |field_format,index|
    case field_format
    when 0x4C # 'L'
      fields[index] = fields[index] * 1.0E-7
    when 0x63, 0x43, 0x45 # 'c', 'C', 'E'
      fields[index] = fields[index] * 1.0E-2
    else
    end
  end

  if fields.nil? || fields.empty?
    raise "No fields"
  end

  return LogMessage.new( self, fields )
end