-
Notifications
You must be signed in to change notification settings - Fork 153
Enable Set/Reset (write 0x29 to Z_MSB) #192
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
Closed
Closed
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
49aef65
Add files via upload
hussam-qawaqzeh 07ea954
Add 'QMC5883P' to device choices in MagDevice
hussam-qawaqzeh 43a758a
Add MAG_QMC5883P to MagDevice.h enum
hussam-qawaqzeh 1eb1931
Add support for MagQMC5338P device detection
hussam-qawaqzeh 0505cd4
Add QMC5883P to supported magnetometers list
hussam-qawaqzeh 8d0e721
Update MagDevice.cpp
hussam-qawaqzeh a0dd25c
BusDevice.cpp
hussam-qawaqzeh 96c18e8
Update MagQMC5338P.h
hussam-qawaqzeh 7d77190
Update Hardware.cpp
hussam-qawaqzeh 56dfbc7
Merge branch 'rtlopez:master' into master
hussam-qawaqzeh a2247f2
Update MagQMC5338P.h
hussam-qawaqzeh 32501f8
Update MagQMC5338P.h
hussam-qawaqzeh 7c6abd5
Update MagQMC5338L.h
hussam-qawaqzeh acc7ef9
Update Hardware.cpp
hussam-qawaqzeh d90cada
Update MagQMC5338L.h
hussam-qawaqzeh 1c84555
Update MagQMC5338P.h
hussam-qawaqzeh ac005fd
remove UTF symbols from code
hussam-qawaqzeh 22858a2
Update MagQMC5338P.h
hussam-qawaqzeh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,9 @@ | |
| #include "MagDevice.h" | ||
| #include "BusDevice.h" | ||
|
|
||
| // address for QMC5883P | ||
| #define QMC5883P_ADDRESS 0x2C | ||
| #define QMC5883P_DEFAULT_ADDRESS 0x2C | ||
|
|
||
| // QMC5883P records according to Adafruit specifications | ||
| #define QMC5883P_REG_CHIPID 0x00 | ||
| #define QMC5883P_REG_XOUT_LSB 0x01 | ||
| #define QMC5883P_REG_XOUT_MSB 0x02 | ||
|
|
@@ -20,14 +18,12 @@ | |
| #define QMC5883P_REG_CONTROL1 0x0A | ||
| #define QMC5883P_REG_CONTROL2 0x0B | ||
|
|
||
| // Range values (as defined in Adafruit lib) | ||
| #define QMC5883P_RANGE_30G 0x00 | ||
| #define QMC5883P_RANGE_12G 0x01 | ||
| #define QMC5883P_RANGE_8G 0x02 | ||
| #define QMC5883P_RANGE_2G 0x03 | ||
|
|
||
| // Mode values | ||
| #define QMC5883P_MODE_CONTINUOUS 0x03 // Continuous measurement mode | ||
| #define QMC5883P_MODE_CONTINUOUS 0x03 | ||
|
|
||
| namespace Espfc { | ||
| namespace Device { | ||
|
|
@@ -45,18 +41,22 @@ class MagQMC5338P : public MagDevice | |
|
|
||
| if (!testConnection()) return 0; | ||
|
|
||
| // We use ±8G (0x02) as the default range for the drone. | ||
| _currentRange = QMC5883P_RANGE_8G; | ||
| setMode(_currentRange); | ||
| // 🔑 Step 1: Enable Set/Reset (write 0x29 to Z_MSB) | ||
| _bus->writeByte(_addr, QMC5883P_REG_ZOUT_MSB, 0x29); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // Continuous operating mode | ||
| uint8_t ctrl1 = (QMC5883P_MODE_CONTINUOUS) | // bits [1:0] | ||
| (0x02 << 2) | // ODR = 100Hz (0x02) | ||
| (0x03 << 4) | // OSR = 1 (0x03) ← faster | ||
| (0x00 << 6); // DSR = 1 | ||
| // 🔑 Step 2: Set Range in CONTROL2[3:2] (Adafruit style) | ||
| _currentRange = QMC5883P_RANGE_8G; | ||
| setMode(_currentRange); // This will write to CONTROL2 | ||
|
|
||
| // 🔑 Step 3: Configure CONTROL1 | ||
| uint8_t ctrl1 = | ||
| QMC5883P_MODE_CONTINUOUS | // [1:0] | ||
| (0x02 << 2) | // ODR = 100Hz [3:2] | ||
| (0x03 << 4) | // OSR = 1 [5:4] | ||
| (0x00 << 6); // DSR = 1 [7:6] | ||
| _bus->writeByte(_addr, QMC5883P_REG_CONTROL1, ctrl1); | ||
|
|
||
| // Initial reading | ||
| // Initial read | ||
| uint8_t buffer[6]; | ||
| _bus->read(_addr, QMC5883P_REG_XOUT_LSB, 6, buffer); | ||
|
|
||
|
|
@@ -69,7 +69,7 @@ class MagQMC5338P : public MagDevice | |
| return 0; | ||
| } | ||
|
|
||
| // in QMC5883P: X = [MSB=0x02, LSB=0x01] → buffer[1], buffer[0] | ||
| // 🔑 Read raw values EXACTLY like Adafruit (no sign flip yet) | ||
| v.x = (int16_t)((buffer[1] << 8) | buffer[0]); | ||
| v.y = (int16_t)((buffer[3] << 8) | buffer[2]); | ||
| v.z = (int16_t)((buffer[5] << 8) | buffer[4]); | ||
|
|
@@ -78,29 +78,31 @@ class MagQMC5338P : public MagDevice | |
| } | ||
|
|
||
| const VectorFloat convert(const VectorInt16& v) const override { | ||
| // 🔑 Use Adafruit's conversion factors (based on actual range) | ||
| float lsb_per_gauss; | ||
| switch (_currentRange) { | ||
| case QMC5883P_RANGE_30G: lsb_per_gauss = 1000.0f; break; | ||
| case QMC5883P_RANGE_12G: lsb_per_gauss = 2500.0f; break; | ||
| case QMC5883P_RANGE_8G: lsb_per_gauss = 3750.0f; break; | ||
| case QMC5883P_RANGE_2G: lsb_per_gauss = 15000.0f; break; | ||
| default: lsb_per_gauss = 3750.0f; // fallback to 8G | ||
| case QMC5883P_RANGE_30G: lsb_per_gauss = 1000.0f; break; | ||
| case QMC5883P_RANGE_12G: lsb_per_gauss = 2500.0f; break; | ||
| case QMC5883P_RANGE_8G: lsb_per_gauss = 3750.0f; break; | ||
| case QMC5883P_RANGE_2G: lsb_per_gauss = 15000.0f; break; | ||
| default: lsb_per_gauss = 3750.0f; | ||
| } | ||
| float scale = 1.0f / lsb_per_gauss; | ||
| return VectorFloat{ v.x * scale, v.y * scale, v.z * scale }; | ||
| } | ||
|
|
||
| int getRate() const override { | ||
| return 100; // Based on ODR = 100Hz | ||
| return 100; | ||
| } | ||
|
|
||
| virtual MagDeviceType getType() const override { | ||
| return MAG_QMC5883P; | ||
| } | ||
|
|
||
| // 🔑 CORRECT setMode: writes range to CONTROL2[3:2] | ||
| void setMode(uint8_t range) { | ||
| _currentRange = range; | ||
| // Set the bits to [3:2] in CONTROL2 | ||
| // Range is in bits [3:2] of CONTROL2 | ||
| uint8_t ctrl2 = (range << 2); | ||
| _bus->writeByte(_addr, QMC5883P_REG_CONTROL2, ctrl2); | ||
| } | ||
|
|
@@ -121,3 +123,5 @@ class MagQMC5338P : public MagDevice | |
| } | ||
|
|
||
| #endif | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plase remove UTF symbols from code.