VEX2 Competition

Revision as of 16:25, 30 April 2012 by Bfeher (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
CORTEX → Competition Overview

There are three possible configurations that can be used to test your ROBOTC program for a VEX Competition.
  1. Using legacy 75 MHz crystals for wireless communications between transmitters and robot.
  2. Using the new VEXNET for wireless communications. And the manual switch competition emulator
  3. Using the new VEXNET for wireless communication. And the ROBOTC IDE connected via orange cable for competition testing.

Each of these is discussed in the next set of pages.

There are three states that need to be handled in a user’s competition program. These are:

State Explanation
DISABLED The robot is DISABLED. This condition can occur befor the start of either the Autonomous or User Control session. All motors and servos are disabled.
AUTONOMOUS A timed (20 second) duration period. Input from the joysticks is blocked. User program can control motors.
User Control Input from the joysticks is available. User program can control motors and servos.

Competition Template

In normal ROBOTC programming, every program is based around a “task main” function, as shown below:

task main()

In a competition program, things are a little different. To keep things simple, however, a Competition Template program is built into ROBOTC for use in VEX Competitions. The template provides a common starting point for teams when competing in an event. Rather than a single “main” task, the template has three sections, each matched to a specific section of the competition:

  • void pre_auton() – For running code that the robot needs to get “set up” before the competition begins
  • task autonomous() – For code the robot should run during the autonomous period (20 seconds by default)
  • task usercontrol() – For code the robot should run during the user control period (120 seconds by default)

The template and its accompanying “include” file handle the background work for the competition, such as monitoring the duration of each phase during a match, and communicating with the Vex Field Controller. All you need to do is tell the robot what to do in each phase.

Open a Competition Template

To open a competition template:

Go to File, select "New" and select "Competition Template"
Open comp 1.png
Note: The competition template is a read-only file, so be sure to save a copy of the file somewhere else on your computer.

Set Up Competition Control and Timing

#pragma competitionControl(Competition)
#pragma autonomousDuration(20)
#pragma userControlDuration(120)

With ROBOTC, the competition control is completely user defined and controlled via software, so there are no jumpers required for testing. By changing these settings, the template can be adapted to work with any competition. The three lines above set the VEX controller into the “competition” mode and specify the timing for the match.

#pragma competitionControl(competition_mode) – Controls the Competition mode that the Vex robot will function in. There are two different competition modes that you can pass to this statement:

  • OFF – Standard setting. No competition controls.
  • Competition – Competition mode. The VEX will respond to Field Controller commands and switch between Autonomous and User Control modes at the competition-specified times. Use this mode for competitions.

#pragma autonomousDuration(time_in_seconds) - Defines the duration of the autonomous phase of a VEX competition. Poor performance may result if this setting does not match the Field Controller settings.

#pragma userControlDuration(time_in_seconds) - Defines the duration of the user control phase of a VEX competition. Poor performance may result if this setting does not match the Field Controller settings.

Program and Test Competition Code

Phase 1: Pre-Competition

void pre_auton()
  // All activities that occur before the competition starts
  // Example: clearing encoders, setting servo positions, ...

Place your initialization code inside this function.
During the pre-competition section, code can be executed to configure your robot before the competition begins. Valid code for this section includes tasks such as configuring sensors, setting up reflected motors and setting initial servo positions.

Phase 2: Autonomous

task autonomous()
  // .....................................................................................
  // Insert user code here.
  // .....................................................................................
  AutonomousCodePlaceholderForTesting();  // Remove this function call once you have "real" code.

Place your Autonomous Period code inside this function. The AuonomousCodePlaceholderForTesting() function is only there as a placeholder and can be removed.

During the autonomous period, the robot performs actions autonomously for the length of time specified by the “autonomousDuration(time)” setting. While the robot cannot accept commands from the Transmitter during this time, it still requires that the Transmitter’s signal be present as a safety precaution.

You cannot skip the autonomous period during a competition by shutting the RF Transmitter off. If you shut the RF Transmitter off during the autonomous period, the VEX’s internal timers will pause, potentially causing your robot to enter the User Control period later than it should.

You do not need to use any bIfiAutonomousMode commands when competition programming.

Phase 3: Operator Control

task usercontrol()
  // User control code here, inside the loop
    // 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.
    // .....................................................................................
    UserControlCodePlaceholderForTesting(); // Remove this function call once you have "real" code.

Place your User Controlled Period code inside this function. UserControlCodePlaceholderForTesting() is only there for testing and can be removed once you have placed your own code inside of the while(true) loop.

During the operator controlled period, the robot can accept commands from the Transmitter. This segment of code executes immediately after the autonomous period ends. If you shut the Transmitter off during this period you will not increase the length of the Operator Controller period as the Field Control System determines and controls the length of the human player period.

Test Your Code

To test your competition code without a field control kit, follow these steps:
  1. Download code to VEX controller
  2. Disconnect the programming cable from the VEX controller
  3. Turn both the VEX and the Transmitter off (if not already)
  4. Place the VEX robot on a flat surface
  5. Turn the VEX controller on
  6. Wait 5 seconds
  7. Turn the Transmitter on… your Autonomous code should begin, and your User Control code should follow after the correct amount of time has elapsed.

If you are using VEXnet, please see the "Testing with VEXnet" topics under the "Competition with ROBOTC" heading.

Testing with Crystals

There is no “control” channel to signal between the game control / transmitters and the robot. The only control that the robot can detect is whether it is receiving a 75 MHz radio signal. So this is used to control a match.

The three states of the match are controlled as follows:

Robot 75 MHz Transmitter State Description
On AUTONOMOUS VEX master CPU is internally timing the autonomous mode (20 seconds). When timer expires it will switch to USER CONTROL state.
On USER CONTROL VEX master CPU is internally timing the user control mode (120 seconds). When timer expires it will switch to DISABLED state.

It’s very easy to simulate a Competition match without the need for any additional hardware. Just do the following:

  1. Start with ROBOT and Transmitter powered off.
  2. Turn on the ROBOT. User’s pre-autonomous code will run and the “competition” will be in a disabled stated.
  3. Turn on the transmitter. Robot will run autonomous code. After 20 seconds, the robot will run USER CONTROL code.
  4. Repeat steps 1 to 3 to simulate another match.

The timing for AUTONOMOUS (20 seconds) and USER CONTROL (120 seconds) is specified via special “#pragma” statements found at the start of your program. See the "Competition Template" page for more information.

Anytime you would like to re-test your competition code, make sure to start from step #1, or else the competition template may not execute correctly.

Testing with VEXnet - Manual

Innovation First has developed a simple “manual” switch that enables a team to simulate the stages of a competition. There are two manual two-position switches:
  • One switch toggles between DISABLED and one of the two operational states.
  • The second switch controls whether the robot is in AUTONOMOUS or USER CONTROL (Driver) mode.

Vex2 competition vexnet.png

Testing with VEXnet - Debugger

In ROBOTC you can directly simulate a VEXNET match from the ROBOTC debugger. The VEXNET transmitter needs to be connected via the orange programming cable to your PC for the debugger to function.

The ROBOTC IDE has a new window that allows you to put your robot into any one of the three competition states.
Vex2 competition vexnet debugger.png
Simply clicking on any of the three buttons will put your robot into the appropriate state.

NOTE: There is no duration timing when in this mode. To transition from AUTONOMOUS to USER CONTROL you need to click the “User Control” button. The VEXNET firmware will not perform timing and invoke the transition.