From 7291d4d861abad9c36ed75519bf60b3ae814197b Mon Sep 17 00:00:00 2001 From: Ali Jahangiri <75624145+aliphys@users.noreply.github.com> Date: Fri, 10 May 2024 11:14:04 +0200 Subject: [PATCH] Expand CI coverage to four examples from the Nicla Sense ME User Manual (#139) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add ActivityRecognition sketch * add magnetometer example * Add three examples * Spelling corrections * Update Arduino_BHY2/examples/Pressure/Pressure.ino Co-authored-by: Christopher Méndez <49886387+mcmchris@users.noreply.github.com> --------- Co-authored-by: Christopher Méndez <49886387+mcmchris@users.noreply.github.com> --- Arduino_BHY2/examples/AccelGyro/AccelGyro.ino | 60 +++++++++++++++++++ .../ActivityRecognition.ino | 37 ++++++++++++ .../examples/Magnetometer/Magnetometer.ino | 39 ++++++++++++ Arduino_BHY2/examples/Pressure/Pressure.ino | 36 +++++++++++ 4 files changed, 172 insertions(+) create mode 100644 Arduino_BHY2/examples/AccelGyro/AccelGyro.ino create mode 100644 Arduino_BHY2/examples/ActivityRecognition/ActivityRecognition.ino create mode 100644 Arduino_BHY2/examples/Magnetometer/Magnetometer.ino create mode 100644 Arduino_BHY2/examples/Pressure/Pressure.ino diff --git a/Arduino_BHY2/examples/AccelGyro/AccelGyro.ino b/Arduino_BHY2/examples/AccelGyro/AccelGyro.ino new file mode 100644 index 00000000..8d7d3d75 --- /dev/null +++ b/Arduino_BHY2/examples/AccelGyro/AccelGyro.ino @@ -0,0 +1,60 @@ +/* + * This example shows how to use the access the IMU data and plot it in real time. + * + * Every 50 milliseconds, this sketch will send the x,y and z accelerometer (SensorID 4) and + * gyroscope (SensorID 22) values over serial from the BHI260AP six axis IMU. + * These six values are visually displayed with the Serial Plotter in the Arduino IDE. + * + * Instructions: + * 1. Upload this sketch to your Nicla Sense ME board. + * 2. Open the Serial Plotter at a baud rate of 115200. + * 3. The three axis values for both the accelerometer and gyroscope will be printed to the Serial Plotter. + * 4. Optionally, you can change the visibility of each data by clicking on the legend. + * + * Initial author: @mcmchris + */ + +#include "Arduino_BHY2.h" + +SensorXYZ accel(SENSOR_ID_ACC); +SensorXYZ gyro(SENSOR_ID_GYRO); + + +void setup() { + Serial.begin(115200); + BHY2.begin(); + accel.begin(); + gyro.begin(); +} + +void loop() { + static auto printTime = millis(); + + // Update function should be continuously polled + BHY2.update(); + + if (millis() - printTime >= 50) { + printTime = millis(); + + // Accelerometer values + Serial.print("acc_X:"); + Serial.print(accel.x()); + Serial.print(","); + Serial.print("acc_Y:"); + Serial.print(accel.y()); + Serial.print(","); + Serial.print("acc_Z:"); + Serial.print(accel.z()); + Serial.print(","); + + // Gyroscope values + Serial.print("gyro_X:"); + Serial.print(gyro.x()); + Serial.print(","); + Serial.print("gyro_Y:"); + Serial.print(gyro.y()); + Serial.print(","); + Serial.print("gyro_Z:"); + Serial.println(gyro.z()); + } +} \ No newline at end of file diff --git a/Arduino_BHY2/examples/ActivityRecognition/ActivityRecognition.ino b/Arduino_BHY2/examples/ActivityRecognition/ActivityRecognition.ino new file mode 100644 index 00000000..98a543f5 --- /dev/null +++ b/Arduino_BHY2/examples/ActivityRecognition/ActivityRecognition.ino @@ -0,0 +1,37 @@ +/* + * This example shows how to use the Nicla Sense ME library to detect activity and send it over serial. + * + * Every 1 second, this sketch will send the latest activity detected via the BHI260AP sensor to the serial port. + * Without any additional training, it is possible to detect Still, Walking, Running and Tilting activities + * A full description of supported activities is available in Table 93 of the BHI260AP datasheet. + * + * Instructions: + * 1. Upload this sketch to your Nicla Sense ME board. + * 2. Open the Serial Monitor at a baud rate of 115200. + * 3. Observe the detected activity, by moving the Nicla Sense ME board. + * + * Initial author: @mcmchris + */ + +#include "Arduino_BHY2.h" + +SensorActivity active(SENSOR_ID_AR); + +unsigned long previousMillis = 0; // will store last time the sensor was updated +const long interval = 1000; + + +void setup() { + Serial.begin(115200); + BHY2.begin(); + active.begin(); +} + +void loop() { + BHY2.update(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + Serial.println(String("Activity info: ") + active.toString()); + } +} \ No newline at end of file diff --git a/Arduino_BHY2/examples/Magnetometer/Magnetometer.ino b/Arduino_BHY2/examples/Magnetometer/Magnetometer.ino new file mode 100644 index 00000000..890e6d03 --- /dev/null +++ b/Arduino_BHY2/examples/Magnetometer/Magnetometer.ino @@ -0,0 +1,39 @@ +/* + * This example shows how to use the access the magnetometer data and send it over serial. + * + * Every 1 second, this sketch will send the heading of the magnetometer over serial. + * by calculating the arctangent between the x and y axis and multiplied in a conversion + * factor to convert the headings from radians to degrees. + * + * Instructions: + * 1. Upload this sketch to your Nicla Sense ME board. + * 2. Open the Serial Monitor at a baud rate of 115200. + * 3. The heading of the magnetometer will be printed to the serial monitor. + * + * Initial author: @mcmchris + */ + +#include "Arduino_BHY2.h" +#include "Math.h" + +SensorXYZ magnetometer(SENSOR_ID_MAG); + +float heading = 0; +unsigned long previousMillis = 0; // will store last time the sensor was updated +const long interval = 1000; + +void setup() { + Serial.begin(115200); + BHY2.begin(); + magnetometer.begin(); +} + +void loop() { + BHY2.update(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + heading = round(atan2(magnetometer.x(), magnetometer.y()) * 180.0 / PI); + Serial.println(String(heading) + "º"); + } +} \ No newline at end of file diff --git a/Arduino_BHY2/examples/Pressure/Pressure.ino b/Arduino_BHY2/examples/Pressure/Pressure.ino new file mode 100644 index 00000000..e4f8ac6c --- /dev/null +++ b/Arduino_BHY2/examples/Pressure/Pressure.ino @@ -0,0 +1,36 @@ +/* + * This example shows how to use the access the pressure data and send it over serial. + * + * Every 1 second, this sketch will send the pressure in hPa over serial. + * SensorID 129 (SENSOR_ID_BARO) is read with the Sensor class from the BMP390. + * + * Instructions: + * 1. Upload this sketch to your Nicla Sense ME board. + * 2. Open the Serial Monitor at a baud rate of 115200. + * 3. The pressure will be printed to the serial monitor. + * + * Initial author: @mcmchris + */ + +#include "Arduino_BHY2.h" + + +unsigned long previousMillis = 0; // will store last time the sensor was updated +const long interval = 1000; + +Sensor pressure(SENSOR_ID_BARO); + +void setup() { + Serial.begin(9600); + BHY2.begin(); + pressure.begin(); +} + +void loop() { + BHY2.update(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + Serial.println(String(pressure.value()) + " hPa"); + } +} \ No newline at end of file