Skip to content

Commit d5f04ec

Browse files
committedDec 17, 2024·
IMU and ACC updated
1 parent 79423af commit d5f04ec

File tree

5 files changed

+48
-112
lines changed

5 files changed

+48
-112
lines changed
 
Loading

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

Lines changed: 48 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -539,99 +539,67 @@ In the `loop()` function:
539539
- The length of the extracted audio data is stored in the `len` variable.
540540
- The extracted audio data is passed to the G722 encoder, which compresses the audio and sends it to the serial port.
541541
542-
To extract the audio data on a computer, you will need to set up the serial port as raw and dump the data to a file (e.g., test.g722). Then, you can open the file with a software like [Audacity](https://www.audacityteam.org/) to play back the audio.
542+
To extract the audio data on a **Linux computer**, you will need to set up the serial port as raw:
543+
544+
```bash
545+
stty -F /dev/ttyACM0 115200 raw
546+
```
547+
Dump the data to a file (e.g., test.g722):
548+
549+
```bash
550+
cat /dev/ttyACM0 > test.g722
551+
```
552+
Then, you can open the file with a software like [Audacity](https://www.audacityteam.org/) to play back the audio.
543553
544554
#### Machine Learning and Audio Analysis
545555
546556
You can use the Nicla Voice and the [Machine Learning Tools](https://cloud.arduino.cc/machine-learning-tools/) of the Arduino Cloud to create your own audio analysis Machine Learning models. Check out this [tutorial](https://docs.arduino.cc/tutorials/nicla-voice/getting-started-ml) and start with Machine Learning with the Nicla Voice.
547557
548-
### IMU and Magnetometer
558+
### IMU
549559
550560
The Nicla Voice features an advanced IMU and a magnetometer, which allows the board to sense motion, orientation, and magnetic fields. The IMU on the Nicla Voice board is the BMI270 from Bosch®. It consists of a 3-axis accelerometer and a 3-axis gyroscope. They can provide information about the board's motion, orientation, and rotation in a 3D space. The BMI270 is designed for wearables and offers low power consumption and high performance, making it suitable for various applications, such as gesture recognition, motion tracking, or stabilization.
551561
552562
![Nicla Voice onboard IMU](assets/user-manual-11.png)
553563
554-
The onboard magnetometer of the Nicla Voice can be used to determine the board's orientation relative to Earth's magnetic field, which is helpful for compass applications, navigation, or detecting the presence of nearby magnetic objects. The magnetometer on the Nicla Voice board is the BMM150, also from Bosch®. It is a 3-axis sensor that measures the strength and direction of magnetic fields surrounding the board.
555-
556-
![Nicla Voice onboard magnetometer](assets/user-manual-12.png)
557-
558564
#### Accelerometer and Gyroscope Data
559565
560-
The example sketch 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. The sketch needs the `BMI270_Init.h` header file to be in the same directory as the sketch. You can download the example sketch and the header files [here](assets/nv_acc_gyro_test.rar).
566+
The example sketch 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. The sketch needs the `BMI270_Init.h` header file to be in the same directory as the sketch. You can download the example sketch and the header files [here](assets/nv_acc_gyro_test.zip).
561567
562-
```arduino
563-
/**
564-
Nicla Voice accelerometer and gyroscope test sketch
565-
Name: nv_acc_gyro_test.ino
566-
Purpose: Sketch tests onboard accelerometer and gyroscope (BMI270)
567-
568-
@author Arduino PRO Content Team
569-
@version 1.0 22/05/23
570-
*/
568+
***For this example to work you must update the NDP processor, see the details on this [section](#ndp120-processor-firmware-update).***
571569
570+
```arduino
572571
#include "NDP.h"
573572
#include "BMI270_Init.h"
574573
575574
// Named constants
576-
#define READ_START_ADDRESS 0x0C
577-
#define READ_BYTE_COUNT 16
578-
#define SENSOR_DATA_LENGTH 16
575+
#define READ_START_ADDRESS 0x0C
576+
#define READ_BYTE_COUNT 16
577+
#define SENSOR_DATA_LENGTH 16
579578
580579
// Accelerometer range is set to +/-2g
581-
// Raw accelerometer data is represented as a signed 16-bit integer
582-
// Raw accelerometer data can be converted to acceleration in m/s^2 units using the following scale factor:
583-
#define ACCEL_SCALE_FACTOR ((2.0 / 32767.0) * 9.8)
580+
// Raw accelerometer data is represented as a signed 16-bit integer
581+
// Raw accelerometer data can be converted to acceleration in m/s^2 units using the following scale factor:
582+
#define ACCEL_SCALE_FACTOR ((2.0 / 32767.0) * 9.8)
584583
585584
// Gyroscope has a sensitivity of 16.4 LSB/dps
586-
#define GYRO_SCALE_FACTOR (1 / 16.4)
587-
588-
/**
589-
Turns on and off the onboard blue LED.
590-
591-
@param label to be printed on the Serial Monitor.
592-
*/
593-
void ledBlueOn(char* label) {
594-
nicla::leds.begin();
595-
nicla::leds.setColor(blue);
596-
delay(200);
597-
nicla::leds.setColor(off);
598-
Serial.println(label);
599-
nicla::leds.end();
600-
}
601-
602-
/**
603-
Turns on and off the onboard green LED.
604-
*/
605-
void ledGreenOn() {
606-
nicla::leds.begin();
607-
nicla::leds.setColor(green);
608-
delay(200);
609-
nicla::leds.setColor(off);
610-
nicla::leds.end();
611-
}
612-
613-
/**
614-
Blinks onboard red LED periodically every 200 ms.
615-
*/
616-
void ledRedBlink() {
617-
while (1) {
618-
nicla::leds.begin();
619-
nicla::leds.setColor(red);
620-
delay(200);
621-
nicla::leds.setColor(off);
622-
delay(200);
623-
nicla::leds.end();
624-
}
625-
}
585+
#define GYRO_SCALE_FACTOR (1 / 16.4)
626586
627587
// Macros for checking the sensor status.
628-
#define CHECK_STATUS(s) do {if (s) {Serial.print("SPI access error in line "); Serial.println(__LINE__); for(;;);}} while (0)
588+
#define CHECK_STATUS(s) \
589+
do { \
590+
if (s) { \
591+
Serial.print("SPI access error in line "); \
592+
Serial.println(__LINE__); \
593+
for (;;) \
594+
; \
595+
} \
596+
} while (0)
629597
630598
void setup() {
631599
int status;
632600
uint8_t __attribute__((aligned(4))) sensor_data[SENSOR_DATA_LENGTH];
633601
634-
// Initiate Serial communication for debugging and monitoring.
602+
// Initiate Serial communication for debugging and monitoring.
635603
Serial.begin(115200);
636604
637605
// Initialize Nicla Voice board's system functions.
@@ -641,19 +609,10 @@ void setup() {
641609
nicla::disableLDO();
642610
nicla::leds.begin();
643611
644-
// Set up error and event handlers:
645-
// - In case of error, the red LED will blink.
646-
// - In case of match, the blue LED will turn on.
647-
// - In case of any event, the green LED will turn on.
648-
NDP.onError(ledRedBlink);
649-
NDP.onMatch(ledBlueOn);
650-
NDP.onEvent(ledGreenOn);
651-
652612
// NDP processor initialization with firmwares and models
653613
Serial.println("- NDP processor initialization...");
654614
NDP.begin("mcu_fw_120_v91.synpkg");
655615
NDP.load("dsp_firmware_v91.synpkg");
656-
NDP.load("ei_model.synpkg");
657616
Serial.println("- NDP processor initialization done!");
658617
659618
// Set the BMI270 sensor in SPI mode, then read sensor data.
@@ -682,7 +641,7 @@ void setup() {
682641
status = NDP.sensorBMI270Write(0x59, 0x00);
683642
CHECK_STATUS(status);
684643
685-
// Sensor configuration.
644+
// Sensor configuration.
686645
Serial.println("- BMI270 initialization starting...");
687646
status = NDP.sensorBMI270Write(0x5E, sizeof(bmi270_maximum_fifo_config_file), (uint8_t*)bmi270_maximum_fifo_config_file);
688647
CHECK_STATUS(status);
@@ -697,13 +656,13 @@ void setup() {
697656
698657
// Configure the device to normal power mode with both accelerometer and gyroscope operational.
699658
// Set the accelerometer and gyroscope settings such as measurement range and data rate.
700-
status = NDP.sensorBMI270Write(0x7D, 0x0E); // Normal power mode
659+
status = NDP.sensorBMI270Write(0x7D, 0x0E); // Normal power mode
701660
CHECK_STATUS(status);
702661
status = NDP.sensorBMI270Write(0x40, 0xA8); // Accelerometer configuration.
703662
CHECK_STATUS(status);
704663
status = NDP.sensorBMI270Write(0x41, 0x00); // Set the accelerometer range to +/- 2g.
705664
CHECK_STATUS(status);
706-
status = NDP.sensorBMI270Write(0x42, 0xA9); // Gyroscope configuration.
665+
status = NDP.sensorBMI270Write(0x42, 0xA9); // Gyroscope configuration.
707666
CHECK_STATUS(status);
708667
status = NDP.sensorBMI270Write(0x43, 0x00); // Set the gyroscope range to +/- 2000 dps.
709668
CHECK_STATUS(status);
@@ -737,7 +696,7 @@ void loop() {
737696
y_gyr_raw = (0x0000 | sensor_data[8] | sensor_data[9] << 8);
738697
z_gyr_raw = (0x0000 | sensor_data[10] | sensor_data[11] << 8);
739698
740-
// Convert raw accelerometer data to acceleration expressed in m/s^2.
699+
// Convert raw accelerometer data to acceleration expressed in m/s^2.
741700
x_acc = x_acc_raw * ACCEL_SCALE_FACTOR;
742701
y_acc = y_acc_raw * ACCEL_SCALE_FACTOR;
743702
z_acc = z_acc_raw * ACCEL_SCALE_FACTOR;
@@ -746,8 +705,8 @@ void loop() {
746705
x_gyr = x_gyr_raw * GYRO_SCALE_FACTOR;
747706
y_gyr = y_gyr_raw * GYRO_SCALE_FACTOR;
748707
z_gyr = z_gyr_raw * GYRO_SCALE_FACTOR;
749-
750-
// Print accelerometer data (expressed in m/s^2).
708+
709+
// Print accelerometer data (expressed in m/s^2).
751710
Serial.print("x_acc:");
752711
Serial.print(x_acc);
753712
Serial.print(",");
@@ -767,7 +726,7 @@ void loop() {
767726
Serial.print("z_gyr:");
768727
Serial.println(z_gyr);
769728
770-
delay(1000);
729+
delay(10);
771730
}
772731
```
773732
@@ -778,59 +737,36 @@ First, the necessary libraries are included:
778737
- `NDP.h` and `BMI270_Init.h` for the Nicla Voice board's basic functions and the IMU control.
779738
- Macros are defined for checking the status of the IMU; these macros allow the sketch to detect and handle sensor errors.
780739
781-
Next, user functions `ledBlueOn()`, `ledGreenOn()`, and `ledRedBlink()` definition:
782-
783-
- 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.
784-
785740
Next, in the `setup()` function:
786741
787-
- The serial communication is initialized at a baud rate of 115200.
742+
- The serial communication is initialized at a baud rate of `115200`.
788743
- 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.
789744
- Error and event handlers are initialized.
790-
- 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 Machine Learning model (`ei_model.synpkg`).
745+
- 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`), and the NDP processor's internal DSP firmware (`dsp_firmware_v91.synpkg`).
791746
- 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.
792747
793748
Finally, in the `loop()` function:
794749
795750
- Memory is allocated for the sensor data; data is then read from the sensor and stored in this allocated space.
796-
- Raw sensor data is then parsed and extracted into raw accelerometer and gyroscope data. This data is represented as 16-bit signed integers ranging from -32 768 to 32 767.
751+
- Raw sensor data is then parsed and extracted into raw accelerometer and gyroscope data. This data is represented as 16-bit signed integers ranging from -32768 to 32767.
797752
- Raw sensor data is converted into understandable and standard unit measurements; for the accelerometer, data is converted to meters per second squared, and for the gyroscope, data is converted to degrees per second.
798753
- Converted accelerometer and gyroscope data are printed on the Serial Monitor, allowing the user to observe sensor data in real-time.
799754
800755
After uploading the example code, you should see accelerometer and gyroscope data on the IDE's Serial Monitor as shown below:
801756
802757
![Nicla Voice onboard IMU data on the IDE's Serial Monitor](assets/user-manual-13.png)
803758
804-
Let's use also the Arduino IDE Serial Plotter to test the example IMU sketch; let's start visualizing only accelerometer data. To do so, comment the gyroscope data output as shown below:
759+
Let's use also the Arduino IDE Serial Plotter to test the example IMU sketch, open the IDE's Serial Plotter by navigating to **Tools > Serial Plotter**. After a while, you should see a real-time graph showing data from the board's onboard accelerometer and gyroscope, as shown below:
805760
806-
```arduino
807-
// Print accelerometer data (expressed in meters per second squared).
808-
Serial.print("x_acc:");
809-
Serial.print(x_acc);
810-
Serial.print(",");
811-
Serial.print("y_acc:");
812-
Serial.print(y_acc);
813-
Serial.print(",");
814-
Serial.print("z_acc:");
815-
Serial.println(z_acc);
761+
![Nicla Voice onboard IMU data on the IDE's Serial Plotter](assets/imu_gyro.gif)
816762
817-
/* Print gyroscope data (expressed in degrees per second).
818-
Serial.print("x_gyr:");
819-
Serial.print(x_gyr);
820-
Serial.print(",");
821-
Serial.print("y_gyr:");
822-
Serial.print(y_gyr);
823-
Serial.print(",");
824-
Serial.print("z_gyr:");
825-
Serial.println(z_gyr); */
826-
```
827-
828-
Upload the example sketch again and open the IDE's Serial Plotter by navigating to **Tools > Serial Plotter**. After a while, you should see a real-time graph showing data from the board's onboard accelerometer, as shown below (move the board):
763+
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>), the gyroscope measurements on the three-axis will stay close to zero.
829764
765+
### Magnetometer
830766
831-
![Nicla Voice onboard accelerometer data on the IDE's Serial Plotter](assets/user-manual-14.gif)
767+
The onboard magnetometer of the Nicla Voice can be used to determine the board's orientation relative to Earth's magnetic field, which is helpful for compass applications, navigation, or detecting the presence of nearby magnetic objects. The magnetometer on the Nicla Voice board is the BMM150, also from Bosch®. It is a 3-axis sensor that measures the strength and direction of magnetic fields surrounding the board.
832768
833-
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.
769+
![Nicla Voice onboard magnetometer](assets/user-manual-12.png)
834770
835771
#### Magnetometer Data
836772

0 commit comments

Comments
 (0)
Please sign in to comment.