Automatic acceleration/deceleration of launcher

I’m trying to get our launcher to automatically accelerate and decelerate. While the same thing could be done by the driver, we’d prefer an automatic solution.

The slow acceleration code works fine, but after that it doesn’t decelerate no matter what. It’s not caught in a loop- other functions still work fine. The base drives and the intake works fine, but the launcher just doesn’t stop.

Here’s the code and related variables, isolated:


int launcherAccel = 0;

if (vexRT[Ch3Xmtr2] == -127)
	{
		while(launcherAccel > -127) //slowly accelerates launcher
		{
			launcherAccel -= 1;
			motor[topRightLaunch] = launcherAccel;
			motor[topLeftLaunch] = launcherAccel;
			motor[bottomRightLaunch] = launcherAccel;
			motor[bottomLeftLaunch] = launcherAccel;
		}
	}
	if (vexRT[Ch3Xmtr2] == 0) //slowly decelerates launcher
		while(launcherAccel < 0)
  	{
  		launcherAccel += 1;
  		motor[topRightLaunch] = launcherAccel;
			motor[topLeftLaunch] = launcherAccel;
			motor[bottomRightLaunch] = launcherAccel;
			motor[bottomLeftLaunch] = launcherAccel;
		}	

vexRT[Ch3Xmtr2] == 0

This is most likely the cause of your problem; while in theory a joystick should return to a true center 0 value when released, they frequently will ‘stick’ at a small value (usually ±10 or smaller).

To resolve this, you will want to create a deadband value and check to make sure that the joystick is within that deadband range. For example:


if (vexRT[Ch3Xmtr2] < 10 && vexRT[Ch3Xmtr2] > -10)

This will check to see if the joystick value is less than positive 10 AND greater than negative 10: if the value falls within that range, the launcher will start to decelerate.

This code can also be written with variables, or by using the absolute value command:

//Create variable to store deadband value
int deadband = 10;

if (vexRT[Ch3Xmtr2] < deadband && vexRT[Ch3Xmtr2] > -deadband)
//Create variable to store deadband value
int deadband = 10;

if (abs(vexRT[Ch3Xmtr2]) < deadband)

On a side note, have you considered using buttons, instead of the joystick, for this behavior (as it looks like you’re only looking for the min/max values of the joystick)?

I have considered buttons, but our drivers prefer the joystick.

I might switch it over eventually.

I’ll add the corrections and report back.

Adding the deadband worked- to an extent.

The launcher doesn’t decelerate all the way. It slowly decelerates, like I want, but it doesn’t decelerate to 0, and I fail to see why it’s doing so in the code.