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.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/px4_log_reader/message_descriptor.rb', line 13

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.



10
11
12
# File 'lib/px4_log_reader/message_descriptor.rb', line 10

def field_list
  @field_list
end

#formatObject (readonly)

Returns the value of attribute format.



9
10
11
# File 'lib/px4_log_reader/message_descriptor.rb', line 9

def format
  @format
end

#format_specifierObject (readonly)

Returns the value of attribute format_specifier.



11
12
13
# File 'lib/px4_log_reader/message_descriptor.rb', line 11

def format_specifier
  @format_specifier
end

#lengthObject (readonly)

Returns the value of attribute length.



8
9
10
# File 'lib/px4_log_reader/message_descriptor.rb', line 8

def length
  @length
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/px4_log_reader/message_descriptor.rb', line 6

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/px4_log_reader/message_descriptor.rb', line 7

def type
  @type
end

Class Method Details

.build_field_list(fields) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/px4_log_reader/message_descriptor.rb', line 74

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/px4_log_reader/message_descriptor.rb', line 82

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/px4_log_reader/message_descriptor.rb', line 35

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



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/px4_log_reader/message_descriptor.rb', line 144

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



68
69
70
# File 'lib/px4_log_reader/message_descriptor.rb', line 68

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

#to_sObject



164
165
166
167
168
169
170
171
# File 'lib/px4_log_reader/message_descriptor.rb', line 164

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/px4_log_reader/message_descriptor.rb', line 119

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