Floating Point
-
I gather that floating point is still converted to/from a string representation in 1.4
A quick scan seems to indicate that the GCC software 4 byte floating point representation for the Arduino is IEEE compatible, as is RPi and Intel chip based software. Maybe I missed something, tho.
I'm not sure about endian-ness for all the platforms (in Floating Point format, which could differ from integers). But I'd think that could be adjusted for a MySensor OTA standard if need be.
So I'm probably missing something, but it would comfort my mind to understand this better... why can't MySensors transmit binary floating point?
Thanks
-
I gather that floating point is still converted to/from a string representation in 1.4
A quick scan seems to indicate that the GCC software 4 byte floating point representation for the Arduino is IEEE compatible, as is RPi and Intel chip based software. Maybe I missed something, tho.
I'm not sure about endian-ness for all the platforms (in Floating Point format, which could differ from integers). But I'd think that could be adjusted for a MySensor OTA standard if need be.
So I'm probably missing something, but it would comfort my mind to understand this better... why can't MySensors transmit binary floating point?
Thanks
@Zeph The issue here is called serialization -- how to store data in a defined way which can be exchanged between different hardware/platforms.
If data is just being exchanged between ATMega's then I don't see a problem in just storing the float value directly in the native format. When exchanging between different architectures we should take endianness & floating point format into account.
This http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#serialization describes some simple code to store floating point in IEEE-754 format, but I think it is still too much overhead for ATMega's...I think the focus should lie on fast (de)serialization for the Arduino platform, to assure conversion between sensor and gateway has little overhead (both computational & storage).
IMHO we should just store floating point values in the format of the primary MySensors platform (Arduino with ATMega328) and convert on other platforms when required. In general other platforms will have more processing power, so it makes sense to let them 'feel the pain' of conversion, and not have the default platform pay all the time for standardization.
-
I believe the pi is bi (ARM), but current default implementation is little-endian just like the Arduino (if i remember correct).
-
@Zeph The issue here is called serialization -- how to store data in a defined way which can be exchanged between different hardware/platforms.
If data is just being exchanged between ATMega's then I don't see a problem in just storing the float value directly in the native format. When exchanging between different architectures we should take endianness & floating point format into account.
This http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#serialization describes some simple code to store floating point in IEEE-754 format, but I think it is still too much overhead for ATMega's...I think the focus should lie on fast (de)serialization for the Arduino platform, to assure conversion between sensor and gateway has little overhead (both computational & storage).
IMHO we should just store floating point values in the format of the primary MySensors platform (Arduino with ATMega328) and convert on other platforms when required. In general other platforms will have more processing power, so it makes sense to let them 'feel the pain' of conversion, and not have the default platform pay all the time for standardization.
@Yveaux
That's pretty much what I was leading to as well.The primary platform currently is GCC-AVR software floating point from AVR to AVR over the air. Since this is the lowest powered node type and the most ubiquitous, it's floating point format would
seem natural for the OTA standard. We don't tend to need doubles in this niche.And what I was reading was that this is IEEE standard format, so the only question in binary adjustment to another IEEE floating point system should be possibly changing endianness.
Even if a Raspberrry Pi were used as the wireless hub directly, what is the issue? Does it store 4 byte floating point numbers differently than the Arduino compiler? If so can it not swap bytes as needed? (As you say, it has more processing and memory resources).
So I was curious why floats are not already sent in binary format over the air.
I've come up with one hypothesis. Even if we send floats OTA as binary, they may need to be converted to text strings for the API. I do see that there is a role for describing how many digits of precision make sense to avoid temperatures like 23.4999987 degrees. So keeping the precision of a variable as a hint could be useful. But I'm not yet seeing a good reason to accept the overhead of converting to and from a text string for OTA floats.
(Of course, I'm thinking that if you are tracking the type and name of a variable, and for floats the precision, then you could similarly track a scaling factor for variables transferred as integers. Temp could be reported as a float with 1 decimal place of accuracy, or as a two byte integer with a scaling factor of 0.1)
-
@Yveaux
That's pretty much what I was leading to as well.The primary platform currently is GCC-AVR software floating point from AVR to AVR over the air. Since this is the lowest powered node type and the most ubiquitous, it's floating point format would
seem natural for the OTA standard. We don't tend to need doubles in this niche.And what I was reading was that this is IEEE standard format, so the only question in binary adjustment to another IEEE floating point system should be possibly changing endianness.
Even if a Raspberrry Pi were used as the wireless hub directly, what is the issue? Does it store 4 byte floating point numbers differently than the Arduino compiler? If so can it not swap bytes as needed? (As you say, it has more processing and memory resources).
So I was curious why floats are not already sent in binary format over the air.
I've come up with one hypothesis. Even if we send floats OTA as binary, they may need to be converted to text strings for the API. I do see that there is a role for describing how many digits of precision make sense to avoid temperatures like 23.4999987 degrees. So keeping the precision of a variable as a hint could be useful. But I'm not yet seeing a good reason to accept the overhead of converting to and from a text string for OTA floats.
(Of course, I'm thinking that if you are tracking the type and name of a variable, and for floats the precision, then you could similarly track a scaling factor for variables transferred as integers. Temp could be reported as a float with 1 decimal place of accuracy, or as a two byte integer with a scaling factor of 0.1)
-
Yes, I had worries on how the RPi compiler treated IEEE754 with respect to negative numbers and byte order. If someone can swear on their mothers grave that RPi uses the same standard we could send them in a binary format over the air. But we still need a conversion over serial line protocol (which must be handled by gateway).
-
Yes, I had worries on how the RPi compiler treated IEEE754 with respect to negative numbers and byte order. If someone can swear on their mothers grave that RPi uses the same standard we could send them in a binary format over the air. But we still need a conversion over serial line protocol (which must be handled by gateway).
@hek said:
we still need a conversion over serial line protocol (which must be handled by gateway)
It's better to have the gateway do it, then the sensor nodes. Sensor nodes are low power and have limited resources (w.r.t. gateway and application hardware) and should not have to worry about floating point conversions.
-
The pi uses little-endian as default. And:
pi@pidome-server ~ $ readelf /usr/bin/gcc -A
Attribute Section: aeabi
File Attributes
Tag_CPU_name: "6"
Tag_CPU_arch: v6
Tag_ARM_ISA_use: Yes
Tag_THUMB_ISA_use: Thumb-1
Tag_FP_arch: VFPv2
Tag_ABI_PCS_wchar_t: 4
Tag_ABI_FP_denormal: Needed
Tag_ABI_FP_exceptions: Needed
Tag_ABI_FP_number_model: IEEE 754
Tag_ABI_align_needed: 8-byte
Tag_ABI_align_preserved: 8-byte, except leaf SP
Tag_ABI_enum_size: int
Tag_ABI_HardFP_use: SP and DP
Tag_ABI_VFP_args: VFP registers
Tag_DIV_use: Not allowed -
The Raspberry Pi definitely does use IEEE 754 floating point.
I'm not certain about the byte order, but that should be easy to test.
union test_t { float f; uint8_t b[4]; } myUnion; myUnion.f = 3.14159265; for(int i = 0; i < 4; i++) { Serial.print(myUnion.b[i], HEX); Serial.print(" "); } Serial.println();Swap out a printf on the Rpi
(Note: I am not assuming that an architecture which is little endian for integers must be little endian for floats - tho it usually is)
-
I would also like you to think about a fixed point format (http://en.wikipedia.org/wiki/Fixed-point_arithmetic).
A lot of sensor values can be represented in e.g. 8.8 or even 16.16. Think of e.g. temperatures (range -127..+127 is usually sufficient and I've yet to see an affordable sensor reporting in more than 256 steps per degree ), humidity (0..100%, 256 steps/degree), battery level (0..100%) etc.
Fixed point arithmetic requires very little resources compared to floating point and has no trouble converting between different architectures (apart from endianness).
For full efficiency sensor libraries should also support this format, as it doesn't make much sense to have a sensor library report in floating point and convert this to fixed point by MySensors.
A good Arduino-style fixed point library would help IMHO. Did a quick google on the subject, but didn't find much (apart from https://code.google.com/p/libfixmath/)I think support for real floating point values should still be possible, but this could be an interesting addition.
What do you think?
-
The Raspberry Pi definitely does use IEEE 754 floating point.
I'm not certain about the byte order, but that should be easy to test.
union test_t { float f; uint8_t b[4]; } myUnion; myUnion.f = 3.14159265; for(int i = 0; i < 4; i++) { Serial.print(myUnion.b[i], HEX); Serial.print(" "); } Serial.println();Swap out a printf on the Rpi
(Note: I am not assuming that an architecture which is little endian for integers must be little endian for floats - tho it usually is)
@Zeph
-mlittle-endian
Generate code for a processor running in little-endian mode. This is the default for all standard configurations.
-mbig-endian
Generate code for a processor running in big-endian mode; the default is to compile code for a little-endian processor.At: https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html
In my Java app (but then again on the PI is also default little-endian) The below is default used with Arduino based connected devices and on purpose hinted little-endian, just in case.
public static float byteArrayToFloat(byte[] bytes) { // Byte to float conversion return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getFloat(); } -
I would also like you to think about a fixed point format (http://en.wikipedia.org/wiki/Fixed-point_arithmetic).
A lot of sensor values can be represented in e.g. 8.8 or even 16.16. Think of e.g. temperatures (range -127..+127 is usually sufficient and I've yet to see an affordable sensor reporting in more than 256 steps per degree ), humidity (0..100%, 256 steps/degree), battery level (0..100%) etc.
Fixed point arithmetic requires very little resources compared to floating point and has no trouble converting between different architectures (apart from endianness).
For full efficiency sensor libraries should also support this format, as it doesn't make much sense to have a sensor library report in floating point and convert this to fixed point by MySensors.
A good Arduino-style fixed point library would help IMHO. Did a quick google on the subject, but didn't find much (apart from https://code.google.com/p/libfixmath/)I think support for real floating point values should still be possible, but this could be an interesting addition.
What do you think?
I would also like you to think about a fixed point format
See my suggestion above about defining an optional scaling factor for each variable, as we can define a precision for each floating variable now.
Simple decade scaling: integer factors of ten, using the same configured integer that would define precision for floats.
or
Flexible scaling: configure a floating point factor by which the integer OTA value would be multiplied to give real world units.
So a temperature of 29.7 could be sent as a 16 bit integer 297 with the gateway knowing (from configuratiion) that it must be multiplied by 0.1 before being converted to a string. Either approach could handle that.
The flexible version could also handles things like angles that are reported in degrees, or 0..1023 or radians or whatever, with the full accuracy of the sensor.
I think you are suggesting that we adopt one or more standard fixed point formats as additional OTA variable formats (as well as supporting the math function in the node library). I can certainly see value in that, as well. Like floating point, you would need to configure a precision when converting to decimal values (since 0.1 is not an accurate binary fraction in fixed point either). So 29.7 degrees would be encoded as 29*256 + ((256 * 7) / 10). = 7603, and converted to a float that would be 29.69921875. If like a float this fixed point value was configured with one digit of decimal accuracy, we'd get the 29.7 value again.
Either of these might sometimes let a node avoid the floating point libraries entirely in many cases, even when it wants to report non-integer values.
Could be handy if we ever get ATtiny nodes.
-
I would also like you to think about a fixed point format (http://en.wikipedia.org/wiki/Fixed-point_arithmetic).
A lot of sensor values can be represented in e.g. 8.8 or even 16.16. Think of e.g. temperatures (range -127..+127 is usually sufficient and I've yet to see an affordable sensor reporting in more than 256 steps per degree ), humidity (0..100%, 256 steps/degree), battery level (0..100%) etc.
Fixed point arithmetic requires very little resources compared to floating point and has no trouble converting between different architectures (apart from endianness).
For full efficiency sensor libraries should also support this format, as it doesn't make much sense to have a sensor library report in floating point and convert this to fixed point by MySensors.
A good Arduino-style fixed point library would help IMHO. Did a quick google on the subject, but didn't find much (apart from https://code.google.com/p/libfixmath/)I think support for real floating point values should still be possible, but this could be an interesting addition.
What do you think?
@Yveaux said:
Yep, would also be a good addition. Floating points is really crazy inefficient to use on an Arduino.
I will look at sending floats binary now... my secret knock sensor will have to wait :(.
Does anyone want to help on the fixed point stuff? -
@Yveaux said:
Yep, would also be a good addition. Floating points is really crazy inefficient to use on an Arduino.
I will look at sending floats binary now... my secret knock sensor will have to wait :(.
Does anyone want to help on the fixed point stuff? -
@hek said:
Does anyone want to help on the fixed point stuff?
I can try to put some skeleton together to get the interface right, as this is probably the hardest part.
-
@hek @Yveaux
Let me see if I am understanding.The payload types would be enhanced.
typedef enum { P_STRING, P_BYTE, P_INT16, P_UINT16, P_LONG32, P_ULONG32, P_CUSTOM } payload;to add an 8:8 and/or 16:16 fixed point formats, eg: P_FIX8P8 or P_FIX16P16. I'm guessing only signed fixed point, no unsigned?
And to add a 4 byte binary floating point P_FLOAT32?
One small suggestion: put P_CUSTOM first, so its numeric code doesn't change when you add additional formats. Or if it's too late for that, we can skip over P_CUSTOM for these new formats.
And also the payload setters:
MyMessage& set(const char* value); MyMessage& set(uint8_t value); MyMessage& set(double value, uint8_t decimals); MyMessage& set(unsigned long value); MyMessage& set(long value); MyMessage& set(unsigned int value); MyMessage& set(int value);would be enhanced to support these types.
MyMessage& set(double value, uint8_t decimals);could be unchanged as seen by the user even if the underlying OTA representation became binary. But we might add something like:
MyMessage& set(fix8p8 value, uint8_t: decimals); MyMessage& set(fix16p16 value, uint8_t: decimals);(the decimals parameter is needed for this like for floats, as described earlier)
This implies creating new C++ types, in this example "fix8p8", which is basically a int16_t with an implicit radix point in the middle.
Adding is simple. Multiply of fix8p8 is easy because you can use a long as temp before renormalizing, but multiply of fix16p16 gets trickier, of course.
Another discussion to have before it's set in stone...
Adding fixed point support both OTA and within library code has value, so I'm not against it. Getting the library right and educating users is going to be some work, tho. I use fixed point math fairly often, but it definitely has some gotchas that we are biting off.
The concept of having a scaling factor (see a few messages back) may be an easier step to implement and educate. It's an easy concept: every integer step represents X units, so the integer value must be multiplied by the scaling factor to get the real value in units. Default = 1.0 with both the same, as now.
The simplest version just scales by factors of 10, and could be implemented by adding a second integer to the set() function. In this version to send -12.5 you use set(-125,1), or to send 0.14 you use set(14,2). This can be interpreted into a string without even using floating point math, you just adjust where to insert a decimal point in a printed integer.
(The slightly more complex version would use an arbitrary floating point factor as the scale, so you could use 0.1 or 3.56 or whatever).
(Aside: from the viewpoint of the gateway, the fixed point enhancement is equivalent to having a fixed scaling factor of 2*-8 or 2^-16)
These enhancements are not mutually exclusive, but I would think that one of the scaled integer version might be easier to implement and understand as a first step.
-
@Zeph said:
(the decimals parameter is needed for this like for floats, as described earlier)
I don't see why the decimals parameter is needed. Currently it is used for the amount of decimals converted to textual presentation. This is not required for fixed point presentation (unless you want a scaling factor).
IMHO scaling just complicates things too much -- you also need to exchange the scaling factor with the gateway.This implies creating new C++ types, in this example "fix8p8", which is basically a int16_t with an implicit radix point in the middle.
Adding is simple. Multiply of fix8p8 is easy because you can use a long as temp before renormalizing, but multiply of fix16p16 gets trickier, of course.
My idea is to just wrap the new types in a class library, which allows for easy conversion and maths with these new fixedpt types.
Getting the library right and educating users is going to be some work, tho. I use fixed point math fairly often, but it definitely has some gotchas that we are biting off.
The library should shield regular users from the internals and pitfalls of fixed point. Most sketches just get a value from a sensor library and pass it on to MySensors, without modifying the value.
As part of this exercise we also have to modify these libraries which return their values in float-format, as it doesn't make sense to keeps floats in partly... -
@hek @Yveaux
Let me see if I am understanding.The payload types would be enhanced.
typedef enum { P_STRING, P_BYTE, P_INT16, P_UINT16, P_LONG32, P_ULONG32, P_CUSTOM } payload;to add an 8:8 and/or 16:16 fixed point formats, eg: P_FIX8P8 or P_FIX16P16. I'm guessing only signed fixed point, no unsigned?
And to add a 4 byte binary floating point P_FLOAT32?
One small suggestion: put P_CUSTOM first, so its numeric code doesn't change when you add additional formats. Or if it's too late for that, we can skip over P_CUSTOM for these new formats.
And also the payload setters:
MyMessage& set(const char* value); MyMessage& set(uint8_t value); MyMessage& set(double value, uint8_t decimals); MyMessage& set(unsigned long value); MyMessage& set(long value); MyMessage& set(unsigned int value); MyMessage& set(int value);would be enhanced to support these types.
MyMessage& set(double value, uint8_t decimals);could be unchanged as seen by the user even if the underlying OTA representation became binary. But we might add something like:
MyMessage& set(fix8p8 value, uint8_t: decimals); MyMessage& set(fix16p16 value, uint8_t: decimals);(the decimals parameter is needed for this like for floats, as described earlier)
This implies creating new C++ types, in this example "fix8p8", which is basically a int16_t with an implicit radix point in the middle.
Adding is simple. Multiply of fix8p8 is easy because you can use a long as temp before renormalizing, but multiply of fix16p16 gets trickier, of course.
Another discussion to have before it's set in stone...
Adding fixed point support both OTA and within library code has value, so I'm not against it. Getting the library right and educating users is going to be some work, tho. I use fixed point math fairly often, but it definitely has some gotchas that we are biting off.
The concept of having a scaling factor (see a few messages back) may be an easier step to implement and educate. It's an easy concept: every integer step represents X units, so the integer value must be multiplied by the scaling factor to get the real value in units. Default = 1.0 with both the same, as now.
The simplest version just scales by factors of 10, and could be implemented by adding a second integer to the set() function. In this version to send -12.5 you use set(-125,1), or to send 0.14 you use set(14,2). This can be interpreted into a string without even using floating point math, you just adjust where to insert a decimal point in a printed integer.
(The slightly more complex version would use an arbitrary floating point factor as the scale, so you could use 0.1 or 3.56 or whatever).
(Aside: from the viewpoint of the gateway, the fixed point enhancement is equivalent to having a fixed scaling factor of 2*-8 or 2^-16)
These enhancements are not mutually exclusive, but I would think that one of the scaled integer version might be easier to implement and understand as a first step.
@Zeph said:
The payload types would be enhanced.
typedef enum { P_STRING, P_BYTE, P_INT16, P_UINT16, P_LONG32, P_ULONG32, P_CUSTOM } payload;Darn, just realized we only got 3 bits to describe payload type. We need another one to fit the new ones.
MyMessage& set(double value, uint8_t decimals);Shouldn't this be set(float, uint8_t). Wouldn't it be confusing to have double-argument when only sending 32-bit float?
-
@Zeph said:
The payload types would be enhanced.
typedef enum { P_STRING, P_BYTE, P_INT16, P_UINT16, P_LONG32, P_ULONG32, P_CUSTOM } payload;Darn, just realized we only got 3 bits to describe payload type. We need another one to fit the new ones.
MyMessage& set(double value, uint8_t decimals);Shouldn't this be set(float, uint8_t). Wouldn't it be confusing to have double-argument when only sending 32-bit float?
-
@Zeph said:
The payload types would be enhanced.
typedef enum { P_STRING, P_BYTE, P_INT16, P_UINT16, P_LONG32, P_ULONG32, P_CUSTOM } payload;Darn, just realized we only got 3 bits to describe payload type. We need another one to fit the new ones.
MyMessage& set(double value, uint8_t decimals);Shouldn't this be set(float, uint8_t). Wouldn't it be confusing to have double-argument when only sending 32-bit float?