From 9acd8a0903c776491377dc2c26e6dac7d1a9afcc Mon Sep 17 00:00:00 2001 From: mprograms <44033294+mprograms@users.noreply.github.com> Date: Thu, 29 Aug 2019 13:22:31 -0400 Subject: [PATCH] Updates & Fixes --- changelog.md | 30 +++++++++++++ examples/bearing/bearing.ino | 3 +- examples/direction/direction.ino | 4 +- readme.md | 73 ++++++++++++++++++-------------- src/QMC5883LCompass.cpp | 29 +++++++------ src/QMC5883LCompass.h | 8 ++-- 6 files changed, 96 insertions(+), 51 deletions(-) create mode 100644 changelog.md diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..daf4694 --- /dev/null +++ b/changelog.md @@ -0,0 +1,30 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [v1.0.1] +### Changed +- Modified the way getBearing() and getDirection() worked. Both functions now require that the azimuth be passed to them as a parameter. +- Updated readme.md to reflect the above change. +- Updated /examples/bearing/bearing.ino to reflect above change. +- Updated /examples/direction/direction.ino to reflect above change. + +### Fixed +- Corrections made to readme.md +- Renamed smoothing() to _smoothing() since this is a private function. +- Renamed writeReg() to _writeReg() since this is a private function. + +## [v1.0.0] +### Added +- Documentation to readme.md +- Initial commit. + +## [v0.0.2] +### Added +- Added getBearing() and getDirection() functions. + +## [v0.0.1] +### Added +- Initial creation. \ No newline at end of file diff --git a/examples/bearing/bearing.ino b/examples/bearing/bearing.ino index df77c38..8a13ed9 100644 --- a/examples/bearing/bearing.ino +++ b/examples/bearing/bearing.ino @@ -23,8 +23,9 @@ void setup() { void loop() { compass.read(); + byte a = compass.getAzimuth(); // Output here will be a value from 0 - 15 based on the direction of the bearing / azimuth. - byte b = compass.getBearing(); + byte b = compass.getBearing(a); Serial.print("B: "); Serial.print(b); diff --git a/examples/direction/direction.ino b/examples/direction/direction.ino index 16ee59f..ccf1181 100644 --- a/examples/direction/direction.ino +++ b/examples/direction/direction.ino @@ -21,9 +21,11 @@ void setup() { void loop() { compass.read(); + + byte a = compass.getAzimuth(); char myArray[3]; - compass.getDirection(myArray); + compass.getDirection(myArray, a); Serial.print(myArray[0]); Serial.print(myArray[1]); diff --git a/readme.md b/readme.md index da2f76c..d073915 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,6 @@ # QMC5883L Compass Arduino Library -by [MRPrograms](https://github.com/mprograms/QMC5883LCompass/) +by [MPrograms](https://github.com/mprograms/QMC5883LCompass/) | [Github Project Page](https://github.com/mprograms/QMC5883LCompass/) @@ -68,9 +68,9 @@ To get the X, Y, or Z sensor readings, simply call the desired function. ``` void loop(){ - int x = getX(); - int y = getY(); - int z = getZ(); + int x = compass.getX(); + int y = compass.getY(); + int z = compass.getZ(); } ``` @@ -79,35 +79,40 @@ To get the calculated azimuth (compass degree) value, simply call `getAzimuth(); ``` void loop(){ - int a = getAzimuth(); + int a = compass.getAzimuth(azimuth); } ``` #### Getting Direction / Bearings QMC5883L Compass Library calculates the direction range and direction in which the sensor is pointing. There are two functions you can call. -To get a 16 point value of the direction the sensor is facing you can call `getBearing()`. This will divide the 360 range of the compass into 16 parts and return a value of 0-15 in clockwise order. In this case 0 = N, 4 = E, 8 = S, 12 = W. This function is helpful if you wish to roll your own direction output function without the need for calculations. +To get a 16 point value of the direction the sensor is facing you can call `getBearing(azimuth)`. This will divide the 360 range of the compass into 16 parts and return a value of 0-15 in clockwise order. In this case 0 = N, 4 = E, 8 = S, 12 = W. This function is helpful if you wish to roll your own direction output function without the need for calculations. ``` void loop(){ - byte b = getBearing(); + azimuth = compass.getAzimuth(); + byte b = compass.getBearing(azimuth); } ``` -To get a 16 point text representation of the direction the sensor is facing you can call `getDirection();`. This will produce a char array[3] with letters representing each direction. Because we can't return an array we need to pass the values by reference. +To get a 16 point text representation of the direction the sensor is facing you can call `getDirection(azimuth);`. This will produce a char array[3] with letters representing each direction. Because we can't return an array we need to pass the values by reference. ``` void loop(){ + azimuth = compass.getAzimuth(); char myArray[3]; - getDirection(myArray) + getDirection(myArray, azimuth); } ``` If you want to print these values you can do so like this: ``` void loop(){ + azimuth = compass.getAzimuth(); char myArray[3]; - getDirection(myArray) + + getDirection(myArray, azimuth); + Serial.print(myArray[0]); Serial.print(myArray[1]); Serial.print(myArray[2]); @@ -144,9 +149,9 @@ void loop() { a = compass.getAzimuth(); - b = compass.getBearing(); + b = compass.getBearing(a); - compass.getDirection(myArray); + compass.getDirection(myArray, a); Serial.print("X: "); @@ -189,7 +194,7 @@ X: 1005 Y: -147 Z: 1281 Azimuth: 352 Bearing: 15 Direction: NNW ### Changing Chip Settings -The QMC5583L chip provides several different settings you can select. You can change a setting like so: +The QMC5583L chip provides several different settings you can select. #### Change I2C Address @@ -208,25 +213,29 @@ You can also change the mode, sensitivity, sample rate and output rate of the QM The values to set each mode are in the table below and were taken from the [QST QMC5583L datasheet](https://nettigo.pl/attachments/440). -MODE CONTROL (MODE) -Standby | 0x00 -Continuous | 0x01 - -OUTPUT DATA RATE (ODR) -10Hz | 0x00 -50Hz | 0x04 -100Hz | 0x08 -200Hz | 0x0C - -FULL SCALE (RNG) -2G | 0x00 -8G | 0x10 - -OVER SAMPLE RATIO (OSR) -64 | 0xC0 -128 | 0x80 -256 | 0x40 -512 | 0x00 +| MODE CONTROL (MODE) | Value | +| ----------------------- | ----- | +| Standby | 0x00 | +| Continuous | 0x01 | + +| OUTPUT DATA RATE (ODR) | Value | +| ----------------------- | ----- | +| 10Hz | 0x00 | +| 50Hz | 0x04 | +| 100Hz | 0x08 | +| 200Hz | 0x0C | + +| FULL SCALE (RNG) | Value | +| ----------------------- | ----- | +| 2G | 0x00 | +| 8G | 0x10 | + +| OVER SAMPLE RATIO (OSR) | Value | +| ----------------------- | ----- | +| 64 | 0xC0 | +| 128 | 0x80 | +| 256 | 0x40 | +| 512 | 0x00 | --- ## Smoothing Sensor Output diff --git a/src/QMC5883LCompass.cpp b/src/QMC5883LCompass.cpp index 3ab8461..f895a12 100644 --- a/src/QMC5883LCompass.cpp +++ b/src/QMC5883LCompass.cpp @@ -67,7 +67,8 @@ QMC5883LCompass::QMC5883LCompass() { @since v0.1; **/ void QMC5883LCompass::init(){ - writeReg(0x0B,0x01); + Wire.begin(); + _writeReg(0x0B,0x01); setMode(0x01,0x0C,0x10,0X00); } @@ -93,7 +94,7 @@ void QMC5883LCompass::setADDR(byte b){ @since v0.1; **/ // Write register values to chip -void QMC5883LCompass::writeReg(byte r, byte v){ +void QMC5883LCompass::_writeReg(byte r, byte v){ Wire.beginTransmission(_ADDR); Wire.write(r); Wire.write(v); @@ -109,7 +110,7 @@ void QMC5883LCompass::writeReg(byte r, byte v){ **/ // Set chip mode void QMC5883LCompass::setMode(byte mode, byte odr, byte rng, byte osr){ - writeReg(0x09,mode|odr|rng|osr); + _writeReg(0x09,mode|odr|rng|osr); } @@ -121,7 +122,7 @@ void QMC5883LCompass::setMode(byte mode, byte odr, byte rng, byte osr){ **/ // Reset the chip void QMC5883LCompass::setReset(){ - writeReg(0x0A,0x80); + _writeReg(0x0A,0x80); } // 1 = Basic 2 = Advanced @@ -149,7 +150,7 @@ void QMC5883LCompass::read(){ _vRaw[2] = (int)(int16_t)(Wire.read() | Wire.read() << 8); if ( _smoothUse ) { - smoothing(); + _smoothing(); } byte overflow = Wire.read() & 0x02; @@ -176,7 +177,7 @@ void QMC5883LCompass::read(){ @since v0.3; **/ -void QMC5883LCompass::smoothing(){ +void QMC5883LCompass::_smoothing(){ byte max = 0; byte min = 0; @@ -264,12 +265,13 @@ int QMC5883LCompass::getAzimuth(){ Divide the 360 degree circle into 16 equal parts and then return the a value of 0-15 based on where the azimuth is currently pointing. - @since v0.2 + @since v1.0.1 - function now requires azimuth parameter. + @since v0.2.0 - initial creation @return byte direction of bearing */ -byte QMC5883LCompass::getBearing(){ - unsigned long a = getAzimuth() / 22.5; +byte QMC5883LCompass::getBearing(int azimuth){ + unsigned long a = azimuth / 22.5; unsigned long r = a - (int)a; byte sexdec = 0; sexdec = ( r >= .5 ) ? ceil(a) : floor(a); @@ -289,7 +291,7 @@ byte QMC5883LCompass::getBearing(){ ( if direction is in 1 / NNE) char myArray[3]; - compass.getBearing(myArray); + compass.getDirection(myArray, azimuth); Serial.print(myArray[0]); // N Serial.print(myArray[1]); // N @@ -298,10 +300,11 @@ byte QMC5883LCompass::getBearing(){ @see getBearing(); - @since v0.2 + @since v1.0.1 - function now requires azimuth parameter. + @since v0.2.0 - initial creation */ -void QMC5883LCompass::getDirection(char* myArray){ - int d = getBearing(); +void QMC5883LCompass::getDirection(char* myArray, int azimuth){ + int d = getBearing(azimuth); myArray[0] = _bearings[d][0]; myArray[1] = _bearings[d][1]; myArray[2] = _bearings[d][2]; diff --git a/src/QMC5883LCompass.h b/src/QMC5883LCompass.h index cd9e50d..b66f2e7 100644 --- a/src/QMC5883LCompass.h +++ b/src/QMC5883LCompass.h @@ -19,11 +19,11 @@ class QMC5883LCompass{ int getY(); int getZ(); int getAzimuth(); - byte getBearing(); - void getDirection(char* myArray); + byte getBearing(int azimuth); + void getDirection(char* myArray, int azimuth); private: - void writeReg(byte reg,byte val); + void _writeReg(byte reg,byte val); bool _smoothUse = false; byte _smoothSteps = 5; bool _smoothAdvanced = false; @@ -33,7 +33,7 @@ class QMC5883LCompass{ int _vScan = 0; long _vTotals[3] = {0,0,0}; int _vSmooth[3] = {0,0,0}; - void smoothing(); + void _smoothing(); const char _bearings[16][3] = { {' ', ' ', 'N'}, {'N', 'N', 'E'},