How To Use Inertial Sensor

  Inertia.setHeading(0, degrees);
  while(Inertia.heading(degrees > 90)){
  Drivetrain.turn(left);
  }

This doesn’t work. It gives me an error for the >, and if I remove it, there is no error, but it doesn’t fulfill my purpose. How would i make it so that the robot turns until the sensor reads 90 degrees? Also, if I were to successfully do this, would it make completely accurate turns?

1 Like

You need to put the “> 90” outside of the parentheses, like this:
while(Inertia.heading(degrees) > 90) {}

1 Like

I ran this, and the robot just didn’t move.

Sorry if this was the confusion, but you still need to have the Drivetrain.turn(left); function inside the while loop. I’m not sure why I didn’t include it in my reply.

1 Like

probably needs to be “less than”, e.g. while(Inertia.heading(degrees) < 90) {}

1 Like

You have to say “while not” instead of while

I did. I had exactly this:

  Inertia.setHeading(0, degrees);
  while(Inertia.heading(degrees) > 90) {
    Drivetrain.turn(left);
  }

This should, in theory, do a perfect 90 or 270 degree turn, right? But it’s not moving.

Guessing this is related to your other posting?

In this instance, you first set the heading to 0. You then start your while loop with Inertia.heading(degrees) equal to zero. The while loop, as configured, will only run when Inertia.heading(degrees) is greater than 90 so, in short, it will never execute the turn. You want your loop to run when Inertia.heading(degrees) is less than 90.

You also may want to investigate using rotation instead of heading.

Of course, as to the theoretical perfect 90? The saying attributed to either (or both) Yogi Berra and Albert Einstein very much applies to VEX: " In theory there is no difference between theory and practice** - in practice there is"

3 Likes

Also just noticed that you indicate a left turn? Don’t you mean right turn?

2 Likes

What @willjlong said will move the robot, but it will never stop. This is because you never told the robot to stop. After the while loop, you also need to tell the drivetrain to stop.

2 Likes

First off you need to calibrate your inertia/gyroscope (they are same thing). You also are going to want to round the inertia heading because the while commands are slower.

I believe when turning left the robot’s head is negative. For this reason, you would want

Inertia.setHeading(0, degrees);
while(Inertia.heading(degrees) > -90) {
    Drivetrain.turn(left);
}

You could do this for tuning right

Inertia.setHeading(0, degrees);
while(Inertia.heading(degrees) < 90) {
    Drivetrain.turn(right);
}

I would use the rotation instead of heading as once you get to 180 or -180 it loops around to the other end of the scale. Rotation keeps increasing until you reset it. This allows you to turn more than 180 degrees.

Also if you overshoot the target the robot would go all the way back round. And this would repeat forever, meaning the robot would turn on the spot forever.

I would suggest researching PID controllers or adding an inertial sensor to your drive train in the port config and using the turn-to-heading function. What you are trying to do is built in the VEXCode V5 drivetrain tool.

Hope this helps. :slight_smile:

1 Like

How do I calibrate it?

There is a Blocky block under sensors and I’m not sure in c or python

Since you’re using a drivetrain you can just include the inertial sensor inside the making of that. Inside the robot-config.cpp file, find the line where your drivetrain is declared and change it to look something like this, just with the names of your motors / motor groups and the name of your inertial sensor.

smartdrive Drivetrain = smartdrive(LeftDriveSmart, RightDriveSmart, Inertial, 319.19, 320, 40, mm, 1);

Once that is done, you can turn your robot to 90 degrees by using the function

Drivetrain.turnToRotation(90, degrees);

I already figured it out, but thank you!

1 Like

Another Solution:

while(Inertia.heading(degrees > 90)){ // Your comparission needs to be somewhere else
 Drivetrain.turn(left); 
 }

you are basicly saying is the unit degrees > 90 that doesn’t make sense to the computer and your comparasion is also the wrong direction because that will not be true when you start., 0 is not greater than 90 so it exits. What you actually need is this updated code :

 Drivetrain.turn(right); // This is most efficent 
while(Inertia.heading(degrees) < 90){ 
    task::sleep(25) // You were telling it to constantly turn which is not needed
 }     
Drivetrain.stop( );// Put your stop here (I haven't worked with drivetrain functions much thou)