Imu accuracy information

Hello everyone,
I haven't bought Nicla Sens ME yet and I need some information.
my goal is to recover some data from a boat. I currently use Arduino MKR and IMU MKR shield (based on BME055) but I'm having huge problems determining the heading from Euler angles (it's very inaccurate, even 180°).
I wanted to ask how from Nicla Sens ME the Euler angles and their precision could be determined.
I read a bit on the forum and I saw that the sensor calibrates itself after a while of being in motion.
is this sketch valid?

/* 
 * This sketch shows how nicla can be used in standalone mode.
 * Without the need for an host, nicla can run sketches that 
 * are able to configure the bhi sensors and are able to read all 
 * the bhi sensors data.
*/

#include "Arduino.h"
#include "Arduino_BHY2.h"

SensorXYZ accel(SENSOR_ID_ACC);
SensorXYZ gyro(SENSOR_ID_GYRO);
Sensor temp(SENSOR_ID_TEMP);
Sensor gas(SENSOR_ID_GAS);
SensorQuaternion rotation(SENSOR_ID_RV);

void setup()
{
  Serial.begin(115200);
  while(!Serial);

  BHY2.begin();

  accel.begin();
  gyro.begin();
  temp.begin();
  gas.begin();
  rotation.begin();
}

void loop()
{
  static auto printTime = millis();

  // Update function should be continuously polled
  BHY2.update();

  if (millis() - printTime >= 1000) {
    printTime = millis();

    Serial.println(String("acceleration: ") + accel.toString());
    Serial.println(String("gyroscope: ") + gyro.toString());
    Serial.println(String("temperature: ") + String(temp.value(),3));
    Serial.println(String("gas: ") + String(gas.value(),3));
    Serial.println(String("rotation: ") + rotation.toString());
  }
short accX = accelerometer.x();
short accY = accelerometer.y();
short accZ = accelerometer.z();

short gyroX = gyroscope.x();
short gyroY = gyroscope.y();
short gyroZ = gyroscope.z();

short roll = rotation.x();
short pitch = rotation.y();
short heading = rotation.z();
}

does this sketch use the on-chip sensor fusion algorithm?
thank you in advance for whoever will answer me

Large errors in yaw are due to the magnetometer, which MUST be calibrated before use.

Best overview of calibration: Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers

Practical demonstration: https://forum.pololu.com/t/correcting-the-balboa-magnetometer/14315

would it make sense instead to calibrate it with a gps?
going along a straight stretch, can the GPS route be compared with the one read by the magnetometer and can the error be found from there?
I'm inexperienced in this field and the link you forwarded me seems like mathematically very complex

No.

going along a straight stretch, can the GPS route be compared with the one read by the magnetometer and can the error be found from there?

The heading error is found that way. The GPS has no way of knowing which direction the bow points.

the link you forwarded me seems like mathematically very complex

The math is complex, but you don't need to do any math. The programs do that.

thank you very much for your information, I'll have to go and re-read the links you sent me very carefully, because at the moment I don't quite understand how to calibrate.
from what I understand I have to:

  1. upload a sketch that sends me the magnetometer data via serial
  2. rotate the compass in all Cartesian axes
  3. take a program (which I don't quite understand what it is) and enter the raw data
  4. the program will give me 3 correction values to apply I don't know how in the sketch of my project in which I'm going to use an ECompass

The procedure IS complex, and it will take some study on your part to go through it. That is the only way you will get accurate data out of the magnetometer.

I describe all the individual steps, and post ALL the code, for two different methods of calibrating the magnetometer in the second link, post #2 above.

There are twelve. Three offsets and a 3x3 matrix for the method described in the first link, post #2.

There are six for the simpler, "axial min/max" method discussed in the second link, post #2.

I'm sorry if I'm being persistent but I'm trying to understand.
in this post we talk about self-calibration and from what I understood by reading the documentation the BHI260AP has a sensor fusion algorithm that already interfaces with the magnetometer.
so all these algorithms that run inside the IMU are not valid and do I have wrong data on the output?

edit:
i cant launch magneto on Mac
and in this Link a sketch called Calibrate2 is provided, is it for calibrating the magnetometer without using external software?

They would not be, if the sensors are not calibrated correctly. I haven't seen the code, but no filter can correct for systematic errors, like incorrect sensor offsets.

a sketch called Calibrate2 is provided

I wrote that sketch, which uses a much simpler and less accurate method of calibrating the sensors. It produces six parameters, three offsets and three scale factors, which, as I demonstrated in that post, cannot correct all the problems.

i cant launch magneto on Mac

It is a standard C program that requires no special operating system features. It will run on the Mac if compiled and linked properly.

I've looked a bit, it seems that Xcode is missing a library, I'll look for some other C compiler for mac. (If you can recommend one, all the better.)

as for the rest, I'll try to use the simplified sketch in the meantime and see what results I get.
maybe I get a precision compatible with my purposes.

I use and recommend Code::Blocks on a Windows PC.

I'm sorry but I just can't start magneto. the Malloc library is giving a lot of problems and is not included