Driver Control Problems

Hello Vex Community,

Our team is having problems with our driver control program. Nothing in the driver control program works except for our autostack task button but only if it is at the top of the usercontrol code. What needs to be done to make it work? Attached is some of our code.

Thanks in advance!


task autostack();
task usercontrol();

task usercontrol(){

	motor[port8] = vexRT[Ch2]; //prt 8 right y
	motor[port2] = vexRT[Ch3]; //prt 2 left y
	motor[port3] = vexRT[Ch2]; //prt 3 right single
	motor[port9] = vexRT[Ch3]; //prt 9 left single

	//button6 (arm motors)
		if(vexRT[Btn6U] == 1) //arm up
	{
		motor[port5] = -127;
		motor[port6] = -127;
		motor[port7] = -127;
	}
	else if(vexRT[Btn6D] == 1) //arm down
	{
		motor[port5] = 127;
		motor[port6] = 127;
		motor[port7] = 127;
	}

	else
	{
		motor[port5] = 0;
		motor[port6] = 0;
		motor[port7] = 0;

		//button5 (mobile goal intake)
			if(vexRT[Btn5U] == 1) //mobi up
		{
			motor[port4] = -127;
		}
		else if(vexRT[Btn5D] == 1) //mobi down
		{
			motor[port4] = 127;
		}

		else
		{
			motor[port1] = 0;

				if(vexRT[Btn7U] == 1) //chain bar  up
			{
				motor[port1] = 127;
			}
			else if(vexRT[Btn7D] == 1) //chain bar down
			{
				motor[port1] = -127;
			}

			else
			{
				motor[port1] = 0;

				if(vexRT[Btn8U] == 1) //cone intake up
				{
					motor[port10] = 127;
				}
				else if(vexRT[Btn8D] == 1) // cone intake down
				{
					motor[port10] = -127;
				}
				else
				{
					motor[port10] = 0;

	if(vexRT[Btn8R] == 1)
	{
		startTask(autostack);
	}
	else if(vexRT[Btn8L] == 1)
		stopTask(autostack);


				}
			}
		}
	}
}
task autostack();
task t1();
task t2();
task t3();

task autostack()
{
if(SensorValue[rightultrasonic] == 11 || SensorValue[rightultrasonic] == 12 )
	startTask(t1);
else if(SensorValue[rightultrasonic] == 13 || SensorValue[rightultrasonic] == 14)
	startTask(t2);
else if(SensorValue[rightultrasonic] == 15 || SensorValue[rightultrasonic] == 16)
	startTask(t3);
task t1();
....
task t2();
....
task t3();
....
}

Put all of your driver control task in a while(true) loop.
Edit: Place everything within the task inside the loop, such that the loop is within the task itself.

This. Tasks do not implicitly loop themselves. If we want something to run once until called again, we would usually make use of a function. I am not entirely sure what your code is trying to achieve, but I suggest your read up on multitasking (tasks) as functions and their respective uses cases - I do not believe calling single use tasks for stacking heights is the correct way of approaching this problem.

So for your main use control task and auto stack task, try this:

task usercontrol(){
	while(true) {
		motor[port8] = vexRT[Ch2]; //prt 8 right y
		motor[port2] = vexRT[Ch3]; //prt 2 left y
		motor[port3] = vexRT[Ch2]; //prt 3 right single
		motor[port9] = vexRT[Ch3]; //prt 9 left single

		//button6 (arm motors)
		if(vexRT[Btn6U] == 1) //arm up
		{
			motor[port5] = -127;
			motor[port6] = -127;
			motor[port7] = -127;
		}
		else if(vexRT[Btn6D] == 1) //arm down
		{
			motor[port5] = 127;
			motor[port6] = 127;
			motor[port7] = 127;
		}

		else
		{
			motor[port5] = 0;
			motor[port6] = 0;
			motor[port7] = 0;

			//button5 (mobile goal intake)
			if(vexRT[Btn5U] == 1) //mobi up
			{
				motor[port4] = -127;
			}
			else if(vexRT[Btn5D] == 1) //mobi down
			{
				motor[port4] = 127;
			}

			else
			{
				motor[port1] = 0;

				if(vexRT[Btn7U] == 1) //chain bar  up
				{
					motor[port1] = 127;
				}
				else if(vexRT[Btn7D] == 1) //chain bar down
				{
					motor[port1] = -127;
				}

				else
				{
					motor[port1] = 0;

					if(vexRT[Btn8U] == 1) //cone intake up
					{
						motor[port10] = 127;
					}
					else if(vexRT[Btn8D] == 1) // cone intake down
					{
						motor[port10] = -127;
					}
					else
					{
						motor[port10] = 0;

						if(vexRT[Btn8R] == 1)
						{
							startTask(autostack);
						}
						else if(vexRT[Btn8L] == 1)
							stopTask(autostack);


					}
				}
			}
		}
		wait1Msec(25);
	}
}

task autostack()
{
	while(true) {
		if(SensorValue[rightultrasonic] == 11 || SensorValue[rightultrasonic] == 12 )
			startTask(t1);
		else if(SensorValue[rightultrasonic] == 13 || SensorValue[rightultrasonic] == 14)
			startTask(t2);
		else if(SensorValue[rightultrasonic] == 15 || SensorValue[rightultrasonic] == 16)
			startTask(t3);
		wait1Msec(25); // Don't hog the CPU
	}
}

Dont need driver control if your robot is fully autonomous.

Hello again,

Using the code provided by @EvolvingJon the driver control functions properly but when using the autostack task it does not function. A snippet of the code is attached below. Is there any way to make both of these work?

task t1();
task t2();
task t3();
task autostack();

task usercontrol(){
	while(true) {
		motor[port8] = vexRT[Ch2]; //prt 8 right y
		motor[port2] = vexRT[Ch3]; //prt 2 left y
		motor[port3] = vexRT[Ch2]; //prt 3 right single
		motor[port9] = vexRT[Ch3]; //prt 9 left single

		//button6 (arm motors)
		if(vexRT[Btn6U] == 1) //arm up
		{
			motor[port5] = -127;
			motor[port6] = -127;
			motor[port7] = -127;
		}
		else if(vexRT[Btn6D] == 1) //arm down
		{
			motor[port5] = 127;
			motor[port6] = 127;
			motor[port7] = 127;
		}

		else
		{
			motor[port5] = 0;
			motor[port6] = 0;
			motor[port7] = 0;

			//button5 (mobile goal intake)
			if(vexRT[Btn5U] == 1) //mobi up
			{
				motor[port4] = -127;
			}
			else if(vexRT[Btn5D] == 1) //mobi down
			{
				motor[port4] = 127;
			}

			else
			{
				motor[port1] = 0;

				if(vexRT[Btn7U] == 1) //chain bar  up
				{
					motor[port1] = 127;
				}
				else if(vexRT[Btn7D] == 1) //chain bar down
				{
					motor[port1] = -127;
				}

				else
				{
					motor[port1] = 0;

					if(vexRT[Btn8U] == 1) //cone intake up
					{
						motor[port10] = 127;
					}
					else if(vexRT[Btn8D] == 1) // cone intake down
					{
						motor[port10] = -127;
					}
					else
					{
						motor[port10] = 0;

						if(vexRT[Btn8R] == 1)
						{
							startTask(autostack);
						}
						else if(vexRT[Btn8L] == 1)
							stopTask(autostack);


					}
				}
			}
		}
		wait1Msec(25);
	}
}

task autostack()
{
	while(true) {
		if(SensorValue[rightultrasonic] == 11 || SensorValue[rightultrasonic] == 12 )
			startTask(t1);
		else if(SensorValue[rightultrasonic] == 13 || SensorValue[rightultrasonic] == 14)
			startTask(t2);
		else if(SensorValue[rightultrasonic] == 15 || SensorValue[rightultrasonic] == 16)
			startTask(t3);
		wait1Msec(25); // Don't hog the CPU
	}
}


task t1() /////////////////////////////////////////////////////////////////////// 11-12 CENTIMETERS
{
	while(true){
	int nBatteryLevel = nImmediateBatteryLevel;
	motor[port1] = 60; //move bar to stop CONE 1
	motor[port5] = 30; //lift down to reset
	motor[port6] = 30;
	motor[port7] = 30;
	wait1Msec(200);
	motor[port1] = 0; //stop bar and lift
	motor[port5] = 0;
	motor[port6] = 0;
	motor[port7] = 0;
	wait1Msec(30); //stop wait 1
	motor[port5] = (-(((9- (nBatteryLevel/1000)) *6)+40)); //lift up
	motor[port6] = (-(((9- (nBatteryLevel/1000)) *6)+40));
	motor[port7] = (-(((9- (nBatteryLevel/1000)) *6)+40));
	wait1Msec(130);
	motor[port5] = 0; //stop lift
	motor[port6] = 0;
	motor[port7] = 0;
	wait1Msec(30); //stop wait 2
	motor[port1] = (-(((9- (nBatteryLevel/1000)) *6)+70)); //run bar down
	motor[port10] = 127; //intake cone
	wait1Msec(200); //wait to just run bar down
	motor[port5] = -(-(((9- (nBatteryLevel/1000)) *6)+40)); //lift down onto cone
	motor[port6] = -(-(((9- (nBatteryLevel/1000)) *6)+40));
	motor[port7] = -(-(((9- (nBatteryLevel/1000)) *6)+40));
	wait1Msec(300);
	motor[port5] = 0; //stop lift
	motor[port6] = 0;
	motor[port7] = 0;
	wait1Msec(30); //stop wait 3
	motor[port10] = 50;
	wait1Msec(50);
	motor[port1] = -(-(((9- (nBatteryLevel/1000)) *6)+70)); //bar back up
	wait1Msec(250);
	motor[port5] = -(-(((9- (nBatteryLevel/1000)) *6)+40)); //lift down onto cone
	motor[port6] = -(-(((9- (nBatteryLevel/1000)) *6)+40));
	motor[port7] = -(-(((9- (nBatteryLevel/1000)) *6)+40));
	motor[port1] = 0; //stop bar
	motor[port1] = 20;
	wait1Msec(700); //stop wait 4
	motor[port5] = 0; //lift down onto cone
	motor[port6] = 0;
	motor[port7] = 0;
	motor[port10] = (-(((9- (nBatteryLevel/1000)) *6)+90)); //score
	wait1Msec(350);
	motor[port1] = 0;
	motor[port10] = 0; //stop cone 1
	wait1Msec(50);

I’m not 100% sure what the problem is, but I believe your code needs a bit of an overhaul.

First of all, the tasks t1, t2, and t3 should be functions. If I understand correctly, these include the code for the robot to stack at different heights. As EvolvingJon stated, these are single-call functions. I don’t really think you need to include an infinite loop in these either.

Currently, if you press the autostack button, you have driver control running at the same time as your autostack. This may not be good, as t1 (and I assume t2 and t3) involve ports 1, 5, 6, and 7, and the values for these are continuously defined in the driver control loop that is running simultaneously. I believe the correct way to go about this is to separate the tasks into base control and arm control (currently you have driver control and autostack, which share some similarities in what they do).

So, you would have the base control in the user control loop. Before the user control loop starts, you would want to start the other task (arm control). Now, onto the arm control task. This task will consist of two major components: a driver control part and an autostack part. All of the driver controls that you need for stacking will be included here, instead of the user control loop. In this task, you will toggle between the driver control part and the autostack part of the task using the autostack button you have assigned (8R and 8L).

As some programming tips, it’s a good idea to name your motors. Also, you have

motor[port5] = 0;
motor[port6] = 0;
motor[port7] = 0;

or something similar several times throughout your code. This is a very good opportunity to use functions instead, such as,

void setLift (int power)
{
motor [port5] = power;
motor [port6] = power;
motor [port7] = power;
}

and call it with,


setLift (0);

instead of typing it all out every time.

EDIT: Stupid spacing

You’ve got a separate problem here in the middle of your code. Each time through the loop you are setting port1 to 0, and then setting it based on what buttons are being pushed. What happens is that you’re rapidly switching instructions to port1 from 0 to either 127 or -127. Plus, you already have an “else” catchall for port1 lower down that sets it to 0 if neither button is pushed. I recommend just deleting the first motor[port1] = 0; statement.

else
			{
				motor[port1] = 0;

				if(vexRT[Btn7U] == 1) //chain bar  up
				{
					motor[port1] = 127;
				}
				else if(vexRT[Btn7D] == 1) //chain bar down
				{
					motor[port1] = -127;
				}

				else
				{
					motor[port1] = 0;