Difference between revisions of "ARDUINO MEGA Functions Joysticks"

From ROBOTC API Guide
Jump to: navigation, search
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
<yambe:breadcrumb>ARDUINO_MEGA_Functions_and_Variables|Functions and Variables</yambe:breadcrumb>
+
{{DISPLAYTITLE:2560 (MEGA) Joystick Functions}}
 +
<yambe:breadcrumb self="2560 (MEGA) Joystick">ARDUINO_MEGA_Functions_and_Variables|Functions and Variables</yambe:breadcrumb>
 +
<br />
  
 
The Arduino supports a USB based joystick to send data from the PC over the USB link to the controller. You can access the joysticks by opening the "Joystick Control - Basic" joystick debugger window. For more information, see the different variables the joystick data will populate to below.
 
The Arduino supports a USB based joystick to send data from the PC over the USB link to the controller. You can access the joysticks by opening the "Joystick Control - Basic" joystick debugger window. For more information, see the different variables the joystick data will populate to below.
 +
 +
[[File:JoystickDebugger.png]]
  
 
'''Example Code:'''
 
'''Example Code:'''
 
   <syntaxhighlight lang="ROBOTC">
 
   <syntaxhighlight lang="ROBOTC">
   TPCJoystick joystick;
+
   TPCJoystick joystick;                         //Create the 'joystick' structure to store joystick data.
  getPCJoystickSettings(joystick);
+
 
 
+
 
   while(true)
 
   while(true)
 
   {
 
   {
     motor[servo_2] = joystick.joy1_y1;
+
     getPCJoystickSettings(joystick);           //Get the latest data from the joysticks and update the structure.
    motor[servo_3] = joystick.joy1_y2;
+
  
     if(joystick.joy1_Buttons & 0x01)
+
    motor[servo_2] = joystick.joy1_y1;          //Assign a motor the value of a joystick axis
 +
    motor[servo_3] = joystick.joy1_y2;          //Assign a motor the value of a joystick axis
 +
 
 +
     if(joystick.joy1_Buttons & 0x01)           //Check if button 1 from the button bit-mask is pressed.
 
       writeDebugStream("Button 1 Pressed");
 
       writeDebugStream("Button 1 Pressed");
  
     if(joystick.joy1_TopHat == 0)
+
     if(joystick.joy1_TopHat == 0)               //Check if the tophat is pressed in the up direction.
 
       writeDebugStream("Top of TopHat Pressed");
 
       writeDebugStream("Top of TopHat Pressed");
 
   }
 
   }
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
<br />
  
 
{{tl|1|1}}
 
{{tl|1|1}}
 
<br />
 
<br />
  
== Controller 1 ==
+
== getPCJoystickSettings ==
 +
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 +
|-
 +
| class="functionType"| <span class="bigKeywordB">getPCJoystickSettings</span><span class="bigCodePunc">(<span class="bigKeywordB">TPCJoystick</span> &<span class="bigCodeBasic">joystick</span>)</span>
 +
|-
 +
| style="font-family:Verdana, Geneva, sans-serif; color:black; background-color:#FFFFFF; text-align:left; font-size:100%;"|([[Data_Types#dataType_void|void]]) 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.
 +
|-
 +
|
 +
{| class="parameterTable" cellpadding="5%" width="100%"
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Parameter
 +
  ! width="60%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Explanation
 +
  ! width="20%" style="border-style: solid; border-width: 0px 0px 1px 0px"|Data Type
 +
  |-
 +
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|''&joystick''
 +
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|The joystick to grab updated values from.
 +
  | style="border-style: solid; border-width: 1px 0px 0px 0px"|[[Data_Types#dataType_TPCJoystick|TPCJoystick]]
 +
  |}
 +
|-
 +
|
 +
  {|
 +
  |-
 +
  |<syntaxhighlight lang="ROBOTC">
 +
while(true)                        // infinite loop:
 +
{
 +
  getPCJoystickSettings(joystick);    // update buttons and joysticks
 +
 
 +
  // do things with joystick here
 +
}
 +
</syntaxhighlight>
 +
  |-
 +
  |}
 +
|-
 +
|}
 +
<br />
 +
 
 +
== TPCJoystick Structure Members ==
 
=== joy1_Buttons ===
 
=== joy1_Buttons ===
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
 
{| style="color:black;" width="100%" cellpadding="5%" cellspacing="0" border="0"
Line 30: Line 71:
 
| 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.  
 
|-
 
|-
 
|
 
|
Line 38: Line 79:
 
while(true)                        // infinite loop:
 
while(true)                        // infinite loop:
 
{
 
{
   getJoystickSettings(joystick);    // update buttons and joysticks
+
   getPCJoystickSettings(joystick);    // update buttons and joysticks
 
    
 
    
   if(joystick.joy1_Buttons == 32)    // if Button 6 is pressed on joy1:
+
   if(joystick.joy1_Buttons & 0x20)    // if Button 6 is pressed on joy1:
 
   {
 
   {
     motor[motorA] = 50;                // motorA is run at a power level of 50
+
     motor[servo_2] = 50;                // servo_2 is run at a power level of 50
 
   }
 
   }
 
}
 
}
Line 65: Line 106:
 
while(true)                        // infinite loop:
 
while(true)                        // infinite loop:
 
{
 
{
   getJoystickSettings(joystick);    // update buttons and joysticks
+
   getPCJoystickSettings(joystick);    // update buttons and joysticks
 
    
 
    
 
   if(joystick.joy1_TopHat == 0)      // if the topmost button on joy1's D-Pad ('TopHat') is pressed:
 
   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
+
     motor[servo_2] = 50;                // servo_2 is run at a power level of 50
 
   }
 
   }
 
}
 
}
Line 92: Line 133:
 
while(true)                        // infinite loop:
 
while(true)                        // infinite loop:
 
{
 
{
   getJoystickSettings(joystick);    // update buttons and joysticks
+
   getPCJoystickSettings(joystick);    // update buttons and joysticks
   motor[motorB] = joystick.joy1_x1;  // motorB's powerlevel is set to the left stick's current x-value
+
   motor[servo_2] = joystick.joy1_x1;  // servo_2's powerlevel is set to the left stick's current x-value
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 115: Line 156:
 
while(true)                        // infinite loop:
 
while(true)                        // infinite loop:
 
{
 
{
   getJoystickSettings(joystick);    // update buttons and joysticks
+
   getPCJoystickSettings(joystick);    // update buttons and joysticks
   motor[motorB] = joystick.joy1_y1;  // motorB's powerlevel is set to the left stick's current y-value
+
   motor[servo_2] = joystick.joy1_y1;  // servo_2's powerlevel is set to the left stick's current y-value
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 138: Line 179:
 
while(true)                        // infinite loop:
 
while(true)                        // infinite loop:
 
{
 
{
   getJoystickSettings(joystick);    // update buttons and joysticks
+
   getPCJoystickSettings(joystick);    // update buttons and joysticks
   motor[motorB] = joystick.joy1_x2;  // motorB's powerlevel is set to the right stick's current x-value
+
   motor[servo_2] = joystick.joy1_x2;  // servo_2's powerlevel is set to the right stick's current x-value
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 161: Line 202:
 
while(true)                        // infinite loop:
 
while(true)                        // infinite loop:
 
{
 
{
   getJoystickSettings(joystick);    // update buttons and joysticks
+
   getPCJoystickSettings(joystick);    // update buttons and joysticks
   motor[motorB] = joystick.joy1_y2;  // motorB's powerlevel is set the right stick's current y-value
+
   motor[servo_2] = joystick.joy1_y2;  // servo_2's powerlevel is set the right stick's current y-value
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 20:53, 11 May 2012

ARDUINOFunctions and Variables → 2560 (MEGA) Joystick


The Arduino supports a USB based joystick to send data from the PC over the USB link to the controller. You can access the joysticks by opening the "Joystick Control - Basic" joystick debugger window. For more information, see the different variables the joystick data will populate to below.

JoystickDebugger.png

Example Code:

  TPCJoystick joystick;                         //Create the 'joystick' structure to store joystick data.
 
  while(true)
  {
    getPCJoystickSettings(joystick);            //Get the latest data from the joysticks and update the structure.
 
    motor[servo_2] = joystick.joy1_y1;          //Assign a motor the value of a joystick axis
    motor[servo_3] = joystick.joy1_y2;          //Assign a motor the value of a joystick axis
 
    if(joystick.joy1_Buttons & 0x01)            //Check if button 1 from the button bit-mask is pressed.
      writeDebugStream("Button 1 Pressed");
 
    if(joystick.joy1_TopHat == 0)               //Check if the tophat is pressed in the up direction.
      writeDebugStream("Top of TopHat Pressed");
  }


Color Key
Function:
Variable:


getPCJoystickSettings

getPCJoystickSettings(TPCJoystick &joystick)
(void) 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.
Parameter Explanation Data Type
&joystick The joystick to grab updated values from. TPCJoystick
while(true)                        // infinite loop:
{
  getPCJoystickSettings(joystick);     // update buttons and joysticks
 
  // do things with joystick here
}


TPCJoystick Structure Members

joy1_Buttons

TPCJoystick joy1_Buttons
(TPCJoystick) Returns a "Bit Map" for the 12 buttons on Controller #1.
while(true)                        // infinite loop:
{
  getPCJoystickSettings(joystick);     // update buttons and joysticks
 
  if(joystick.joy1_Buttons & 0x20)    // if Button 6 is pressed on joy1:
  {
    motor[servo_2] = 50;                // servo_2 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:
{
  getPCJoystickSettings(joystick);     // update buttons and joysticks
 
  if(joystick.joy1_TopHat == 0)      // if the topmost button on joy1's D-Pad ('TopHat') is pressed:
  {
    motor[servo_2] = 50;                // servo_2 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:
{
  getPCJoystickSettings(joystick);     // update buttons and joysticks
  motor[servo_2] = joystick.joy1_x1;  // servo_2'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:
{
  getPCJoystickSettings(joystick);     // update buttons and joysticks
  motor[servo_2] = joystick.joy1_y1;  // servo_2'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:
{
  getPCJoystickSettings(joystick);     // update buttons and joysticks
  motor[servo_2] = joystick.joy1_x2;  // servo_2'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:
{
  getPCJoystickSettings(joystick);     // update buttons and joysticks
  motor[servo_2] = joystick.joy1_y2;  // servo_2's powerlevel is set the right stick's current y-value
}