Autonomous Trouble (motors not spinning together)

So I’m trying to program an autonomous but my commands are running separately when I’m trying to run some commands at the same time. Any help would be appreciated. Thanks!

Here is my code

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

// ---- START VEXCODE CONFIGURED DEVICES ----
// Robot Configuration:
// [Name]               [Type]        [Port(s)]
// Drivetrain           drivetrain    20, 11          
// Controller1          controller                    
// ---- END VEXCODE CONFIGURED DEVICES ----

#include "vex.h"

using namespace vex;

// A global instance of competition
competition Competition;
// defining motors
// define your global instances of motors and other devices here
vex::motor     Left(vex::PORT11, vex::gearSetting::ratio18_1,true);
vex::motor     Right(vex::PORT20, vex::gearSetting::ratio18_1,false);
vex::motor     LeftLift(vex::PORT1, vex::gearSetting::ratio36_1,true);
vex::motor     RightLift(vex::PORT10, vex::gearSetting::ratio36_1,false);
vex::motor     SecondLift(vex::PORT13, vex::gearSetting::ratio36_1,false);
vex::motor     LeftClaw(vex::PORT3, vex::gearSetting::ratio18_1,true);
vex::motor     RightClaw(vex::PORT6, vex::gearSetting::ratio18_1,false);
vex::controller con(vex::controllerType::primary);

/*---------------------------------------------------------------------------*/
/*                          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();

  // 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) {
{
Left.rotateFor(1000,rotationUnits::deg,55,velocityUnits::pct,true);
Right.rotateFor(1000,rotationUnits::deg,55,velocityUnits::pct,true);
}
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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) {
  int LeftLiftSpeedPCT = 90;
  int RightLiftSpeedPCT = 90;
  int LeftClawSpeedPCT = 100;
  int RightClawSpeedPCT = 100;

  // User control code here, inside the loop
///Button Y up pri lift
  while (1) {
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////
    //
    //
    //
    //////////////////////////////////////////////////////////////////////////////////////////////////////
  
    if(con.ButtonUp.pressing()) {
      RightLift.spin(directionType::rev, RightClawSpeedPCT, velocityUnits::pct);
    }
    else if (con.ButtonDown.pressing())  {
      RightLift.spin(directionType::fwd, RightClawSpeedPCT, velocityUnits::pct);
    }
    else{
      RightLift.stop(brakeType::brake);
    }
    if(con.ButtonUp.pressing()) {
      LeftLift.spin(directionType::rev,  LeftClawSpeedPCT, velocityUnits::pct);
    }
    else if (con.ButtonDown.pressing())  {
      LeftLift.spin(directionType::fwd, LeftClawSpeedPCT, velocityUnits::pct);
    }
    else{
     LeftLift.stop(brakeType::brake);
    }
   
    //////////////
    if(con.ButtonL1.pressing()) {
      SecondLift.spin(directionType::fwd, LeftLiftSpeedPCT, velocityUnits::pct);
    }
    else if (con.ButtonL2.pressing())  {
      SecondLift.spin(directionType::rev, LeftLiftSpeedPCT, velocityUnits::pct);
    }
    else{
      SecondLift.stop(brakeType::brake);
    }
if(con.ButtonR1.pressing()) {
      RightClaw.spin(directionType::rev, RightClawSpeedPCT, velocityUnits::pct);
    }
    else if (con.ButtonR2.pressing())  {
      RightClaw.spin(directionType::fwd, RightClawSpeedPCT, velocityUnits::pct);
    }
    else if (con.ButtonY.pressing())  {
      RightClaw.spin(directionType::rev, RightClawSpeedPCT, velocityUnits::pct);
    }
    else if (con.ButtonA.pressing())  {
      RightClaw.spin(directionType::fwd, RightClawSpeedPCT, velocityUnits::pct);
    }
    else{
      RightClaw.stop(brakeType::brake);
    }
    if(con.ButtonR1.pressing()) {
      LeftClaw.spin(directionType::rev,  LeftClawSpeedPCT, velocityUnits::pct);
    }
    else if (con.ButtonR2.pressing())  {
      LeftClaw.spin(directionType::fwd, LeftClawSpeedPCT, velocityUnits::pct);
    }
    else if (con.ButtonRight.pressing())  {
      LeftClaw.spin(directionType::rev, LeftClawSpeedPCT, velocityUnits::pct);
    }
    else if (con.ButtonLeft.pressing())  {
      LeftClaw.spin(directionType::fwd, LeftClawSpeedPCT, velocityUnits::pct);
    }
    else{
     LeftClaw.stop(brakeType::brake);
    }
    ///
    ////////
    /////////
    

    // 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.

    // ........................................................................
    // 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()  {
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);

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

  // Prevent main from exiting with an infinite loop.
  while (true) {
    vex::task::sleep(100);
  }
}

edit by mods: added code tags

For the first line in autonomous, change the true to false.
This part of the code will determine whether that line is blocking or non blocking.

If you set them to true, it will wait until the first line is done, then it would do the next one.

If you set the first one to false and the second one to true, it will run both lines at the same time.

1 Like

Ok thanks! My comp is Saturday this really helps!

Also, just for future reference, wrap your code in ```cpp at the beginning and ``` at the end for nicely formatted code. Makes it a ton easier to read.

2 Likes

Ok I’m trying to learn how to program in Vexcode this will help a lot thank you both!

1 Like

Sorry if I wasn’t clear but that is just for the forum. You don’t need it for VexCode.

oh ok thanks still it will help me alot!

1 Like

Instead of

Left.rotateFor(1000,rotationUnits::deg,55,velocityUnits::pct,true); Right.rotateFor(1000,rotationUnits::deg,55,velocityUnits::pct,true);

Try using something like this

ChutePush.spin(vex::directionType::fwd); RightIntake.spin(vex::directionType::rev); LeftIntake.spin(vex::directionType::rev); vex::task::sleep(2800);

The sleep code will make it run for 2800 milliseconds

Actually if you still want to use rotation use

LA1.startrotateFor(1000,rotationUnits::deg,55,velocityUnits::pct,true);

im just trying to move some motors at the same time so i can run my auton

My comp is on sat too. As a btw in my macro both move @ the same time.

you may also want to consider using motor_group() to simplify things.

motor_group Claw(LeftClaw, RightClaw);

makes both motors act as one! - not sure this is what you want but FWIW in the future

another FWIW, you brakeType:hold will hold the lift’s or claw’s position and actually apply counter-force to keep the position (PID) Where as .brake just stops the motor but will freewheel

Good luck this weekend!

1 Like