-
Notifications
You must be signed in to change notification settings - Fork 0
Magnetometer driver #176
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
base: main
Are you sure you want to change the base?
Magnetometer driver #176
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,27 +1,29 @@ | ||
| #include <Adafruit_LIS3MDL.h> | ||
| #include <SparkFun_MMC5983MA_Arduino_Library.h> | ||
|
|
||
| #include "sensors.h" | ||
| #include "hal.h" | ||
|
|
||
| Adafruit_LIS3MDL LIS3MDL; // global static instance of the sensor | ||
| SFE_MMC5983MA MMC5983; // global static instance of the sensor | ||
|
|
||
| ErrorCode MagnetometerSensor::init() { | ||
| if (!LIS3MDL.begin_SPI(LIS3MDL_CS)) { // Checks if sensor is connected | ||
| if (MMC5983.begin(MMC5983_CS) == false) { // Checks if sensor is connected | ||
|
Contributor
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. Any reason for this change? (Specifically the |
||
| return ErrorCode::MagnetometerCouldNotBeInitialized; | ||
| } | ||
| LIS3MDL.setOperationMode(LIS3MDL_CONTINUOUSMODE); // Reading continuously, instead of single-shot or off | ||
| LIS3MDL.setDataRate(LIS3MDL_DATARATE_155_HZ); | ||
| LIS3MDL.setRange(LIS3MDL_RANGE_4_GAUSS); // Earth's magnetic field is 1/2 gauss, can detect high current | ||
| return ErrorCode::NoError; | ||
| } | ||
|
|
||
| Magnetometer MagnetometerSensor::read() { | ||
| // read from aforementioned global instance of sensor | ||
| LIS3MDL.read(); | ||
| uint32_t cx, cy, cz; | ||
| double X, Y, Z; | ||
|
|
||
| float mx = LIS3MDL.x_gauss; | ||
| float my = LIS3MDL.y_gauss; | ||
| float mz = LIS3MDL.z_gauss; | ||
| Magnetometer reading{mx, my, mz}; | ||
| MMC5983.getMeasurementXYZ(&cx, &cy, &cz); | ||
|
|
||
| double sf = (double)(1 << 17); | ||
| X = ((double)cx - sf)/sf; | ||
| Y = ((double)cy - sf)/sf; | ||
| Z = ((double)cz - sf)/sf; | ||
|
|
||
| Magnetometer reading{X, Y, Z}; | ||
|
Contributor
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. I'm going to assume for this review that this is from the datasheet, but it does look like "magic math" to me... Could you add a doc comment? |
||
| return reading; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -166,9 +166,9 @@ struct GPS { | |
| * @brief data from the magnetometer | ||
| */ | ||
| struct Magnetometer { | ||
| float mx; | ||
| float my; | ||
| float mz; | ||
| double mx; | ||
| double my; | ||
| double mz; | ||
|
Comment on lines
+169
to
+171
Contributor
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. Discussed this, I think its OK. I want a second opinion though @MuhammadAli8209 @tjmcmanamen38 Mag isn't buffered to my knowledge, so worst case this just doubles the mag log size from 12 --> 24, which isnt world ending. |
||
| }; | ||
|
|
||
| struct Quaternion { | ||
|
|
@@ -275,4 +275,4 @@ struct PyroState { | |
| struct CameraData { | ||
| uint8_t camera_state = 255; | ||
| float camera_voltage = 0; | ||
| }; | ||
| }; | ||
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.
OK for now, maybe let's clean it up later.
Context (@MuhammadAli8209 ): Adafruit sensor libraries bring in a bunch of adafruit helper libraries that are needed for the BNO sensor. Since we're removing the BNO, we'll remove this dependency when we merge the LSM changes.
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.
lgtm 👍