Skip to content

Commit

Permalink
Updates & Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mprograms committed Aug 29, 2019
1 parent b130060 commit 9acd8a0
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 51 deletions.
30 changes: 30 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 2 additions & 1 deletion examples/bearing/bearing.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion examples/direction/direction.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
73 changes: 41 additions & 32 deletions readme.md
Original file line number Diff line number Diff line change
@@ -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/)

Expand Down Expand Up @@ -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();
}
```

Expand All @@ -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]);
Expand Down Expand Up @@ -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: ");
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
29 changes: 16 additions & 13 deletions src/QMC5883LCompass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
}


Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -176,7 +177,7 @@ void QMC5883LCompass::read(){
@since v0.3;
**/
void QMC5883LCompass::smoothing(){
void QMC5883LCompass::_smoothing(){
byte max = 0;
byte min = 0;

Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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];
Expand Down
8 changes: 4 additions & 4 deletions src/QMC5883LCompass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'},
Expand Down

0 comments on commit 9acd8a0

Please sign in to comment.