Difference between revisions of "VEX2 Competition"

From ROBOTC API Guide
Jump to: navigation, search
Line 23: Line 23:
 
*<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">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">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)
+
*<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.
 
|-
 
|-
 
|}
 
|}
Line 35: Line 37:
  
  
== Analog Sensors ==
+
== 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="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:
+
|To open a 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.
 
|-
 
|-
 
|
 
|
=== Light ===
 
[[File:Vex light sensor.png]]
 
<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.
 
<br />
 
 
{|
 
  |-
 
  |
 
<syntaxhighlight lang="ROBOTC">
 
#pragma config(Sensor, in1,    lightSensor,    sensorReflection)
 
//*!!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
 
 
 
  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>
 
  |-
 
  |}
 
 
|-
 
|-
 
|
 
|
 
=== 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 />
 
 
{|
 
  |-
 
  |
 
<syntaxhighlight lang="ROBOTC">
 
#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
 
 
 
  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("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>
 
  |-
 
  |}
 
|-
 
|
 
=== 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 />
 
 
{|
 
  |-
 
  |
 
<syntaxhighlight lang="ROBOTC">
 
#pragma config(Sensor, in1,    lineFollower,  sensorLineFollower)
 
//*!!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
 
 
 
  while(true)                    // infinite loop:
 
  {
 
    if(SensorValue(lineFollower) < 950) // if the lineFollower sensor reads a value less than 950:
 
    {
 
      // 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
 
    }
 
    else  // lineFollower sensor reads a value greater than or equal to 950:
 
    {
 
      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
 
    }
 
  }
 
}
 
</syntaxhighlight>
 
  |-
 
  |}
 
|-
 
|
 
 
=== Gyro ===
 
[[File:vex_gyro.png]]
 
<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].''
 
<br />
 
 
{|
 
  |-
 
  |
 
<syntaxhighlight lang="ROBOTC">
 
#pragma config(Motor, port2, rightMotor, tmotorNormal, openLoop)
 
#pragma config(Motor, port3, leftMotor, tmotorNormal, openLoop, reversed)
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard !!*//
 
 
/*+++++++++++++++++++++++++++++++++++++++++++++| Notes |++++++++++++++++++++++++++++++++++++++++++++
 
Gyro Based Turns - Basic
 
-This program instructs your robot to turn for the specified number of degrees in "degrees10".
 
-For best sensor results, clear out the gyro and manually configure it at the begging of the code.
 
-The Gyro is configured by default to provide values from 0 to -3600 for clockwise rotation,
 
and 0 to 3600 for counter-clockwise rotation
 
 
Robot Model(s): Swervebot
 
 
[I/O Port] [Name] [Type] [Description]
 
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,
 
away from the Cortex
 
--------------------------------------------------------------------------------------------------*/
 
 
task main()
 
{
 
  //Completely clear out any previous sensor readings by setting the port to "sensorNone"
 
  SensorType[in8] = sensorNone;
 
  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
 
  //SensorScale[in8] = 260;
 
  //Adjust SensorFullCount to set the "rollover" point. 3600 sets the rollover point to +/-3600
 
  //SensorFullCount[in8] = 3600;
 
 
  //Specify the number of degrees for the robot to turn (1 degree = 10, or 900 = 90 degrees)
 
  int degrees10 = 900;
 
 
  //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
 
  motor[rightMotor] = -5;
 
  motor[leftMotor] = 5;
 
  wait1Msec(250);
 
}
 
</syntaxhighlight>
 
  |-
 
  |}
 
|-
 
|
 
 
=== Accelerometer ===
 
[[File:vex_accel.png]]
 
<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)''.
 
<br />
 
 
{|
 
  |-
 
  |
 
<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()
 
{
 
  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>
 
  |-
 
  |}
 
 
|-
 
|-
 
|
 
|

Revision as of 18:45, 6 March 2012

Main >> CORTEX >> Competitions with ROBOTC


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:

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.