Difference between revisions of "VEX2 Competition"

From ROBOTC API Guide
Jump to: navigation, search
 
(40 intermediate revisions by one user not shown)
Line 1: Line 1:
 +
<yambe:breadcrumb self="Competition Overview">VEX2|CORTEX</yambe:breadcrumb>
 +
<br />
  
{| style="font-family:Verdana, Genega, sans-sarif; font-size:80%;color:gray;" width="100%" cellpadding="0%" cellspacing="0" border="0"
+
{|
 
|-
 
|-
|  
+
|There are three possible configurations that can be used to test your ROBOTC program for a VEX Competition.
''[[Main_Page|Main]] >> [[VEX2_Main|CORTEX]] >> [[VEX2:_Competition|Competitions with ROBOTC]] ''
+
 
 +
#Using legacy 75 MHz crystals for wireless communications between transmitters and robot.
 +
#Using the new VEXNET for wireless communications. And the manual switch competition emulator
 +
#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:<br />
 +
 
 +
{| class="parameterTable" cellpadding="5%"
 +
  ! style="color:white; background-color:#c0504d; border-color:#000; border-style: solid; border-width: 2px 0px 1px 0px"|State
 +
  ! style="color:white; background-color:#c0504d; border-color:#000; border-style: solid; border-width: 2px 0px 1px 0px"|Explanation
 +
  |-
 +
  | style="color:white; background-color:#c0504d; border-color:#000; border-style: solid; border-width: 1px 0px 0px 0px"|DISABLED
 +
  | style="color:black; background-color:#d8d8d8; border-color:#000; border-style: solid; border-width: 1px 0px 0px 0px"|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.
 +
  |-
 +
  | style="color:white; background-color:#c0504d; border-color:#000; border-style: solid; border-width: 0px 0px 0px 0px"|AUTONOMOUS
 +
  | style="color:black; background-color:#f4f4f4; border-color:#000; border-style: solid; border-width: 0px 0px 0px 0px"|A timed (20 second) duration period. Input from the joysticks is blocked. User program can control motors.
 +
  |-
 +
  | style="color:white; background-color:#c0504d; border-color:#000; border-style: solid; border-width: 0px 0px 2px 0px"|User Control
 +
  | style="color:black; background-color:#d8d8d8; border-color:#000; border-style: solid; border-width: 0px 0px 2px 0px"|Input from the joysticks is available. User program can control motors and servos.
 +
  |-
 +
  |}
 
|-
 
|-
 
|}
 
|}
 +
<br />
  
 +
{{tl|1|}}
 +
<br />
  
{| class="wikiText"
+
== Competition Template ==
 +
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
|In normal ROBOTC programming, every program is based around a “task main” function, as shown below:<br />
+
|
 +
In normal ROBOTC programming, every program is based around a “task main” function, as shown below:<br />
  
<syntaxhighlight lang="ROBOTC">
+
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"  cellpadding="5%" cellspacing="0" border="0"
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 
task main()
 
task main()
 
{
 
{
Line 18: Line 49:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  |-
 +
  |}
 +
<br />
 +
 +
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:<br />
 +
 +
*<span class="keywordBI">void </span><span class="codeBasic">pre_auton</span><span class="codePunc">()</span> – For running code that the robot needs to get “set up” before the competition begins
 +
*<span class="keywordBI">task </span><span class="codeBasic">autonomous</span><span class="codePunc">()</span> – For code the robot should run during the autonomous period (20 seconds by default)
 +
*<span class="keywordBI">task </span><span class="codeBasic">usercontrol</span><span class="codePunc">()</span> – For code the robot should run during the user control period (120 seconds by default)<br />
 +
 +
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.
 
|-
 
|-
 
|}
 
|}
 +
<br />
  
 
+
=== Open a Competition Template ===
{|
+
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
|-
 
|-
|style="vertical-align:top"| __TOC__
+
|To open a competition template:<br />
 +
Go to File, select "New" and select "Competition Template"<br />
 +
[[File:open_comp_1.png]]<br />
 +
''Note:'' The competition template is a read-only file, so be sure to save a copy of the file somewhere else on your computer.
 
|-
 
|-
 
|}
 
|}
 +
<br />
  
 
+
=== Set Up Competition Control and Timing ===
== Analog Sensors ==
+
 
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
|-
 
|There are 5 main types of Analog Sensors for the VEX CORTEX:
 
 
|-
 
|-
 
|
 
|
=== Light ===
+
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" cellpadding="5%" cellspacing="0" border="0"
[[File:Vex light sensor.png]]
+
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
#pragma competitionControl(Competition)
 +
#pragma autonomousDuration(20)
 +
#pragma userControlDuration(120)
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 
<br />
 
<br />
The Light sensor, also known as the Reflection sensor, returns values ranging between 0 and 4095. 0 is the lightest reading and 4095 is the darkest.
+
 
 +
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.<br />
 +
 
 +
<span class="keywordBI">#pragma </span><span class="keywordI">competitionControl</span><span class="codePunc">(</span><span class="keywordI">competition_mode</span><span class="codePunc">)</span> – Controls the Competition mode that the Vex robot will function in. There are two different competition modes that you can pass to this statement:<br />
 +
 
 +
*<span class="keywordI">OFF</span> – Standard setting. No competition controls.
 +
*<span class="keywordI">Competition</span> – 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.
 +
 
 +
<span class="keywordBI">#pragma </span><span class="keywordI">autonomousDuration</span><span class="codePunc">(</span><span class="keywordI">time_in_seconds</span><span class="codePunc">)</span> - 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.<br />
 +
 
 +
<span class="keywordBI">#pragma </span><span class="keywordI">userControlDuration</span><span class="codePunc">(</span><span class="keywordI">time_in_seconds</span><span class="codePunc">)</span> - 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.<br />
 +
 
 +
 
 +
 
 +
|-
 +
|}
 
<br />
 
<br />
  
{|
+
=== Program and Test Competition Code ===
 +
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
|
 +
==== Phase 1: Pre-Competition ====
 +
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" cellpadding="5%" cellspacing="0" border="0"
 
   |-
 
   |-
   |
+
   |<syntaxhighlight lang="ROBOTC">
<syntaxhighlight lang="ROBOTC">
+
void pre_auton()
#pragma config(Sensor, in1,    lightSensor,    sensorReflection)
+
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
+
 
+
task main()
+
 
{
 
{
   wait1Msec(2000);                // wait 2 seconds before exectuing following code
+
   // All activities that occur before the competition starts
   bMotorReflected[port2] = true;  // reflects direction of motor on port 2
+
   // Example: clearing encoders, setting servo positions, ...
 
+
  while(true)                    // infinite loop:
+
  {
+
    clearLCDLine(0);                                // clear the top VEX LCD line
+
    clearLCDLine(1);                                // clear the bottom VEX LCD line
+
   
+
    setLCDPosition(0,0);                            // set the VEX LCD cursor the first line, first space
+
    displayNextLCDString("Light Sensor:");          // display "Light Sensor:" on the top line
+
   
+
    setLCDPosition(1,0);                            // set the VEX LCD cursor the second line, first space
+
    displayNextLCDNumber(SensorValue(lightSensor)); // display the reading of the lightSensor sensor
+
   
+
    wait1Msec(50);                                  // wait 50 milliseconds to help display properly
+
  }
+
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
 
   |}
 
   |}
|-
 
|
 
 
=== Potentiometer ===
 
[[File:Vex pot.png]]
 
<br />
 
Returns an analog value between 0 and 4095 ''(although mechanical stops my limit the values to between 5 and 4092)''.
 
 
<br />
 
<br />
  
{|
+
Place your initialization code inside this function.<br />
 +
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.<br />
 +
 
 +
==== Phase 2: Autonomous ====
 +
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" cellpadding="5%" cellspacing="0" border="0"
 
   |-
 
   |-
   |
+
   |<syntaxhighlight lang="ROBOTC">
<syntaxhighlight lang="ROBOTC">
+
task autonomous()
#pragma config(Sensor, in1,    potentiometer,  sensorPotentiometer)
+
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
+
 
+
task main()
+
 
{
 
{
   wait1Msec(2000);                // wait 2 seconds before exectuing following code
+
   // .....................................................................................
   bMotorReflected[port2] = true;  // reflects direction of motor on port 2
+
  // Insert user code here.
 +
   // .....................................................................................
 
    
 
    
   while(true)                    // infinite loop:
+
   AutonomousCodePlaceholderForTesting(); // Remove this function call once you have "real" code.
  {
+
    clearLCDLine(0);                                 // clear the top VEX LCD line
+
    clearLCDLine(1);                                  // clear the bottom VEX LCD line
+
   
+
    setLCDPosition(0,0);                              // set the VEX LCD cursor the first line, first space
+
    displayNextLCDString("Potentiometer:");          // display "Potentiometer:" on the top line
+
   
+
    setLCDPosition(1,0);                              // set the VEX LCD cursor the second line, first space
+
    displayNextLCDNumber(SensorValue(potentiometer)); // display the reading of the potentiometer sensor
+
   
+
    wait1Msec(50);                                    // wait 50 milliseconds to help display properly
+
  }
+
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
   |-
 
   |-
 
   |}
 
   |}
|-
 
|
 
=== Line Follower ===
 
[[File:vex_linefollower.png]]
 
<br />
 
Returns values ranging between 0 and 4095.  0 is the lightest reading and 4095 is the darkest.
 
 
<br />
 
<br />
  
{|
+
Place your Autonomous Period code inside this function. The <span class="codeBasic">AuonomousCodePlaceholderForTesting</span><span class="codePunc">()</span> function is only there as a placeholder and can be removed.<br />
  |-
+
  |
+
<syntaxhighlight lang="ROBOTC">
+
#pragma config(Sensor, in1,    lineFollower,  sensorLineFollower)
+
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
+
  
task main()
+
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. <br />
 +
 
 +
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.<br />
 +
 
 +
You do not need to use any <span class="keywordB">bIfiAutonomousMode</span> commands when competition programming.<br />
 +
 
 +
==== Phase 3: Operator Control ====
 +
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" cellpadding="5%" cellspacing="0" border="0"
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
task usercontrol()
 
{
 
{
   wait1Msec(2000);                // wait 2 seconds before exectuing following code
+
   // User control code here, inside the loop
  bMotorReflected[port2] = true;  // reflects direction of motor on port 2
+
 
 
+
   while(true)  
   while(true)                     // infinite loop:
+
 
   {
 
   {
     if(SensorValue(lineFollower) < 950) // if the lineFollower sensor reads a value less than 950:
+
     // 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.
      // turn left:
+
 
      motor[port2] = 50;  // motor on port 2 is run at power level 50
+
    // .....................................................................................
      motor[port3] = 0;  // motor on port 3 is stopped at power level 0
+
     // Insert user code here. This is where you use the joystick values to update your motors, etc.
     }
+
     // .....................................................................................
    else  // lineFollower sensor reads a value greater than or equal to 950:
+
 
     {
+
    UserControlCodePlaceholderForTesting(); // Remove this function call once you have "real" code.
      motor[port2] = 0;  // motor on port 2 is stopped at power level 0
+
      motor[port3] = 50; // motor on port 3 is run at power level 50
+
    }
+
 
   }
 
   }
 
}
 
}
Line 146: Line 176:
 
   |-
 
   |-
 
   |}
 
   |}
 +
<br />
 +
 +
Place your User Controlled Period code inside this function. <span class="codeBasic">UserControlCodePlaceholderForTesting</span><span class="codePunc">()</span> is only there for testing and can be removed once you have placed your own code inside of the while(true) loop.<br />
 +
 +
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.<br />
 +
 
 
|-
 
|-
|
+
|}
 +
<br />
  
=== Gyro ===
+
=== Test Your Code ===
[[File:vex_gyro.png]]
+
{| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
|To test your competition code without a field control kit, follow these steps:
 +
 
 +
#Download code to VEX controller
 +
#Disconnect the programming cable from the VEX controller
 +
#Turn both the VEX and the Transmitter off (if not already)
 +
#Place the VEX robot on a flat surface
 +
#Turn the VEX controller on
 +
#Wait 5 seconds
 +
#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.
 +
|-
 +
|}
 
<br />
 
<br />
As the Gyro is turned, the values it returns are in '''tenths of degrees''',  positive and negative. Thus, a SensorValue of 3600 equals 360 degrees,  or one full rotation. When the sensor is mounted horizontally,  counter-clockwise movements will return values from 0 to -3600;  clockwise movements will return values from 0 to 3600. Once the gyro  completes one full revolution, the sensor value will “roll-over” to 0 by  default (for example: …3597, 3598, 3599, 3600, 0, 1, 2, 3,…). To change  the “roll-over” point, un-comment line 33 (from the code below) and change the value of  “SensorFullCount” from 3600 to the desired value (7200, 18000, ect)
 
  
''For more information on the Gyro sensor, check out the ROBOTC blog post: [http://www.robotc.net/blog/2011/10/13/programming-the-vex-gyro-in-robotc/ Programming the VEX Gyro in ROBOTC].''
+
== Testing with Crystals ==
 +
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|'''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:
 
<br />
 
<br />
  
{|
+
{| class="parameterTable" cellpadding="5%" style="color:#943634;"
 +
  ! style="background-color:white; border-color:#943634; border-style: solid; border-width: 2px 0px 1px 0px"|Robot
 +
  ! style="background-color:white; border-color:#943634; border-style: solid; border-width: 2px 0px 1px 0px"|75 MHz Transmitter
 +
  ! style="background-color:white; border-color:#943634; border-style: solid; border-width: 2px 0px 1px 0px"|State
 +
  ! style="background-color:white; border-color:#943634; border-style: solid; border-width: 2px 0px 1px 0px"|Description
 
   |-
 
   |-
   |
+
   | style="background-color:#efd3d2; border-color:#943634; border-style: solid; border-width: 1px 0px 0px 0px"|'''OFF'''
<syntaxhighlight lang="ROBOTC">
+
  | style="background-color:#efd3d2; border-color:#943634; border-style: solid; border-width: 1px 0px 0px 0px"|Off
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop)
+
  | style="background-color:#efd3d2; border-color:#943634; border-style: solid; border-width: 1px 0px 0px 0px"|
#pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop, reversed)
+
  | style="background-color:#efd3d2; border-color:#943634; border-style: solid; border-width: 1px 0px 0px 0px"|
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
+
  |-
 +
  | style="background-color:white; border-color:#943634; border-style: solid; border-width: 0px 0px 2px 0px" rowspan="4"|'''ON'''
 +
  | style="background-color:white; border-color:#943634; border-style: solid; border-width: 0px 0px 0px 0px"|Off
 +
  | style="background-color:white; border-color:#943634; border-style: solid; border-width: 0px 0px 0px 0px"|DISABLED
 +
  | style="background-color:white; border-color:#943634; border-style: solid; border-width: 0px 0px 0px 0px"|
 +
  |-
 +
  | style="background-color:#efd3d2; border-color:#943634; border-style: solid; border-width: 0px 0px 0px 0px"|On
 +
  | style="background-color:#efd3d2; border-color:#943634; border-style: solid; border-width: 0px 0px 0px 0px"|AUTONOMOUS
 +
  | style="background-color:#efd3d2; border-color:#943634; border-style: solid; border-width: 0px 0px 0px 0px"|VEX master CPU is internally timing the autonomous mode (20 seconds). When timer expires it will switch to USER CONTROL state.
 +
  |-
 +
  | style="background-color:white; border-color:#943634; border-style: solid; border-width: 0px 0px 0px 0px"|Off
 +
  | style="background-color:white; border-color:#943634; border-style: solid; border-width: 0px 0px 0px 0px"|DISABLED
 +
  | style="background-color:white; border-color:#943634; border-style: solid; border-width: 0px 0px 0px 0px"|
 +
  |-
 +
  | style="background-color:#efd3d2; border-color:#943634; border-style: solid; border-width: 0px 0px 2px 0px"|On
 +
  | style="background-color:#efd3d2; border-color:#943634; border-style: solid; border-width: 0px 0px 2px 0px"|USER CONTROL
 +
  | style="background-color:#efd3d2; border-color:#943634; border-style: solid; border-width: 0px 0px 2px 0px"|VEX master CPU is internally timing the user control mode (120 seconds). When timer expires it will switch to DISABLED state.
 +
  |-
 +
  |}
 +
<br />
  
/*+++++++++++++++++++++++++++++++++++++++++++++| Notes |++++++++++++++++++++++++++++++++++++++++++++
+
It’s very easy to simulate a Competition match without the need for any additional hardware. Just do the following:
Gyro Based Turns - Basic
+
#Start with ROBOT and Transmitter powered off.
-This program instructs your robot to turn for the specified number of degrees in "degrees10".
+
#Turn on the ROBOT. User’s pre-autonomous code will run and the “competition” will be in a disabled stated.
-For best sensor results, clear out the gyro and manually configure it at the begging of the code.
+
#Turn on the transmitter. Robot will run autonomous code. After 20 seconds, the robot will run USER CONTROL code.
-The Gyro is configured by default to provide values from 0 to -3600 for clockwise rotation,
+
#Repeat steps 1 to 3 to simulate another match.
and 0 to 3600 for counter-clockwise rotation
+
  
Robot Model(s): Swervebot
+
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.
  
[I/O Port] [Name] [Type] [Description]
+
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.
Motor Port 2 rightMotor VEX Motor Right side motor
+
|-
Motor Port 3 leftMotor VEX Motor Left side motor
+
|}
Analog Port 8 in8 VEX Gyro Top-center mounted,
+
<br />
away from the Cortex
+
--------------------------------------------------------------------------------------------------*/
+
  
task main()
+
== Testing with VEXnet - Manual ==
{
+
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
  //Completely clear out any previous sensor readings by setting the port to "sensorNone"
+
|-
  SensorType[in8] = sensorNone;
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|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:
  wait1Msec(1000);
+
  //Reconfigure Analog Port 8 as a Gyro sensor and allow time for ROBOTC to calibrate it
+
  SensorType[in8] = sensorGyro;
+
  wait1Msec(2000);
+
  
  //Adjust SensorScale to correct the scaling for your gyro
+
*One switch toggles between DISABLED and one of the two operational states.
  //SensorScale[in8] = 260;
+
*The second switch controls whether the robot is in AUTONOMOUS or USER CONTROL (Driver) mode.
  //Adjust SensorFullCount to set the "rollover" point. 3600 sets the rollover point to +/-3600
+
<br />
  //SensorFullCount[in8] = 3600;
+
[[File:vex2_competition_vexnet.png]]
 +
|-
 +
|}
 +
<br />
  
  //Specify the number of degrees for the robot to turn (1 degree = 10, or 900 = 90 degrees)
+
== Testing with VEXnet - Debugger ==
  int degrees10 = 900;
+
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|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.
  
  //While the absolute value of the gyro is less than the desired rotation...
 
  while(abs(SensorValue[in8]) < degrees10)
 
  {
 
    //...continue turning
 
    motor[rightMotor] = 25;
 
    motor[leftMotor] = -25;
 
  }
 
  
  //Brief brake to stop some drift
+
The ROBOTC IDE has a new window that allows you to put your robot into any one of the three competition states.
  motor[rightMotor] = -5;
+
  motor[leftMotor] = 5;
+
  wait1Msec(250);
+
}
+
</syntaxhighlight>
+
  |-
+
  |}
+
|-
+
|
+
 
+
=== Accelerometer ===
+
[[File:vex_accel.png]]
+
 
<br />
 
<br />
Returns an analog value between 0 and 4095.  Each Accelerometer Sensor has 3 cables, X, Y, and Z.  The ports you attach them to don't have to be in any particular order ''(however you WILL need to use 3 ports)''.
+
[[File:vex2_competition_vexnet_debugger.png]]
 
<br />
 
<br />
 +
Simply clicking on any of the three buttons will put your robot into the appropriate state.
  
{|
 
  |-
 
  |
 
<syntaxhighlight lang="ROBOTC">
 
#pragma config(Sensor, in1,    xAxis,          sensorAccelerometer)
 
#pragma config(Sensor, in2,    yAxis,          sensorAccelerometer)
 
#pragma config(Sensor, in3,    zAxis,          sensorAccelerometer)
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 
  
task main()
+
''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.
{
+
  int nBiasValues[3];
+
 
+
  int X_Accel;
+
  int Y_Accel;
+
  int Z_Accel;
+
 
+
  wait1Msec(500); // bias values are being calculated
+
 
+
  /* store the bias values in an array so that they can be  
+
  * displayed in the ROBOTC global variables debug window */
+
  nBiasValues[0] = SensorBias[xAxis];
+
  nBiasValues[1] = SensorBias[yAxis];
+
  nBiasValues[2] = SensorBias[zAxis];
+
 
+
  while(true)
+
  {
+
    /* also store the actual sensor values so that they can be
+
    * easily displayed in the ROBOTC global variables debug window */
+
    X_Accel = SensorValue[xAxis];
+
    Y_Accel = SensorValue[yAxis];
+
    Z_Accel = SensorValue[zAxis];
+
   
+
    wait1Msec(100);
+
  }
+
}
+
</syntaxhighlight>
+
  |-
+
  |}
+
|-
+
|
+
 
|-
 
|-
 
|}
 
|}
 
<br />
 
<br />

Latest revision as of 16:25, 30 April 2012

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
 
  while(true) 
  {
    // 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
OFF Off
ON Off DISABLED
On AUTONOMOUS VEX master CPU is internally timing the autonomous mode (20 seconds). When timer expires it will switch to USER CONTROL state.
Off DISABLED
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.