Go to Post FIRST isn't for wimps. It never has been. - JaneYoung [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 31-08-2012, 19:27
sergioCorral842's Avatar
sergioCorral842 sergioCorral842 is offline
College Student
FRC #0842 (Falcon Robotics)
Team Role: Alumni
 
Join Date: Aug 2011
Rookie Year: 2012
Location: Phoenix, Arizona
Posts: 161
sergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to behold
Reading multiple joysticks on C# using SlimDX.

Hello,

I am currently working on a scouting system for the upcoming FRC Season and I am having trouble getting more than one joystick to be read. The program receives data from the joysticks (like how many balls were attempted and made) then saves it to notepad. From notepad it can be read using Excel. The program works fine with one controller. Is there a way to do the same thing but with 6 different USB connected joysticks?
  #2   Spotlight this post!  
Unread 01-09-2012, 01:39
EHaskins EHaskins is offline
Needs to change his user title.
AKA: Eric Haskins
no team (CARD #6 (SCOE))
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Elkhorn, WI USA
Posts: 998
EHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond repute
Send a message via MSN to EHaskins
Re: Reading multiple joysticks on C# using SlimDX.

I'd be willing to look at your code. If you don't want to post it publicly, PM me.

This should give you a starting place.

Code:
        public static Joystick[] GetSticks()
        {
            var sticks = new List<SlimDX.DirectInput.Joystick>();
            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // create the device
                try
                {
                    var stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-1000, 1000);
                    }

                    sticks.Add(stick);

                    Console.WriteLine(stick.Information.InstanceName);
                }
                catch (DirectInputException)
                {
                }
            }
            return sticks.ToArray();
        }
__________________
Eric Haskins KC9JVH

Last edited by EHaskins : 01-09-2012 at 01:42. Reason: Missed a bracket
  #3   Spotlight this post!  
Unread 05-09-2012, 19:05
sergioCorral842's Avatar
sergioCorral842 sergioCorral842 is offline
College Student
FRC #0842 (Falcon Robotics)
Team Role: Alumni
 
Join Date: Aug 2011
Rookie Year: 2012
Location: Phoenix, Arizona
Posts: 161
sergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to behold
Re: Reading multiple joysticks on C# using SlimDX.

I will use that for the starting point thank you and here is the code if anyone is interested.

Code:
public Form1()
        {
            InitializeComponent();
            BootGamePort();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            BootGamePort();

        }

Joystick mainScouter; 
        JoystickState state = new JoystickState();

        void BootGamePort()
        {
            DirectInput dinput = new DirectInput();

            foreach (DeviceInstance device in dinput.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                try
                {
                    if (count == 0)
                    {
                        mainScouter = new Joystick(dinput, device.InstanceGuid);
                    }

                    count = 1;
                    break;
                }

                catch (DirectInputException)
                {
                }

            }
            if (mainScouter == null)
            {
                return;
            }
            

            

            foreach (DeviceObjectInstance deviceObject in mainScouter.GetObjects())
            {
                if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                    mainScouter.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
            }

            

            mainScouter.Acquire();
        }

        public void timer1_Tick(object sender, EventArgs e)
        {

            if (mainScouter.Acquire().IsFailure)
            {
                return;
            }

            if (mainScouter.Poll().IsFailure)
            {
                return;
            }

           

            state = mainScouter.GetCurrentState();
           

            if (Result.Last.IsFailure)
            {
                return;
            }

            bool[] buttons = state.GetButtons();
           


            String strText = "";
            

            for (int b = 0; b < buttons.Length; b++)
            {
                if (buttons[b])
                {
                    strText += b.ToString("00 ", CultureInfo.CurrentCulture);
                    lblbuttons.Text = strText;
                }

               
            }

Last edited by sergioCorral842 : 06-09-2012 at 10:44.
  #4   Spotlight this post!  
Unread 11-09-2012, 19:08
sergioCorral842's Avatar
sergioCorral842 sergioCorral842 is offline
College Student
FRC #0842 (Falcon Robotics)
Team Role: Alumni
 
Join Date: Aug 2011
Rookie Year: 2012
Location: Phoenix, Arizona
Posts: 161
sergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to behold
Re: Reading multiple joysticks on C# using SlimDX.

I tested the code that EHaskins provided me and it can read every USB gamepad. I developed code so it can show which buttons are being pressed, but it only works if each gamepad is under a timer. The timer is used to refresh the labels which each button is assigned to change. The problem that I get in the code is in this line:
Code:
SlimDX.DirectInput.Joystick stick = new SlimDX.DirectInput.Joystick(Input. device.InstanceGuild);
I declared this line at the beginning of the code and it gives me an error with "device" and "Input". The errors read: “A field initializer cannot the reference the non-static field, method, or property” for both. So my question is why isn’t Input and device being referenced?
  #5   Spotlight this post!  
Unread 11-09-2012, 19:53
EHaskins EHaskins is offline
Needs to change his user title.
AKA: Eric Haskins
no team (CARD #6 (SCOE))
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Elkhorn, WI USA
Posts: 998
EHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond repute
Send a message via MSN to EHaskins
Re: Reading multiple joysticks on C# using SlimDX.

First, shouldn't that be a comma after Input you the code you posted. If that's just a typo when copying to CD, there's probably a problem on the line before or after it.

Second, you should do that with a single timer. Something like:

Code:
Joystick[] sticks;
void init() {//pageload, or new, or something similar
  Sticks = GetSticks();//using my code from previous post
}
void TimerHandler (...){
  for (int i = 0; i < sticks.Length; i++){
    StickHandlingLogic(sticks[i], i); //replace the identifier with something meaningful to your application.
  }
}
void StickhandlingLogic(Joystick stick, int id) { //pass the stick and some type of identifier, since you want to know which users stick this is
  //Process the joystick data here
}
__________________
Eric Haskins KC9JVH
  #6   Spotlight this post!  
Unread 16-09-2012, 17:29
sergioCorral842's Avatar
sergioCorral842 sergioCorral842 is offline
College Student
FRC #0842 (Falcon Robotics)
Team Role: Alumni
 
Join Date: Aug 2011
Rookie Year: 2012
Location: Phoenix, Arizona
Posts: 161
sergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to beholdsergioCorral842 is a splendid one to behold
Re: Reading multiple joysticks on C# using SlimDX.

Thanks for the help EHaskins. Now I am able to use all 6 gamepads that I need. Right now, whenever I press a certain button a counter on an integer goes up by one and is displayed on a label like this:
Code:
if (buttons[0])
{
    basketsAttempted++;
}
The problem I am facing now is that I want to be able to decrease the value of the integer by one when i push the left bumper and a certain button like this:
Code:
if (buttons[4] & buttons[0])
{
    basketsAttempted--;
}
If i try to use the same button and left bumper to decrease the value, it will not decrease. It only decreases if you use the left bumper and any other button but the one you used to increase it. Is there a way to use the same button and left bumper to decrease the value?
  #7   Spotlight this post!  
Unread 16-09-2012, 17:39
EHaskins EHaskins is offline
Needs to change his user title.
AKA: Eric Haskins
no team (CARD #6 (SCOE))
Team Role: College Student
 
Join Date: Jan 2006
Rookie Year: 2006
Location: Elkhorn, WI USA
Posts: 998
EHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond reputeEHaskins has a reputation beyond repute
Send a message via MSN to EHaskins
Re: Reading multiple joysticks on C# using SlimDX.

Try this. Either one is functionally the same, but I think the second may be more concise.

Code:
if (buttons[4] && buttons[0])
{
    basketsAttempted--;
}
else if (buttons[0])
{
    basketsAttempted++;
}

OR

if (buttons[0]){
  if (buttons[4){
    basketsAttempted--;
  }
  else{
    basketsAttempted++;
  }
}
BTW, what was happening when you used the same button for both the add and subtract operation is that both were testing true, and canceling each other out. Effectively you were doing: x = x + 1 - 1;
__________________
Eric Haskins KC9JVH
  #8   Spotlight this post!  
Unread 12-12-2012, 10:47
gauravchhabra gauravchhabra is offline
Junior Member
no team
 
Join Date: Dec 2012
Location: India
Posts: 2
gauravchhabra is an unknown quantity at this point
Re: Reading multiple joysticks on C# using SlimDX.

Hey Eric Haskins,

i am trying to program the dance pad programmed for a Dancing Revolution Game, could any one tell how i can take the input from their button..?? because i tried using the serial port input but it didn't worked out.

even i used this code but its not returning the data.

JoystickState state = new JoystickState();

// JoystickState state = joystick.CurrentJoystickState; ( If i use this and delete the above command then i get a error for Object reference not set to an instance of an object. ( NULL Reference Exception )


//Capture Buttons.
byte[] buttons = state.GetButtons();
for (int i = 0; i < buttons.Length; i++)
{
if (buttons[i] != 0)
{
info += "Button:" + i + " ";
label1.Text = info;

}
}
  #9   Spotlight this post!  
Unread 12-12-2012, 11:30
gauravchhabra gauravchhabra is offline
Junior Member
no team
 
Join Date: Dec 2012
Location: India
Posts: 2
gauravchhabra is an unknown quantity at this point
Re: Reading multiple joysticks on C# using SlimDX.

Hey Eric whats app..??
i am trying to program the dance pad programmed for a Dancing Revolution Game, could any one tell how i can take the input from their button..?? because i tried using the serial port input but it didn't worked out.

even i used this code but its not returning the data.

JoystickState state = new JoystickState();

// JoystickState state = joystick.CurrentJoystickState; ( If i use this and delete the above command then i get a error for Object reference not set to an instance of an object. ( NULL Reference Exception )


//Capture Buttons.
byte[] buttons = state.GetButtons();
for (int i = 0; i < buttons.Length; i++)
{
if (buttons[i] != 0)
{
info += "Button:" + i + " ";
label1.Text = info;

}
}
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 00:43.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi