I’m using IMEs on my base to drive straight and to the correct distance and it works but sometimes the robot keeps going forward or keeps turning, etc. and doesn’t stop when the target is reached. When the robot gets stuck turning or going forward, the lights on the IMEs are still flashing green so I think the IMEs aren’t damaged
- Do I have to reset the IMEs in another place other than in preauton and in the function itself.
- Is it better to use abs() f an encoder or to multiply the value by -1?
This is the code for the 4 drive functions
/// pot
void Backward (int distance, int speed, int correction)
{
resetMotorEncoder(port9);
resetMotorEncoder(port2);
while (getMotorEncoder(port9) < distance && ((getMotorEncoder(port2)) * -1 ) < distance)
{
if( getMotorEncoder(port9) > ((getMotorEncoder(port2)) * -1) )
{
motor[LDriveBack] = speed - correction;
motor[RDriveBack] = -speed;
}
if( getMotorEncoder(port9) < ((getMotorEncoder(port2)) * -1) )
{
motor[LDriveBack] = speed;
motor[RDriveBack] = -speed + correction ;
}
else if ( getMotorEncoder(port9) == ((getMotorEncoder(port2)) * -1) )
{
motor[LDriveBack] = speed;
motor[RDriveBack] = -speed;
}
}
motor[LDriveBack] = 0;
motor[RDriveBack] = 0;
}
void Forward (int distance, int speed, int correction)
{
resetMotorEncoder(port9);
resetMotorEncoder(port2);
while (((getMotorEncoder(port9)) * -1) < distance && getMotorEncoder(port2) < distance)
{
if( getMotorEncoder(port2) > ((getMotorEncoder(port9)) * -1) )
{
motor[LDriveBack] = -speed ;
motor[RDriveBack] = speed - correction;
}
if( getMotorEncoder(port2) < ((getMotorEncoder(port9)) * -1) )
{
motor[LDriveBack] = -speed + correction;
motor[RDriveBack] = speed ;
}
else if ( ((getMotorEncoder(port9)) * -1) == getMotorEncoder(port2) )
{
motor[LDriveBack] = -speed;
motor[RDriveBack] = speed;
}
}
motor[LDriveBack] = 0;
motor[RDriveBack] = 0;
}
void TurnR (int distance, int speed, int correction)
{
resetMotorEncoder(port9);
resetMotorEncoder(port2);
while (getMotorEncoder(port2) < distance && ((getMotorEncoder(port9)) *-1) < distance)
{
if( getMotorEncoder(port9) > getMotorEncoder(port2) )
{
motor[LDriveBack] = speed - correction;
motor[RDriveBack] = speed;
}
if( getMotorEncoder(port9) < getMotorEncoder(port2) )
{
motor[LDriveBack] = speed;
motor[RDriveBack] = speed - correction ;
}
else if ( getMotorEncoder(port9) == getMotorEncoder(port2) )
{
motor[LDriveBack] = speed;
motor[RDriveBack] = speed;
}
}
motor[LDriveBack] = 0;
motor[RDriveBack] = 0;
}
void TurnL (int distance, int speed, int correction)
{
resetMotorEncoder(port9);
resetMotorEncoder(port2);
while ( ((getMotorEncoder(port9)) *-1) < distance && ((getMotorEncoder(port2)) *-1) < distance)
{
if( getMotorEncoder(port9) > getMotorEncoder(port2) )
{
motor[LDriveBack] = -speed ;
motor[RDriveBack] = -speed + correction;
}
if( getMotorEncoder(port9) < getMotorEncoder(port2) )
{
motor[LDriveBack] = -speed + correction ;
motor[RDriveBack] = -speed ;
}
else if ( getMotorEncoder(port9) == getMotorEncoder(port2) )
{
motor[LDriveBack] = -speed;
motor[RDriveBack] = -speed;
}
}
motor[LDriveBack] = 0;
motor[RDriveBack] = 0;
}
Thanks in advance