First draft HMC5883 magnetometer sensor using physical quantities, outputting via serial port using std::cout on mbed os 5

magnetometer.cpp

Committer:
skyscraper
Date:
2020-03-26
Revision:
9:87a7169b4d5c
Parent:
8:c5dc1ce10722
Child:
10:75c8ce89aeb7

File content as of revision 9:87a7169b4d5c:


#include "magnetometer.h"
#include "quan_time.h"
#include "hmc5883.h"
#include "resourceManager.h"

namespace {
    hmc5883L hmc5883L_ID1{resource::i2c1,0x3D};
}

bool mag_init()
{
// allow magnetometer hardware time to start
   if ( Kernel::get_ms_count() < 500U){
       ThisThread::sleep_until(500U);
   }
   if (! hmc5883L_ID1.detected(true)){
      return false; // usr has been notified
   }
   
   hmc5883L_ID1.set_idle_mode();
   
   constexpr auto earth_magnetic_field_flux_density = 31.869_uT;
   bool const success =
        hmc5883L_ID1.set_samples_average(8) &&
        hmc5883L_ID1.set_range( earth_magnetic_field_flux_density * 2U);
   
    // gains from calib engine
    quan::three_d::vect<double> gain{1.37689,1.35057,1.56647};
    hmc5883L_ID1.set_gain(gain);
    
    // offsets from calib engine
    quan::three_d::vect<
        quan::magnetic_flux_density::uT
    > offset{10.5724_uT,-10.869_uT,2.241468_uT};
    hmc5883L_ID1.set_offset(offset);
    return success;
}

bool mag_start_measurement()
{
    return hmc5883L_ID1.start_measurement();  
}

bool mag_data_ready()
{
    return hmc5883L_ID1.data_ready();
}

bool mag_read(quan::three_d::vect<quan::magnetic_flux_density::uT> & v)
{
    return hmc5883L_ID1.read(v);
}