2022-12-02 04:37:37 -05:00
|
|
|
//
|
|
|
|
// FILE: A1301.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2024-01-01 11:20:13 -05:00
|
|
|
// VERSION: 0.2.2
|
2022-12-02 04:37:37 -05:00
|
|
|
// DATE: 2010-07-22
|
|
|
|
// PURPOSE: Arduino library for A1301 A1302 magnetometer.
|
2023-04-25 04:25:42 -04:00
|
|
|
// URL: https://github.com/RobTillaart/A1301
|
2022-12-02 04:37:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
#include "A1301.h"
|
|
|
|
|
|
|
|
|
2023-04-25 04:25:42 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// CONSTRUCTOR
|
|
|
|
//
|
2022-12-02 04:37:37 -05:00
|
|
|
HALL::HALL(uint8_t pin)
|
|
|
|
{
|
2022-12-02 06:52:49 -05:00
|
|
|
_pin = pin;
|
2023-04-25 04:25:42 -04:00
|
|
|
_maxADC = 1023;
|
|
|
|
_mVStep = 5000.0 / _maxADC; // default 10 bit 5V ADC (UNO)
|
|
|
|
_midPoint = _maxADC * 0.5; // default middle
|
2022-12-02 06:52:49 -05:00
|
|
|
_prevGauss = 0;
|
|
|
|
_lastGauss = 0;
|
2023-09-20 13:16:07 -04:00
|
|
|
_GaussmV = 1.0 / 2.5;
|
2023-04-25 04:25:42 -04:00
|
|
|
_maxGauss = 500.0;
|
2022-12-02 04:37:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HALL::begin(float voltage, uint16_t steps)
|
|
|
|
{
|
2023-04-25 04:25:42 -04:00
|
|
|
_maxADC = steps;
|
|
|
|
_mVStep = voltage / steps;
|
|
|
|
_midPoint = steps * 0.5;
|
2022-12-02 04:37:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-25 04:25:42 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// MIDPOINT
|
|
|
|
//
|
2022-12-02 04:37:37 -05:00
|
|
|
void HALL::setMidPoint(float midPoint)
|
|
|
|
{
|
|
|
|
_midPoint = midPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-01-01 11:20:13 -05:00
|
|
|
float HALL::autoMidPoint(uint8_t times)
|
|
|
|
{
|
|
|
|
_midPoint = raw(times);
|
|
|
|
return _midPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 04:37:37 -05:00
|
|
|
float HALL::getMidPoint()
|
|
|
|
{
|
|
|
|
return _midPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-25 04:25:42 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// SENSITIVITY
|
|
|
|
//
|
2022-12-02 04:37:37 -05:00
|
|
|
void HALL::setSensitivity(float sensitivity)
|
|
|
|
{
|
2023-09-20 13:16:07 -04:00
|
|
|
_GaussmV = 1.0 / sensitivity;
|
2022-12-02 04:37:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float HALL::getSensitivity()
|
|
|
|
{
|
2023-09-20 13:16:07 -04:00
|
|
|
return 1.0 / _GaussmV;
|
2022-12-02 04:37:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// READ
|
|
|
|
//
|
|
|
|
float HALL::raw(uint8_t times)
|
|
|
|
{
|
|
|
|
float sum = 0;
|
|
|
|
if (times == 0) times = 1;
|
|
|
|
for (int i = 0; i < times; i++)
|
|
|
|
{
|
2023-04-25 04:25:42 -04:00
|
|
|
// yield();
|
2022-12-02 04:37:37 -05:00
|
|
|
sum += analogRead(_pin);
|
|
|
|
}
|
|
|
|
if (times > 1) sum /= times;
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float HALL::read(uint8_t times)
|
|
|
|
{
|
|
|
|
float milliVolts = (raw(times) - _midPoint) * _mVStep;
|
|
|
|
_prevGauss = _lastGauss;
|
2023-09-20 13:16:07 -04:00
|
|
|
_lastGauss = milliVolts * _GaussmV;
|
2022-12-02 04:37:37 -05:00
|
|
|
return _lastGauss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-20 13:16:07 -04:00
|
|
|
float HALL::determineNoise(uint8_t times)
|
|
|
|
{
|
|
|
|
uint16_t ma = 0;
|
|
|
|
uint16_t mi = 65535;
|
|
|
|
if (times < 2) times = 2;
|
|
|
|
for (uint8_t i = 0; i < times; i++)
|
|
|
|
{
|
|
|
|
uint16_t r = analogRead(_pin);
|
|
|
|
if (r < mi) mi = r;
|
|
|
|
if (r > ma) ma = r;
|
|
|
|
}
|
|
|
|
return (ma - mi) * _mVStep * _GaussmV;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 04:37:37 -05:00
|
|
|
float HALL::readExt(float raw)
|
|
|
|
{
|
|
|
|
float milliVolts = (raw - _midPoint) * _mVStep;
|
|
|
|
_prevGauss = _lastGauss;
|
2023-09-20 13:16:07 -04:00
|
|
|
_lastGauss = milliVolts * _GaussmV;
|
2022-12-02 04:37:37 -05:00
|
|
|
return _lastGauss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ANALYSE
|
|
|
|
//
|
2023-04-25 04:25:42 -04:00
|
|
|
bool HALL::isNull()
|
|
|
|
{
|
|
|
|
return (_lastGauss == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 06:52:49 -05:00
|
|
|
bool HALL::isNorth()
|
2022-12-02 04:37:37 -05:00
|
|
|
{
|
|
|
|
return (_lastGauss > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 06:52:49 -05:00
|
|
|
bool HALL::isSouth()
|
2022-12-02 04:37:37 -05:00
|
|
|
{
|
|
|
|
return (_lastGauss < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-20 13:16:07 -04:00
|
|
|
bool HALL::isRising()
|
|
|
|
{
|
|
|
|
return _lastGauss > _prevGauss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool HALL::isFalling()
|
|
|
|
{
|
|
|
|
return _lastGauss < _prevGauss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 04:37:37 -05:00
|
|
|
float HALL::lastGauss()
|
|
|
|
{
|
|
|
|
return _lastGauss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float HALL::prevGauss()
|
|
|
|
{
|
|
|
|
return _prevGauss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-20 13:16:07 -04:00
|
|
|
float HALL::deltaGauss()
|
|
|
|
{
|
|
|
|
return _lastGauss - _prevGauss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float HALL::angle()
|
|
|
|
{
|
|
|
|
return atan2(_prevGauss, _lastGauss);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-25 04:25:42 -04:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// CONVERTORS
|
|
|
|
//
|
2022-12-02 04:37:37 -05:00
|
|
|
float HALL::Tesla(float Gauss)
|
|
|
|
{
|
|
|
|
return Gauss * 0.0001;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float HALL::mTesla(float Gauss)
|
|
|
|
{
|
|
|
|
return Gauss * 0.1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float HALL::uTesla(float Gauss)
|
|
|
|
{
|
|
|
|
return Gauss * 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 06:52:49 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2023-04-25 04:25:42 -04:00
|
|
|
// SATURATION LEVEL
|
2022-12-02 06:52:49 -05:00
|
|
|
//
|
2023-04-25 04:25:42 -04:00
|
|
|
void HALL::setMaxGauss(float maxGauss)
|
2022-12-02 06:52:49 -05:00
|
|
|
{
|
2023-04-25 04:25:42 -04:00
|
|
|
_maxGauss = abs(maxGauss);
|
2022-12-02 06:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float HALL::getMaxGauss()
|
|
|
|
{
|
|
|
|
return _maxGauss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool HALL::isSaturated()
|
|
|
|
{
|
|
|
|
return (abs(_lastGauss) >= _maxGauss);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-25 04:25:42 -04:00
|
|
|
float HALL::saturationLevel()
|
|
|
|
{
|
|
|
|
return 100.0 * abs(_lastGauss / _maxGauss);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 04:37:37 -05:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// DERIVED
|
|
|
|
//
|
|
|
|
A1301::A1301(uint8_t pin) : HALL(pin)
|
|
|
|
{
|
2023-09-20 13:16:07 -04:00
|
|
|
_GaussmV = 1.0 / 2.5;
|
2022-12-02 04:37:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
A1302::A1302(uint8_t pin) : HALL(pin)
|
|
|
|
{
|
2023-09-20 13:16:07 -04:00
|
|
|
_GaussmV = 1.0 / 1.3;
|
2022-12-02 04:37:37 -05:00
|
|
|
}
|
|
|
|
|
2022-12-02 06:52:49 -05:00
|
|
|
A1324::A1324(uint8_t pin) : HALL(pin)
|
|
|
|
{
|
2023-09-20 13:16:07 -04:00
|
|
|
_GaussmV = 1.0 / 5.0;
|
2022-12-02 06:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
A1325::A1325(uint8_t pin) : HALL(pin)
|
|
|
|
{
|
2023-09-20 13:16:07 -04:00
|
|
|
_GaussmV = 1.0 / 3.125;
|
2022-12-02 06:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
A1326::A1326(uint8_t pin) : HALL(pin)
|
|
|
|
{
|
2023-09-20 13:16:07 -04:00
|
|
|
_GaussmV = 1.0 / 2.5;
|
2022-12-02 06:52:49 -05:00
|
|
|
}
|
|
|
|
|
2022-12-02 04:37:37 -05:00
|
|
|
|
|
|
|
// -- END OF FILE --
|
|
|
|
|