Skip to main content

Samotestiranje

Što ovaj primjer prikazuje​

Provjera rade li X i Y kanali senzora pomoću ugrađenog samotesta senzora BMM350. Senzor generira interno polje od oko 130 µT na jednoj osi istodobno i mjeri razliku. Kanal prolazi test kada je razlika najmanje 130 µT. Skica izvodi test jednom, u setup().

Primjer koda​

Select Board
selfTest.ino
/**
**************************************************
*
* @file selfTest.ino
*
* @brief Example showing how to run the BMM350's built-in self-test.
* The self-test generates an internal ~130 uT magnetic field on
* the X and Y channels and reports the resulting field
* difference; per the datasheet (section 5.1.6), a channel is
* considered working if its reported value is >= 130 uT.
*
* @link solde.red/333359 : Soldered BMM350 Geomagnetic Sensor breakout
*
* @authors Soldered Electronics
***************************************************/
#include <BMM350-SOLDERED.h>

// Create an instance of the BMM350 sensor
Soldered_BMM350 magSensor;

// Minimum self-test field difference to consider a channel passing,
// per the datasheet's self-test section
const float SELF_TEST_THRESHOLD_UT = 130.0;

void setup()
{
// Initialize serial for output
Serial.begin(115200);
Serial.println("BMM350 Example - Self Test");

// Initialize I2C bus
Wire.begin();

// Check if sensor is connected and initialize it.
// Address is optional, defaults to 0x14 (ADSEL tied low on this breakout)
while (magSensor.begin() != BMM350_OK)
{
Serial.println("Error: BMM350 not connected, check wiring and I2C address!");
delay(1000);
}

Serial.println("BMM350 connected!");

// Run the self-test. The sensor automatically returns to normal mode
// afterwards since begin() already left it there.
struct bmm350_self_test result;
if (magSensor.performSelfTest(&result) == BMM350_OK)
{
Serial.print("X axis field: ");
Serial.print(result.out_ust_x, 2);
Serial.print(" uT -> ");
Serial.println(result.out_ust_x >= SELF_TEST_THRESHOLD_UT ? "PASS" : "FAIL");

Serial.print("Y axis field: ");
Serial.print(result.out_ust_y, 2);
Serial.print(" uT -> ");
Serial.println(result.out_ust_y >= SELF_TEST_THRESHOLD_UT ? "PASS" : "FAIL");
}
else
{
Serial.println("Self-test failed to run.");
}
}

void loop()
{
}

Ovaj primjer koristi i Wire, Serial i delay(), koji dolaze s Arduino jezgrom i nisu dio BMM350 biblioteke. delay() pauzira skicu na zadani broj milisekundi: delay(1000) čeka jednu sekundu između pokušaja povezivanja.

Očekivani rezultat, pri 115200 baud: dvije početne linije, zatim dvije linije s rezultatom, X axis field: i Y axis field:, svaka s razlikom polja u uT i oznakom PASS ili FAIL.

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
BMM350 Example - Self Test
BMM350 connected!
X axis field: 148.37 uT -> PASS
Y axis field: 151.92 uT -> PASS

Ako se test ne može izvesti, skica umjesto toga ispisuje Self-test failed to run.

Korištene funkcije​

Soldered_BMM350()returns Soldered_BMM350

Konstruktor. Stvara objekt senzora; ništa se ne šalje senzoru dok se ne pozove begin().

ReturnsObjekt senzora.

int8_t begin(uint8_t address = 0x14, TwoWire &wirePort = Wire)returns int8_t

Inicijalizira senzor preko I2C-a, zatim postavlja brzinu izlaznih podataka od 100 Hz s usrednjavanjem 4 uzorka, omogućuje X, Y i Z os te pokreće normalni način rada.

ReturnsBMM350_OK (0) u slučaju uspjeha, inače kôd pogreške.

Parametri

TipNazivOpis
uint8_taddressI2C adresa. Zadano je 0x14; koristite 0x15 kada se JP5 premjesti.
TwoWire &wirePortI2C sabirnica koja se koristi. Zadano je Wire.
int8_t performSelfTest(struct bmm350_self_test *result)returns int8_t

Izvodi ugrađeni samotest senzora na X i Y osi i sprema rezultat. Članovi out_ust_x i out_ust_y u rezultatu sadrže razliku polja na svakoj osi u µT.

ReturnsBMM350_OK (0) u slučaju uspjeha, inače kôd pogreške.

Parametri

TipNazivOpis
struct bmm350_self_test *resultMjesto za spremanje rezultata samotesta.

Sastavljanje cjeline​

begin() ostavlja senzor u normalnom načinu rada, pa se nakon samotesta vraća u normalni način rada, kako stoji u komentaru skice. Skica uspoređuje out_ust_x i out_ust_y s SELF_TEST_THRESHOLD_UT (130.0) te za svaku os ispisuje PASS ili FAIL. loop() je prazna: za ponovno izvođenje testa resetirajte pločicu.