Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added compassflip and compassoffset & fixed declination calculations when using negative values #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/QMC5883LCompass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ void QMC5883LCompass::setMode(byte mode, byte odr, byte rng, byte osr){
* then: setMagneticDeclination(-19, 43);
*/
void QMC5883LCompass::setMagneticDeclination(int degrees, uint8_t minutes) {
_magneticDeclinationDegrees = degrees + minutes / 60;
_magneticDeclinationDegrees = degrees < 0 ? 0 - (abs(degrees) + minutes / 60) : degrees + minutes / 60;
}


Expand Down Expand Up @@ -409,9 +409,18 @@ int QMC5883LCompass::_get(int i){
@return int azimuth
**/
int QMC5883LCompass::getAzimuth(){
read();
float heading = atan2( getY(), getX() ) * 180.0 / PI;
if (compassFlip){
heading = atan2(-getY(), getX()) * 180.0 / PI;;
}
heading += _magneticDeclinationDegrees;
return (int)heading % 360;
heading = heading - offsetDegrees;
// Ensure heading is in the range of 0 to 360 degrees
if (heading < 0) {
heading += 360;
}
return heading;
}


Expand Down
105 changes: 54 additions & 51 deletions src/QMC5883LCompass.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,66 @@ class QMC5883LCompass{

public:
QMC5883LCompass();
void init();
void init();
void setADDR(byte b);
void setMode(byte mode, byte odr, byte rng, byte osr);
void setMagneticDeclination(int degrees, uint8_t minutes);
void setSmoothing(byte steps, bool adv);
void calibrate();
void setCalibration(int x_min, int x_max, int y_min, int y_max, int z_min, int z_max);
void setCalibrationOffsets(float x_offset, float y_offset, float z_offset);
void setCalibrationScales(float x_scale, float y_scale, float z_scale);
void setMagneticDeclination(int degrees, uint8_t minutes);
void setSmoothing(byte steps, bool adv);
void calibrate();
void setCalibration(int x_min, int x_max, int y_min, int y_max, int z_min, int z_max);
void setCalibrationOffsets(float x_offset, float y_offset, float z_offset);
void setCalibrationScales(float x_scale, float y_scale, float z_scale);
float getCalibrationOffset(uint8_t index);
float getCalibrationScale(uint8_t index);
void clearCalibration();
void setReset();
float getCalibrationScale(uint8_t index);
void clearCalibration();
void setReset();
void read();
int getX();
int getY();
int getZ();
int getAzimuth();
byte getBearing(int azimuth);
void getDirection(char* myArray, int azimuth);

int getX();
int getY();
int getZ();
int getAzimuth();
byte getBearing(int azimuth);
void getDirection(char* myArray, int azimuth);
bool compassFlip = false;
int offsetDegrees = 0;

private:
void _writeReg(byte reg,byte val);
int _get(int index);
float _magneticDeclinationDegrees = 0;
bool _smoothUse = false;
byte _smoothSteps = 5;
bool _smoothAdvanced = false;
byte _ADDR = 0x0D;
int _vRaw[3] = {0,0,0};
int _vHistory[10][3];
int _vScan = 0;
long _vTotals[3] = {0,0,0};
int _vSmooth[3] = {0,0,0};
void _smoothing();
float _offset[3] = {0.,0.,0.};
float _scale[3] = {1.,1.,1.};
int _vCalibrated[3];
void _applyCalibration();
const char _bearings[16][3] = {
{' ', ' ', 'N'},
{'N', 'N', 'E'},
{' ', 'N', 'E'},
{'E', 'N', 'E'},
{' ', ' ', 'E'},
{'E', 'S', 'E'},
{' ', 'S', 'E'},
{'S', 'S', 'E'},
{' ', ' ', 'S'},
{'S', 'S', 'W'},
{' ', 'S', 'W'},
{'W', 'S', 'W'},
{' ', ' ', 'W'},
{'W', 'N', 'W'},
{' ', 'N', 'W'},
{'N', 'N', 'W'},
};
int _get(int index);
float _magneticDeclinationDegrees = 0;

bool _smoothUse = false;
byte _smoothSteps = 5;
bool _smoothAdvanced = false;
byte _ADDR = 0x0D;
int _vRaw[3] = {0,0,0};
int _vHistory[10][3];
int _vScan = 0;
long _vTotals[3] = {0,0,0};
int _vSmooth[3] = {0,0,0};
void _smoothing();
float _offset[3] = {0.,0.,0.};
float _scale[3] = {1.,1.,1.};
int _vCalibrated[3];
void _applyCalibration();
const char _bearings[16][3] = {
{' ', ' ', 'N'},
{'N', 'N', 'E'},
{' ', 'N', 'E'},
{'E', 'N', 'E'},
{' ', ' ', 'E'},
{'E', 'S', 'E'},
{' ', 'S', 'E'},
{'S', 'S', 'E'},
{' ', ' ', 'S'},
{'S', 'S', 'W'},
{' ', 'S', 'W'},
{'W', 'S', 'W'},
{' ', ' ', 'W'},
{'W', 'N', 'W'},
{' ', 'N', 'W'},
{'N', 'N', 'W'},
};



Expand Down