Autonomous multiple moving motors at differing speeds

Hi all! I am having trouble moving multiple motors at one time in autonomous, and have it at adjustable speeds. Is there a code for this, and how could I implement it? Thanks!

1 Like

Did you try this method?

vexforum.com/t/vexcode-motor-groups-and-drivetrain-example/69161

1 Like

What problems are you having? Is it an issue with motors not moving simultaneously, or just a need for the right way to set up a motor to move at different speeds in different parts of the code?

My friend @Logan10622D is having trouble with them moving simultaneously and I am wondering how to set it to move at diff speeds

My problem is just having multiple motors move at once

If you want to move multiple motors and you are using rotateFor (or rotateTo), add a false as the last parameter.

Motor1.rotateFor( 500, degrees, 40, percent, false);
Motor2.rotateFor(500, degrees, 35, percent, true);

The false pretty much means “go to the next line” if that helps. You can see in the code Motor1 will go at 40% while Motor2 will go at 35%

4 Likes

So correct me if im worng, the percent is how fast?

1 Like

Yes, the number that comes before percent is how fast it goes

1 Like

What does the 500 do in that code?

1 Like

ok great thank you!
time to program an auton in a day without a field!

I think the 500 is how far

2 Likes

The 500 is the total degrees you want the motor to move.

Read it like this:
“Motor1 rotateFor 500 degrees at 40% (don’t wait to finish)
Motor2 rotateFor 500 degrees at 35% (wait to finish)”

7 Likes

ok, so uh im getting this error message: image

Code
// VEX V5 C++ Project
#include "vex.h"
#include <algorithm>
#include <cmath>
// ---- START VEXCODE CONFIGURED DEVICES ----
// ---- END VEXCODE CONFIGURED DEVICES ----

using namespace vex;
#include "functions.h"
// A global instance of competition
competition Competition;
//#region config_globals
vex::motor back_right_motor(vex::PORT1, vex::gearSetting::ratio18_1, true);
vex::motor back_left_motor(vex::PORT10, vex::gearSetting::ratio18_1, false);
vex::motor front_right_motor(vex::PORT11, vex::gearSetting::ratio18_1, true);
vex::motor front_left_motor(vex::PORT20, vex::gearSetting::ratio18_1, false);
vex::motor intake_arm_mover(vex::PORT5, vex::gearSetting::ratio36_1, false);
vex::motor intake_left(vex::PORT12, vex::gearSetting::ratio36_1, false);
vex::motor intake_right(vex::PORT19, vex::gearSetting::ratio36_1, false);
vex::motor tray_mover(vex::PORT16, vex::gearSetting::ratio36_1, false);
vex::controller con(vex::controllerType::primary);
//#endregion config_globals

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

  int HighTowerMacroPCT = 99;
  int inc = 0;
  int mode = 0;
motor_group   leftDrive( front_left_motor, back_left_motor );
motor_group   RightDrive( front_right_motor, back_right_motor );
motor_group   AllDrive( front_left_motor, front_right_motor, back_left_motor, back_right_motor);
/*---------------------------------------------------------------------------*/
/*                          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) {
  // front_left_motor.spin(fwd);
  tray_mover.rotateFor( 500, degrees, 40, percent, true);
  intake_left.rotateFor( 500, degrees, 40, percent, false);
  intake_right.rotateFor( 500, degrees, 40, percent, false);
  back_left_motor.spin(fwd); // forward);
  front_right_motor.spin(fwd);
  front_left_motor.spin(fwd);
  back_right_motor.spin(fwd);
  wait(2, seconds);
  back_left_motor.spin(reverse);
  front_right_motor.spin(reverse);
  front_left_motor.spin(reverse);
  back_right_motor.spin(reverse);
  wait(3, seconds); 
  back_left_motor.stop();
  front_left_motor.stop();
  back_right_motor.stop();
  front_right_motor.stop();
  /* 
  tray_mover.spin(fwd);
  wait(0.5, seconds);
  intake_left.spin(reverse);
  intake_right.spin(reverse);
  tray_mover.spin(fwd);
  wait(1, seconds);
  tray_mover.spin(reverse);
  wait(2, seconds);
  */
}

/*---------------------------------------------------------------------------*/
/*                                                                           */
/*                              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 HighTower()
  {
    intake_arm_mover.setVelocity(40, percent);
    intake_arm_mover.spinToPosition(HighTowerMacroPCT, (rotationUnits)35,false);
    intake_arm_mover.setBrake(brakeType::hold);
  }
// R1 button script
void R1Script()
{
tray_mover.spin(fwd);
intake_arm_mover.spin(fwd);
}

void usercontrol(void) {
  // User control code here, inside the loop
  while (1) {

    // Setting the speed of the intake rollers and the arms holding the intake
    // rollers

    while (true) {
      int intakeleftSpeedPCT = 200;
      int intakerightSpeedPCT = 200;
      int intakearmSpeedPCT = 75;
      int traymoverSpeedPCT = 25;
      int traymover2speedPCT = 75;

      // respond to the R2 button pressing
      if (con.ButtonR2.pressing())
      {
        //con.Screen.clearScreen();
        con.Screen.print("test = %d",inc);
        con.Screen.newLine();
        Brain.Screen.print("%d",inc);
        Brain.Screen.newLine();
        inc++; 
        mode = 1;

        tray_mover.spin(directionType::rev, traymover2speedPCT, velocityUnits::pct);
        intake_arm_mover.spin(directionType::rev, intakearmSpeedPCT, velocityUnits::pct);
        wait(500, msec); // Sleep the task for a short amount of time
        tray_mover.stop(brakeType::brake);
        wait(1000, msec); // Sleep the task for a short amount of time
        intake_arm_mover.stop(brakeType::brake);
      }
      else
      {
        mode = 0;
      }

      if (mode==0)
      {
        // intake left control
        if (con.ButtonL1.pressing()) 
        {
          intake_left.spin(directionType::fwd, intakeleftSpeedPCT, velocityUnits::pct);
        } 
        else if (con.ButtonL2.pressing()) 
        {
          intake_left.spin(directionType::rev, intakeleftSpeedPCT, velocityUnits::pct);
        } 
        else 
        {
          intake_left.stop(brakeType::brake);
        }

        //intake right control
        if (con.ButtonL1.pressing()) 
        {
          intake_right.spin(directionType::rev, intakerightSpeedPCT, velocityUnits::pct);
        } 
        else if (con.ButtonL2.pressing()) 
        {
          intake_right.spin(directionType::fwd, intakerightSpeedPCT, velocityUnits::pct);
        } 
        else 
        {
          intake_right.stop(brakeType::brake);
        }

        // intake arm mover control
        if (con.ButtonUp.pressing()) 
        {
          intake_arm_mover.spin(directionType::fwd, intakearmSpeedPCT, velocityUnits::pct);
        } 
        else if (con.ButtonDown.pressing()) 
        {
          intake_arm_mover.spin(directionType::rev, intakearmSpeedPCT, velocityUnits::pct);
        } 
        else 
        {
          intake_arm_mover.stop(brakeType::brake);
        }

        // tray mover control
        if (con.ButtonLeft.pressing()) 
        {
          tray_mover.spin(directionType::fwd, traymover2speedPCT, velocityUnits::pct);
        } 
        else if (con.ButtonRight.pressing()) 
        {
          tray_mover.spin(directionType::rev, traymoverSpeedPCT, velocityUnits::pct);
        } 
        else 
        {
          tray_mover.stop(brakeType::brake);
        }
        
      
        // Get the raw sums of the X and Y joystick axes
        double front_left =
            (double)(con.Axis3.position(pct) + con.Axis4.position(pct));
        double back_left =
            (double)(con.Axis3.position(pct) - con.Axis4.position(pct));
        double front_right =
            (double)(con.Axis3.position(pct) - con.Axis4.position(pct));
        double back_right =
            (double)(con.Axis3.position(pct) + con.Axis4.position(pct));

        // Find the largest possible sum of X and Y
        double max_raw_sum =
            (double)(abs(con.Axis3.position(pct)) + abs(con.Axis4.position(pct)));

        // Find the largest joystick value
        double max_XYstick_value = (double)(std::max(
            abs(con.Axis3.position(pct)), abs(con.Axis4.position(pct))));

        // The largest sum will be scaled down to the largest joystick value, and
        // the others will be scaled by the same amount to preserve directionality
        if (max_raw_sum != 0) {
          front_left = front_left / max_raw_sum * max_XYstick_value;
          back_left = back_left / max_raw_sum * max_XYstick_value;
          front_right = front_right / max_raw_sum * max_XYstick_value;
          back_right = back_right / max_raw_sum * max_XYstick_value;
        }

        // Now to consider rotation
        // Naively add the rotational axis
        front_left = front_left + con.Axis1.position(pct);
        back_left = back_left + con.Axis1.position(pct);
        front_right = front_right - con.Axis1.position(pct);
        back_right = back_right - con.Axis1.position(pct);

        // What is the largest sum, or is 100 larger?
        max_raw_sum =
            std::max(std::abs(front_left),
                    std::max(std::abs(back_left),
                              std::max(std::abs(front_right),
                                      std::max(std::abs(back_right), 100.0))));

        // Scale everything down by the factor that makes the largest only 100, if
        // it was over
        front_left = front_left / max_raw_sum * 100.0;
        back_left = back_left / max_raw_sum * 100.0;
        front_right = front_right / max_raw_sum * 100.0;
        back_right = back_right / max_raw_sum * 100.0;

        // Write the manipulated values out to the motors
        front_left_motor.spin(fwd, front_left, velocityUnits::pct);
        back_left_motor.spin(fwd, back_left, velocityUnits::pct);
        front_right_motor.spin(fwd, front_right, velocityUnits::pct);
        back_right_motor.spin(fwd, back_right, velocityUnits::pct);
      }
    }
  }

  // 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() {
  // Run the pre-autonomous function.
  pre_auton();

  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(autonomous);
  Competition.drivercontrol(usercontrol);

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

Try changing percent to velocityUnits::pct

1 Like