Go to Post Hmmm... I'm pretty sure that twig isn't legal, since the tree it came from started fabrication before kickoff.... - lukevanoort [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 09-07-2013, 15:06
Hekaitomix Hekaitomix is offline
Registered User
no team
 
Join Date: Jul 2013
Location: italy
Posts: 5
Hekaitomix is an unknown quantity at this point
Reading one Joystick (PS1/PS2) using slimDX and C#

Hello everybody. I've a problem. I've connected to the computer a joystick (PS1) to an adapter called "Konig electronic" which is connected to the pc through a usb cable. I want to control it with a c# program. I've used slimDX dll but it doesn't work. When i use a PSX simulation software the joystick works, but when i try to control it with a c# code it doesn't work. please help me.

I want to get the pressed buttons, from PS1 joystick, and display it into label2.

PS: Sorry for my bad english but i'm italian!

My C# project is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SlimDX.DirectInput;

namespace JoyStick
{
public partial class Form1 : Form
{
List<DeviceInstance> directInputList = new List<DeviceInstance>();
DirectInput directInput = new DirectInput();
SlimDX.DirectInput.Joystick gamepad;
SlimDX.DirectInput.JoystickState state;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
directInputList.AddRange(directInput.GetDevices(De viceClass.GameController, DeviceEnumerationFlags.AttachedOnly));
gamepad = new SlimDX.DirectInput.Joystick(directInput, directInputList[0].InstanceGuid);
}

private void button1_Click(object sender, EventArgs e)
{

timer1.Start();
}

private void button2_Click(object sender, EventArgs e)
{
//gamepad.Unacquire();
timer1.Stop();
}

private void timer1_Tick(object sender, EventArgs e)
{
if (gamepad.Acquire().IsFailure)
return;


if (gamepad.Poll().IsFailure)
return;


if (SlimDX.Result.Last.IsFailure)
return;

state = gamepad.GetCurrentState();

bool[] buttons = state.GetButtons();

for (int i = 0; i < buttons.Length; i++)
if (buttons[i])
label2.Text = i.ToString();

lbl_np.Text = Convert.ToString(buttons.Length);

gamepad.Unacquire();
}


}
}

Last edited by Hekaitomix : 09-07-2013 at 15:09.
  #2   Spotlight this post!  
Unread 09-07-2013, 22:54
RyanCahoon's Avatar
RyanCahoon RyanCahoon is offline
Disassembling my prior presumptions
FRC #0766 (M-A Bears)
Team Role: Engineer
 
Join Date: Dec 2007
Rookie Year: 2007
Location: Mountain View
Posts: 689
RyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond reputeRyanCahoon has a reputation beyond repute
Re: Reading one Joystick (PS1/PS2) using slimDX and C#

Welcome to the Chief Delphi forum!
This is a community dedicated to various robotics competitions for high school students and their adult mentors.

The community here doesn't use slimDX, in general, though you may find somebody who uses it for their day job or for side projects. You'd probably have better luck posting on a forum with an active slimDX community:

slimdx-devel mailing list: https://groups.google.com/forum/#!forum/slimdx-devel
Stack Overflow: http://stackoverflow.com/questions/tagged/slimdx

Since slimDX is a managed wrapper around DirectX, you may find some help on the DirectX forums as well: http://xboxforums.create.msdn.com/fo...aspx?GroupID=7

Also, if you suspect that it's a device problem, you may want to contact the manufacturer: http://www.konigelectronic.com/en_us/service-support
__________________
FRC 2046, 2007-2008, Student member
FRC 1708, 2009-2012, College mentor; 2013-2014, Mentor
FRC 766, 2015-, Mentor
  #3   Spotlight this post!  
Unread 10-07-2013, 03:30
Rangel's Avatar
Rangel Rangel is online now
John Rangel
FRC #0842 (Falcon Robotics)
Team Role: Mentor
 
Join Date: Jan 2010
Rookie Year: 2009
Location: Phoenix, AZ
Posts: 745
Rangel has a reputation beyond reputeRangel has a reputation beyond reputeRangel has a reputation beyond reputeRangel has a reputation beyond reputeRangel has a reputation beyond reputeRangel has a reputation beyond reputeRangel has a reputation beyond reputeRangel has a reputation beyond reputeRangel has a reputation beyond reputeRangel has a reputation beyond reputeRangel has a reputation beyond repute
Re: Reading one Joystick (PS1/PS2) using slimDX and C#

Here is a similar thread for more insight on using slimDX:
http://www.chiefdelphi.com/forums/sh...d.php?t=108111

I'll get our programmer to take a look at your problem and see if he comes up with anything.
__________________
2012 Dean's List Winner
2011-2014 Arizona Regional Winners
2016 Las Vegas Regional Winner
2014-? Mentor


  #4   Spotlight this post!  
Unread 10-07-2013, 11:13
Hekaitomix Hekaitomix is offline
Registered User
no team
 
Join Date: Jul 2013
Location: italy
Posts: 5
Hekaitomix is an unknown quantity at this point
Re: Reading one Joystick (PS1/PS2) using slimDX and C#

thank you anyway!!
  #5   Spotlight this post!  
Unread 10-07-2013, 18:54
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 one Joystick (PS1/PS2) using slimDX and C#

Hello,
I used Slim DX and C# to create a scouting system that used ps3 controllers to change several different numbers. Here is the part of my code that gets all the usb joysticks connected to the computer.

Code:
namespace FalconScoutingSoftware
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            GetSticks();
            Sticks = GetSticks();
            stick1 = stick;
            timer1.Enabled = true;
        }

        DirectInput Input = new DirectInput();
        SlimDX.DirectInput.Joystick stick;
        Joystick[] Sticks;
        Joystick stick1;
   
        //Thumstick variables.
        int yValue = 0;
        int xValue = 0;
        int zValue = 0;
        int rotationZValue = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            Joystick[] joystick = GetSticks();
        }

        public Joystick[] GetSticks()
        {

            List<SlimDX.DirectInput.Joystick> sticks = new List<SlimDX.DirectInput.Joystick>(); // Creates the list of joysticks connected to the computer via USB.
            foreach (DeviceInstance device in Input.GetDevices(DeviceClass.GameController, DeviceEnumerationFlags.AttachedOnly))
            {
                // Creates a joystick for each game device in USB Ports
                try
                {
                    stick = new SlimDX.DirectInput.Joystick(Input, device.InstanceGuid);
                    stick.Acquire();

                    // Gets the joysticks properties and sets the range for them.
                    foreach (DeviceObjectInstance deviceObject in stick.GetObjects())
                    {
                        if ((deviceObject.ObjectType & ObjectDeviceType.Axis) != 0)
                            stick.GetObjectPropertiesById((int)deviceObject.ObjectType).SetRange(-100, 100);
                    }

                    // Adds how ever many joysticks are connected to the computer into the sticks list.
                    sticks.Add(stick);
                }
                catch (DirectInputException)
                {
                }
            }
            return sticks.ToArray();
        }

        //Creates the StickHandlingLogic Method which takes all the joysticks in the sticks List and puts them into a timer.
        public void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < Sticks.Length; i++)
            {
                StickHandlingLogic(Sticks[i], i);
            }
        }
   }
}
This will be the method that is called by the timer every time it ticks:
Code:
        void StickHandlingLogic(Joystick stick, int id)
        {
            // Creates an object from the class JoystickState.
            JoystickState state = new JoystickState();

            state = stick.GetCurrentState(); //Gets the state of the joystick
            
            //These are for the thumbstick readings
            yValue = -state.Y;
            xValue = state.X;
            zValue = state.Z;
            rotationZValue = -state.RotationZ;

            bool[] buttons = state.GetButtons(); // Stores the number of each button on the gamepad into the bool[] butons.
            //Here is an example on how to use this for the joystick in the first index of the array list
            if(id == 0)
            {
                  // This is when button 0 of the gamepad is pressed, the label will change. Button 0 should be the square button.
                  if(buttons[0])
                  {
                        label1.Text = "Worked";
                  }
                  
            }
       }
__________________
Team 842 Falcon Robotics: 2011-2015 (President, Head Programmer, Drive Team)
Arizona Regional Winners: 2012-2014 (Thanks 610, 1212, 1726, 3944, 2486, 2403)
Curie Division Semi-Finalists: 2014 (Thanks 1311, 2013, 2928)
Las Vegas Semi-Finalists: 2015 (Thanks 3495, 4415)
Curie Division Quarter-Finalists: 2015 (Thanks 107, 228, 649)

2015 - 2016: Programming, Drive Team, and Strategy Mentor for Team 3187, The Titans

Studying Computer Science- Software Engineering at ASU (Class of 2019)
  #6   Spotlight this post!  
Unread 10-07-2013, 20:45
Hekaitomix Hekaitomix is offline
Registered User
no team
 
Join Date: Jul 2013
Location: italy
Posts: 5
Hekaitomix is an unknown quantity at this point
Re: Reading one Joystick (PS1/PS2) using slimDX and C#

Thank you! but it doesn't work! I've tryed also with:

for(int i = 0; i < buttons.length; i++)
if(buttons[i])
label2.Text = "pressed";

... on every id ...

but nothing... it's like the program can't read data from joystick.. :/
  #7   Spotlight this post!  
Unread 10-07-2013, 21:09
Hekaitomix Hekaitomix is offline
Registered User
no team
 
Join Date: Jul 2013
Location: italy
Posts: 5
Hekaitomix is an unknown quantity at this point
Re: Reading one Joystick (PS1/PS2) using slimDX and C#

oooh yeeeesss!!

I've solved the problem!! i've connected the adapter to the pc, debugged the program and after i've connected the joystick to the adapter.. and it works!

now, i've a long programming night thank you!
  #8   Spotlight this post!  
Unread 10-07-2013, 21:13
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 one Joystick (PS1/PS2) using slimDX and C#

Quote:
Originally Posted by Hekaitomix View Post
oooh yeeeesss!!

I've solved the problem!! i've connected the adapter to the pc, debugged the program and after i've connected the joystick to the adapter.. and it works!

now, i've a long programming night thank you!
Glad you figured it out. If you run into any issues on it feel free to pm me.
__________________
Team 842 Falcon Robotics: 2011-2015 (President, Head Programmer, Drive Team)
Arizona Regional Winners: 2012-2014 (Thanks 610, 1212, 1726, 3944, 2486, 2403)
Curie Division Semi-Finalists: 2014 (Thanks 1311, 2013, 2928)
Las Vegas Semi-Finalists: 2015 (Thanks 3495, 4415)
Curie Division Quarter-Finalists: 2015 (Thanks 107, 228, 649)

2015 - 2016: Programming, Drive Team, and Strategy Mentor for Team 3187, The Titans

Studying Computer Science- Software Engineering at ASU (Class of 2019)
  #9   Spotlight this post!  
Unread 11-07-2013, 08:54
Hekaitomix Hekaitomix is offline
Registered User
no team
 
Join Date: Jul 2013
Location: italy
Posts: 5
Hekaitomix is an unknown quantity at this point
Re: Reading one Joystick (PS1/PS2) using slimDX and C#

Quote:
Originally Posted by sergioCorral842 View Post
Glad you figured it out. If you run into any issues on it feel free to pm me.
count on it!
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 03:24.

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