2

I'm using HMC5883L sensor to measure magnetic field but when I look to the data on my ESP8266 it misses the decimals... The value should be, for example, "-234.54" and I got only "-234.00". I'm trying to fix this looking into the library but it didn't work.

My structures in HMC5883L.h:

struct MagnetometerScaled
{
    float XAxis;
    float YAxis;
    float ZAxis;
};

struct MagnetometerRaw
{
    float XAxis;
    float YAxis;
    float ZAxis;
};

Parts of HMC5883L.cpp in use:

MagnetometerRaw HMC5883L::ReadRawAxis()
{
  const int8_t* buffer = Read(DataRegisterBegin, 6);
  MagnetometerRaw raw = MagnetometerRaw();
  raw.XAxis = (buffer[0] << 8) | buffer[1];
  raw.ZAxis = (buffer[2] << 8) | buffer[3];
  raw.YAxis = (buffer[4] << 8) | buffer[5];
  delete [] buffer;
  return raw;
}

MagnetometerScaled HMC5883L::ReadScaledAxis()
{
  MagnetometerRaw raw = ReadRawAxis();
  MagnetometerScaled scaled = MagnetometerScaled();
  scaled.XAxis = raw.XAxis * m_Scale; // m_scale = 0.92 -> A float value
  scaled.ZAxis = raw.ZAxis * m_Scale; // m_scale = 0.92 -> A float value
  scaled.YAxis = raw.YAxis * m_Scale; // m_scale = 0.92 -> A float value
  return scaled;
}

int8_t* HMC5883L::Read(int address, int length)
{
  Wire.beginTransmission(HMC5883L_Address);
  Wire.write(address);
  Wire.endTransmission();

  Wire.beginTransmission(HMC5883L_Address);
  Wire.requestFrom(HMC5883L_Address, length);

  // uint8_t buffer[length];
  int8_t *buffer = new int8_t[length];
  if(Wire.available() == length)
  {
    for(uint8_t i = 0; i < length; i++)
    {
      buffer[i] = Wire.read();
    }
  }
  Wire.endTransmission();

  return buffer;
}

In run this code (basically) using:

HMC5883L magnetometer;
MagnetometerScaled scaledVals;
scaledVals = magnetometer.ReadScaledAxis();
Serial.println(scaledVals.XAxis);

If I left some important part of the code behind, just ask for it :)

EDIT1: If I run Serial.println(1.01); it prints the value correctly (output = 1.01). So, Serial.println() is not truncating float values...

EDIT2: As I see, the problem seems to be with the type returned by Read() function. Maybe if I change it to char* instead of int8_t I'll be able to receive the decimals on the right way. My question related to this is with the ReadRawAxis() function. How to get the data from buffer?

Leandro
  • 1,114
  • 1
  • 12
  • 15
  • *when I look to the data on my ESP8266 it misses the decimals...* - how are you looking at the data? if you are using some formatting function like `sprintf` are you using the correct template definition for a `float`? –  May 12 '15 at 17:54
  • What is the value for m_Scale? – Ross May 12 '15 at 17:56
  • possible duplicate of [Arduino sprintf float not formatting](http://stackoverflow.com/questions/27651012/arduino-sprintf-float-not-formatting) –  May 12 '15 at 18:06
  • @JarrodRoberson I'm using `Serial.println(scaled.XAxis);` If I run this code on my Arduino, it works ok. The problem only shows up if I run it in ESP8266 – Leandro May 12 '15 at 18:25
  • @Ross `m_scale = 0.92;` It is defined as a `float` – Leandro May 12 '15 at 18:26
  • You are supposed to add the requested code or answers as an **edit** to your question, not as more comments, otherwise others will just end up asking the same thing. –  May 13 '15 at 06:04
  • Try just printing something like `.println(1.01)` and see if it truncates the values. –  May 13 '15 at 06:07
  • 1
    @JarrodRoberson I made the changes on my question, sorry about that. I'm also going to try your last suggestion right now :D – Leandro May 13 '15 at 16:13
  • Just updated with a new alternative, if someone could guide me throw this... I don't know C++ very well – Leandro May 14 '15 at 16:44

0 Answers0