Adafruit LSM6DSOX gyro

I have a problem using the adafruit LSM6DSOX module. I'm trying to set the gyro x value to a variable but it doesn't seem to be working. Could somebody please help me?
`#include <Adafruit_LSM6DSOX.h>

float angle = 0;
float angle_previous = 0;
float time = 0;
float time_previous = millis();
float gyro_previous = 0;
Adafruit_LSM6DSOX imu;
void setup() {
Serial.begin(115200);
if(!imu.begin_I2C()){
Serial.println("IMU cannot be found");
}

}

void loop() {
sensors_event_t gyro;
sensors_event_t accel;
sensors_event_t temp;
imu.getEvent(&gyro, &accel, &temp);
angle = (gyro_previous+gyro.gyro.x)*(millis()-time_previous)/2 + angle_previous;
angle_previous = angle;
gyro_previous = gyro;
time_previous = millis();

}`

it doesn't seem to be working

Please give useful details about the actual problem, like the full text of error messages. Posting hints are given in the "How to get the best out of this forum" post.

It is a good idea to start with one of the working Adafruit examples provided in the library for that IMU, and make sure you understand it, before writing your own code.

The error is the following: error: cannot convert 'sensors_event_t' to 'float' in assignment
gyro_previous = gyro;
I would like some help to figure out which type of variable can be assigned to a sensors_event_t variable.

generally when using a library for the first time it is a good idea to run some of the example code
e.g. have a look at adafruit_lsm6dsox_test

in loop() it shows how to use sensors_event_t and extract the results

void loop() {

  //  /* Get a new normalized sensor event */
  sensors_event_t accel;
  sensors_event_t gyro;
  sensors_event_t temp;
  sox.getEvent(&accel, &gyro, &temp);

  Serial.print("\t\tTemperature ");
  Serial.print(temp.temperature);
  Serial.println(" deg C");

I already ran that code but I would like to know what type of variable is sensors_event_t.

it is a union - see structsensors__event__t.html

Use something like this instead.
gyro_previous = gyro.gyro.x

However, this expression
angle = (gyro_previous+gyro.gyro.x)*(millis()-time_previous)/2 + angle_previous;
does not appear to take into account the rate gyro contribution and scale factor properly, and will likely produce nonsensical angles.

I’m sorry but I don’t understand still what type of variable can be assigned to sensors_event_t

A structure of the same type.

https://www.w3schools.com/cpp/cpp_structs.asp

Thank you !
I have one last question: how do I find out the structure type?
Could you link some code on how to create a similar struct and assign the gyro value?

Read post #6.

Could you link some code on how to create a similar struct and assign the gyro value?

It is trivial, if you understand what a struct is.

I don’t understand it

Then it would be a good idea to carefully read through the tutorial I linked in post #9, and if that doesn't make the concept clear, perhaps some other tutorials.

  angle = (gyro_previous+gyro.gyro.x)*(millis()-time_previous)/2 + angle_previous;

doesn't make sense even from an "I know nothing about this" perspective.

angle = ((gyro_previous+gyro.gyro.x)*(millis()-time_previous) + angle_previous)/2;

is at least the average of the old reading and the new reading.

You may want to use a general version of that known as a leaky integrator, a kind of low pass filter with an adjustable factor for taking contributions to the new reading as some, not necessarily half, the new reading, and some, also not half the old reading, viz;

angle = (gyro_previous+gyro.gyro.x)*(millis()-time_previous);
angle = 0.1 * angle + 0.9 * angle_previous;

"A little of the new angle, and most of the previous angle"

Just use two constants that add up to 1.0. (0.1 and 0.9 in the example). The bigger the portion of the new angle you use, the faster the value will converge. You can try different values; the serial monitor plotting function can be useful.

Don't take my word for it:

Wikipedia Leaky Integration.

Here's a demo someone cooked up, play with it:

HTH

a7

Well, sort of.

The gyro outputs a rotation rate in degrees per second (after properly scaling the raw data), and to integrate that to measure an angle in 1 dimension only, one can do something like this in pseudocode.

angle = 0.0;
now = millis();
loop:
rate=scalefactor*get_gyro_raw_rate();
loop_time_in_seconds =  (millis()-now) / 1000.0;
now = millis();
angle = rate*loop_time_in_seconds + angle;
print(angle);
goto loop:

PS: I'm not advocating use of goto.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.