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
  4. Advanced

Tasks

Multithreading made simple

Tasks allow the code to execute multiple functions simultaneously. For example, one task could handle the driving inputs, and another task could spin a catapult or flywheel at the same time.

Tasks themselves are functions that return an integer (it's C++, go along with it). You can put whatever code you want inside of it, but usually there's a while loop so actions within the task execute more than once. Here's a basic task (called myTask) that spins a flywheel forward:

int myTask() {
  while (true) {
    //do something here
    Flywheel.spin(forward); //just an example
    wait(25, msec);
  }
  
  return 0; //C++ technicality
}

Tasks can be created with this line of code, and will not run until they have been created. The variable spinFlywheel stores the task internally for future reference.

task spinFlywheel = task(myTask);

If you want the task to stop running, call the stop() function on it:

spinFlywheel.stop();

We recommend using tasks for controlling lifts, catapults, or flywheels when the control algorithm required is non-trivial.

Example

Here's an example of using a task to spin a motor when a distance sensor detects an object. Let's start by making the task itself:

int spinMotorTask() {
  while (true) {
    if (DistanceSensor.objectDistance(mm) < 50) {
      Motor.spin(forward, 11, volt); //spin the motor when the distance sensor picks up an object
    } else {
      Motor.stop(); //stop the motor when the sensor doesn't
    }
    
    wait(20, msec); //don't hog the CPU
  }
  
  return 0; //C++ technicality
}

Now, in the main function, we'll call the task like so:

task myTask = task(spinMotorTask);

This code checks every 20 milliseconds to see if the distance sensor detects an object, and spins the motor forward if it does.

PreviousGraphing PIDsNextAdvanced Drive Code

Last updated 10 months ago

Was this helpful?

💻
🔴
🎛️
📋