Starting to Implement the RTIMULib Library

I’ve continued to work on the example sketch provided for RTIMULib (as referenced in my previous post). As stands, the sketch is quite cryptic and roundabout, so I’ve made an effort to clean it up and split the code up more.

The sketch used to just print off the fused pose data over serial, but now I’ve actually made a function that, when called, will return the current Euler angles, pressure and temperature recorded by (and fused from) the IMU.

Firstly, I had to find a way of setting all 5 of these values from inside a callable function. Now I could just use global variables – and may still do, if this seems easier – but that doesn’t seem like a very clean or efficient way to do it to me.

The main other thing I can do is return multiple variables from the function. Now of course I can’t do this explicitly as such, and so I researched it a bit and determined the best thing to do is to define a custom variable type as a structure containing the 5 variables (floats) that I need, and then assign this return type to the function.

So I made a new data structure `OutputData` using keywords `typedef struct`:

typedef struct { //Creating variable type to hold all output data
  float x;
  float y;
  float z;
  float pressure;
  float temp;
}OutputData;

This means that now, anywhere in the sketch, I can use variables of type `OutputData` which will contain the listed 5 floats.

To make an instance of this variable type, I just initialise a variable:

OutputData newvariable;

And can then access the subvariables (probably not an actual term but makes sense in my head) with `newvariable.x`, `newvariable.y`, `newvariable.z`, `newvariable.pressure` and `newvariable.temp`, all of which are floats.

This makes it quite easy to use my function. The function definition goes as follows:

OutputData IMUMain() {
  OutputData return_values; //This will be used to return from the function

  //Function body

  return_values.x = thingyX;
  return_values.y = thingyY;
  return_values.z = thingyZ;
  return_values.pressure = thingyPressure;
  return_values.temp = thingyTemp;
  
  return return_values;
}

(Where `thingyX`, `thingyY` and so on represent the output values ascertained by the function body)

Then I can just call the function from within, say, the main loop:

void loop() {
  OutputData sensors; //Create an 'OutputData' variable to read the function
  sensors = IMUMain(); //read the function to 'sensors'
  Serial.print("X: ");
  Serial.print(sensors.x);
  Serial.print(" Y: ");
  Serial.print(sensors.y);
  Serial.print(" Z: ");
  Serial.print(sensors.z);
  Serial.print(" pressure: ");
  Serial.print(sensors.pressure);
  Serial.print(" temp: ");
  Serial.print(sensors.temp);
  Serial.println();  delay(1000); //Do this once a second
}

The first two lines are the most important here. I need to have a variable of the same compound type to read the function I defined into, but once that is done I can just read that variable for everything.

I could even make this a global variable… (full circle!)

Anyway, running that code, with everything else in the sketch of course, produces the following over serial:

New example sketch over serial
New example sketch/functions over serial

It works just as I’d expect it to! I still can’t believe that there isn’t an easier way to do this, especially since someone went to the trouble to make this whole library in the first place. Oh well, I’m probably missing something blatantly obvious but I’ll make do for now.

Next up I need to finalise the functions I’ll be using in the main sketch and the syntax for calling them, and then make any global variables I need as well as converting the pressure reading to an altitude. I’ll also need to zero all of the readings in the main sketch – or more likely, just use the delta value.

Hopefully I’ll get to the end of dealing with this sensor fusion soon. I still don’t know if this library is actually going to work well, but let’s give it a try.


Edit: Although I will still try to get this to work, and have spent a long time on it, the library uses up a lot of flash memory and is, as I have pointed out, fairly cumbersome to implement, and so I’m still looking around elsewhere. I’ve found Michael Baker’s Polulu_Open_IMU which promises to be like the first sketch I tried but using an improved algorithm. For the sake of having the best and fastest solution, even if it means losing a day or so (and Christmas is coming up!) I might give it a whirl to see if it suits my needs.

One thought to “Starting to Implement the RTIMULib Library”

Leave a Reply

Your email address will not be published. Required fields are marked *