V5 micro-movement & task vs thread

Hi again - we wrote a function to do small drive-motor movements via buttons X,Y,A,B

These are running in a task (below) using the default priority )is there a difference b/tween task & thread?)

The thing is when we push the buttons the movement is unpredictable - sometimes it doesn’t move at all, sometimes a little and other times the full 60 degrees

Any thoughts are appreciated

int tLift(void){ //ARM & CLAW TASK
while(1){
if(abs(Controller1.Axis3.value())>deadBand){
L_Lift.spin(vex::directionType::fwd, Controller1.Axis3.value()*adjLift, vex::velocityUnits::rpm);
R_Lift.spin(vex::directionType::fwd, Controller1.Axis3.value()*adjLift, vex::velocityUnits::rpm);
}else if(Controller1.ButtonL1.pressing()){
Claw.spin(vex::directionType::fwd, 50, vex::velocityUnits::pct);
}else if(Controller1.ButtonL2.pressing()){
Claw.spin(vex::directionType::rev, 50, vex::velocityUnits::pct);
// micro moves
}else if(Controller1.ButtonX.pressing()){ //move fwd
rDrive(30,-30,30,30,0);
}else if(Controller1.ButtonB.pressing()){ //move back
rDrive(-30,30,30,30,0);
}else if(Controller1.ButtonY.pressing()){ //move left
rDrive(-30,-30,30,30,0);
}else if(Controller1.ButtonA.pressing()){ //move right
rDrive(30,30,30,30,0);
}else{
L_Lift.stop(brakeType::hold);
R_Lift.stop(brakeType::hold);
Claw.stop(brakeType::hold);
}
vex::task::sleep(5);
}
return 0;
}//end tLift

void rDrive(double lDeg, double rDeg, int l, int r, bool b){ // drive by relative distance
L_Drive.rotateFor(lDeg, vex::rotationUnits::deg,l, vex::velocityUnits::pct, false);
R_Drive.rotateFor(rDeg, vex::rotationUnits::deg,r, vex::velocityUnits::pct, b);
}//end rDrive

More Readable version of you code
int tLift(void) //ARM & CLAW TASK
{
	while(1)
	{
		if(abs(Controller1.Axis3.value()) > deadBand)
		{
			L_Lift.spin(vex::directionType::fwd, Controller1.Axis3.value() * adjLift, vex::velocityUnits::rpm);
			R_Lift.spin(vex::directionType::fwd, Controller1.Axis3.value() * adjLift, vex::velocityUnits::rpm);
		}
		else if(Controller1.ButtonL1.pressing())
		{
			Claw.spin(vex::directionType::fwd, 50, vex::velocityUnits::pct);
		}
		else if(Controller1.ButtonL2.pressing())
		{
			Claw.spin(vex::directionType::rev, 50, vex::velocityUnits::pct);// micro moves
		}
		else if(Controller1.ButtonX.pressing()) //move fwd
		{
			rDrive(30, -30, 30, 30, 0);
		}
		else if(Controller1.ButtonB.pressing()) //move back
		{
			rDrive(-30, 30, 30, 30, 0);
		}
		else if(Controller1.ButtonY.pressing()) //move left
		{
			rDrive(-30, -30, 30, 30, 0);
		}
		else if(Controller1.ButtonA.pressing()) //move right
		{
			rDrive(30, 30, 30, 30, 0);
		}
		else
		{
			L_Lift.stop(brakeType::hold);
			R_Lift.stop(brakeType::hold);
			Claw.stop(brakeType::hold);
		}

		vex::task::sleep(5);
	}

	return 0;
}

void rDrive(double lDeg, double rDeg, int l, int r, bool b) // drive by relative distance
{
	L_Drive.rotateFor(lDeg, vex::rotationUnits::deg, l, vex::velocityUnits::pct, false);
	R_Drive.rotateFor(rDeg, vex::rotationUnits::deg, r, vex::velocityUnits::pct, b);
}

The problem is that you are using .rotateFor() for user-controlled driving. This will cause the robot to try and get a little bit forward at every moment, most likely using PID, and basically not what you want.

The way you should do is, similar to the lift…

L_Drive.spin(vex::directionType::fwd, rValue, vex::velocityUnits::pct);
R_Drive.spin(vex::directionType::fwd, lValue, vex::velocityUnits::pct);

This way the motors will spin a constant speed, which should give you the results that you are looking for. Also it is easier to use vex::velocityUnits::pct rather than rpm since it is in percentages.

I would recommend you to put the driving on the joysticks. In the game precise driving is important and if you can only drive at one speed to will either be too fast or too slow. For this reason many teams use joysticks for the drive. If you want stuff like the lift on a joystick, remember you can always use a partner controller.

In the end they both do the same thing, but have a different interface. vex::task is meant to be similar to robot C tasks, while vex::thread is mean to be like the more advanced std::thread.

Here are some threads about this…
Class specific vex::thread
Vex::thread constructor for initialization - I gave an incorrect answer

2 Likes

Thank you for this excellent explanation.

1 Like