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?

1
  • RobotC is not C! Commented Apr 14, 2017 at 15:21

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.