Difference between revisions of "NXT Using Joysticks"

From ROBOTC API Guide
Jump to: navigation, search
(Joystick Control - Competition)
(Joystick Control - Competition)
Line 130: Line 130:
 
|[[File:NXT Joystick Competition.png]]
 
|[[File:NXT Joystick Competition.png]]
 
|-
 
|-
|As you can see, the window has a few more options than the "Joystick Control - Simple" screen did.
+
|As you can see, the window has a few more options than the "Joystick Control - Simple" screen did. The radio buttons under 'Mode' simulate the different stages in the competition. The debugger window also allows you to choose which tele-op program on the NXT you would like to use. This allows you to test your program combinations quickly and effectively.
 
|-
 
|-
 
|[[File:single_joystick_game2.gif]]
 
|[[File:single_joystick_game2.gif]]
Line 138: Line 138:
 
|[[File:dual_joystick2.gif]]
 
|[[File:dual_joystick2.gif]]
 
|-
 
|-
 +
|<br />
 +
|-
 +
 
|}
 
|}
 
<br />
 
<br />

Revision as of 21:20, 11 September 2012

NXT → Using Joysticks


For ROBOTC Joystick NXT functions, check out the NXT Joystick Functions page!


ROBOTC has built two different Joystick Controller stations built into the interactive debugger. "Joystick Control - Simple" is a debugger window to control the NXT via a Logitech USB remote control. "Joystick Control - Game" is a full featured Controller Station which is used mainly for FIRST Tech Challange or other competitions that are NXT or TETRIX based.



Joystick Control

ROBOTC supports using Logitech USB joystick controllers to drive your NXT over the USB or Bluetooth communication link. This allows the user to send commands to their robot in real time, rather than having only pre-programmed behaviors.The joystick functionality works by taking data from the joystick controller and sends it to the NXT over the debugger link as a single message.


ROBOTC has a driver included to take this Bluetooth packet and break it into data, such as variables, that your robot can use. This driver file is provided as an include file. To use this include file, add this line of code to your program:


#include "JoystickDriver.c"


This include file will create 12 new variables for your program to use. These variables can be used to assign motor speeds, active blocks of code, or control different behaviors. For more examples of using these variables, see the sample code provided under the "Remote Control" sample code folder.


Before you can use these 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:

getJoystickSettings(joystick);

#include "JoystickDriver.c"     // Tells ROBOTC to include the driver file for the joystick.
task main()
{
 while(true)
  {
    getJoystickSettings(joystick);  // Update Buttons and Joysticks
    motor[motorC] = joystick.joy1_y1;
    motor[motorB] = joystick.joy1_y2;
  }
}


Buttons

Once you have updated your joystick values, you can now use the variables joystick.joy1_Buttons or joystick.joy2_Buttons to access a bit-masked value of all 12 buttons sent. This value, however, is not useable in your program right away.


ROBOTC has added two new functions, joy1Btn(button) and joy2Btn(button) to allow you to know which buttons have been pressed when multiple buttons are pressed at a time. Using joy1Btn(button) and joy2Btn(button), each button returns the a value of 1 if pressed and a value of 0 if not pressed. This will allow you to write your program to work with multiple buttons simultaenously.

#include "JoystickDriver.c"     // Tells ROBOTC to include the driver file for the joystick.
task main()
{
 while(true)
  {
    getJoystickSettings(joystick);  // Update Buttons and Joysticks
 
    if(joy1Btn(1) == 1)                  // If Joy1-Button1 is pressed:
    {
      motor[motorA] = 100;              // Turn Motor A On at full power
    }
    else                            // If Joy1-Button1 is NOT pressed:
    {
     motor[motorA] = 0;                 // Turn Motor A Off
    }
  }
}
Joystick front.gifJoystick top.gif


Joystick Control - Simple

After your program has been downloaded and the debugger is opened, you can open the Controller Station by:
  • Downloading your program and starting the debugger
  • Go to the "Robot" menu
  • Choose the "Debug Windows" sub-menu
  • Click on the "Joystick Control - Simple" menu option to open the Controller Station
Screenshot-2012-09-11 17.00.58.png
Once the Controller Station is opened, ROBOTC will look for any joysticks attached to your computer via USB. You can choose which joystick you want to robot to be controlled with by changed the joystick under the available drop-down menu. If you have no joysticks available, this list will be empty and ROBOTC will alert you that you have "No Controllers Configured"
Select joystick2.gif
You can see what data is being generated by the Joystick Station by looking at the X1, Y1, POV, X2, Y2 and Buttons display directly below the dropdown menu. This will give you realtime feedback of what values are being sent to your NXT from the Joystick Station. This data will also be illustrated with green dots to reflect the values and button presses.


ROBOTC will send joystick data to your NXT over Bluetooth or USB, but only when the Joystick Control window is opened. You will need to have the debugger open to use the Joystick Station.

Single joystick2.gif


Joystick Control - Competition

There is also a second Joystick Control window, "Joystick Control - Game". This window is specifically designed to emulate the FIRST Tech Challenge game mode. To test your FTC competition programs, you can use the controller station to mimic what the Field Management System will do. This includes switching between Autonomous and User Control, Changing if your robot is on the blue or red alliance and also disabling (or pausing) your robot. These commands can be found on the left side of the Joystick Station.


You can open the Controller Station by:

  • Go to the "Window" menu
  • Choose the "Menu Level" sub-menu
  • Click on the "Expert" or "Super User" menu option
NXT Joystick Level.png
After setting the Menu Level to "Expert" or "Super User", you can then access the "Joystick Control - Game" station by:
  • Downloading your program and starting the debugger
  • Going to the "Robot" menu
  • Choose the "Debug Windows" sub-menu
  • Click on the "Joystick Control - Competition" menu option to open the Controller Station
NXT Joystick Competition.png
As you can see, the window has a few more options than the "Joystick Control - Simple" screen did. The radio buttons under 'Mode' simulate the different stages in the competition. The debugger window also allows you to choose which tele-op program on the NXT you would like to use. This allows you to test your program combinations quickly and effectively.
Single joystick game2.gif
If you would like to use two controllers, click the "Dual Joysticks" button to expand the Joystick Control window to facilitate two controllers. You can assign the same controller to both Primary and Secondary Joysticks, but this is not recommended.
Dual joystick2.gif