Hello, I poked around searching for this issue, and I found one other person asking it a while ago with no response, I figured I would try my hand..
I've got an HMC5883L hooked up to my teensy3. It's transmitting data that changes as I rotate the magnetometer, but it sometimes gives data as following when using the example code from https://www.loveelectronics.co.uk/Tutorials/8/hmc5883l-tutorial-and-arduino-library
The data looks like this:
The raw values are close enough to certain integer max values, and definitely not correct values for the device from what I've seen of it. In their demos there are values that are negative, but all of mine seem to be from 0 to 65535, which seems to indicate it's reading those values as unsigned ints. The values are from MagnetometerRaw's struct:
Generated in the teensyduino IDE from this call:
Which is from the following in the library:
I am not exactly sure, but it looks to me like the data is being read into an unsigned int array, which is bit shifted into the int variables. I don't know how the site was getting negative values from this, but I don't entirely understand the code, I hope this is sufficient information. Is there something going on here that I can easily fix?
Thanks
I've got an HMC5883L hooked up to my teensy3. It's transmitting data that changes as I rotate the magnetometer, but it sometimes gives data as following when using the example code from https://www.loveelectronics.co.uk/Tutorials/8/hmc5883l-tutorial-and-arduino-library
The data looks like this:
Raw: 65500 65283 64830 Scaled: 65500.00 65283.00 64830.00 Heading: 0.54 Radians 31.15 Degrees
Raw: 65501 65282 64830 Scaled: 65501.00 65282.00 64830.00 Heading: 0.54 Radians 31.15 Degrees
Raw: 65498 65285 64829 Scaled: 65498.00 65285.00 64829.00 Heading: 0.54 Radians 31.16 Degrees
Raw: 65499 65281 64830 Scaled: 65499.00 65281.00 64830.00 Heading: 0.54 Radians 31.15 Degrees
The raw values are close enough to certain integer max values, and definitely not correct values for the device from what I've seen of it. In their demos there are values that are negative, but all of mine seem to be from 0 to 65535, which seems to indicate it's reading those values as unsigned ints. The values are from MagnetometerRaw's struct:
Code:
struct MagnetometerRaw
{
int XAxis;
int YAxis;
int ZAxis;
};
Generated in the teensyduino IDE from this call:
Code:
MagnetometerRaw raw = compass.ReadRawAxis();
Which is from the following in the library:
Code:
MagnetometerRaw HMC5883L::ReadRawAxis()
{
uint8_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];
return raw;
}
uint8_t* HMC5883L::Read(int address, int length)
{
Wire.beginTransmission(HMC5883L_Address);
Wire.send(address);
Wire.endTransmission();
Wire.beginTransmission(HMC5883L_Address);
Wire.requestFrom(HMC5883L_Address, length);
uint8_t buffer[length];
if(Wire.available() == length)
{
for(uint8_t i = 0; i < length; i++)
{
buffer[i] = Wire.receive();
}
}
Wire.endTransmission();
return buffer;
}
I am not exactly sure, but it looks to me like the data is being read into an unsigned int array, which is bit shifted into the int variables. I don't know how the site was getting negative values from this, but I don't entirely understand the code, I hope this is sufficient information. Is there something going on here that I can easily fix?
Thanks