upm/examples/c++/mmc35240.cxx
Lay, Kuan Loon fabf4287d6 upm: enable MMC35240 3-axis magnetic sensor library and example
MMC35240 is 3-axis magnetic sensor from MEMSIC.

This sensor can measure magnetic fields within the full scale range of
+-24 Gauss (G).

The library provided is libupm-mmc35240.so
The example provided is mmc35240-example-cxx where it will print x,y,z axis
when trigger buffer data is ready. It's also print azimuth value.

This sensor requires calibration. Please shake the sensor in figure 8 pattern,
mmc35240-example will print the calibrated level.

As the sensor data is noisy, we have implemented denoise algorithm within the
sensor library.

The azimuth formula is provided by Han, He <he.han@intel.com>.

Signed-off-by: Lay, Kuan Loon <kuan.loon.lay@intel.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
2017-03-16 17:22:40 -07:00

112 lines
3.2 KiB
C++

/*
* Author: Lay, Kuan Loon <kuan.loon.lay@intel.com>
* Copyright (c) 2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <math.h>
#include "mmc35240.hpp"
using namespace std;
int shouldRun = true;
upm::MMC35240* magnetometer;
void
sig_handler(int signo)
{
if (signo == SIGINT)
shouldRun = false;
}
void
data_callback(char* data)
{
float x, y, z;
double azimuth;
int level;
magnetometer->extract3Axis(data, &x, &y, &z);
/* calibrated level
* UNRELIABLE = 0
* ACCURACY_LOW = 1
* ACCURACY_MEDIUM = 2
* ACCURACY_HIGH = >=3
*/
level = magnetometer->getCalibratedLevel();
if ((x == 0) && (y == 0)) {
cout << "Point (0, 0) is invalid!\n" << endl;
return;
}
if (x == 0) {
if (y > 0)
azimuth = 0;
else
azimuth = 180;
} else if (y == 0) {
if (x > 0)
azimuth = 90;
else
azimuth = 270;
} else {
if (x > 0)
azimuth = 90 - atan(y / x) * 180 / M_PI;
else
azimuth = 270 - atan(y / x) * 180 / M_PI;
}
cout << "[Calibrated Level:" << level << ']' << "[Azimuth:" << (int) (360 - azimuth) << ']'
<< '\t' << (int) x << '\t' << (int) y << '\t' << (int) z << "[uT]" << endl;
}
int
main()
{
signal(SIGINT, sig_handler);
//! [Interesting]
// Instantiate a MMC35240 Magnetic Sensor on iio device 5. This configuration is a reference and
// should be changed per platform/board type.
magnetometer = new upm::MMC35240(5);
// Kernel driver does not allow changing the value of scale at run-time, default scale is
// 0.001000
magnetometer->setScale(0.001000);
// Available sampling frequency are 1.5, 13, 25, 50
magnetometer->setSamplingFrequency(25.000000);
magnetometer->enable3AxisChannel();
magnetometer->installISR(data_callback, NULL);
magnetometer->enableBuffer(16);
while (shouldRun) {
sleep(1);
}
magnetometer->disableBuffer();
//! [Interesting]
cout << "Exiting" << endl;
delete magnetometer;
return 0;
}