//Turn to a direction using RPS void turnTo(float angle) { int check = 0; bool stop = false; float start = TimeNow(); while ((RPS.Heading() + hCalibrate > angle + 1 || RPS.Heading() + hCalibrate < angle - 1) && !stop && (start + TIMEOUT > TimeNow())) { showInfo(); if (RPS.Heading() > angle) { if ((RPS.Heading() + hCalibrate - angle) > (angle + 360 - RPS.Heading() + hCalibrate)) { // TURN LEFT if (check == -1) { stop = true; //count++ } check = 1; rightMotor.SetPercent(OUTSIDE_TURN_POWER); leftMotor.SetPercent(INSIDE_TURN_POWER); } else { // TURN RIGHT if (check == 1) { stop = true; //count++ } check = -1; rightMotor.SetPercent(INSIDE_TURN_POWER); leftMotor.SetPercent(OUTSIDE_TURN_POWER); } } else { if ((angle - RPS.Heading() + hCalibrate) > (RPS.Heading() + hCalibrate + 360 - angle)) { // TURN RIGHT if (check == 1) { stop = true; //count++ } check = -1; rightMotor.SetPercent(INSIDE_TURN_POWER); leftMotor.SetPercent(OUTSIDE_TURN_POWER); } else { //TURN LEFT if (check == -1) { stop = true; //count++ } check = 1; rightMotor.SetPercent(OUTSIDE_TURN_POWER); leftMotor.SetPercent(INSIDE_TURN_POWER); } } Sleep(100); rightMotor.Stop(); leftMotor.Stop(); Sleep(100); } rightMotor.Stop(); leftMotor.Stop(); }
Algorithm for turnTo:
- Set check, stop, and start to initial values.
- Run a loop that runs while the RPS heading + the heading calibration are greater than the angle + 1 or angle -1.
- If rps heading is greater than the angle proceed to step 4, if not proceed to step 11.
- If the rps heading + the calibrate – the angle is greater than the angle + 360 proceed to step 5, if not proceed to step 8.
- If check is equal to -1, set stop equal to true.
- Set check equal to 1.
- Set the right motor to the outside power, and the left motor to the inside power. Proceed to step 15.
- If the check is equal to 1, set stop equal to true.
- Set check equal to -1.
- Set the right motor to the inside power, and the left to the outside power. Proceed to step 15.
- If the angle – rps heading + calibrate is greater than rps heading + calibrate + 360 – angle then proceed to step 12. If not proceed to step 15.
- If the check is equal to 1, set stop equal to true.
- Set check equal to -1.
- Set the right motor to inside turn power, and the left to outside turn power. Go to step 15.
- Turn off the right and left motors. If loop not broken go back to step 2.
- Turn off the right and left motors.
- End function.