Inertial Sensor Programming for RMS C++

We recently received our new Inertial Sensor and are wondering about how to program it… specifically with turning. We never used the V4 gyro in the past, but we want to learn how to use the new Inertial Sensor to increase the consistency of our turns. Any examples of how to use this new sensor to increase effectiveness of turns in RMS C++ would be greatly appreciated. Also, if anyone has any advice for using these sensors that would be nice too.

Here is some example code.

Make sure your sensor is in a place that is far away from any motors or devices that cause vibrations since any vibrations cause the sensor to miss calibrate.


Thank you! Also, I’ve seen multiple teams mount their old gyroscopic sensors on rubber joints, would you recommend doing this with the new sensor? Also, should it be mounted towards the center of the chassis?

It does not matter where you put the sensor.
Just make sure that the sensor is level horizontally, because any degree of horizontally off will translate to your turns being off.

Sounds good… thanks for the help! It’s greatly appreciated.

@EagleBots is giving some advice that’s out of date for the new sensor. I’ll mention that further down.

The basics of turning with a sensor (or really, doing anything with a sensor):

  1. Pick a sensor value you would like to reach. In this case, it will be a heading (0-360deg) or yaw value (-180 to 180deg)
  2. Determine what what needs to be done to reach it. This involves reading the current sensor value and doing some math against the value you want to get to. For example, if your robot was facing to heading 340 and you wanted to get to heading 20, you would want to turn 40 degrees past 360 to get to 20, and wouldn’t want to turn 320 degrees through heading 180 to get there.
  3. Do the action you determined you needed to do in part 2, until the sensor reads the value you picked in part 1, plus or minus a bit.

As for out of date advice, the new sensor does not need to be perfectly horizontal as it has gyroscopes in all three axes of rotation, and uses its 3-axis accelerometers to determine where gravity is during calibration and infer its orientation accordingly. It does appear to assume it’s mounted orthogonally to gravity, though, so pitch and roll readings will be “off” by however much you mount it at an angle. I.e. if you mount it on top of an arm pitched down by 30 degrees, it will read an initial pitch of about -30. Yaw/heading is perpendicular to gravity, though, so that should not be thrown off by being mounted at an angle.

As for sensitivity to vibration during operation, I have not tested it as thoroughly as a competition team would, but some basic side-by-side testing by my coworker Sam seemed to indicate that the new sensor is far more robust against vibration than the old gyro was. (You should still avoid wiggling it about while calibrating, though.)


So, is the sample code accurate still then?

For the most part, no. It can only do right turns, whereas a more capable turn-to-heading function would be able to do both. The code snippet provided by @EagleBots would be a partial example of step 3 in the list of steps I gave. It assumes that you have already done step 2 and determined that you need to make a clockwise turn, but it cannot make clockwise turns past the rollover point (180 for yaw, 360 for heading) because of its lazy end condition. Instead of(value <= set_point), a better condition would be something like (abs(value - set_point) <= acceptable_error). It could work for any clockwise rotation if using absolute turn angle, though there usually isn’t much of a case for using it over yaw or heading.


Okay, so I was writing the code, and it isn’t compiling and I don’t know why. Also, I am confused as to whether what sections of the code are part of the functions or not and where they need to be placed in the competition template.

Here is my code:
void IntertialSensorTurn (double DegreeAmount, int velocityUnits)
RightWheel2.setVelocity(100, velocityUnits::pct);
LeftWheel2.setVelocity(100, velocityUnits::pct);

while (InertialSensor.value(rotationUnits::deg) <= DegreeAmount)


Part of the error message of the compiler will tell you why. In your case, it’s because you too faithfully copied code that was written for the gyro and not the inertial sensor:

InertialSensor.value(rotationUnits::deg) // bad, does not work

If you look at the documentation for the inertial sensor class, you will find that the value method takes no arguments. In fact, I’m not even sure what we have value actually returning, so you probably don’t want to use it in the first place. The methods yaw, heading, orientation, and rotation are all much more explicit and descriptive about what they return.

The competition template is a set of three perfectly ordinary functions, with a couple lines in the main function to mark them as special. You are trying to write a perfectly ordinary function(s). Functions can’t be declared and defined inside of other functions. I can elaborate if those hints aren’t enough.