Using a joystick with NXC

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
schedman
Posts: 3
Joined: 27 Oct 2011, 22:06

Using a joystick with NXC

Post by schedman »

I'm teaching a class using NXC and would like my students to write their own NXC code to control a robot that can take driver controlled commands from a joystick. The robot will be connected via Bluetooth to a laptop with a Logitech F310 Gamepad attached via USB. To test out this setup, I've installed the July 24 2012 test release of BricxCC that includes support for NXC 1.2.1 r5 (provides the JoystickMessageRead() function) on my laptop. When I compile and run the following example program provided in Help on an NXT running the v. 1.31 enhanced firmware provided in the test release package:

Code: Select all

/*
struct JoystickMessageType {
  byte JoystickDir;
  byte LeftMotor;
  byte RightMotor;
  byte BothMotors;
  char LeftSpeed;
  char RightSpeed;
  unsigned long Buttons;
};
*/
task main()
{
JoystickMessageType jmt;
while (true)
{
  char result = JoystickMessageRead(MAILBOX1, jmt);
  if (result == NO_ERR)
  {
    NumOut(0, LCD_LINE1, jmt.JoystickDir);
    NumOut(0, LCD_LINE2, jmt.LeftMotor);
    NumOut(0, LCD_LINE3, jmt.RightMotor);
    NumOut(0, LCD_LINE4, jmt.BothMotors);
    ClearLine(LCD_LINE5);
    NumOut(0, LCD_LINE5, jmt.LeftSpeed);
    ClearLine(LCD_LINE6);
    NumOut(0, LCD_LINE6, jmt.RightSpeed);
    ClearLine(LCD_LINE7);
    NumOut(0, LCD_LINE7, jmt.Buttons);
  }
  else
    NumOut(0, LCD_LINE8, result);
    Wait(MS_100);
  }
}
I see only "64" on the last line of the NXT display regardless of what buttons or controls are activated on the attached Logictech Gamepad (have tried separate tests with the NXT connected via Bluetooth and then directly attached via USB with same results.) I've confirmed that the Gamepad is functional and can use it successfully to control the NXT using the Joystick tool accessible from the Tools menu of BricxCC. I'm clearly doing something wrong. Can someone help me understand what it is? Hopefully, it's a simple oversight. :-)

Many thanks to the community!

-Chad
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Using a joystick with NXC

Post by afanofosc »

I blame my documentation team (i.e., me). You need to tell the BricxCC Joystick tool window to operate in the correct mode in order for this code to work. The default mode just sends direct commands to control the motors.

https://www.dropbox.com/s/g824kc9zeijej9p/joystick.png

Double click the joystick tool window background to bring up this configuration dialog.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
schedman
Posts: 3
Joined: 27 Oct 2011, 22:06

Re: Using a joystick with NXC

Post by schedman »

Great! It works just like you said. Thanks.

A few more questions if you don't mind:

What is returned in the struct JoystickMessageType Buttons field?

I'd like to be able to read the raw values of the two Gamepad joysticks. Is there a way to do that?

Is there a way to read the D-Pad (the disk in the upper left corner of the controller)?

Is there a way to more generally read *any* of the buttons/controls from the Gamepad rather than be tied to motor power, etc. as in the struct JoystickMessageType?

Many thanks for the work you have and continue to put into NBC/NXC and BricxCC!

-Chad
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Using a joystick with NXC

Post by mattallen37 »

I would also like support for the entire controller, be it a game pad, driving wheel, 2 axis 2 button joystick, or 9 axis 32 button POV joystick. Is there a way that the user can access either the PC representation of the data, or the actual RAW USB HID info?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Using a joystick with NXC

Post by afanofosc »

Here are some constants that I will be adding to NBCCommon.h for testing which buttons are pressed:

Code: Select all

#define JOY_BUTTON1  0x00000001
#define JOY_BUTTON2  0x00000002
#define JOY_BUTTON3  0x00000004
#define JOY_BUTTON4  0x00000008
#define JOY_BUTTON5  0x00000010
#define JOY_BUTTON6  0x00000020
#define JOY_BUTTON7  0x00000040
#define JOY_BUTTON8  0x00000080
#define JOY_BUTTON9  0x00000100
#define JOY_BUTTON10 0x00000200
#define JOY_BUTTON11 0x00000400
#define JOY_BUTTON12 0x00000800
#define JOY_BUTTON13 0x00001000
#define JOY_BUTTON14 0x00002000
#define JOY_BUTTON15 0x00004000
#define JOY_BUTTON16 0x00008000
#define JOY_BUTTON17 0x00010000
#define JOY_BUTTON18 0x00020000
#define JOY_BUTTON19 0x00040000
#define JOY_BUTTON20 0x00080000
#define JOY_BUTTON21 0x00100000
#define JOY_BUTTON22 0x00200000
#define JOY_BUTTON23 0x00400000
#define JOY_BUTTON24 0x00800000
#define JOY_BUTTON25 0x01000000
#define JOY_BUTTON26 0x02000000
#define JOY_BUTTON27 0x04000000
#define JOY_BUTTON28 0x08000000
#define JOY_BUTTON29 0x10000000
#define JOY_BUTTON30 0x20000000
#define JOY_BUTTON31 0x40000000
#define JOY_BUTTON32 0x80000000
Basically, if bit 0 through 31 is set then you know that button 1 through 32 (respectively) is pressed. I am working on adding an "Ex" joystick message type which will send the raw joystick values that can be obtained via the joyinfoex structure (http://msdn.microsoft.com/en-us/library ... s.85).aspx)

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: Using a joystick with NXC

Post by mattallen37 »

:D :D :D :D :D
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Using a joystick with NXC

Post by afanofosc »

I uploaded a new test release yesterday which includes a raw joystick value capability from the BricxCC Joystick tool window.

http://bricxcc.sourceforge.net/nbc/nxcd ... ample.html

http://bricxcc.sourceforge.net/nbc/nxcd ... tants.html

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
schedman
Posts: 3
Joined: 27 Oct 2011, 22:06

Re: Using a joystick with NXC

Post by schedman »

Very cool. :D

I attached a Logitech Gampad F310 and ran some tests. I'm trying to figure out how to interpret the values returned in the *pos fields of JoystickExMessageType:

Code: Select all

struct JoystickExMessageType {
  unsigned long Xpos; // The x position
  unsigned long Ypos; // The y position
  unsigned long Zpos; // The z position
  unsigned long Rpos; // The rudder/4th axis position
  unsigned long Upos; // The 5th axis position
  unsigned long Vpos; // The 6th axis position
  unsigned long Buttons; // The button states
  unsigned long ButtonNumber; // The current button number pressed
  unsigned long POV; // The point of view state
};
The two joystick positions are returned in Xpos,Ypos (for left one) and Zpos,Rpos (for right one)--not sure about what activates Upos and Vpos. I'm also not getting the expected button number when reading ButtonNumber. Maybe the buttons on my gamepad are different than the ones being returned?

I think I need to do some more reading of the link you referenced earlier:

http://msdn.microsoft.com/en-us/library ... 85%29.aspx

Thanks for adding this to NXC!

-Chad
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 2 guests