> In the mavgen.py code it reads the message fields from the XML, and
> then reorders them according to the base data type size.
right
> My question is: why is this? It doesn't seem to add anything except
> complexity.
it ensures that all data types are natually aligned, which allows the
generated code to be maximally efficient and compact on all CPU types
(many CPUs cannot access values that are not aligned on a byte offset
that is a multiple of the size of the element).
The end result is much more compact code for some microcontrollers. It
is often faster too. It also prevents compilers adding pad bytes in
unexpected places. Not all compilers support directives to disable type
alignment.
> In fact I consider it slightly dangerous, since the
> mapping from the XML field order to the actual field order is now
> pretty complicated and arbitrary.
it is not arbitrary, it is a stable sort on the field size. The code
generator handles all the complexity of ensuring the correct decoding is
used.
Cheers, Tridge
Ah ok, this makes sense too!
One final question, why are there two different enums for data types?
(e.g. both MAVLINK_TYPE_FLOAT = 9, and MAV_VAR_FLOAT = 0). Also, for
the SET_PARAM message, we have:
uint8_t param_type; ///< Onboard parameter type: see MAV_VAR enum
but QGroundControl appears to use the MAVLINK_TYPE values:
qgroundcontrol/src/uas/UAS.cc: p.param_type = MAVLINK_TYPE_FLOAT;
Cheers,
Tim