2

I'm having a SparkFun 9 DoF sensor stick including the following components:

  • ADLX345 - Accelerometer
  • ITG3200 Gyroscope
  • HMC5843 - Magnetometer

Im currently working on a project in which I need to calculate the yaw angle, I tried using the gyroscope and the accelerometer for this and found out that it didn't work for the yaw angle but only for the roll and pitch angles.

Im trying to do my implmentation in Simulink where I read the sensors via I2C through an S-function. My sensor stick is connected to an Arduino Mega and it works well for both the accelerometer and the gyroscope but when I try to do the same thing for the magnetometer all the signals have a constant value of zero.

For doing this i include the two libraries wire.h, wire.cpp, twi.h and twi.c. This is the same libraries that are used in many Arduino sketches for reading I2C-devices.

I use the following code for the initialization:

    if (xD[0]!=1) {

  # ifndef MATLAB_MEX_FILE

  Wire.begin(); // start i2c bus

  Wire.beginTransmission(MAG_ADDRESS); // 0x3C
  Wire.write(MAG_MEASMODE);            // 0x02
  Wire.write(MAG_MEASMODEC);           // 0x00
  Wire.endTransmission();

  # endif   
  // initialization done 
  xD[0]=1;
}

For the output I use the following code

// wait until after initialization is done
if (xD[0]==1) {

# ifndef MATLAB_MEX_FILE

    unsigned int i = 0;         // counter var.

    unsigned char lb_mag[6] = {0};

    int temp_mag[6] = {0};

    Wire.beginTransmission(MAG_ADDRESS); //0x3C
    Wire.write(MAG_XMSB);                //0x03
    Wire.requestFrom(MAG_ADDRESS,6);     //requesting MSB,LSB of XYZ-component
    while (Wire.available()){
        lb_mag[i++] = Wire.read();
    }

    Wire.endTransmission();

    temp_mag[0] = (lb_mag[0] << 8) + lb_mag[1];  // convert x reading to int16
    temp_mag[1] = (lb_mag[2] << 8) + lb_mag[3];  // convert y reading to int16
    temp_mag[2] = (lb_mag[4] << 8) + lb_mag[5];  // convert z reading to int16


    for(i=0;i<3;i++){
       magnet[i] = temp_mag[i];
    }

# endif
}

Does anyone have any ideas on why this does not work? As I mentioned before it works for both the gyroscope and the accelerometer. Anyhow, all help is appreciated! Thanks in advance!

Carl
  • 21
  • 1

0 Answers0