# Coding inertial Sensor

Our inertial sensor is plugged in and working, and my team was wondering how we could code it so that we can make turns. Plz help

Hi Riptide!

The following steps require your inertial sensor to be initialized. Here is the command to do that, which youâ€™d put in your device identification section:

`vex::inertial inertialSensor = vex::inertial(vex::PORT4);`

Step #1: calibrate your sensor at the beginning of auton to 0 degrees. (It usually takes ~3 seconds to calibrate)

`startCalibration();`

Then, turn the amount you think you need to turn. This can be done by turning your left motors in the opposite direction as your right motors, so the robot turns around a centrepoint.

Once you think youâ€™ve reached the correct angle, you can use the inertial sensor to correct. This is the API command for getting the inertial sensor degrees for yaw.

` double currentDeg = inertialSensor.heading(degrees);`

Why is it yaw, and not pitch or roll?

As you can see in the image, yaw is the horizontal rotation of the plane, which is the only way you can turn on a field without flipping your robot sideways or upside down.

Then, compare the degrees from the inertial sensor to the number degrees you want to turn, and then turn that difference amount.
Example:

`if (degreesTurned != goalAngle) {`
`turnBase(goalAngle-degreesTurned);`
`} `

If you keep having to correct a giant amount each time (more than 20 degrees), there might be an issue with your turning function that will be inefficient to fix with an inertial sensor.

Tip: Use a do-while loop to correct at least once each time you turn! A do-while loop always executes at least once because it is a posttest loop, which means it tests the condition after executing the contents of the loop. Here is an example of what that could look like:

`do {`
`turnBase(goalAngle-degreesTurned);`
`} while (degreesTurned != goalAngle);`

5 Likes

Thanks for the help, but is there a definite command to set up with the sensor with 4 motors so that you can just tell it to turn that many degrees? I have tried everything and nothing works.

Potentially worth noting that I believe in both Vexcode and PROS the inertial sensor automatically calibrates at the beginning of the program. So, this technically isnâ€™t needed and leaving it out wonâ€™t create a source of error for when you are debugging.

1 Like

I donâ€™t think thereâ€™s a definite command to just tell the motors, â€śturn this many degrees and use the inertial sensor to correct!â€ť

Coding a function to make corrections is the way my team did it.

EDIT: There is a way to do that! Thank you, I learned something today!

1 Like

A 4-motor drivetrain using the internal sensor will accomplish Inertial-based turning.

4 Likes