Ascend
Coding
Ascend
  • 🚀Introduction
  • VEX Robotics
    • 🛠️Building
      • 📏CAD Design
      • 🔧Best Practices
      • 🏎️Drive Trains
        • 🔩Screw Joints
        • 🥊Boxing
        • 🔵Bearing flats
        • 🛤️Fixing Friction
        • 🔥450 RPM on 2.75"
      • 🏍️Motors
      • 🪨Metal
      • Plastic
      • 🎈Pneumatics
      • ➕Additional Mechanisms
        • 🖨️3D Printing
        • ↔️Ratchets
        • 🏹Catapults
        • 🎣Intakes
        • 🏋️Lifts
    • 💻Coding
      • ⚖️Choosing Your Coding Environment
      • 🔴VEXCode Pro
        • 🖥️Getting Started
        • 🚗Drive Code
        • 🚴Coding Motors
        • 🌬️Coding Pneumatics
        • 🎛️Advanced
          • 📡Coding Autonomous
          • 📈Coding PIDs
            • ⬆️Drive PID Tutorial
            • ↩️Turn PID Tutorial
            • 🎸Tuning PIDs
              • 🔢Ziegler-Nichols Method
              • 📊Graphing PIDs
          • 📋Tasks
          • 🚝Advanced Drive Code
      • 🟡PROS
    • 📗Engineering Notebook
      • 📔Formatting
      • ⭐Notebook Walkthrough
      • 💠Decision Matrices
        • 📌Decision Point
      • ➕Additional Resources
    • 🏆Tournaments
      • 🗣️Team Interviews
        • 🪙Interview Tips
        • ⛳Interview Scoring
        • 🤽‍♀️Interview Practice Questions
      • 🤹Skills
      • ℹ️Ranking and Stats
      • 📷Filming Matches
  • Other
    • 🧑‍💼Management
      • 🕑Time Management
      • 👨‍👩‍👧‍👦Team Management
        • 👮Delegation
        • ⚓Optimal Team Size
      • 🖇️Code Management (GitHub)
      • 🚋Resource Management
      • ⁉️Stuck?
    • 🍎Physics
      • ⚙️Torque
      • 😑Stress Forces
    • 👾Extra stuff
      • 📸96504 Gallery
      • ⛑️VEX Team Resources (High Stakes)
      • 🏎️Driving Simulator
Powered by GitBook
On this page

Was this helpful?

  1. VEX Robotics
  2. Coding
  3. VEXCode Pro

Drive Code

*Technically optional, but highly recommended

PreviousGetting StartedNextCoding Motors

Last updated 10 months ago

Was this helpful?

You probably want the robot to move around. However, the robot doesn't just do this on its own; you have to program it. In this tutorial, we'll show you how to do that with the double arcade control scheme--the left joystick moves the robot forward and backward, and the right joystick turns the robot.

We'll start off by adding the drive motors to the list of devices. In this example, we'll use a 6-motor drivetrain with the left and right motors plugged in to the following ports. Your port numbers can be different; all that matters is that the motors are named well and match up with the ports on the physical robot.

Now, we can start coding. For the sake of clarity, we'll put all of our drive code in a C++ function. Here's the format for a function in C++:

void driveCode() {
 //drives the robot around based on controller input, double arcade controls
}

The first order of business is to get controller inputs. Make sure you have a controller listed in your devices menu:

Now, we can get the inputs from the joysticks and store them in integer variables. The left joystick will control forwards and backwards motion in a straight line, so we'll store that value in a variable called straight. Next, we'll store the value for the right joystick in a variable called turn, since the right joystick turns the robot. Put the following code in the driveCode() function:

//Get controller inputs
int straight = Controller1.Axis3.value(); //gives a number -100 to 100
int turn = Controller1.Axis1.value();

Now, we need to calculate the motor power, in percentage points, that we should apply to each side of the drivetrain. We'll use two variables, left and right, to keep track of the percentage motor power we will apply to the left and right sides of the drivetrain. The following code calculates motor power, and it should be in the driveCode() function, right after the previous code.

//calculate proper motor powers
int left = straight + turn * 0.7; //the 0.7 makes the turns less intense
int right = straight - turn * 0.7;
Why the 0.7?

When adding (or subtracting) turn to straight, most teams multiply turn by a number less than 1 to give the drive better control of the robot while it is turning. For example, 0.5 would make the robot turn slower, 0.8 would make the robot turn very quickly, and 1 is much too fast for most drivers. Experiment with this number to find what your driver likes best; we use 0.7.

Now that we have calculated the motor powers for the left and right sides of the drivetrain, we just have to apply that power to them. Since we have 6 motors in this example, we'll apply the motor powers to all six motors.

//Spin the drive motors at the calculated velocity
Left1.spin(forward, left, percent);
Left2.spin(forward, left, percent);
Left3.spin(forward, left, percent);
Right1.spin(forward, right, percent);
Right2.spin(forward, right, percent);
Right3.spin(forward, right, percent);

Let's put all of it together so far! Make sure to put this function before the main loop.

void driveCode() {
  //drives the robot around based on controller input, double arcade controls
  
  //First, get controller inputs
  int straight = Controller1.Axis3.value(); //gives a number -100 to 100
  int turn = Controller1.Axis1.value();
  
  //Calculate proper motor powers
  int left = straight + turn * 0.7; //the 0.7 makes the turns less intense
  int right = straight - turn * 0.7;

  //Spin the drive motors at the calculated velocity
  Left1.spin(forward, left, percent);
  Left2.spin(forward, left, percent);
  Left3.spin(forward, left, percent);
  Right1.spin(forward, right, percent);
  Right2.spin(forward, right, percent);
  Right3.spin(forward, right, percent);
}

Looks great! but if you add this code to your program, nothing will happen. We've made the function driveCode(), but we haven't called it yet. Now, we need to add in another function called userControl that runs when the driver controlled period in a match is active.

//driver control code, is activated when the driver control period occurs
void userControl() {
  while (true) { //run this code indefinitely
    driveCode(); //update the drive motors to be at the right power
    wait(20, msec); //don't hog the CPU
  }
}

When the controller is told to run driver control during a match, your code needs to recognize that and call the right function. Thus, we'll initialize a Competition variable at the beginning of the main file, after the using namespace vex; line.

competition Competition;

Now, update your main function to call the userControl function when the driver control period starts:

int main() {
  //Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  
  Competition.drivercontrol(userControl);
  //You might have more code below, leave it
  
}

With that, you should have a fully functioning drivetrain! If the driving seems off, reverse directions of the the motors (try many combinations) until it works properly.

💻
🔴
🚗
For a four-motor drive, just leave out Right3 and Left3