I am a mechanical engineer with no knowledge of electronics, Arduino or registers etc. I have
Seed studio xiao nrf52840 sense. I want to convert gyro and accelerometer values correctly to angles(degrees or radians). Once I am confident that thses values are correct I will implement a 1D kalman filter to combine those 2. I am struggling with first step itself.
I read gyro and accelerometer data.
I took some reading to get average zero and subtracted them from sensor value in order to calibrate the sensor.
I tried different factors from this table in image. Such as 8.75, 17.5 ,70
and welcome here, Since there is only a little data on this but lots of info… I thought let’s ask.
I have experimented also with the AWT in the Xiao and this basically sums it up in a small thesis.
HTH
GL PJ & Al
When capturing raw accelerometer and gyroscope data (e.g., from the LSM6DS3 on the Xiao nRF52840 Sense) and you want a meaningful orientation estimate or cleaner motion data, you typically need some form of sensor fusion or filtering. The exact approach depends on your project requirements. Here’s an overview:
1. Basic Filtering (Noise Reduction Only)
Low-pass filter: Smooth out high-frequency noise in your accelerometer data (e.g., to stabilize readings in a static orientation).
High-pass filter: Remove low-frequency drift in your gyro signals or gravitational component from the accelerometer data.
Implementation: You can do this manually by applying a one-pole filter or by using built-in LSM6DS3 digital filters. Some libraries let you configure these filters directly in the sensor.
2. Complementary Filter (Simple Sensor Fusion)
If you just want a rough orientation estimate (pitch/roll/yaw) without the complexity of a full AHRS, a complementary filter often suffices:
Accelerometer for low-frequency orientation info (gravity vector).
Gyroscope for high-frequency changes (fast rotations).
Implementation:
cpp
Copy code
// Pseudocode for roll/pitch with complementary filter
// alpha ~ 0.98 for example
pitch = alpha * (pitch + gyroPitchRate * dt) + (1 - alpha) * accelPitch;
roll = alpha * (roll + gyroRollRate * dt) + (1 - alpha) * accelRoll;
This approach is easy to implement and typically good enough for hobby robotics or stable platforms.
3. AHRS Libraries (Madgwick, Mahony, Kalman)
For more robust 3D orientation, heading, or advanced motion tracking, you can use an Attitude and Heading Reference System (AHRS). These algorithms fuse accelerometer, gyroscope, and optionally magnetometer data into a stable orientation (often a quaternion or Euler angles):
Madgwick filter: Good for low-resource MCUs, offers fast convergence.
Mahony filter: Similar to Madgwick, also widely used in embedded AHRS solutions.
Kalman filter (or Extended/Unscented Kalman Filter): Very powerful, but more CPU/memory intensive, typically used in professional IMU solutions.
For more advanced usage, you can implement your own Extended Kalman Filter on the nRF52840 if you have enough flash/RAM.
4. Built-in LSM6DS3 Features
The LSM6DS3 includes:
On-board FIFO: You can batch up accelerometer/gyro samples.
Embedded pedometer, step detection: If you need step counting or basic motion detection.
Some digital filtering: You can configure its internal low-pass or high-pass filters.
If you only need simpler tasks like motion detection or step counting, you might leverage these built-in features without writing your own filter.
Which Do You Need?
Just smoothing noise? Use built-in LSM6DS3 filters or a simple low-pass approach.
Want to track pitch/roll or basic orientation? A complementary filter is often enough.
Full 3D orientation or stable heading? Use Madgwick/Mahony (for minimal code) or a Kalman filter (for more accuracy and complexity).
Example: Using Madgwick Filter
Include a Madgwick library in your Arduino code.
Collect accelerometer (ax, ay, az) and gyroscope (gx, gy, gz) data in loop().
Feed those into the Madgwick update function:
cpp
Copy code
float ax, ay, az; // from LSM6DS3
float gx, gy, gz; // from LSM6DS3
float dt = 0.01; // or measure actual loop time
Madgwick.updateIMU(gx, gy, gz, ax, ay, az, dt);
// Now get the orientation from Madgwick: quaternion, or euler angles
float roll = Madgwick.getRoll();
float pitch = Madgwick.getPitch();
float yaw = Madgwick.getYaw();
Use roll/pitch/yaw for your application (robot balancing, drone, orientation display, etc.).
Summary
No single “one-size-fits-all filter.” It depends on your performance needs and complexity tolerance.
For quick & easy orientation: complementary filter. (with AWT it’s a great solution)
For more accurate orientation: Madgwick/Mahony.
For high-end sensor fusion: Extended Kalman Filter.
You’ll likely pick one of these approaches to process your Xiao nRF52840 Sense’s accelerometer & gyro data effectively.
SO you know there are Calibration data from the factory in register locations , consult the Datasheet.
Read your values and apply them to your calibration data.
GL PJ
Also there is no Magnetometer so Drift is present and needs to be compensated for or rechecked and zeroed to be HARD core!