Hi, I’ve tried to use the example code on vexcode text to use the inertial sensor for more accurate turns. The program for some reason always turns the right way but never stopped turning. Is there some body who had similar issues as me? If so what did you do to fix the issue?
Have you tried using the inertial function rotation it makes so it is positive both ways. This is the code I used, Hope it helps you, One of the main problems I had was not calibrating it.
void turnRight(double degree, double speed){
turnInertial.setRotation(0, degrees);
leftFWD.spin(forward, speed, pct);// moves close at full speed
rightFWD.spin(reverse, speed, pct);
leftBack.spin(forward, speed, pct);
rightBack.spin(reverse, speed, pct);
task::sleep(200);
double difference= degree - std::abs(turnInertial.rotation());
while(difference>65){
difference= degree - turnInertial.rotation();
task::sleep(20);
}
leftFWD.spin(forward, speed*0.1, pct);//slows down
rightFWD.spin(reverse, speed*0.1, pct);
leftBack.spin(forward, speed*0.1, pct);
rightBack.spin(reverse, speed*0.1, pct);
waitUntil(turnInertial.rotation() > (degree-2));
leftFWD.stop();
rightFWD.stop();
leftBack.stop();
rightBack.stop();
}
Edit: Fixed [code][ /code] tags
In a separate thread, I mentioned the concept of DRY - Don’t Repeat Yourself.
Based purely on the function name turnRight
, I would think that you would be likely to copy-and-paste this code, change the function name to turnLeft
and change the motor directions. This isn’t the best programming practice, as, if you discover a problem with turning right, the same problem is likely to exist when turning left and you’ll have to fix it in 2 places instead of just one.
I would challenge you to think about how to make a general purpose turn
function that could handle both left and right turns.