From 0600bcc12c2f7004c0e4fe4073c97f17beea3928 Mon Sep 17 00:00:00 2001 From: Mathieu Kaelin Date: Thu, 19 May 2022 22:57:14 +0200 Subject: [PATCH] fix rounding error and replace sprintf by Serial.Print for better cross platform support sprintf doesn't support float with all MCUs. RP2040 support it but apparently not the SAMD21 and AVR based Arduino boards. New code should be supported by all Arduino boards. --- .../read-magnetic-fields-with-i2c.ino | 35 +++++++++++++++---- .../read-magnetic-fields-with-spi.ino | 35 +++++++++++++++---- src/mv300sensor.cpp | 6 ++-- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/examples/read-magnetic-fields-with-i2c/read-magnetic-fields-with-i2c.ino b/examples/read-magnetic-fields-with-i2c/read-magnetic-fields-with-i2c.ino index 5174b2a..217e72d 100644 --- a/examples/read-magnetic-fields-with-i2c/read-magnetic-fields-with-i2c.ino +++ b/examples/read-magnetic-fields-with-i2c/read-magnetic-fields-with-i2c.ino @@ -7,7 +7,6 @@ int16_t magFieldBxSigned, magFieldBySigned, magFieldBzSigned, temperatureSigned; double magFieldBxInMilliTesla, magFieldByInMilliTesla, magFieldBzInMilliTesla, temperatureInDegreeCelsius; double norm, theta, phi; uint8_t frameCounter; -char data[400]; void setup() { mv300sensori2c.begin(); @@ -19,14 +18,16 @@ void setup() { mv300sensori2c.writeRegister(17, 1, 0); //Set MODE = 1 (Host Controlled Mode) for(uint8_t i=0;i<20;++i) { registerReadbackValue=mv300sensori2c.readRegister(i, 0); - sprintf(data, "Reg[%u] = %u", i, registerReadbackValue); - Serial.println(data); + Serial.print("Reg "); + Serial.print(i, DEC); + Serial.print(" = "); + Serial.println(registerReadbackValue, DEC); } mv300sensori2c.readRegister(0, 1); //Read register 0 and trigger a new acquisition by setting 2-bit Trigger to 1 } void loop() { - delayMicroseconds(175); //wait for the previous conversion to finish + delayMicroseconds(182); //wait for the previous conversion to finish mv300sensori2c.readMagneticComponents(&magFieldBx, &magFieldBy, &magFieldBz, &temperature, &frameCounter, 1); magFieldBxSigned=twosComplement(magFieldBx, 12); magFieldBySigned=twosComplement(magFieldBy, 12); @@ -37,6 +38,28 @@ void loop() { magFieldBzInMilliTesla=convertMagneticFieldFromLsbToMilliTesla(magFieldBzSigned, 0); temperatureInDegreeCelsius=convertTemperatureFromLsbToDegreeCelsius(temperatureSigned); computeNormThetaPhi(magFieldBxInMilliTesla, magFieldByInMilliTesla, magFieldBzInMilliTesla, &norm, &theta, &phi); - sprintf(data, "Bx [mT]:%+8.3f, By [mT]:%+8.3f, Bz [mT]:%+8.3f, Temperature [°C]:%+6.1f, frame:%u, Norm [mT]:%+8.3f, Theta [°]:%+8.3f, Phi [°]:%+8.3f", magFieldBxInMilliTesla, magFieldByInMilliTesla, magFieldBzInMilliTesla, temperatureInDegreeCelsius, frameCounter, norm, theta, phi); - Serial.println(data); + Serial.print("Bx [mT]:"); + Serial.print(magFieldBxInMilliTesla, 3); + Serial.print(","); + Serial.print("By [mT]:"); + Serial.print(magFieldByInMilliTesla, 3); + Serial.print(","); + Serial.print("Bz [mT]:"); + Serial.print(magFieldBzInMilliTesla, 3); + Serial.print(","); + Serial.print("Temperature [°C]:"); + Serial.print(temperatureInDegreeCelsius, 1); + Serial.print(","); + Serial.print("frame:"); + Serial.print(frameCounter, DEC); + Serial.print(","); + Serial.print("Norm [mT]:"); + Serial.print(norm, 3); + Serial.print(","); + Serial.print("Theta [°]:"); + Serial.print(theta, 3); + Serial.print(","); + Serial.print("Phi [°]:"); + Serial.print(phi, 3); + Serial.println(); } \ No newline at end of file diff --git a/examples/read-magnetic-fields-with-spi/read-magnetic-fields-with-spi.ino b/examples/read-magnetic-fields-with-spi/read-magnetic-fields-with-spi.ino index 041c9b7..e1d0b90 100644 --- a/examples/read-magnetic-fields-with-spi/read-magnetic-fields-with-spi.ino +++ b/examples/read-magnetic-fields-with-spi/read-magnetic-fields-with-spi.ino @@ -9,7 +9,6 @@ int16_t magFieldBxSigned, magFieldBySigned, magFieldBzSigned, temperatureSigned; double magFieldBxInMilliTesla, magFieldByInMilliTesla, magFieldBzInMilliTesla, temperatureInDegreeCelsius; double norm, theta, phi; uint8_t frameCounter; -char data[400]; void setup() { mv300sensorspi.begin(spiSclkClockFrequency, SPI_MODE0, spiChipSelectPin); @@ -19,14 +18,16 @@ void setup() { mv300sensorspi.writeRegister(17, 1, 0); //Set MODE = 1 (Host Controlled Mode) for(uint8_t i=0;i<20;++i) { registerReadbackValue=mv300sensorspi.readRegister(i, 0); - sprintf(data, "Reg[%u] = %u", i, registerReadbackValue); - Serial.println(data); + Serial.print("Reg "); + Serial.print(i, DEC); + Serial.print(" = "); + Serial.println(registerReadbackValue, DEC); } mv300sensorspi.readRegister(0, 1); //Read register 0 and trigger a new acquisition by setting 2-bit Trigger to 1 } void loop() { - delayMicroseconds(175); //wait for the previous conversion to finish + delayMicroseconds(182); //wait for the previous conversion to finish mv300sensorspi.readMagneticComponents(&magFieldBx, &magFieldBy, &magFieldBz, &temperature, &frameCounter, 1); magFieldBxSigned=twosComplement(magFieldBx, 12); magFieldBySigned=twosComplement(magFieldBy, 12); @@ -37,6 +38,28 @@ void loop() { magFieldBzInMilliTesla=convertMagneticFieldFromLsbToMilliTesla(magFieldBzSigned, 0); temperatureInDegreeCelsius=convertTemperatureFromLsbToDegreeCelsius(temperatureSigned); computeNormThetaPhi(magFieldBxInMilliTesla, magFieldByInMilliTesla, magFieldBzInMilliTesla, &norm, &theta, &phi); - sprintf(data, "Bx [mT]:%+8.3f, By [mT]:%+8.3f, Bz [mT]:%+8.3f, Temperature [°C]:%+6.1f, frame:%u, Norm [mT]:%+8.3f, Theta [°]:%+8.3f, Phi [°]:%+8.3f", magFieldBxInMilliTesla, magFieldByInMilliTesla, magFieldBzInMilliTesla, temperatureInDegreeCelsius, frameCounter, norm, theta, phi); - Serial.println(data); + Serial.print("Bx [mT]:"); + Serial.print(magFieldBxInMilliTesla, 3); + Serial.print(","); + Serial.print("By [mT]:"); + Serial.print(magFieldByInMilliTesla, 3); + Serial.print(","); + Serial.print("Bz [mT]:"); + Serial.print(magFieldBzInMilliTesla, 3); + Serial.print(","); + Serial.print("Temperature [°C]:"); + Serial.print(temperatureInDegreeCelsius, 1); + Serial.print(","); + Serial.print("frame:"); + Serial.print(frameCounter, DEC); + Serial.print(","); + Serial.print("Norm [mT]:"); + Serial.print(norm, 3); + Serial.print(","); + Serial.print("Theta [°]:"); + Serial.print(theta, 3); + Serial.print(","); + Serial.print("Phi [°]:"); + Serial.print(phi, 3); + Serial.println(); } \ No newline at end of file diff --git a/src/mv300sensor.cpp b/src/mv300sensor.cpp index cf576d0..48f4a9c 100644 --- a/src/mv300sensor.cpp +++ b/src/mv300sensor.cpp @@ -28,14 +28,12 @@ int16_t twosComplement(uint16_t value, uint8_t numberOfBits) { } double convertMagneticFieldFromLsbToMilliTesla(int16_t magneticFieldInLsb, uint8_t brange) { - int16_t valueInMilliTesla; if (brange == 0) { - valueInMilliTesla=magneticFieldInLsb/7.0; + return magneticFieldInLsb/7.0; } else { - valueInMilliTesla=magneticFieldInLsb/14.0; + return magneticFieldInLsb/14.0; } - return valueInMilliTesla; } double convertTemperatureFromLsbToDegreeCelsius(int16_t temperatureInLsb) {