Difference between revisions of "VEX2 Sensors Overview"

From ROBOTC API Guide
Jump to: navigation, search
(Created page with " {| style="font-family:Verdana, Genega, sans-sarif; font-size:80%;color:gray;" width="100%" cellpadding="0%" cellspacing="0" border="0" |- | ''Main >> [[VEX2_Ma...")
 
Line 11: Line 11:
 
|-
 
|-
 
|''For ROBOTC CORTEX Sensor ''functions'', check out the [[VEX2:_Functions_-_Sensors|CORTEX Sensor Functions]] page!''
 
|''For ROBOTC CORTEX Sensor ''functions'', check out the [[VEX2:_Functions_-_Sensors|CORTEX Sensor Functions]] page!''
 +
|-
 +
|
 +
The VEX 2.0 Cortex is equipped with 20 sensor ports. There are 8 Analog Sensor Ports and 12 Digital Sensor Ports.  Note that unlike the VEX - PIC microcontroller, all Analog sensors must be in Analog ports, while all Digital sensors must be in Digital ports.
 +
 +
There are a variety of functions and variables used for configuring these ports and accessing their values.
 +
 +
Configuring sensors can be complicated. ROBOTC has a built-in wizard that can be used to configure the VEX 2.0 Cortex sensors. The wizard contains a number of PC windows that allow you to set the following fields for the sensor:
 +
 +
*The variable name that you want to assign to the sensor. Using a name like “leftBumper” makes for a more readable program than 'dgtl3'!
 +
*The port that the sensor is connected to.
 +
*The type of sensor – touch, quadrature encoder, sonar, line follower, etc.
  
 
|-
 
|-
Line 23: Line 34:
  
  
== Touch Sensor ==
+
== 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"
 
|-
 
|-
Line 48: Line 59:
 
   motor[motorC] = 75;                  /* with a power level of 75    */
 
   motor[motorC] = 75;                  /* with a power level of 75    */
 
   wait1Msec(1000);                      // wait 1000 milliseconds (1 second) before moving to further code
 
   wait1Msec(1000);                      // wait 1000 milliseconds (1 second) before moving to further code
}
 
</syntaxhighlight>
 
|-
 
|}
 
<br />
 
 
== Light Sensor ==
 
{| 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"
 
|-
 
|The NXT Light Sensor has a range of states between 0 and 100.  The lower the number, the darker the reading is.  The higher the number, the lighter the reading is.  Below is a simple line-following program that uses only one NXT Light Sensor.
 
|-
 
|<syntaxhighlight lang="ROBOTC">
 
#pragma config(Sensor, S3,    lightSensor,        sensorLightActive)
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 
 
task main()
 
{
 
  wait1Msec(50);                        // The program waits 50 milliseconds to initialize the light sensor.
 
  while(true)                          // Infinite loop
 
  {
 
      if(SensorValue(lightSensor) < 45)  // If the Light Sensor reads a value less than 45:
 
      {
 
        motor[motorB] = 60;                  // Motor B is run at a 60 power level.
 
        motor[motorC] = 20;                  // Motor C is run at a 20 power level.
 
      }
 
      else                              // If the Light Sensor reads a value greater than or equal to 45:
 
      {
 
        motor[motorB] = 20;                  // Motor B is run at a 20 power level.
 
        motor[motorC] = 60;                  // Motor C is run at a 60 power level.
 
      }
 
  }
 
}
 
</syntaxhighlight>
 
|-
 
|}
 
<br />
 
 
== Sound Sensor ==
 
{| 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"
 
|-
 
|The NXT Sound Sensor has a range of states between 0 and 100.  The lower the number, the quieter the reading is.  The higher the number, the louder the reading is.  Below is a simple program that maps the Sound Sensor reading to the motor speeds.
 
|-
 
|<syntaxhighlight lang="ROBOTC">
 
#pragma config(Sensor, S2,    soundSensor,        sensorSoundDB)
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 
 
task main()
 
{
 
  wait1Msec(1000);      // A one-second wait is required to cleanly initialize the Sound Sensor.
 
  while(true)            // Infinite loop
 
  {
 
    motor[motorB] = SensorValue[soundSensor];    /* Motors B and C are run at a power level equal */
 
    motor[motorC] = SensorValue[soundSensor];    /* to the value read in by the Sound Sensor.    */
 
  }
 
}
 
</syntaxhighlight>
 
|-
 
|}
 
<br />
 
 
== Ultrasonic (sonar) Sensor ==
 
{| 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"
 
|-
 
|The NXT Ultrasonic Sensor has a range of states between 0 and 255.  This number is representative of the current reading in centimeters.  However, a reading of 255 means that the current sensor reading is out of range.  This is a "range error" and means that the echo is not being read back (looking down a long hall for example).  A reading outside of these numbers indicates that there is no sensor attached or some error with the connection.
 
|-
 
|<syntaxhighlight lang="ROBOTC">
 
#pragma config(Sensor, S4,    sonarSensor,        sensorSONAR)
 
//*!!Code automatically generated by 'ROBOTC' configuration wizard              !!*//
 
 
task main()
 
{
 
  int distance_in_cm = 20;  // Create variable 'distance_in_cm' and initialize it to 20(cm).
 
  while(SensorValue[sonarSensor] > distance_in_cm)  /* While the Sonar Sensor readings are greater */
 
  }                                                  /* than the specified, 'distance_in_cm':      */
 
    motor[motorB] = 75;                    // Motor B is run at a 75 power level
 
    motor[motorC] = 75;                    // Motor C is run at a 75 power level
 
  }
 
  motor[motorB] = 75;    // Motor B is stopped at a 0 power level
 
  motor[motorC] = 75;    // Motor C is stopped at a 0 power level
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 16:09, 15 February 2012

Main >> CORTEX >> Sensors Overview


For ROBOTC CORTEX Sensor functions, check out the CORTEX Sensor Functions page!

The VEX 2.0 Cortex is equipped with 20 sensor ports. There are 8 Analog Sensor Ports and 12 Digital Sensor Ports. Note that unlike the VEX - PIC microcontroller, all Analog sensors must be in Analog ports, while all Digital sensors must be in Digital ports.

There are a variety of functions and variables used for configuring these ports and accessing their values.

Configuring sensors can be complicated. ROBOTC has a built-in wizard that can be used to configure the VEX 2.0 Cortex sensors. The wizard contains a number of PC windows that allow you to set the following fields for the sensor:

  • The variable name that you want to assign to the sensor. Using a name like “leftBumper” makes for a more readable program than 'dgtl3'!
  • The port that the sensor is connected to.
  • The type of sensor – touch, quadrature encoder, sonar, line follower, etc.



Analog Sensors

The NXT Touch Sensor has 2 states, pressed or unpressed. In code this is represented with a 1 for pressed, and a 0 for unpressed. There are no other states for this sensor. Below is a simple program that keeps your program from running until you press the Touch Sensor.
#pragma config(Sensor, S1,     touchSensor,         sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
 
task main()
{
  while(SensorValue(touchSensor) == 0)  // while the Touch Sensor is inactive (hasn't been pressed):
  {
    // DO NOTHING (wait for press)
  }
  while(SensorValue(touchSensor) == 1)  // while the Touch Sensor is active (pressed):
  {
    // DO NOTHING (wait for release)
  }
 
  // YOUR CODE GOES HERE
                                        // otherwise (the touch sensor has been activated [pressed] ):
  motor[motorB] = 75;                   /* run motors B and C forwards */
  motor[motorC] = 75;                   /* with a power level of 75    */
  wait1Msec(1000);                      // wait 1000 milliseconds (1 second) before moving to further code
}