An fully working IMU-Filter and Sensor drivers for the 10DOF-Board over I2C. All in one simple class. Include, calibrate sensors, call read, get angles. (3D Visualisation code for Python also included) Sensors: L3G4200D, ADXL345, HMC5883, BMP085

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HMC5883.cpp Source File

HMC5883.cpp

00001 #include "HMC5883.h"
00002 
00003 HMC5883::HMC5883(PinName sda, PinName scl) : I2C_Sensor(sda, scl, HMC5883_I2C_ADDRESS)
00004 {   
00005     #warning these three offsets are calibration values to get |MAX| = |MIN|
00006     offset[0] = -155; // offset calculated by hand... (min + ((max - min) / 2)
00007     offset[1] = -142; // TODO: make this automatic with saving to filesystem
00008     offset[2] = -33.5;
00009     
00010     // load calibration values
00011     //loadCalibrationValues(scale, 3, "COMPASS_SCALE.txt");
00012     //loadCalibrationValues(offset, 3, "COMPASS_OFFSET.txt");
00013     
00014     // initialize HMC5883
00015     writeRegister(HMC5883_CONF_REG_A, 0x78);                // 8 samples, 75Hz output, normal mode
00016     //writeRegister(HMC5883_CONF_REG_A, 0x19);              // 8 samples, 75Hz output, test mode! (should get constant values from measurement, see datasheet)
00017     writeRegister(HMC5883_CONF_REG_B, 0x20);                // Gain for +- 1.3 gauss (earth compass ~0.6 gauss)
00018     writeRegister(HMC5883_MODE_REG, 0x00);                  // continuous measurement-mode
00019 }
00020 
00021 void HMC5883::read()
00022 {
00023     readraw();
00024     for(int i = 0; i < 3; i++)
00025         data[i] = (float)(raw[i]) - offset[i];
00026 }
00027 
00028 void HMC5883::calibrate(int s)
00029 {
00030     int Min[3];                                             // values for achieved maximum and minimum amplitude in calibrating environment
00031     int Max[3];
00032     
00033     Timer calibrate_timer;                                  // timer to know when calibration is finished
00034     calibrate_timer.start();
00035     
00036     while(calibrate_timer.read() < s)                       // take measurements for s seconds
00037     {
00038         readraw();
00039         for(int i = 0; i < 3; i++) {
00040             Min[i] = Min[i] < raw[i] ? Min[i] : raw[i];      // after each measurement check if there's a new minimum or maximum
00041             Max[i] = Max[i] > raw[i] ? Max[i] : raw[i];
00042         }
00043     }
00044     
00045     for(int i = 0; i < 3; i++) {
00046         //scale[i]= 2000 / (float)(Max[i]-Min[i]);            // calculate scale and offset out of the measured maxima and minima
00047         //offset[i]= 1000 - (float)(Max[i]) * scale[i];       // the lower bound is -1000, the higher one 1000
00048     }
00049     
00050     //saveCalibrationValues(scale, 3, "COM_SCALE");   // save new scale and offset values to flash
00051     //saveCalibrationValues(offset, 3, "COM_OFFSE");
00052 }
00053 
00054 void HMC5883::readraw()
00055 {
00056     char buffer[6];                                         // 8-Bit pieces of axis data
00057     
00058     readMultiRegister(HMC5883_DATA_OUT_X_MSB, buffer, 6);   // read axis registers using I2C
00059     
00060     raw[0] = (short) (buffer[0] << 8 | buffer[1]);          // join 8-Bit pieces to 16-bit short integers
00061     raw[1] = (short) (buffer[4] << 8 | buffer[5]);          // X, Z and Y (yes, order is stupid like this, see datasheet)
00062     raw[2] = (short) (buffer[2] << 8 | buffer[3]);
00063 }
00064 
00065 float HMC5883::get_angle()
00066 {
00067     #define RAD2DEG     57.295779513082320876798154814105
00068 
00069     float Heading;
00070     
00071     Heading = RAD2DEG * atan2(data[0],data[1]);
00072     Heading += 1.367;                   // correction of the angle between geographical and magnetical north direction, called declination
00073                                         // if you need an east-declination += DecAngle, if you need west-declination -= DecAngle
00074                                         // for me in Switzerland, Bern it's ca. 1.367 degree east
00075                                         // see:     http://magnetic-declination.com/
00076                                         // for me:  http://www.swisstopo.admin.ch/internet/swisstopo/de/home/apps/calc/declination.html
00077     if(Heading < 0)  
00078         Heading += 360;                 // minimum 0 degree
00079         
00080     if(Heading > 360)   
00081         Heading -= 360;                 // maximum 360 degree
00082 
00083     return Heading;
00084 }
00085     
00086