我目前在RobotC中为Vex 2.0 Cortex编程。我使用编码器使我的机器人直行。为什么我的嵌套while循环不工作?
这是我的代码:
void goforwards(int time)
{
int Tcount = 0;
int speed1 = 40;
int speed2 = 40;
int difference = 10;
motor[LM] = speed1;
motor[RM] = speed2;
while (Tcount < time)
{
nMotorEncoder[RM] = 0;
nMotorEncoder[LM] = 0;
while(nMotorEncoder[RM]<1000)
{
int REncoder = -nMotorEncoder[RM];
int LEncoder = -nMotorEncoder[LM];
if (LEncoder > REncoder)
{
motor[LM] = speed1 - difference;
motor[RM] = speed2 + difference;
if (motor[RM]<= 0)
{
motor[RM] = 40;
motor[LM] = 40;
}
}
if (LEncoder < REncoder)
{
motor[LM] = speed1 + difference;
motor[RM] = speed2 - difference;
if (motor[RM]<= 0)
{
motor[RM] = 40;
motor[LM] = 40;
}
Tcount ++;
}
}
}
}
task main()
{
goforwards(10);
}
供参考,这些都是我的附注设置:
#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)
当我excecute代码,机器人的编码器值非常接近,但机器人停止在达到1000时移动。我认为我写的代码应该在编码器的值达到1千之后将编码器的值返回到0,从而代码应该在shell循环中重复迭代10次(在这种情况下)。我做错了什么?
RobotC是**不是C **! – Olaf