# How do I make tank control with fixed speed percentage?

int main() {
// Initializing Robot Configuration. DO NOT REMOVE!
vexcodeInit();

// Deadband stops the motors when Axis values are close to zero.

while (true) {
// Get the velocity percentage of the left motor. (Axis3)
int leftMotorSpeed = Controller1.Axis3.position();
// Get the velocity percentage of the right motor. (Axis2)
int rightMotorSpeed = Controller1.Axis1.position();

``````// Set the speed of the left motor. If the value is less than the deadband,
// set it to zero.
// Set the speed to zero.
LeftMotor.setVelocity(0, percent);
} else {
// Set the speed to leftMotorSpeed
LeftMotor.setVelocity(leftMotorSpeed, percent);
}

// Set the speed of the right motor. If the value is less than the deadband,
// set it to zero.
// Set the speed to zero
RightMotor.setVelocity(0, percent);
} else {
// Set the speed to rightMotorSpeed
RightMotor.setVelocity(rightMotorSpeed, percent);
}

else if(x != 1){

}

// Spin both motors in the forward direction.
LeftMotor.spin(forward);
RightMotor.spin(forward);

wait(25, msec);
``````

}
}

So you see, I want to be able to move the robot at a fixed speed, instead of it being proportional to the joystick. For example, if you see whats inside the first else statement for leftMotorSpeed, it averages the speed of the robot depending on how far the joystick is. But I dont want that to happen, no matter how far the joystick is, I want the robot to go at a fixed speed of, lets say, 50 percent. How do I do that?

You can simply do thisâ€¦

Edit: this is wrong check below

``````if (abs(leftMotorSpeed) < deadband) {
// Set the speed to zero.
LeftMotor.setVelocity(0, percent);
} else {
// Set the speed to 50
LeftMotor.setVelocity(50, percent);
}
``````

No matter what the joystick value is if it is greater than 5 the velocity will be set to 50.

1 Like

but you see I tried that, but for some reason it wont go backwards

Ohh yeah sorry, I forget that part tooâ€¦

``````if (abs(leftMotorSpeed) < deadband) {
// Set the speed to zero.
LeftMotor.setVelocity(0, percent);
} else if(leftMotorSpeed > 0) {
// Set the speed to 50
LeftMotor.setVelocity(50, percent);
} else {
// Set the speed to -50
LeftMotor.setVelocity(-50, percent);
}
``````

If the joystick value is negative the percentage assigned to the motor should be negative too.

5 Likes

wait wait wait, im confused, why didnt you add â€śabsâ€ť behind leftMotorSpeed in the else if statement?

It is to check weather the value is positive or negative. You want to be able to go forwards and backwards.

2 Likes

thank you so much!!!