void Robot::testOdometry() {
char testCurrent; // use to know if the test is non stop or need to stop and for console printing
char ch;
int lastLeft = 0;
int lastRight = 0;
unsigned long StopRightAt;
unsigned long StopLeftAt;
int TicksStopLeft = 30000; //use to stop the test
int TicksStopRight = 30000;
float MotorLeftPWMTest; // Use this variable instead of motorLeftPWMCurr to avoid change by the motor protection
float MotorRightPWMTest; // Use this variable instead of motorLeftPWMCurr to avoid change by the motor protection
MotorLeftPWMTest = motorSpeedMaxPwm / 2;
MotorRightPWMTest = motorSpeedMaxPwm / 2;
odometryLeft = 0; odometryRight = 0;
resetIdleTime();
Console.println(F("Press 'f' forward"));
Console.println(F("Press 'r' reverse"));
Console.println(F("Press 'z' reset Count"));
Console.println(F("Press 'a' One wheel revolution forward "));
Console.println(F("Press 'b' Five wheel revolution forward"));
Console.println(F("Press 'c' One meter forward and backward non stop wheel stop only when ticks count reach Use to control the ticks/cm"));
Console.println(F("Press 'd' One Rotation 360 Left Use to control the 2 wheel distance WheelBaseCm "));
Console.println(F("Press 'e' One Rotation 360 Rigt Use to control the 2 wheel distance WheelBaseCm "));
Console.println(F("Press 'g' Increase PWM speed test, need to be set before starting the test or test at half max speed "));
Console.println(F(" "));
Console.println (F("Press '0' To Stop"));
Console.println(F(" "));
while (true) {
if ((odometryLeft != lastLeft) || (odometryRight != lastRight)) {
if (testCurrent == 'f') Console.print(F("Non Stop Forward "));
if (testCurrent == 'r') Console.print(F("Non Stop Backward "));
if (testCurrent == 'a') Console.print(F("One wheel revolution "));
if (testCurrent == 'b') Console.print(F("Five wheel revolution "));
if (testCurrent == 'c') Console.print(F("Non stop 1 Meter "));
if (testCurrent == 'd') Console.print(F("Rotation 360 Left "));
if (testCurrent == 'e') Console.print(F("Rotation 360 Righ "));
Console.print(F("PWM = "));
Console.print(MotorLeftPWMTest);
Console.print(F(" left="));
Console.print(odometryLeft);
Console.print(F(" right="));
Console.print(odometryRight);
Console.print(F(" Left Cible "));
Console.print(TicksStopLeft);
Console.print(F(" Right Cible "));
Console.println(TicksStopRight);
lastLeft = odometryLeft;
lastRight = odometryRight;
}
delay(10); //need to test fast to stop correctly
if ((testCurrent == 'a') || (testCurrent == 'b') || (testCurrent == 'd') || (testCurrent == 'e')) { // All test need to stop when reach ticks wanted
if ((odometryLeft >= TicksStopLeft) || (odometryRight >= TicksStopRight)) { //test if we reach the ticks wanted then stop the wheel
MotorLeftPWMTest = 0; MotorRightPWMTest = 0;
setMotorPWM(MotorLeftPWMTest, MotorRightPWMTest, false);
Console.println(F(" "));
Console.print (F("Test Finish at "));
Console.print(F("left="));
Console.print(odometryLeft);
Console.print(F(" right="));
Console.print(odometryRight);
Console.println(F(" "));
odometryLeft = 0; odometryRight = 0;
lastLeft = 0; lastRight = 0;
TicksStopLeft = 30000;
TicksStopRight = 30000;
break;
}
}
if (testCurrent == 'c') {
if ((millis() > StopLeftAt + 2000) && (StopLeftAt != 0)) { // wait 2 sec for the other wheel finish count
Console.println(F("Restart left"));
setMotorPWM(MotorLeftPWMTest, MotorRightPWMTest, false);
StopLeftAt = 0;
}
if ((millis() > StopRightAt + 2000) && (StopRightAt != 0)) { // wait 2 sec for the other wheel finish count
Console.println(F("Restart Right"));
setMotorPWM(MotorLeftPWMTest, MotorRightPWMTest, false);
StopRightAt = 0;
}
if (abs(odometryLeft) >= abs(TicksStopLeft)) {
StopLeftAt = millis();
odometryLeft = 0;
lastLeft = 0;
MotorLeftPWMTest = -1 * MotorLeftPWMTest;
setMotorPWM(0, MotorRightPWMTest, true);
Console.println (F("Stop left wheel"));
}
if (abs(odometryRight) >= abs(TicksStopRight)) {
StopRightAt = millis();
odometryRight = 0;
lastRight = 0;
MotorRightPWMTest = -1 * MotorRightPWMTest;
setMotorPWM(MotorLeftPWMTest, 0, true);
Console.println (F("Stop Right wheel"));
}
}
//read the console
if (Console.available() > 0) {
ch = (char)Console.read();
switch (ch) {
case '0':
MotorLeftPWMTest = 0; MotorRightPWMTest = 0;
setMotorPWM(MotorLeftPWMTest, MotorRightPWMTest, false);
return;
case 'a':
testCurrent = 'a';
setMotorPWM(MotorLeftPWMTest, MotorRightPWMTest, false);
odometryLeft = 0; odometryRight = 0;
TicksStopLeft = odometryTicksPerRevolution;
TicksStopRight = odometryTicksPerRevolution;
break;
case 'b':
testCurrent = 'b';
setMotorPWM(MotorLeftPWMTest, MotorRightPWMTest, false);
odometryLeft = 0; odometryRight = 0;
TicksStopLeft = 5 * odometryTicksPerRevolution;
TicksStopRight = 5 * odometryTicksPerRevolution;
break;
case 'c':
testCurrent = 'c';
Console.println(F(" "));
Console.println (F(" '0' To stop the test "));
Console.println(F(" "));
setMotorPWM(MotorLeftPWMTest, MotorRightPWMTest, false);
odometryLeft = 0; odometryRight = 0;
TicksStopLeft = odometryTicksPerRevolution;
TicksStopRight = odometryTicksPerRevolution;
break;
case 'd':
testCurrent = 'd';
setMotorPWM(MotorLeftPWMTest, 0, false);
odometryLeft = 0; odometryRight = 0;
TicksStopLeft = (int)(2 * PI * odometryWheelBaseCm * odometryTicksPerCm);
TicksStopRight = 30000;
break;
case 'e':
testCurrent = 'e';
setMotorPWM(0, MotorRightPWMTest, false);
odometryLeft = 0; odometryRight = 0;
TicksStopRight = (int)(2 * PI * odometryWheelBaseCm * odometryTicksPerCm);
TicksStopLeft = 30000;
break;
case 'f':
Console.println(F(" "));
Console.println (F(" '0' To stop the test "));
Console.println(F(" "));
testCurrent = 'f';
setMotorPWM(MotorLeftPWMTest, MotorRightPWMTest, false);
break;
case 'g':
MotorLeftPWMTest = MotorLeftPWMTest + 50;
if (MotorLeftPWMTest > 250) MotorLeftPWMTest = 50;
MotorRightPWMTest = MotorLeftPWMTest;
Console.print(F(" New speed is now : "));
Console.println (MotorLeftPWMTest);
Console.println(F(" "));
break;
case 'r':
Console.println(F(" "));
Console.println (F(" '0' To stop the test "));
Console.println(F(" "));
testCurrent = 'r';
setMotorPWM(-MotorLeftPWMTest, -MotorRightPWMTest, false);
break;
case 'z':
testCurrent = 'z';
odometryLeft = 0; odometryRight = 0;
break;
}
}
}
MotorLeftPWMTest = 0; MotorRightPWMTest = 0;
setMotorPWM(MotorLeftPWMTest, MotorRightPWMTest, false);
}