I’m a beginner VEX IQ user and I made a very simple program:
- Drive forward for 20 inches
- Turn right for 90 degrees
- Drive forward for 10 inches
- Drive reverse for 20 inches
- Drive forward for 10 inches
- Turn right for 90 degrees
- Drive forward for 20 inches
It’s supposed to simulate the U-turn of a car, but it never turns as I want it to. For example, when I use “turn right for 90 degrees”, it turns forever. When I use “turn right” it turns for a split second. When I use “turn right” and then “wait until BrainInertial heading in degrees = 90” it turns to 90 degrees and then doesn’t turn for the next “turn right”
I’m wondering if my Gen 2 parts (I’m using the Brain Inertial) are broken or if I’m just bad at coding. Please reply if you have an answer for this behavior.
1 Like
im not really sure how to code for IQ as ive never done it but im sure its similar to what I do.
You have the right idea with the “wait until BrainInertial heading in degrees = 90." Remember, you just told the robot to turn until the inertial returns a heading of 90 degrees. if you ask it to do the same thing’ again, it will not move because the inertial heading is already at 90 degrees. Following?
To fix this I think you can do two things.
- After telling the bot to turn until the inertial heading reads 90, set the inertial heading to 0 degrees. It would look something like this-
while (1) {
//turns the drive right 90 degrees
LeftDrive.spin(forward);
RightDrive.spin(reverse);
//waits for the inertial to return an angle of 90 degrees
waitUntil(Inertial.heading(degrees) == 90);
//stops the turn (not sure if this is necessary)
LeftDrive.stop(brake);
RightDrive.stop(brake);
//resets the inertial heading to zero
Inertial.setHeading(0, degrees);
//turns the drive right 90 degrees again
LeftDrive.spin(forward);
RightDrive.spin(reverse);
//waits for the inertial to return an angle of 90 degrees
waitUntil(Inertial.heading(degrees) == 90);
//stops the turn (not sure if this is necessary)
LeftDrive.stop(brake);
RightDrive.stop(brake);
//resets the inertial heading to zero
Inertial.setHeading(0, degrees);
}
- you can simply add another turn onto your previous one. So if you want to turn 90 degrees, you tell the drive motors to spin until the inertial returns a heading of 90. Next, if you want to turn 90 degrees again, you tell the drive motors to spin until the inertial returns a heading of 180. Something like this should work-
while (1) {
//turns the drive right 90 degrees
LeftDrive.spin(forward);
RightDrive.spin(reverse);
//waits for the inertial to return an angle of 90 degrees
waitUntil(Inertial.heading(degrees) == 90);
//stops the turn (not sure if this is necessary)
LeftDrive.stop(brake);
RightDrive.stop(brake);
//turns the drive right 90 degrees again
LeftDrive.spin(forward);
RightDrive.spin(reverse);
//waits for the inertial to return an angle of 90 degrees
waitUntil(Inertial.heading(degrees) == 180);
//stops the turn (not sure if this is necessary)
LeftDrive.stop(brake);
RightDrive.stop(brake);
}
Pros and Cons
Pros of reseting the inertial sensor
- you will never exceed 359.99 degrees if you reset after every turn
- 90 degrees is always 90 degrees
- you dont have to keep track of what your inertial heading is after every single turn
Pros of stacking turn values
- its faster and easier to code if you have a good memory
- can be better for small projects
Conclusion
I recommend going with the reset option simply because its more intuitive, but you should do what suits your needs.
3 Likes
Since degrees is technically a decimal, this is almost never the actual case. What I mean is, because you have a certain number of bits that represent the value after the decimal, what you will frequently find in code is that you might be able to get to 89.9997 degrees or 90.0003 degrees (example only, depending on what size float/double is being used to store the degrees, there’s probably a lot more digits after the decimal). Point stands, waiting for code to be exactly equal to 90.00000000000 is not a good plan.
What’s best to do here is to compare the turn until you’ve made at least 90 degrees, then stop; in other words, keep turning until your degrees turned is greater than 90.
Now, this means you may overshoot 90 if you’re turning too fast. This is going to start driving you to other mechanisms to get you closer to that “exact” turn you’re looking for.
7 Likes
Ah thank you. I didn’t know you could set the Brain Inertial to a certain amount of degrees. But, as @EngineerMike said, the 1:2 gear ratio on my robot always made it turn infinitely because it could never actually reach 90.00000000000. And, even though I corrected it to BrainInertial heading in degrees > 90, it still would drift over because of the moment it creates when it drives (even if i set the gear ratio to 1:1 which is extremely slow).
Do the gen 2 parts have gyros?
If so, make sure it’s being used.
Also, send a pic of your drivetrain, if its built badly it could be making turning weird.
2 Likes
For bonus points, set your velocity to a proportion of your error away from 90° so that you start the turn at high speed and slow down for precision as you get closer. Probably not feasible to add the integral and derivative in block code though
Can you post the code you’re currently working on? A couple of things could be going on here. My best guess is if your robot is moving while the brain says “Calibrating Inertial”, then it’s going to get confused as to what is left and right.