Current message template file does not correctly handle float constant expresssions. #20444
+11
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changing msg.h.em template to properly handle const references of type float. Without this float values will be interpreted as int's and loose decimal resolution.
Describe problem solved by this pull request
The current message template interprets all constant reference variables as int's regardless of what type is actually defined in the .msg file.
Example .msg file showing how a float might be used.
Ex: custom_actuator_test.msg
float32 CUSTOM_ACTUATOR_MIN = 3.141592653589793238
With the original template file 'msg.h.em' the value of CUSTOM_ACTUATOR_MIN would become 3 instead of the intended value of 3.141592653589793238
Describe your solution
To properly handle floats the 'msg.h.em' file was augmented.
To handle the creation of accurate
#define
values the following changes were made at line 85.@{ for constant in spec.constants: if "float" in constant.type: print('#define %s_%s %s'%(spec.short_name.upper(), constant.name, float(constant.val))) else: print('#define %s_%s %s'%(spec.short_name.upper(), constant.name, int(constant.val))) }@
Finally to handle the accurate creation of
static constexp
variables the following changes were made at line 125.if "float" in type_name: print('\tstatic constexpr %s %s = %s;'%(type_px4, constant.name, float(constant.val))) else: print('\tstatic constexpr %s %s = %s;'%(type_px4, constant.name, int(constant.val)))
Describe possible alternatives
I do not see any other alternative if someone wants to have a 'const expression' of type float32/64 inside there uORB message, and they don't want to loose decimal accuracy.
Test data / coverage
Tested on the bench with a custom message and a custom module that prints the const expression value of the message endlessly.
Additional context
NA