0

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:

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);
}

For reference, these are my Pragma settings:

#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)

When I excecute the code, the Robot's encoder values are very close, but the robot stops moving when they reach 1000. I thought the code I wrote should return the values of the encoders back to 0 after they reach 1 thousand, and thereby the code should re-iterate in the shell loop 10 times (in this case). What have I done wrong?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nosn
  • 127
  • 1
  • 1
  • 6

2 Answers2

1

You are updating Tcount at the wrong place. Do it Just at the end of the outer loop.

What you have written now makes Tcount increase everytime it moves ahead. By the time it reaches 1000 steps, Tcount is already 1000.

Your times is 10. So `Tcount >= time and it wont enter the outer while loop again.

Ajay Brahmakshatriya
  • 8,993
  • 3
  • 26
  • 49
0

It seems that the Control Variable of the inner loop (i.e. nMotorEncoder[RM]) is never updated, this means that the inner loop will iterate forever. That is, you will never go back to the body of external loop

tsik
  • 609
  • 8
  • 10