Skip to main content

Competition Code

Formatting Code for the Vex Robotics Competition

A template with the function callbacks for autonomous and usercontrol mode is required to perform in a vex competition. rolling robots template can be downloaded from gitHub here: Rolling Robots Example Code. The folder CompetitionCode is a blank competition project. Download this as a start for your code, you should rename it to something specific to you and do not return it to the gitHub. Use your own gitHub once you have made any edits.

Competition Template

Here is the code that is in main.cpp you can open a new project and copy paste this code to start or you cold find this code from the example codes that VexCode offers. 

 

NOTE: The competition template has three sections that correspond to the three phases of a competition match: Pre Autonomous (robot setup), Autonomous period, and Driver Controlled Period.

In order for your code to work at a competition, you must:

  1. Leave the code below inside the main function in place.
  2. Add your code inside one of the three function (pre_autonautonomoususercontrol).

Using a Pre-auton Function for Any Setup Steps

file-ARGT3hAOCl.png

Add any setup steps such as gyro calibration, or other sensor resets that should run when the program is started, to the pre_auton function.

NOTE: The code inside the pre_auton function below will run immediately when the program is started, before the autonomous portion of the match begins.

NOTE: If you choose not to use this section, leave it empty.

 

Using the Autonomous Function for Any Autonomous Routine

file-BQNqos8cCP.png

Put the code for your autonomous routine into the autonomous function. Your autonomous function must only contain commands that won’t need any interaction from a user. (E.g., BumperA.pressing())You should avoid any commands that require a user’s interaction. E.g Controller1.ButtonA.pressing();

NOTE: You can copy and paste this code from another project file.

 

Using the usercontrol Function for Any User Controlled Routines that Take Place During the Driver Controlled Period

file-pb1pyLUMH9.png

The usercontrol function must only contain commands controlled by the user. (E.g. Controller1.Axis1.position();)Put your driver control code into the usercontrol function, inside the while(1) loop and before the wait(20, msec) command.

 

/*----------------------------------------------------------------------------*/
/*                                                                            */
/*    Module:       main.cpp                                                  */
/*    Author:       VEX                                                       */
/*    Created:      Thu Sep 26 2019                                           */
/*    Description:  Competition Template                                      */
/*    Update:  DK  6-11-2021                                                  */
/*----------------------------------------------------------------------------*/

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

#include "vex.h"
using namespace vex;

// A global instance of competition
competition Competition;

// define your global Variables here
float pi=3.14;


// Custom Functions


void pre_auton() {
  // Initializing Robot Configuration. DO NOT REMOVE!
  vexcodeInit();
  // All activities that occur before the competition starts
  // Example: clearing encoders, setting initial positions, ...
}


void auton() {
  
}
void driver() {
  // User control code here, inside the loop
  while (true) {
    wait(10, msec); // Sleep the task for a short amount of time to prevent wasted resources.
  }
}
int main() {
  // Set up callbacks for autonomous and driver control periods.
  Competition.autonomous(auton);
  Competition.drivercontrol(driver);
  // Run the pre-autonomous function.
  pre_auton();
  // Prevent main from exiting with an infinite loop.
  while (true) {
    wait(100, msec);
  }
}