Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0dc34b0

Browse files
canchebagurjcarolinares
andauthored
[PC-1280] Magnetometer Working Example Update (#1077)
* Initial commit * Grammar check * Example reverted to show raw sensor data * Grammar check, graphics updated * GIF added * Remove microteslas reference * Update content/hardware/06.nicla/boards/nicla-voice/tutorials/user-manual/content.md Co-authored-by: Julián Caro Linares <j.carolinares@arduino.cc> * Content update (Julian's review) --------- Co-authored-by: Julián Caro Linares <j.carolinares@arduino.cc>
1 parent ccad9fb commit 0dc34b0

File tree

3 files changed

+197
-4
lines changed

3 files changed

+197
-4
lines changed
261 KB
Loading[フレーム]
6.74 MB
Loading[フレーム]

‎content/hardware/06.nicla/boards/nicla-voice/tutorials/user-manual/content.md‎

Lines changed: 197 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ The onboard magnetometer of the Nicla Voice can be used to determine the board's
441441

442442
#### Accelerometer and Gyroscope Data
443443

444-
The example code below shows how to get acceleration (m/s<sup>2</sup>) and angular velocity (in °/s) data from the onboard IMU and streams it to the Arduino IDE Serial Monitor.
444+
The example code below shows how to get acceleration (m/s<sup>2</sup>) and angular velocity (in °/s) data from the onboard IMU and streams it to the Arduino IDE Serial Monitor and Serial Plotter.
445445

446446
```arduino
447447
/**
@@ -537,7 +537,7 @@ void setup() {
537537
Serial.println("- NDP processor initialization...");
538538
NDP.begin("mcu_fw_120_v91.synpkg");
539539
NDP.load("dsp_firmware_v91.synpkg");
540-
NDP.load("alexa_334_NDP120_B0_v11_v91.synpkg");
540+
NDP.load("ei_model.synpkg");
541541
Serial.println("- NDP processor initialization done!");
542542
543543
// Set the BMI270 sensor in SPI mode, then read sensor data.
@@ -669,9 +669,9 @@ Next, user functions `ledBlueOn()`, `ledGreenOn()`, and `ledRedBlink()` definiti
669669
Next, in the `setup()` function:
670670

671671
- The serial communication is initialized at a baud rate of 115200.
672-
- The Nicla Voice board is initialized, and the LDO regulator (used for putting the board into power-saving modes) is disabled to avoid communication problems with the IMU.
672+
- The Nicla Voice board is initialized, and the LDO regulator (used for putting the board into power-saving mode) is disabled to avoid communication problems with the IMU.
673673
- Error and event handlers are initialized.
674-
- NDP processor is initialized; this process includes populating the external Flash memory of the board with the NDP processor internal microcontroller firmware (`mcu_fw_120_v91.synpkg`), the NDP processor internal DSP firmware (`dsp_firmware_v91.synpkg`), and the ML model (`ei_model.synpkg`).
674+
- NDP processor is initialized; this process includes populating the external Flash memory of the board with the NDP processor's internal microcontroller firmware (`mcu_fw_120_v91.synpkg`), the NDP processor's internal DSP firmware (`dsp_firmware_v91.synpkg`), and the ML model (`ei_model.synpkg`).
675675
- The BMI270 sensor is initialized; this includes a software reset, loading the sensor configuration, and setting it into normal power mode with the accelerometer and gyroscope operational.
676676

677677
Finally, in the `loop()` function:
@@ -716,6 +716,199 @@ Upload the example sketch again and open the IDE's Serial Plotter by navigating
716716

717717
When the board is not moving, you should see acceleration measurements close to zero on the x and y-axis, while the z-axis will be close to 1g (approximately 9.81 m/s<sup>2</sup>). If you want to visualize gyroscope readings, uncomment the gyroscope data output and comment on the accelerometer data output; when the board is not moving, you should see gyroscope measurements on the three-axis close to zero.
718718

719+
#### Magnetometer Data
720+
721+
The example code below shows how to get raw magnetic field and Hall resistance data from the onboard magnetometer and stream it to the Arduino IDE Serial Monitor and Serial Plotter.
722+
723+
```arduino
724+
/**
725+
Nicla Voice magnetometer test sketch
726+
Name: nv_mag_test.ino
727+
Purpose: Sketch tests onboard magnetometer (BMM150)
728+
729+
@author Arduino PRO Content Team
730+
@version 1.0 30/05/23
731+
*/
732+
733+
#include "NDP.h"
734+
735+
// Named constants
736+
#define READ_START_ADDRESS 0x42
737+
#define READ_BYTE_COUNT 8
738+
#define SENSOR_DATA_LENGTH 16
739+
740+
/**
741+
Turns on and off the onboard blue LED.
742+
743+
@param label to be printed on the Serial Monitor.
744+
*/
745+
void ledBlueOn(char* label) {
746+
nicla::leds.begin();
747+
nicla::leds.setColor(blue);
748+
delay(200);
749+
nicla::leds.setColor(off);
750+
Serial.println(label);
751+
nicla::leds.end();
752+
}
753+
754+
/**
755+
Turns on and off the onboard green LED.
756+
*/
757+
void ledGreenOn() {
758+
nicla::leds.begin();
759+
nicla::leds.setColor(green);
760+
delay(200);
761+
nicla::leds.setColor(off);
762+
nicla::leds.end();
763+
}
764+
765+
/**
766+
Blinks onboard red LED periodically every 200 ms.
767+
*/
768+
void ledRedBlink() {
769+
while (1) {
770+
nicla::leds.begin();
771+
nicla::leds.setColor(red);
772+
delay(200);
773+
nicla::leds.setColor(off);
774+
delay(200);
775+
nicla::leds.end();
776+
}
777+
}
778+
779+
// Macro definition used for checking the sensor status, print error if SPI access fails.
780+
#define CHECK_STATUS(s) do {\
781+
if (s) {\
782+
Serial.print("SPI access error in line ");\
783+
Serial.println(__LINE__);\
784+
for(;;);\
785+
}\
786+
} while (0)
787+
788+
void setup() {
789+
int status;
790+
uint8_t __attribute__((aligned(4))) sensor_data[SENSOR_DATA_LENGTH];
791+
792+
// Initiate Serial communication for debugging and monitoring.
793+
Serial.begin(115200);
794+
795+
// Initialize Nicla Voice board's system functions.
796+
// Disable the LDO regulator on the Nicla Voice board for power saving.
797+
// Initialize the built-in RGB LED of the Nicla Voice board.
798+
nicla::begin();
799+
nicla::disableLDO();
800+
nicla::leds.begin();
801+
802+
// Set up error and event handlers:
803+
// - In case of error, the red LED will blink.
804+
// - In case of match, the blue LED will turn on.
805+
// - In case of any event, the green LED will turn on.
806+
NDP.onError(ledRedBlink);
807+
NDP.onMatch(ledBlueOn);
808+
NDP.onEvent(ledGreenOn);
809+
810+
// NDP processor initialization with firmwares and models.
811+
Serial.println("- NDP processor initialization...");
812+
NDP.begin("mcu_fw_120_v91.synpkg");
813+
NDP.load("dsp_firmware_v91.synpkg");
814+
NDP.load("ei_model.synpkg");
815+
Serial.println("- NDP processor initialization done!");
816+
817+
// Enable power control bit
818+
status = NDP.sensorBMM150Write(0x4B, 0x01);
819+
CHECK_STATUS(status);
820+
delay(20);
821+
822+
// Read power control byte
823+
status = NDP.sensorBMM150Read(0x4B, 1, sensor_data);
824+
CHECK_STATUS(status);
825+
826+
// Set the magnetometer to active mode (normal operation), with an output data rate of 10 Hz.
827+
status = NDP.sensorBMM150Write(0x4C, 0x00);
828+
CHECK_STATUS(status);
829+
830+
// Read chip ID
831+
status = NDP.sensorBMM150Read(0x40, 1, sensor_data);
832+
CHECK_STATUS(status);
833+
}
834+
835+
void loop() {
836+
// Allocate space for raw sensor data.
837+
uint8_t __attribute__((aligned(4))) sensor_data[SENSOR_DATA_LENGTH];
838+
839+
// Declare variables for magnetometer data.
840+
int16_t x_mag_raw, y_mag_raw, z_mag_raw, hall_raw ;
841+
float x_mag, y_mag, z_mag;
842+
843+
// Read operation status variable.
844+
int status;
845+
846+
// Perform data read from the BMM150 sensor.
847+
// The sensor's read function is called with 0x42 as the start address and 8 as the number of bytes to read.
848+
// Collected data is placed into sensor_data array.
849+
status = NDP.sensorBMM150Read(READ_START_ADDRESS, READ_BYTE_COUNT, sensor_data);
850+
851+
// Check the status of the read operation.
852+
CHECK_STATUS(status);
853+
854+
// The sensor data is read into an array of bytes (8-bit values). Each measurement from the magnetometer consists
855+
// of two bytes, hence the bit shifting and bitwise OR operations to combine these two bytes into one 16-bit value.
856+
// Data for each axis (X, Y, Z) of the magnetometer is extracted from the array.
857+
x_mag_raw = (0x0000 | sensor_data[0] >> 3 | sensor_data[1] << 5);
858+
y_mag_raw = (0x0000 | sensor_data[2] >> 3 | sensor_data[3] << 5);
859+
z_mag_raw = (0x0000 | sensor_data[4] >> 1 | sensor_data[5] << 7);
860+
hall_raw = (0x0000 | sensor_data[6] >> 2 | sensor_data[7] << 6);
861+
862+
// Print raw magnetometer data.
863+
Serial.print("x_mag_raw:");
864+
Serial.print(x_mag_raw);
865+
Serial.print(",");
866+
Serial.print("y_mag_raw:");
867+
Serial.print(y_mag_raw);
868+
Serial.print(",");
869+
Serial.print("z_mag_raw:");
870+
Serial.print(z_mag_raw);
871+
Serial.print(",");
872+
Serial.print("hall_raw:");
873+
Serial.println(hall_raw);
874+
875+
delay(1000);
876+
}
877+
```
878+
879+
Here you can find a step-by-step explanation of the code:
880+
881+
First, the necessary libraries are included:
882+
883+
- `NDP.h` for the Nicla Voice board's basic functions and the magnetometer control.
884+
- Macros are defined for checking the status of the magnetometer; these macros allow the sketch to detect and handle sensor errors.
885+
886+
Next, user functions `ledBlueOn()`, `ledGreenOn()`, and `ledRedBlink()` definition:
887+
888+
- These functions allow the onboard LEDs to flash specific colors to indicate different states: blue for a successful match, green for an event, and red to indicate an error.
889+
890+
Next, in the `setup()` function:
891+
892+
- The serial communication is initialized at a baud rate of 115200.
893+
- The Nicla Voice board is initialized, and the LDO regulator (used for putting the board into power-saving mode) is disabled to avoid communication problems with the magnetometer.
894+
- Error and event handlers are initialized.
895+
- NDP processor is initialized; this process includes populating the external Flash memory of the board with the NDP processor's internal microcontroller firmware (`mcu_fw_120_v91.synpkg`), the NDP processor's internal DSP firmware (`dsp_firmware_v91.synpkg`), and the ML model (`ei_model.synpkg`).
896+
- The BMM150 sensor is initialized; this includes setting it into normal operation with an output data rate (ODR) of 10 Hz.
897+
898+
Finally, in the `loop()` function:
899+
900+
- Memory is allocated for the sensor data; data is then read from the sensor and stored in this allocated space.
901+
- Raw sensor data is then extracted and parsed into raw magnetometer data. Raw sensor data is extracted from the `sensor_data` array, 8-bits at a time, and then combined to form a 16-bit integer for each axis (X, Y, Z) and raw Hall resistance value.
902+
- Raw magnetometer data is printed on the Serial Monitor, allowing the user to observe sensor data in real-time.
903+
904+
After uploading the example code, you should see the magnetometer data on the IDE's Serial Monitor, as shown below:
905+
906+
![Nicla Voice onboard raw magnetometer data on the IDE's Serial Monitor](assets/user-manual-15.png)
907+
908+
Now open the IDE's Serial Plotter by navigating to **Tools > Serial Plotter**. After a while, you should see a real-time graph showing raw data from the board's onboard magnetometer, as shown below (move the board):
909+
910+
![Nicla Voice onboard raw magnetometer data on the IDE's Serial Plotter](assets/user-manual-16.gif)
911+
719912
#### IMU and Machine Learning
720913

721914
The example code below demonstrates how to use the Nicla Voice board to perform Machine Learning inference on IMU data. The code sets up event indicators using the onboard RGB LED and sends IMU data to the NDP processor for inference. The example can be found in the board's built-in examples by navigating to **File > Examples > NDP > IMUDemo**.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /