2017-04-14 179 views
0

我目前在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次(在这种情况下)。我做错了什么?

+0

RobotC是**不是C **! – Olaf

回答

1

您正在更新Tcount错误的地方。这样做只是在外部循环的末尾。

你现在写的东西让Tcount每次都有所增加。当它达到1000步时,Tcount已经是1000.

你的times是10.所以`Tcount> = time并且它不会再次进入outer while循环。

0

似乎内部循环的控制变量(即nMotorEncoder [RM])从不更新,这意味着内部循环将永远迭代。也就是说,你永远不会回到外部循环的身体