LabVIEW Interface for Arduino Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

How to read HMC 5883 Magnetometer

I was wondering if anyone knows how to read the magnetometer in labview. I would appreciate it if anyone can give me a guidance as to how to proceed.

Thanks

0 Kudos
Message 1 of 24
(23,345 Views)

You need to use the I2C functions to interact with it.  The best way that I found was to get working code for Arduino (not for LabVIEW) or other system and then use that to know what settings to use and what registers to access.

If you can find working code for Arduino (not for LabVIEW) and are still unable to figure out a LabVIEW interpretation then post the link to that code here and I can try to translate it into LIFA.

0 Kudos
Message 2 of 24
(8,008 Views)

*/

#include <Wire.h>

//#include <DebugUtils.h>

#include <HMC58X3.h>

HMC58X3 magn;

void setup(void) {

  Serial.begin(9600);

  Wire.begin();

 

  // no delay needed as we have already a delay(5) in HMC5843::init()

  magn.init(false); // Dont set mode yet, we'll do that later on.

  // Calibrate HMC using self test, not recommended to change the gain after calibration.

  magn.calibrate(1, 32); // Use gain 1=default, valid 0-7, 7 not recommended.

  // Single mode conversion was used in calibration, now set continuous mode

  magn.setMode(0);

}

void loop() {

  int ix,iy,iz;

  float fx,fy,fz;

  delay(10);

  // Get values, as ints and floats.

  magn.getValues(&ix,&iy,&iz);

  magn.getValues(&fx,&fy,&fz);

  // also available HMC5843::getValues(float *xyz) you can pass to it an array of 3 floats

 

  // Print int values

  Serial.print("Ints x:");

  Serial.print(ix);

  Serial.print(",");

  Serial.print(iy);

  Serial.print(",");

  Serial.print(iz);

  Serial.print(",");

 

  // Print float values

  Serial.print(" Floats x:");

  Serial.print(fx);

  Serial.print(" y:");

  Serial.print(fy);

  Serial.print(" z:");

  Serial.print(fz);

 

  // a simple heading, assuming it's close to horizontal. See:

  // M.J. Caruso. Applications of magnetic sensors for low cost compass systems.

  // In Position Location and Navigation Symposium, IEEE, 2000. Available from:

  // http://hnc.ru/lib/a%26c%20%28automatic%20%26%20controls%29/sensors/DataSheet/Magnit/Honeywell/lowcos...

  Serial.print(" Heading: ");

  float heading = atan2(fy, fx);

  if(heading < 0) {

    heading += 2 * M_PI;

  }

  Serial.println(heading * 180/M_PI);

  // x and y axis are swapped above with respect to the above paper as our Z axis points to the sky while in the paper it points to the bottom

}

This is the code. For my accelerometer and Gyro I can read the data in labview its only the magnetometer now.

0 Kudos
Message 3 of 24
(8,008 Views)

So you have the accelerometer and gyro working in LabVIEW?  It should essentially be the same thing using different registers to get the magnetometer info.

If you are still having issues then I need the code that actually gets the values via I2C.  This is likely in the getValues() function.

0 Kudos
Message 4 of 24
(8,008 Views)

#include "Arduino.h"

#include <Wire.h>

#ifndef HMC58X3_h

#define HMC58X3_h

#define HMC58X3_ADDR 0x1E // 7 bit address of the HMC58X3 used with the Wire library

#define HMC_POS_BIAS 1

#define HMC_NEG_BIAS 2

// HMC58X3 register map. For details see HMC58X3 datasheet

#define HMC58X3_R_CONFA 0

#define HMC58X3_R_CONFB 1

#define HMC58X3_R_MODE 2

#define HMC58X3_R_XM 3

#define HMC58X3_R_XL 4

#ifdef ISHMC5843

    #define HMC58X3_R_YM (5)    //!< Register address for YM.

    #define HMC58X3_R_YL (6)    //!< Register address for YL.

    #define HMC58X3_R_ZM (7)    //!< Register address for ZM.

    #define HMC58X3_R_ZL (8)    //!< Register address for ZL.

    #define HMC58X3_X_SELF_TEST_GAUSS (+0.55)                       //!< X axis level when bias current is applied.

    #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.

    #define HMC58X3_Z_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.

    /*

        This is my best guess at the LOW, HIGH limit.  The data sheet does not have these values.

    */

    #define SELF_TEST_LOW_LIMIT  (HMC58X3_X_SELF_TEST_GAUSS*0.53)   //!< Low limit 53% of expected value.

    #define SELF_TEST_HIGH_LIMIT (HMC58X3_X_SELF_TEST_GAUSS*1.36)   //!< High limit 136% of expected values.

#else // HMC5883L

    #define HMC58X3_R_YM (7)  //!< Register address for YM.

    #define HMC58X3_R_YL (8)  //!< Register address for YL.

    #define HMC58X3_R_ZM (5)  //!< Register address for ZM.

    #define HMC58X3_R_ZL (6)  //!< Register address for ZL.

    #define HMC58X3_X_SELF_TEST_GAUSS (+1.16)                       //!< X axis level when bias current is applied.

    #define HMC58X3_Y_SELF_TEST_GAUSS (HMC58X3_X_SELF_TEST_GAUSS)   //!< Y axis level when bias current is applied.

    #define HMC58X3_Z_SELF_TEST_GAUSS (+1.08)                       //!< Y axis level when bias current is applied.

    #define SELF_TEST_LOW_LIMIT  (243.0/390.0)   //!< Low limit when gain is 5.

    #define SELF_TEST_HIGH_LIMIT (575.0/390.0)   //!< High limit when gain is 5.

#endif

#define HMC58X3_R_STATUS 9

#define HMC58X3_R_IDA 10

#define HMC58X3_R_IDB 11

#define HMC58X3_R_IDC 12

class HMC58X3

{

  public:

    HMC58X3();

    void init(bool setmode);

    void init(int address, bool setmode);

    void getValues(int *x,int *y,int *z);

    void getValues(float *x,float *y,float *z);

    void getValues(float *xyz);

    void getRaw(int *x,int *y,int *z);

    void getRaw(int *xyz);

    void calibrate(unsigned char gain);     // Original calibrate with a few weaknesses.

    bool calibrate(unsigned char gain,unsigned int n_samples);

    void setMode(unsigned char mode);

    void setDOR(unsigned char DOR);

    void setGain(unsigned char gain);

    void getID(char id[3]);

  private:

    void writeReg(unsigned char reg, unsigned char val);

    float x_scale,y_scale,z_scale,x_max,y_max,z_max;

};

#endif // HMC58X3_h

This is the code that is used by the above code.

0 Kudos
Message 5 of 24
(8,008 Views)

Ok, this is never going to work like this.  Can you give me a link to the HMC58X3 code?

0 Kudos
Message 6 of 24
(8,008 Views)

The same magnetometer is on sparkfuns website and they provided the link.

Here is the code link.

http://www.sparkfun.com/products/10530

0 Kudos
Message 7 of 24
(8,008 Views)

Ok, so I made up a small library for this.  It's basically the same as all the other I2C sensors so I'm not sure what issues you were having if you were able to get the other sensors working.  There is an example VI in there but I can't tell you if it works but everything is done the same way as the example codes that I've looked at.

Let me know if it works for you.

Message 8 of 24
(8,008 Views)

Sorry man I have 2010. Can you please please upload for 2010 too.

0 Kudos
Message 9 of 24
(8,008 Views)
Message 10 of 24
(8,008 Views)