Help with Tasks

I am trying to program my team’s catapult to automatically go down to a specific angle after every launch automatically via Rotation Sensor, but it isn’t working properly. When I run the code on the robot, only the functions in the task can run, and nothing else works (drivetrain, Intake, etc.). Additionally, within the task, I tell the motor to stop running when the rotation sensor reaches a certain angle, but it gives me and error that the expression must be a modifiable Lvalue.
Code (In opcontrol):

pros::Controller master(pros::E_CONTROLLER_MASTER);
	pros::Motor leftwheelA(1);
	pros::Motor leftwheelB(2);
	pros::Motor leftwheelC(3); 
	leftwheelC.set_reversed(true); 
	pros::Motor rightwheelA(5); 
	pros::Motor rightwheelB(6); 
	pros::Motor rightwheelC(7);
	rightwheelC.set_reversed(true); 
	pros::Motor Intake(8);  
	pros::Motor Slap(9); 
	pros::Rotation RSensor(19);
	leftwheelA.set_gearing(pros::E_MOTOR_GEARSET_06);
	leftwheelB.set_gearing(pros::E_MOTOR_GEARSET_06); 
	leftwheelC.set_gearing(pros::E_MOTOR_GEARSET_06);
	rightwheelA.set_gearing(pros::E_MOTOR_GEARSET_06); 
	rightwheelB.set_gearing(pros::E_MOTOR_GEARSET_06);
	rightwheelC.set_gearing(pros::E_MOTOR_GEARSET_06); 
	Intake.set_gearing(pros::E_MOTOR_GEARSET_06);
	Slap.set_gearing(pros::E_MOTOR_GEARSET_36); 
	Slap.set_brake_mode(pros::E_MOTOR_BRAKE_HOLD);
	void my_task_fn(void* param); 
	{
		while (true) 
		{
			if (master.get_digital(DIGITAL_L1)) 
			{
				Slap.move_velocity(-100); 
			}
			else if (RSensor.get_position() != 40) 
			{
				Slap.move_velocity(-100); 
			}
(Lvalvue error Here) >> else if (RSensor.get_position() = 40) 
			{
				Slap.brake(); 
			}
			else 
			{
				Slap.brake(); 
			}
		}
	}	
	while (true) {
	// Chassis/Drivetrain Code 
	int power = master.get_analog(ANALOG_RIGHT_X);
    int turn = master.get_analog(ANALOG_LEFT_Y);
    int left = power - turn;
    int right = power + turn;
	leftwheelA.move(left);
	leftwheelB.move(left); 
	leftwheelC.move(left); 
	rightwheelA.move(right); 
	rightwheelB.move(right); 
	rightwheelC.move(right); 

	//Intake Code

	if(master.get_digital(DIGITAL_R2)) 
	{
		Intake.move_velocity(100); 
	}
	else if (master.get_digital(DIGITAL_R1))
	{
		Intake.move_velocity(-100); 
	}
	else 
	{
		Intake.move_velocity(0); 
	}
	pros::Task CataTask (my_task_fn, (void*)"PROS", "My Task");
}
}

edit: code tags added by mods, please remember to use them,

Put a delay in your task function’s loop. Delays are very very very very very important. You are starving the V5’s resources without one. Also, you are creating that task on every iteration of the loop right now. Create the task outside of the for loop. Also, did you define the task function inside another function? And put a semicolon in it’s definition? Did this compile successfully?
Should look more like this:


pros::Motor mymotor(3);
// declare devices and stuff here

void mytaskfunction() {
    while(true) {
        //do stuff
        pros::delay(10);
    }
}

void opcontrol() {
    pros::Task catatask(mytaskfunction);
    while(true) {
        //opcontrol stuff
        pros::delay(10);
    }
}
4 Likes

It may be best to avoid them altogether, you can often do everything within opcontrol().

6 Likes

Where should I initialize the Motors in the code if not in opcontrol?

In the same place I did in my example. Above all the functions so that all the functions can use them. You can do the set gearing, set reversed, etc commands inside of the initialize function, which is the first thing to run when the program starts

1 Like

That worked great. But now, within the task, the motor does not stop spinning even then the rotation sensor reaches the specificized value. How do I make the motor stop when it reaches the rotation sensor value specified?
code (just task)
void my_task_fn(void* param)
{

	while (true) 
	{
		RSensor.reset_position(); 
		int CataPosition = RSensor.get_position();

		if (master.get_digital(DIGITAL_L1)) 
		{
			Slap.move_velocity(-100); 
		}
		else if (CataPosition != 10) 
		{
			while (CataPosition != 10)
			{
				Slap.move_velocity(-100); 
			}
		}
		else 
		{
			Slap.brake(); 
		}
		pros::delay(10); 
	}
}
  1. you are resetting the encoder position and then immediately getting it. That means your CataPosition variable is always 0.
  2. don’t use == or != to figure out how to move the cata position. Use <, >, <=, or >=.
  3. in the inner while loop, you don’t update the CataPosition variable. There is also not a delay
2 Likes

How and where should I update the CataPosition Variable?

Inside the inner loop. CataPosition = RSensor.get_position();

1 Like

I changed the code but the catapult is still going backwards endlessly, even when I set the angle low.

void my_task_fn(void* param)
	{

		while (true) 
		{
			int CataPosition = RSensor.get_position();

			if (master.get_digital(DIGITAL_L1)) 
			{
				Slap.move_velocity(-100); 
			}
			else if (CataPosition < 1) 
			{
				while (CataPosition <= 1)
				{
					Slap.move_velocity(-100); 
				}
			}
			else 
			{
				Slap.brake(); 
			}
			pros::delay(10);
			 
		}
	}

Again, you haven’t updated the value of CataPosition inside of the while loop that reads while(CataPosition <= 1). And there is no delay inside that while loop either.

1 Like

I added the delay and updated CataPosition within the while loop. Now the catapult just jerks a little bit and stops, despite changing the angle to different values.
void my_task_fn(void* param)
{
while (true)
{
RSensor.reset_position();
if (master.get_digital(DIGITAL_L1))
{
Slap.move_velocity(-100);
}
else if (CataPosition < 200)
{

			while (CataPosition <= 200)	
			{ 	 
				CataPosition = RSensor.get_position();
				Slap.move_velocity(-100);
				pros::delay(10); 
			}
		}
		else 
		{
			Slap.brake(); 
		}
		pros::delay(10); 
	}
}

Ok, now you dont set the value of CataPosition before your conditionals. I implore you to go through the first 2 chapters of https://www.learncpp.com/ and come back to this code later.

3 Likes

I’ll do that. Thanks for all your help.