User control problem

I just wrote a code last night for driving with mecanum wheels that has a toggle button that reverses the drive. It worked fine when it was isolated, but as soon as I incorporated it into the main code, the drive wouldn’t work and it would jitter. I know that it’s a programming error, and I need a second person’s opinion on this because I am the only programmer on my team.

Here is the user control portion of my code:

[PHP]int drivermode=1;
task drive()
{

while (true)
{
  if(vexrt[Btn7D] && vexrt[Btn8D] && drivermode==1)
  {Drivermode=2;
  	wait1Msec(1000);
  	}
  		  if(vexrt[Btn7D] && vexrt[Btn8D] && drivermode==2)
  		  {
  		  	drivermode=1;
  		  	wait1Msec(1000);
  		  	}

  		  	if(drivermode==2);
  		  	{

int X2 = 0, Y1 = 0, X1 = 0, threshold = 15;

//Create “deadzone” for Y1/Ch3
if(abs(vexRT[Ch3]) > threshold)
Y1 = vexRT[Ch3];
else
Y1 = 0;
//Create “deadzone” for X1/Ch4
if(abs(vexRT[Ch4]) > threshold)
X1 = vexRT[Ch4];
else
X1 = 0;
//Create “deadzone” for X2/Ch1
if(abs(vexRT[Ch1]) > threshold)
X2 = vexRT[Ch1];
else
X2 = 0;

motor[port3] = Y1 - X2 - X1;//rightfront
motor[port2] = Y1 - X2 + X1; //rightback
motor[port5] = Y1 + X2 + X1; //leftfront
motor[port4] = Y1 + X2 - X1; //left back
wait1Msec(1);
}

  		  	if(drivermode==1);
  		  		  		  	{

int X2 = 0, Y1 = 0, X1 = 0, threshold = 15;

//Create “deadzone” for Y1/Ch3
if(abs(vexRT[Ch3]) > threshold)
Y1 = vexRT[Ch3];
else
Y1 = 0;
//Create “deadzone” for X1/Ch4
if(abs(vexRT[Ch4]) > threshold)
X1 = vexRT[Ch4];
else
X1 = 0;
//Create “deadzone” for X2/Ch1
if(abs(vexRT[Ch1]) > threshold)
X2 = vexRT[Ch1];
else
X2 = 0;

motor[port3] = -Y1 - X2 + X1;//rightfront
motor[port2] = -Y1 - X2 - X1; //rightback
motor[port5] = -Y1 + X2 - X1; //leftfront
motor[port4] = -Y1 + X2 + X1; //left back
wait1Msec(1);
}

}

}

task Flipper()
{
while(true){//claw code
if(vexRT[Btn5U])
{
motor[port6] = 60;

}
else if (vexRT[Btn5D])
{motor[port6] = -60;
}
else
{
motor[port6] = 0;
}
}}

task Claw(){
while(true){
if(vexRT[Btn8U])
{

motor[port8] = 60;

}
else if (vexRT[Btn8D])
{
motor[port8] = -30;

}
else
{

motor[port8] = 0;

}
}
}

task Arm(){ // arm code

while(true) {
if(vexRT[Btn6U] == 1)
{
motor[port1] = motor[port9] = motor[port7] = motor[port10] = 110;

}

else if(vexRT[Btn6D] ==1)
{

motor[port1] =  motor[port9] = motor[port7] =  motor[port10] = -100;

}
else
{
motor[port1] = motor[port9] = motor[port7] = motor[port10] = 0;
}

}
}

task usercontrol() //This is the code that runs all of the tasks.
{
startTask(Arm);
wait1Msec(1);
startTask(drive);
wait1Msec(1);
startTask(Flipper);
wait1Msec(1);
startTask(Claw);
wait1Msec(1);
}[/PHP]

Thanks!

Semi colon after if( drivermode == 1), same for the other test. Remove those and it should work.

well that was an easy fix. Thanks a lot, Jpearman.

It was an easy fix :slight_smile:

If anyone else wants to use this type of code, that is reverse what is considered the front of the robot, there are a few things that can be done to streamline the code.

First, the code that reads the joysticks is duplicated, it can be outside the two conditional tests and used by either. The other thing is that when you look at the mecanum drive algorithm, all that happens is that Y1 and X1 are negated. A simplified version may look like this.

int drivermode = 1;

task drive()
{
    ////Create "deadzone" variables. Adjust threshold value to increase/decrease deadzone
    int X2 = 0, Y1 = 0, X1 = 0, threshold = 15;
    
    while (true)
    {
        if(vexRT[Btn7D] && vexRT[Btn8D]) {
            // 1 becomes 0, 0 becomes 1
            drivermode = 1 - drivermode;
            // wait for release
            while(vexRT[Btn7D] && vexRT[Btn8D])
                wait1Msec(10);
        }
        
        // Clear variables
        Y1 = 0;X1 = 0;X2 = 0;

        //Create "deadzone" for Y1/Ch3
        if(abs(vexRT[Ch3]) > threshold)
            Y1 = vexRT[Ch3];
        
        //Create "deadzone" for X1/Ch4
        if(abs(vexRT[Ch4]) > threshold)
            X1 = vexRT[Ch4];

        //Create "deadzone" for X2/Ch1
        if(abs(vexRT[Ch1]) > threshold)
            X2 = vexRT[Ch1];
    
        if( drivermode == 1 ) {
            Y1 = -Y1; X1 = -X1;
        }
        
        motor[port3] = Y1 - X2 - X1; //rightfront
        motor[port2] = Y1 - X2 + X1; //rightback
        motor[port5] = Y1 + X2 + X1; //leftfront
        motor[port4] = Y1 + X2 - X1; //left back
        
    wait1Msec(10);
    }
}