Skip to content

Commit

Permalink
fix self test functionality (it's a good idea to read the datasheet)
Browse files Browse the repository at this point in the history
  • Loading branch information
robosam2003 committed Mar 11, 2023
1 parent 064e8d4 commit 1f5f2c6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 41 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test_framework = unity
;lib_deps = throwtheswitch/unity
extra_scripts = extra_script.py
monitor_speed = 115200
test_port = COM14
test_port = COM18

;required to make the units test behave
test_build_src = true
Expand Down
57 changes: 23 additions & 34 deletions src/LIS3MDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,59 +118,48 @@ namespace LIS3MDL {
delay(20); // wait 20ms
device->write_reg(REGISTER::CTRL_REG3, 0X00);

delay(20);
// Power up, check the ZYXDA bit in the STATUS_REG register
while(!get_mag_drdy_status());
get_raw_mag(); // read the OUT regs and discard the data
Vector<double , 3> discard = get_mag(); // read the OUT regs and discard the data

// Average 5 samples
Vector<int32_t , 3> average_NOST = {0, 0, 0};
for (int i = 0; i < 5; i++) {
Vector<double , 3> average_NOST = {0, 0, 0};
for (int i = 0; i < 20; i++) {
while(!get_mag_drdy_status());
Vector<int16_t, 3> raw = get_raw_mag();
average_NOST += (Vector<int32_t, 3>) raw; // sum the values
Vector<double, 3> raw = get_mag();
average_NOST += raw; // sum the values
delay(20);
}
average_NOST /= 5; // divide by 5 to get the average
average_NOST /= 20; // divide by 5 to get the average

// Enable self test
device->write_reg(REGISTER::CTRL_REG1, 0X1D);
delay(60); // wait 60ms

// Power up, check the ZYXDA bit in the STATUS_REG register
while(!get_mag_drdy_status());
get_raw_mag(); // read the OUT regs and discard the data
Vector<double , 3> discard2 = get_mag(); // read the OUT regs and discard the data

Vector<int16_t, 3> minST = {0, 0, 0};
Vector<int16_t, 3> maxST = {0, 0, 0};
Vector<int32_t, 3> average_ST = {0, 0, 0};

Vector<double, 3> average_ST = {0, 0, 0};
// Take 5 samples
Vector<int16_t, 3> raw;
for (int i = 0; i < 5; i++) {
Vector<double, 3> raw;
for (int i = 0; i < 20; i++) {
while(!get_mag_drdy_status());
raw = get_raw_mag();
average_ST += (Vector<int32_t, 3>) raw; // sum the values

// find the min and max values of each axis
for (int j = 0; j < 3; j++) {
if (raw[j] < minST[j]) {
minST[j] = raw[j];
}
if (raw[j] > maxST[j]) {
maxST[j] = raw[j];
}
}
raw = get_mag();
average_ST += raw; // sum the values

delay(20);
}
average_ST /= 5; // divide by 5 to get the average
average_ST /= 20; // divide by 5 to get the average

// Disable self test
device->write_reg(REGISTER::CTRL_REG1, 0X1C);

// Print out all the min, max, average and raw values
Serial.println("Min, Max, Average_NOST, Average_ST, Raw");
Serial.println("Average_NOST, Average_ST, Raw");
for (int i = 0; i < 3; i++) {
Serial.print(minST[i]);
Serial.print(", ");
Serial.print(maxST[i]);
Serial.print(", ");
Serial.print(average_NOST[i]);
Serial.print(", ");
Serial.print(average_ST[i]);
Expand All @@ -183,13 +172,13 @@ namespace LIS3MDL {
// pass &= (abs(minST[i]) <= (raw[i] - average_NOST[i])) &&
// ((raw[i] - average_NOST[i]) <= abs(maxST[i]));
// }
if ( (abs(minST[0]) <= (average_ST[0] - average_NOST[0])) && ((average_ST[0] - average_NOST[0]) <= abs(maxST[0])) &&
(abs(minST[1]) <= (average_ST[1] - average_NOST[1])) && ((average_ST[1] - average_NOST[1]) <= abs(maxST[1]))
// (abs(minST[2]) <= (average_ST[2] - average_NOST[2])) && ((average_ST[2] - average_NOST[2]) <= abs(maxST[2]))
if ( (abs(1.0) <= abs(average_ST[0] - average_NOST[0])) && (abs(average_ST[0] - average_NOST[0]) <= abs(3.0)) && // limits fpr
(abs(1.0) <= abs(average_ST[1] - average_NOST[1])) && (abs(average_ST[1] - average_NOST[1]) <= abs(3.0)) &&
(abs(0.1) <= abs(average_ST[2] - average_NOST[2])) && (abs(average_ST[2] - average_NOST[2]) <= abs(1.0))
) {
return true;
}
else{
else {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/LIS3MDL.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ namespace LIS3MDL {
static void interrupt_service_routine();

/**
* @brief A defualt configuration for the magnetometer - use always call this in your setup.
* @brief A default configuration for the magnetometer - use always call this in your setup.
*/
void default_configuration();

Expand Down
37 changes: 32 additions & 5 deletions test/test_LIS3MDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <unity.h>
#include "LIS3MDL.h"

#define LIS_CS 37
#define LIS_CS 10
#define CS_LSM 40
#define LIS_INT 39
#define LIS_INT 32
LIS3MDL::LIS3MDL LISTest(LIS_CS, SPI, 4000000);

void setUp(void) {
Expand All @@ -31,18 +31,18 @@ void test_who_am_i(void) {

void test_get_mag(void) {
Vector<double, 3> mag = LISTest.get_mag();
TEST_ASSERT(mag.norm() != 0);
TEST_ASSERT(mag.norm() != 0); // The magnetic field is not very likely be zero
}

void test_get_temp_celsius(void) {
double temp = LISTest.get_temp_celsius();
TEST_ASSERT(temp != 0);
TEST_ASSERT(temp != 0); // temperature is not very likely to be zero
}

void test_get_raw_mag(void) {
// Set ODR to low so that the two reads are probably the same.
LISTest.set_mag_ODR(LIS3MDL::OUTPUT_DATA_RATES::ODR_155_HZ);
// read twice in quick succession
// read twice in quick succession, they should be equal
Vector<double, 3> mag_d = LISTest.get_mag();
Vector<int16_t, 3> mag = LISTest.get_raw_mag();
Vector<double, 3> mag_conv = LISTest.convert_raw_mag_to_gauss(mag);
Expand All @@ -52,6 +52,32 @@ void test_get_raw_mag(void) {
LISTest.set_mag_ODR(LIS3MDL::OUTPUT_DATA_RATES::ODR_1000_HZ);

}
// Commenting out because interrupts dont work that way.
//#ifdef LIS_INT
//void test_interrupt(void) {
// LISTest.get_mag();
// int initial = digitalRead(LIS_INT);
//
// LISTest.set_interrupt_active_high(true);
// // set the interrupts on the axis
// LISTest.set_interrupt_on_axis(LIS3MDL::AXIS::X_AXIS, true);
// LISTest.set_interrupt_on_axis(LIS3MDL::AXIS::Y_AXIS, true);
// LISTest.set_interrupt_on_axis(LIS3MDL::AXIS::Z_AXIS, true);
//
// LISTest.set_interrupt_threshold(0.1);
// delay(10);
// int final = digitalRead(LIS_INT);
//
// LISTest.set_interrupt_on_axis(LIS3MDL::AXIS::X_AXIS, false);
// LISTest.set_interrupt_on_axis(LIS3MDL::AXIS::Y_AXIS, false);
// LISTest.set_interrupt_on_axis(LIS3MDL::AXIS::Z_AXIS, false);
// LISTest.get_mag();
// TEST_ASSERT_EQUAL(initial, 0);
// TEST_ASSERT_EQUAL(final, 1);
//}
//
//#endif


// Self test
void test_self_test(void) {
Expand All @@ -64,6 +90,7 @@ int main(int argc, char **argv) {
RUN_TEST(test_get_mag);
RUN_TEST(test_get_temp_celsius);
RUN_TEST(test_get_raw_mag);

RUN_TEST(test_self_test);
UNITY_END();

Expand Down

0 comments on commit 1f5f2c6

Please sign in to comment.