Create my own game controller

Comments & issues
Post Reply
Tchooby
Fresh meat
Posts: 4
Joined: Thu Jan 17, 2019 2:28 pm
Contact:

Create my own game controller

Post by Tchooby »

Hello, and sorry for my bad english, i'm a french guy.
Here's my project :
I'm quadriplegic and i used to play video games using Razer Hydra Joystick with my mouth, emulating keyboard with software Joytokey. Problem, their joystick are no longer producted and very expensive when they are sold by private person.
So, i took decision to try to create my own game controller with Phidgets product.
I bought Thumbstick Phidget, Touch Keypad Phidget and of course VINT Hub Phidget.
All of this are well detected by my PC, but now i'm kind of stuck.
I'm a really newbie in programming and i don't know where to start, i downloaded the voltageinput example trying to understand basics,but i don't even know which file open first.
Can you guys give me some advice on what learn first etc please?
Thanks by advance!!
jdecoux
Labview Developer
Posts: 161
Joined: Mon Nov 13, 2017 10:20 am
Contact:

Re: Create my own game controller

Post by jdecoux »

If you are new to programming in general, I would recommend starting with Python, as it's a good language that is easier to get running than other options.

If you are using Python, you can follow the instructions on our page about using Phidgets in Python with IDLE to get started. IDLE is the development environment that comes packaged with any Python installation, and is probably the easiest one to get started with.

If you prefer a different programming language, we provide a similar page for a number of other programming languages and development environments, that you can select from here.
Tchooby
Fresh meat
Posts: 4
Joined: Thu Jan 17, 2019 2:28 pm
Contact:

Re: Create my own game controller

Post by Tchooby »

Hello, thanks for the advice dude, so i started learn Python, but now i have some questions :D

Here is my first success :

Code: Select all

    
def onAttachHandler(self):
    e = self
    print("Phidget attached!") 
    # Get device informations and display it
    serialNumber = e.getDeviceSerialNumber()
    hubPort = e.getHubPort()
    channel = e.getChannel()
    deviceclass = e.getDeviceClassName()
    channelClassName = e.getChannelClassName()
    print("\nSerial number : " + str(serialNumber) + "\nHubPort : " + str(hubPort) 
    + "\nChannel : " + str(channel) + "\nDeviceClass : " + deviceclass + "\nChannelClass : " + channelClassName )
    # Set datainterval and voltageratiochangetrigger
    e.setDataInterval(20)
    print("\nSetting DataInterval to 20ms")
    e.setVoltageRatioChangeTrigger(0.1)
    print("Setting Voltage Ratio ChangeTrigger to 0.1")
    # Set the sensortype
    if(e.getChannelSubclass() == ChannelSubclass.PHIDCHSUBCLASS_VOLTAGERATIOINPUT_SENSOR_PORT):
        print("\tSetting VoltageRatio SensorType")
        e.setSensorType(VoltageRatioSensorType.SENSOR_TYPE_VOLTAGERATIO)
 
    
def onVoltageRatioChangeHandler(self, voltageRatio):
    print("Voltage Ratio: " + str(voltageRatio))
    keyboard = PyKeyboard()
    if voltageRatio > 0.2:
        keyboard.press_key('a')
    elif 0 < voltageRatio < 0.2:
        keyboard.release_key('a')
            
def main():
    try:
        vi = VoltageRatioInput()
    except PhidgetException as e:
        sys.stderr.write("Runtime Error -> Creating VoltageRatioInput: \n\t")
        DisplayError(e)
        raise
    except RuntimeError as e:
        sys.stderr.write("Runtime Error -> Creating VoltageRatioInput: \n\t" + e)
        raise
    
    vi.setDeviceSerialNumber(539622)
    vi.setHubPort(0)
    vi.setIsHubPortDevice(0)
    vi.setChannel(1)
    
    vi.setOnAttachHandler(onAttachHandler)
        
    print("\nSetting OnVoltageRatioChangeHandler...")
    vi.setOnVoltageRatioChangeHandler(onVoltageRatioChangeHandler)
               
    try:
        vi.openWaitForAttachment(5000)
    except PhidgetException as e:
        PrintOpenErrorMessage(e, vi)
        raise EndProgramSignal("Program Terminated: Open Failed")
    print("Sampling data for 10 seconds...")
        
    print("You can do  tuff with your Phidgets here and/or in the event handlers.")
               
    time.sleep(10)
     
main()   
    
Basically it print voltage ratio and press 'a' if > 0.2V. It work for one axe of my thumbstick.
Now i would like to make it work with the two axes simultaneously but i'm kinda stuck. I guess i have to make another main function, changing channel adress of device parameters, but how to start two function main?
The answer is the "thread" thing in think but i can't make it work without multiple errors...
Thanks for your help !
jdecoux
Labview Developer
Posts: 161
Joined: Mon Nov 13, 2017 10:20 am
Contact:

Re: Create my own game controller

Post by jdecoux »

Glad to hear you're getting it working!

In fact, you only need one main. You just need to create another variable for the other axis, which you can handle in much the same way as the first axis. And you can name them whatever you want.

Code: Select all

def main():
    try:
        vi1 = VoltageRatioInput()
        anotherAxis = VoltageRatioInput()
       
        vi1.setChannel(0)
        anotherAxis.setChannel(1)
    	
        #etc.
Tchooby
Fresh meat
Posts: 4
Joined: Thu Jan 17, 2019 2:28 pm
Contact:

Re: Create my own game controller

Post by Tchooby »

Ah exact!
I did it, it work but not very well :
My first VoltageRatioChangeHandler work perfectly but not the second one (i create two different def to assign a different key ofc) it's like there's some "lag" i don't know how to explain.. it act like the changetrigger is not working well.
Is there any way to "optimize" code ?
I tried to create two attachhandler but it change nothing.
jdecoux
Labview Developer
Posts: 161
Joined: Mon Nov 13, 2017 10:20 am
Contact:

Re: Create my own game controller

Post by jdecoux »

For the fastest, most consistent response, you'll want to set ChangeTrigger to 0.

ChangeTrigger limits the amount of data from the device by only reporting sensor data (in this case the joystick position) when it changes by the given amount from the last reported data.

For example, if your change trigger is 0.2, and the last reported measurement was 0.15, the joystick will have to move past either 0.35 or -0.05 to get the next data point.
Tchooby
Fresh meat
Posts: 4
Joined: Thu Jan 17, 2019 2:28 pm
Contact:

Re: Create my own game controller

Post by Tchooby »

It work perfectly, thanks dude!
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests