I'm currently programming in RobotC, for a Vex 2.0 Cortex. I'm using encoders to make my robot go straight.
This is my code:
#pragma config(I2C_Usage, I2C1, i2cSensors)
#pragma config(Sensor, dgtl2, , sensorDigitalIn)
#pragma config(Sensor, dgtl7, , sensorDigitalOut)
#pragma config(Sensor, I2C_1, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Sensor, I2C_2, , sensorQuadEncoderOnI2CPort, , AutoAssign )
#pragma config(Motor, port1, RM, tmotorVex393_HBridge, openLoop, reversed, encoderPort, I2C_2)
#pragma config(Motor, port10, LM, tmotorVex393_HBridge, openLoop, encoderPort, I2C_1)
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
/* Port 1 is right motor*/
//all functions expect no reverse motors (in port menu)
//**GLOBAL VARIABLES**
int buttonSTATE = 0;
//**MOVE FUNCTIONS**
void goforwards(int time)
{
int Tcount = 0;
int speed1 = 30;
int speed2 = 30;
int difference = 5;
motor[LM] = speed1;
motor[RM] = speed2;
while (Tcount < time)
{
nMotorEncoder[RM] = 0;
nMotorEncoder[LM] = 0;
while(nMotorEncoder[RM]<3000)
{
int REncoder = -nMotorEncoder[RM];
int LEncoder = -nMotorEncoder[LM];
if (LEncoder > REncoder)
{
motor[LM] = speed1 - difference;
motor[RM] = speed2 + difference;
}
if (LEncoder < REncoder)
{
motor[LM] = speed1 + difference;
motor[RM] = speed2 - difference;
}
wait1Msec(100);
}
Tcount ++;
}
}
//**CONTROL STRUCTURE:**
task main()
{
goforwards(1);
}
When I execute the code, the Robot's encoder values are very close, but the robot quickly starts to veer to the left. What are possible causes of this? Is it something in the code?