Help understanding Tasks

We have a ‘clawbot’ and I’m trying to understand working w/tasks. It seems the task monopolizes any motor in it

All of the 'rive motor-related tasks are running in tDrive() and arm/claw stuff in another task (tArm())

We have a button -> function [flipCap()] to lift the arm while driving forward defined in tArm()

However when we call the method the drive part doesn’t work.

Seeing tDrive() is in charge of drive motors I figured I needed to stop the task, execute the actions, and re-start the task.

However this didn’t work!

Long story short the only way I got it to work is by putting the button -> method in BOTH tasks.

This strikes me as very wrong and wondering if anyone had thoughts on what to do.

Thanks

Can you post your code?

2 Likes

Gladly. However w/VCS I’m not very good about saving versions so I’ll have to re-create it.

Do you want the relevant code or whole enchilada?

Thanks!

I recreated the code and copied the relevant code - hope this helps

Thanks again!

void usercontrol( void ) {
    //EVOKE TASKS
    vex::task t(tArm); //start arm task
    t.setPriority(6);  //medium priority
	vex::task d(tDrive); //start drive task 
    d.setPriority(6);  //medium priority
	
	while(1){
	}
}
  
int tDrive(){
    while (1){
        //DRIVE
        LR.spin(directionType::fwd, (Controller1.Axis1.value() + Controller1.Axis2.value())*adjSpeed, velocityUnits::pct);
        RR.spin(directionType::fwd, (Controller1.Axis1.value() - Controller1.Axis2.value())*adjSpeed, velocityUnits::pct);
        LF.spin(directionType::fwd, (Controller1.Axis1.value() + Controller1.Axis2.value())*adjSpeed, velocityUnits::pct);
        RF.spin(directionType::fwd, (Controller1.Axis1.value() - Controller1.Axis2.value())*adjSpeed, velocityUnits::pct);
	}
	return 0;
}


int tArm(void){ //ARM & GRABBER CONTROL TASK
    while(1){
      if(Controller1.Axis3.value()>deadBand){  //ATTEMPT TO SIMULATE A 4-BAR ARM/CLAW - NOT PERFECT ESP GOING DOWN
        GrabberArm.rotateTo(LeftArm.rotation(rotationUnits::deg)*adjGrab,rotationUnits::deg);
        LeftArm.spin(vex::directionType::fwd, Controller1.Axis3.value()*0.5, vex::velocityUnits::rpm);
        RightArm.spin(vex::directionType::fwd, Controller1.Axis3.value()*0.5, vex::velocityUnits::rpm);
      }else if(Controller1.Axis3.value()<deadBand*-1){  
        LeftArm.spin(vex::directionType::fwd, Controller1.Axis3.value()*0.5, vex::velocityUnits::rpm);
        RightArm.spin(vex::directionType::fwd, Controller1.Axis3.value()*0.5, vex::velocityUnits::rpm);      
        GrabberArm.rotateTo(LeftArm.rotation(rotationUnits::deg)*(adjGrab*0.7),rotationUnits::deg);
      }else if(Controller1.ButtonL1.pressing()){
          rGrab(50,20,1); 
      }else if(Controller1.ButtonL2.pressing()){
          rGrab(-50,20,1); 
      }else if(Controller1.ButtonLeft.pressing()){
          lowCap();
      }else if(Controller1.ButtonRight.pressing()){
          highCap();
      }else if(Controller1.ButtonUp.pressing()){
          loadBall();
      }else if(Controller1.ButtonDown.pressing()){
          flipCap();	//******** THIS IS THE OFFENDING CODE
      }else if(Controller1.ButtonR1.pressing()){
          punchBall();
      }else{
          LeftArm.stop(brakeType::hold);
          RightArm.stop(brakeType::hold);
          GrabberArm.stop(brakeType::hold);
      }
      vex::task::sleep(20);
    }   
    return 0;
}//end tArm

void flipCap(void){ //HERE'S THE PROBLEM FUNCTION
	d.Stop();		//stop drive task
    rArm(100,50,0);     
    vex::task::sleep(200);   
    rDrive(100,-100,50,50,1);    //IF BLOCKING ROBOT LOCKS UP, NOT IT DOES NOTHING
    vex::task::sleep(50);   
	vex::task d(tDrive); //start task 
    d.setPriority(6);  //medium priority
}//end lift cap


void rDrive(double lDeg, double rDeg, int l, int r, bool b){  
    LR.rotateFor(lDeg, vex::rotationUnits::deg,l, vex::velocityUnits::pct, false); //This command must be non blocking.
    LF.rotateFor(lDeg, vex::rotationUnits::deg,l, vex::velocityUnits::pct, false); //This command must be non blocking.
    RR.rotateFor(rDeg, vex::rotationUnits::deg,r, vex::velocityUnits::pct, false); //This command must be non blocking.
    RF.rotateFor(rDeg, vex::rotationUnits::deg,r, vex::velocityUnits::pct,b); //This command is blocking so the program will wait here until the right motor is done.  
    vex::task::sleep(50);
}//end eDrive

void rArm(double deg, int s, bool b){
    LeftArm.rotateFor(deg, vex::rotationUnits::deg,s, vex::velocityUnits::pct, false); //This command must be non blocking.
    RightArm.rotateFor(deg, vex::rotationUnits::deg,s, vex::velocityUnits::pct,b); //This command is blocking so the program will wait here until the right motor is done.  
}//end eArm

Try throwing some delays in the infinite loops of the drive and main tasks. The system should work without them but the way you have it now with mutiple loops wil no delays, it may cause some unexpected behavior with the scheduler.

Aside from that at first glance it looks like it should work.

2 Likes

Yes! Pauses in the loops could be the ticket!!

The strange thing is I stop the drive task and it still doesn’t relinquish control. Maybe it doesn’t stop fast enough?

IDK but will put the pauses in for sure!

Thanks for looking at it.

Best 2U!!