I need help on how to make a function for the autonomous period that, with an inertia sensor, completes turns of 45, 90, 180 degrees, I program in vex code v5 pro
this is the vexcode pro API
- Save your starting rotation
- Compare the starting rotation to the goal to see which way to turn
- Turn until the rotation target is met
Basic Code:
void Turn(double TargetRot){
double StartingRot = Inertial.rotation();
if(Inertial.rotation() < TargetRot){ // Turn right
LeftMotors.spin(forward, 20, percent);
RightMotors.spin(reverse, 20, percent);
waitUntil(TargetRot > StartingRot);
}
else{ // Turn left
LeftMotors.spin(reverse, 20, percent);
RightMotors.spin(forward, 20, percent);
waitUntil(TargetRot > StartingRot);
}
LeftMotors.stop(hold);
RightMotors.stop(hold);
}
To make it not so slow while keeping accuracy, I’d recommending having the speed dynamically change throughout the turn by the amount it has completed so that it starts fast but ends slow.
If you don’t want to do pids or s-curves it’s more consistent to just turn for a set amount of time and then stop as if you use the imu you need to turn slowly or else you will over shoot like crazy.
For the code do something similar to this(I use pros so I don’t know the syntax for the motor commands)
For(int i = 0; i < milliseconds/delay;i++){
Leftside();
Rightside();
Wait(delay);
}
A few things to keep in mind when tuning the delay
- Make sure to do it on the field tiles
- Keep your motors cool(you will end with number way bigger than they should)
- have a consistent setup to test the rotation i use tape on the ground
Good luck
In my experience, moving the robot by time varies by the current battery percentage. An autonomous I made that went forward for 1.1 seconds and grabbed something completely undershot the target when run with a battery at 50% because I tuned the code with the battery near 100%.
You probably should just jump to using pids for it. It’s pretty simple to implement even though moderately difficult to tune. You also you just use s-curves which are a curve that starts with half a sine have goes straight and then another sign wave.