Skip to content

Commit

Permalink
fix rounding error and replace sprintf by Serial.Print for better cro…
Browse files Browse the repository at this point in the history
…ss 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.
  • Loading branch information
mathieukaelin committed May 19, 2022
1 parent 87fb0e3 commit 0600bcc
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
}
6 changes: 2 additions & 4 deletions src/mv300sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0600bcc

Please sign in to comment.