Vex V5 usercontrol loop issue. sos

Our while loop for controlling motors works as intended, but when we add the while loop into our user control section, we can no longer control our drive train. If we comment out our while loop, we regain control of the drive train. What would cause an issue like this? The issue is inside of our user control function, but we may be missing something somewhere else. We are first year competitors and had everything working before. We heard we had to use a template and are trying to get our code working with the competition template. Our code is below:

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       VEX                                                       */
/*    Created:      Thu Sep 26 2019                                           */
/*    Description:  Competition Template                                      */
/*                                                                            */
/*----------------------------------------------------------------------------*/

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// lift                 motor         7               
// launcher             motor         9               
// frontSucker2         motor         2               
// frontSucker1         motor         1               
// Drivetrain           drivetrain    12, 11          
// Controller1          controller                    
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;

competition Competition;

void initialize() {
  lift.setVelocity(100, percent);
  frontSucker1.setVelocity(100, percent);
  frontSucker2.setVelocity(100, percent);
  launcher.setVelocity(100, percent);
  Drivetrain.setDriveVelocity(30, percent);
}

void liftUp() {
  lift.spin(reverse);
  launcher.spin(reverse);
}

void liftDown() {
  lift.spin(forward);
  launcher.spin(forward);
}

void suckerIn() {
  frontSucker1.spin(reverse);
  frontSucker2.spin(reverse);
  }

void suckerOut() {
  frontSucker1.spin(forward);
  frontSucker2.spin(forward);
}

void liftStop() {
  lift.stop();
  launcher.stop();
}

void suckerStop() {
  frontSucker1.stop();
  frontSucker2.stop();
}

void allStop() {
  lift.stop();
  frontSucker1.stop();
  frontSucker2.stop();
  launcher.stop();
}


void pre_auton(void) {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();

  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...
}

void autonomous(void) {
initialize();

  Drivetrain.setDriveVelocity(50, percent);
  Brain.Screen.print("autonomous");

}

void usercontrol(void) {
  Drivetrain.setDriveVelocity(100, percent);

  while (1) {

    if(Controller1.ButtonR1.pressing()){
      suckerIn();
    }
    else if(Controller1.ButtonL1.pressing()){
      suckerOut();
    }
    else{
      suckerStop();
    }
    if(Controller1.ButtonR2.pressing()){
      liftUp();
    }
    else if(Controller1.ButtonL2.pressing()){
      liftDown();
    }
    else{
      liftStop();
    }

    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.
  autonomous();
  usercontrol();
  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);
  }
}

It looks like the code for controlling your drive train is outside of the while loop. what this means is as soon as it enters the while loop it will never call ( Drivetrain.setDriveVelocity(100, percent); ) again. I would try moving that line of code inside the curly brackets of the while loop so it continuously calls it every 20ms.

1 Like

The drivetrain object can be controlled using the controller. Can I see the controller setup? It’s usually there where you allow for the drivetrain to be controlled using the controller.
For example, if I wanted a tank drive(or each side of the robot being controlled by a joystick), I would do this:
image

1 Like

Our controller setup is identical to yours.

Can I see how you configured your drivetrain then? This should work. Also, could you post the robot_config.cpp file?

1 Like
#include "vex.h"

using namespace vex;
using signature = vision::signature;
using code = vision::code;

// A global instance of brain used for printing to the V5 Brain screen
brain  Brain;

// VEXcode device constructors
motor lift = motor(PORT7, ratio18_1, false);
motor launcher = motor(PORT9, ratio18_1, false);
motor frontSucker2 = motor(PORT2, ratio18_1, true);
motor frontSucker1 = motor(PORT1, ratio18_1, false);
motor LeftDriveSmart = motor(PORT12, ratio18_1, false);
motor RightDriveSmart = motor(PORT11, ratio18_1, true);
drivetrain Drivetrain = drivetrain(LeftDriveSmart, RightDriveSmart, 319.19, 337.82, 355.59999999999997, mm, 0.6211180124223602);
controller Controller1 = controller(primary);

// VEXcode generated functions
// define variable for remote controller enable/disable
bool RemoteControlCodeEnabled = true;
// define variables used for controlling motors based on controller inputs
bool DrivetrainLNeedsToBeStopped_Controller1 = true;
bool DrivetrainRNeedsToBeStopped_Controller1 = true;

// define a task that will handle monitoring inputs from Controller1
int rc_auto_loop_function_Controller1() {
  // process the controller input every 20 milliseconds
  // update the motors based on the input values
  while(true) {
    if(RemoteControlCodeEnabled) {
      // calculate the drivetrain motor velocities from the controller joystick axies
      // left = Axis3
      // right = Axis2
      int drivetrainLeftSideSpeed = Controller1.Axis3.position();
      int drivetrainRightSideSpeed = Controller1.Axis2.position();
      
      // check if the value is inside of the deadband range
      if (drivetrainLeftSideSpeed < 5 && drivetrainLeftSideSpeed > -5) {
        // check if the left motor has already been stopped
        if (DrivetrainLNeedsToBeStopped_Controller1) {
          // stop the left drive motor
          LeftDriveSmart.stop();
          // tell the code that the left motor has been stopped
          DrivetrainLNeedsToBeStopped_Controller1 = false;
        }
      } else {
        // reset the toggle so that the deadband code knows to stop the left motor next time the input is in the deadband range
        DrivetrainLNeedsToBeStopped_Controller1 = true;
      }
      // check if the value is inside of the deadband range
      if (drivetrainRightSideSpeed < 5 && drivetrainRightSideSpeed > -5) {
        // check if the right motor has already been stopped
        if (DrivetrainRNeedsToBeStopped_Controller1) {
          // stop the right drive motor
          RightDriveSmart.stop();
          // tell the code that the right motor has been stopped
          DrivetrainRNeedsToBeStopped_Controller1 = false;
        }
      } else {
        // reset the toggle so that the deadband code knows to stop the right motor next time the input is in the deadband range
        DrivetrainRNeedsToBeStopped_Controller1 = true;
      }
      
      // only tell the left drive motor to spin if the values are not in the deadband range
      if (DrivetrainLNeedsToBeStopped_Controller1) {
        LeftDriveSmart.setVelocity(drivetrainLeftSideSpeed, percent);
        LeftDriveSmart.spin(forward);
      }
      // only tell the right drive motor to spin if the values are not in the deadband range
      if (DrivetrainRNeedsToBeStopped_Controller1) {
        RightDriveSmart.setVelocity(drivetrainRightSideSpeed, percent);
        RightDriveSmart.spin(forward);
      }
    }
    // wait before repeating the process
    wait(20, msec);
  }
  return 0;
}

/**
 * Used to initialize code/tasks/devices added using tools in VEXcode Pro.
 * 
 * This should be called at the start of your int main function.
 */
void vexcodeInit( void ) {
  task rc_auto_loop_task_Controller1(rc_auto_loop_function_Controller1);
}

Screenshot 2020-12-16 135558

The robot_config.cpp as well as the drivetrain setting looks correct. I guess the only thing left is to delete the command setting the drivetrain to 100% speed at the very begininning of the user control function.

3 Likes

@bigpaw2003 did it work? If it didn’t, I would just recommend programming your drive function.

1 Like

Yea, unfortunately it didn’t work out. Thank you for all the help!