Running Actions Simultaneously

Hello,
Our team is currently using Vexcode Pro V5 and we needed a way to make our drivetrain move forward, while at the same time spinning one of our motors. I’ve seen stuff for PROS and RobotC but I couldn’t find anything on Vexcode Pro. I saw some things on tasks and all that but wasn’t sure how to set it up or do it, is it possible to make this work off Vexcode Pro?

TLDR - How do you run two commands simultaneously on Vexcode Pro V5?

Could you share your current code?

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// FR                   motor         1               
// FL                   motor         11              
// BL                   motor         3               
// BR                   motor         4               
// Controller1          controller                    
// GyroA                inertial      6               
// Intake               motor         7               
// Shooter              motor         12              
// Roller               motor         8               
// Indexer              motor         13              
// Piston               digital_out   A               
// Drivetrain           drivetrain    15, 16, 17, 18  
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;

motor_group leftDrive(FL,BL);
motor_group rightDrive(FR,BR);

smartdrive robotDrive(leftDrive, rightDrive, GyroA, 12.56, 16, 16, distanceUnits::in);

// A global instance of competition
competition Competition;

// define your global instances of motors and other devices here

/*---------------------------------------------------------------------------*/
/*                          Pre-Autonomous Functions                         */
/*                                                                           */
/*  You may want to perform some actions before the competition starts.      */
/*  Do them in the following function.  You must return from this function   */
/*  or the autonomous and usercontrol tasks will not be started.  This       */
/*  function is only called once after the V5 has been powered on and        */
/*  not every time that the robot is disabled.                               */
/*---------------------------------------------------------------------------*/


void pre_auton(void) {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  GyroA.calibrate();
  while (GyroA.isCalibrating())
  wait(100, msec);   GyroA.calibrate();


void printHeading();
  while(1){
    Brain.Screen.setCursor(3, 3);
    Brain.Screen.setFont(mono60);
    Brain.Screen.print(GyroA.heading());
    FR.efficiency(percent);
    BR.efficiency(percent);
    FL.efficiency(percent);
    BL.efficiency(percent);
   
}
  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              Autonomous Task                              */
/*                                                                           */
/*  This task is used to control your robot during the autonomous phase of   */
/*  a VEX Competition.                                                       */
/*                                                                           */
/*  You must modify the code to add your own robot specific commands here.   */
/*---------------------------------------------------------------------------*/

void autonomous(void) {
  // ..........................................................................
  // Insert autonomous user code here.
  // ..........................................................................
  
 

}





//Macros Config

void pshh (){
  Indexer.spinFor(forward,45,degrees);
  wait (200, msec);
  Indexer.spinFor(reverse,45,degrees);
}

void reverseIntake (){
  Intake.spin(reverse);
}
void pewpew (){
  Piston.set(true);
  wait (800, msec);
  Piston.set(false);
}

void shooterNope (){
  Shooter.stop(hold);
}

void intakeNope (){
  Intake.stop(hold);
}

void pistonUp (){
  Piston.set(true);
}
void pistonDown (){
  Piston.set(false);
}


//Ideas for shooter 
  //L2 spins flywheel
  //R2 is the indexer 


/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              User Control Task                            */
/*                                                                           */
/*  This task is used to control your robot during the user control phase of */
/*  a VEX Competition.                                                       */
/*                                                                           */
/*  You must modify the code to add your own robot specific commands here.   */
/*---------------------------------------------------------------------------*/

void usercontrol(void) {
  // User control code here, inside the loop
  
  // MOTOR VALUES 
  robotDrive.setDriveVelocity(100,percent);
  Roller.setVelocity(100,percent);
  Roller.setMaxTorque(100,percent);
  Intake.setVelocity(100,percent);
  Intake.setMaxTorque(100,percent);
  Shooter.setVelocity(100,percent);
  Shooter.setMaxTorque(100,percent);
  Indexer.setVelocity(100,percent);
  Indexer.setMaxTorque(100,percent); 
  

  // VARIABLES 
  double power = 0, turn = 0;
  double rOutput = 0, lOutput= 0;
  double deadzone = 8;


  
  while (1) {
    // This is the main execution loop for the user control program.
    // Each time through the loop your program should update motor + servo
    // values based on feedback from the joysticks.
    
     //DRIVECODE  
      
      while (abs(Controller1.Axis3.position()) > deadzone || abs(Controller1.Axis1.position()) > deadzone){

        power = Controller1.Axis1.position() / 10.58;
        turn = Controller1.Axis3.position() / 10.58;

        rOutput = -turn + power;
        lOutput = turn + power;

        rightDrive.spin(fwd, rOutput, voltageUnits::volt);
        leftDrive.spin(fwd, lOutput, voltageUnits::volt);
             }
                 
      FR.setStopping(hold);
      BR.setStopping(hold); 
      BL.setStopping(hold);  
      FL.setStopping(hold);
      robotDrive.stop();           
    
  
    // BUTTON CONFIG 
      //Shooter 
      if (Controller1.ButtonR1.pressing()){
      Shooter.spin(forward);
      }
      Controller1.ButtonA.pressed(shooterNope);
      Controller1.ButtonY.pressed(reverseIntake);
     
      //Intake
      if (Controller1.ButtonL2.pressing()){
        Intake.spin(forward);
      }
      Controller1.ButtonL1.pressed(intakeNope);
      //Piston
      Controller1.ButtonUp.pressed(pistonUp);
      Controller1.ButtonDown.pressed(pistonDown);
      
      

    // ........................................................................
    // Insert user code here. This is where you use the joystick values to
    // update your motors, etc.
    // ........................................................................

    wait(20, msec); // Sleep the task for a short amount of time to
                    // prevent wasted resources.
  }
}

//
// Main will set up the competition functions and callbacks.
//
int main() {
  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);

  // Run the pre-autonomous function.
  pre_auton();

  // Prevent main from exiting with an infinite loop.
  while (true) {
    wait(100, msec);
  }
}

I’d want to use it for my autonomous period

You may want to look into the motor.startRotateFor function for your specific usecase. In addition, some functions have an additional argument called bool waitForCompletion which can be used to make the code do the function but not wait for it to complete before moving on to the next line of code.

Also, I noticed you have some function callbacks (button.pressed(function here); )these should not be in your userControl while(1) loop but should instead be put inside of int main without the need for a while(true)loop

1 Like

Start by using the competition template then add your code, just got to file I believe ( I’m not at a computer atm), then go to example codes or something like that. Find the competition template just insert your code in there

ttha5377 is already using the competition template as shown in their previous post. The post you replied to is clarifying that they want the action of driving and spinning one of their motor to occur simulataneously in their autonomous code.

1 Like

I still am having some difficulty understanding how to do it in practice, how would I attach the waitForCompletion to the function? Could you show me an example? Thank you

So waitForCompletion is probably not what you actually want to use here. After looking at the api, the best way to do is to just tell the motor to spin with no target or time specified. This will keep spinning the motor until you tell it to stop. waitForCompletion would be used if you want the motor to spin to a specific target or number of rotations while also driving.
Example:

Intake.spin(fwd, 100, pct);
drive(100, 100, 3);

This is assuming you have a drive function.

4 Likes

Ah thank you, this would actually work

Something I learned recently and ABSOLUTELY LOVE is multitasking. Im not sure I can describe it well, and I dont have any pictures. Just go to open examples > scroll until you see one labeled multitasking > read through the comments on the code. I have about 4 tasks going simultaneously: emergency stop button, vibrate control when flywheel is up to speed, diagnostic code to make sure motors arent overheating, and the actual drive code.

2 Likes