rotation_fusion.ino

Arduino Nicla Sense ME · File : rotation_fusion.ino · Language : Arduino · Use case : Lire l’orientation fusionnée (quaternion)

The BHI260AP runs Bosch BSX fusion on its own core: the orientation quaternion (accel + gyro + compass) arrives precomputed, sparing the nRF52832. Object attitude in 23×23 mm — the base of a stabilizer or gesture tracker.

Hardware

An Arduino Nicla Sense ME (nRF52832, BLE only) with Bosch BHI260AP hub, BMM150, BMP390, BME688 sensors, RGB LED on an I2C driver and LiPo charger. The board alone. Keep it still for a few seconds at boot: the hub aligns (gyro + magnetic).

Wiring

No wiring. Keep large metal masses and magnets away from the magnetometer for clean fusion (the BMM150 is sensitive to ferrous disturbance).

How it works

SensorQuaternion enables like other BHY2 sensors; the hub pushes the fused quaternion; the example derives roll via atan2. The fusion stays valid even in slow motion.

Running it

Programmed from the Arduino IDE (ArduinoCore-mbed); Arduino_BHY2 and Nicla_System libraries; USB through a serial bridge.

Variants

Switch to SensorEuler if available for direct angles, send the quaternion over BLE for AR, or combine with the hub step counter for an activity tracker.

rotation_fusion.ino

// Fusion BSX du hub BHI260AP : quaternion d'orientation absolu
// (accéléro + gyro + magnétomètre fondus dans le hub, pas sur le
// nRF52832 — le CPU principal reste libre).

#include "Arduino_BHY2.h"

SensorQuaternion rotation(0);

void setup() {
  Serial.begin(115200);
  BHY2.begin();
  rotation.begin();
  BHY2.update();
}

void loop() {
  BHY2.update();

  if (rotation.checkNewData()) {
    Serial.print("q = [");
    Serial.print(rotation.w()); Serial.print(", ");
    Serial.print(rotation.x()); Serial.print(", ");
    Serial.print(rotation.y()); Serial.print(", ");
    Serial.print(rotation.z());
    Serial.println("]");

    // Exemple : angle de roulis dérivé du quaternion
    float roll = atan2f(2 * (rotation.w() * rotation.x() +
                             rotation.y() * rotation.z()),
                        1 - 2 * (rotation.x() * rotation.x() +
                                 rotation.y() * rotation.y()));
    Serial.print("roll = ");
    Serial.println(roll * 57.2958);   // rad -> degrés
  }
  delay(20);
}

← Back to the board