How to bypass blocking functions

Hi all! I’ve been working on macros for raising/lowering my arms (my team has a complex tray), and I ran into an issue with blocking functions

an example macro:

if(Controller1.ButtonX.pressing()) {
      cubeTilt.startSpinTo(225, rotationUnits::deg, tiltSpeed, velocityUnits::dps);

      armLift.spinTo(600, rotationUnits::deg, armSpeed, velocityUnits::dps);
}

The problem though is that when I’m driving the bot and hit the button while moving, I cannot control the driving until the lift stops. I think I heard that there is a way around this (i heard it called multitasking), but I have no idea how to do this in my code.
Any help is appreciated!

add “, false” to the end of your spinTo command. (without the quotes)

1 Like

This thread talks about macros and might be relevant for you to understand.

If this is in your usercontrol() while(1) loop, then you cannot use blocking functions without the consequence you are experiencing. In the while(1) loop you can do something like this:

int run_macro=0;
int cube_done=0;
int arm_done=0;

while (1) {
  // normal drive code
  if ( arm button pressing) {
    // your code
    run_macro=0; // if user takes control stop macro
    arm_done=0;
    cube_done=0;   
  }
  if (cube button pressing) {
    // you code
   run_macro=0;
   arm_done=0;
   cube_done=0;
  }
  ...

  // macro code 
  if (macro button pressing && run_macro=0) {
   run_macro = 1;
  }

  if (run_macro) {
    if (cubeTilt.position(rotationUnits::deg) < 255) {
      cubeTilt.spin(directionType::fwd, tiltSpeed, velocityUnits::dps);
      cube_done=0;
    } else {
     cube_done=1;
    }
    if (armLift.position(rotationUnits::deg) < 600) {
       armLift.spin(directionType::fwd, armSpeed, velocityUnits::dps);
       arm_done=0;
    } else {
      arm_done=1; 
    }
    if (arm_done && cube_done) {
      run_macro=0;
    }
  }
}

ed: added the stop if pushing the cube or arm button

4 Likes

that should work, i think

this does not work bc it skips straight to the next command

But so long as you don’t move the lift right away, you should be able to drive at least right?

yeah, but the arm doesn’t go all the way

even with adding that?

should i add stop if pushing drive?

1 Like

yes
extra characters to meet the limit

1 Like

hmm

extra characters

Thanks! this worked great!
really smart way around it.

Yes it in the “else” of arm and cube control.