Difference between revisions of "NXT Functions Joystick Control"

From ROBOTC API Guide
Jump to: navigation, search
 
(One intermediate revision by one user not shown)
Line 10: Line 10:
  
 
{{tl|1|1}}
 
{{tl|1|1}}
 +
<br />
 +
 +
== getJoystickSettings ==
 +
{| 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%;"|Before you can use the joystick variables, you have to "update" the variables by getting the newest packet of data from the joysticks. Because the joystick station may only send updates every 50-100ms, you should update your joystick values as often as possible to get the most up to date joystick data. To "update" your joystick variables, use this function.
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
while(true)                        // infinite loop:
 +
{
 +
  getJoystickSettings(joystick);    // update buttons and joysticks
 +
 
 +
  // do things with joystick here
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|}
 
<br />
 
<br />
  
Line 18: Line 40:
 
| class="variableType"| <span class="bigKeywordB">TPCJoystick joy1_Buttons</span>
 
| class="variableType"| <span class="bigKeywordB">TPCJoystick joy1_Buttons</span>
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_TPCJoystick|TPCJoystick]]) Returns a "Bit Map" for the 12 buttons on Controller #1. For more information on how to use buttons to control actions, See the [[NXT:_Using_Joysticks#Buttons|"Using Buttons"]] help section.
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_TPCJoystick|TPCJoystick]]) Returns a "Bit Map" for the 12 buttons on Controller #1. For more information on how to use buttons to control actions, See the [[NXT_Using_Joysticks#Buttons|"Using Buttons"]] help section.
 
|-
 
|-
 
|
 
|
Line 165: Line 187:
 
| class="variableType"| <span class="bigKeywordB">TPCJoystick joy2_Buttons</span>
 
| class="variableType"| <span class="bigKeywordB">TPCJoystick joy2_Buttons</span>
 
|-
 
|-
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_TPCJoystick|TPCJoystick]]) Returns a "Bit Map" for the 12 buttons on Controller #2. For more information on how to use buttons to control actions, See the [[NXT:_Using_Joysticks#Buttons|"Using Buttons"]] help section.
+
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_TPCJoystick|TPCJoystick]]) Returns a "Bit Map" for the 12 buttons on Controller #2. For more information on how to use buttons to control actions, See the [[NXT_Using_Joysticks#Buttons|"Using Buttons"]] help section.
 
|-
 
|-
 
|
 
|

Latest revision as of 20:14, 11 May 2012

NXTFunctions and Variables → Joystick Control


For information about Joystick Control and the NXT, as well as a setup tutorial, please see the article: NXT: Using Joysticks.


Color Key
Function:
Variable:


getJoystickSettings

Before you can use the joystick variables, you have to "update" the variables by getting the newest packet of data from the joysticks. Because the joystick station may only send updates every 50-100ms, you should update your joystick values as often as possible to get the most up to date joystick data. To "update" your joystick variables, use this function.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
 
  // do things with joystick here
}


Controller 1

joy1_Buttons

TPCJoystick joy1_Buttons
(TPCJoystick) Returns a "Bit Map" for the 12 buttons on Controller #1. For more information on how to use buttons to control actions, See the "Using Buttons" help section.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
 
  if(joystick.joy1_Buttons == 32)    // if Button 6 is pressed on joy1:
  {
    motor[motorA] = 50;                // motorA is run at a power level of 50
  }
}


joy1_TopHat

TPCJoystick joy1_TopHat
(TPCJoystick) Returns the value of the direction pad (or "Top Hat") on Controller #1. A value of -1 is returned when nothing is pressed, and a value of 0 to 7 for selected "octant" when pressed.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
 
  if(joystick.joy1_TopHat == 0)      // if the topmost button on joy1's D-Pad ('TopHat') is pressed:
  {
    motor[motorA] = 50;                // motorA is run at a power level of 50
  }
}


joy1_x1

TPCJoystick joy1_x1
(TPCJoystick) Value of the X Axis on the Left Joystick on Controller #1. Ranges in values between -128 to +127.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
  motor[motorB] = joystick.joy1_x1;  // motorB's powerlevel is set to the left stick's current x-value
}


joy1_y1

TPCJoystick joy1_y1
(TPCJoystick) Value of the Y Axis on the Left Joystick on Controller #1. Ranges in values between -128 to +127.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
  motor[motorB] = joystick.joy1_y1;  // motorB's powerlevel is set to the left stick's current y-value
}


joy1_x2

TPCJoystick joy1_x2
(TPCJoystick) Value of the X Axis on the Right Joystick on Controller #1. Ranges in values between -128 to +127.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
  motor[motorB] = joystick.joy1_x2;  // motorB's powerlevel is set to the right stick's current x-value
}


joy1_y2

TPCJoystick joy1_y2
(TPCJoystick) Value of the Y Axis on the Right Joystick on Controller #1. Ranges in values between -128 to +127.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
  motor[motorB] = joystick.joy1_y2;  // motorB's powerlevel is set the right stick's current y-value
}


Controller 2

joy2_Buttons

TPCJoystick joy2_Buttons
(TPCJoystick) Returns a "Bit Map" for the 12 buttons on Controller #2. For more information on how to use buttons to control actions, See the "Using Buttons" help section.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
 
  if(joystick.joy2_Buttons == 32)    // if Button 6 is pressed on joy2:
  {
    motor[motorA] = 50;                // motorA is run at a power level of 50
  }
}


joy2_TopHat

TPCJoystick joy2_TopHat
(TPCJoystick) Returns the value of the direction pad (or "Top Hat") on Controller #2. A value of -1 is returned when nothing is pressed, and a value of 0 to 7 for selected "octant" when pressed.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
 
  if(joystick.joy2_TopHat == 0)      // if the topmost button on joy2's D-Pad ('TopHat') is pressed:
  {
    motor[motorA] = 50;                // motorA is run at a power level of 50
  }
}


joy2_x1

TPCJoystick joy2_x1
(TPCJoystick) Value of the X Axis on the Left Joystick on Controller #2. Ranges in values between -128 to +127.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
  motor[motorB] = joystick.joy2_x1;  // motorB's powerlevel is set to the left stick's current x-value
}


joy2_y1

TPCJoystick joy2_y1
(TPCJoystick) Value of the Y Axis on the Left Joystick on Controller #2. Ranges in values between -128 to +127.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
  motor[motorB] = joystick.joy2_y1;  // motorB's powerlevel is set to the left stick's current y-value
}


joy2_x2

TPCJoystick joy2_x2
(TPCJoystick) Value of the X Axis on the Right Joystick on Controller #2. Ranges in values between -128 to +127.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
  motor[motorB] = joystick.joy2_x2;  // motorB's powerlevel is set to the right stick's current x-value
}


joy2_y2

TPCJoystick joy2_y2
(TPCJoystick) Value of the Y Axis on the Right Joystick on Controller #2. Ranges in values between -128 to +127.
while(true)                        // infinite loop:
{
  getJoystickSettings(joystick);     // update buttons and joysticks
  motor[motorB] = joystick.joy2_y2;  // motorB's powerlevel is set the right stick's current y-value
}