2

I've bought a standard game controller that has vibration motors. It identifies itself as "SHANWAN Android Gamepad" but seems to be compatible to others as it works well on Linux out of the box.

I found a script that enables me to read data from the game controller device path at https://gist.github.com/rdb/8864666 . I am now wondering: How can I control the vibration motors? I could find only XBox API code samples which are not helpful for obvious reasons. Has anyone an idea about how to controle the vibration motors?

Regis May
  • 3,070
  • 2
  • 30
  • 51

1 Answers1

2

The script you linked only looks for joydev devices. On Linux, joydev is the older gamepad interface and doesn't support vibration. The path to the joydev device node is probably something like /dev/input/js0.

The newer evdev-based interface supports force feedback. Check /proc/bus/input/devices to find the evdev node that corresponds to the joydev node. As an example, I have a wired Xbox 360 gamepad connected and get the following output:

I: Bus=0003 Vendor=045e Product=028e Version=0110
N: Name="Microsoft X-Box 360 pad"
P: Phys=usb-0000:00:14.0-1.1/input0
S: Sysfs=/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1.1/2-1.1:1.0/input/input27
U: Uniq=
H: Handlers=event16 js0 
B: PROP=0
B: EV=20000b
B: KEY=7cdb000000000000 0 0 0 0
B: ABS=3003f
B: FF=107030000 0

The line starting with "H:" indicates that /dev/input/js0 and /dev/input/event16 both refer to the Xbox gamepad. /dev/input/event16 is the evdev node.

To test vibration on the device, use the fftest command with the path to the evdev node. If it supports vibration it should give an OK result for one or more of the effects it tries to upload to the device:

$ fftest /dev/input/event16
Force feedback test program.
HOLD FIRMLY YOUR WHEEL OR JOYSTICK TO PREVENT DAMAGES

Device /dev/input/event16 opened
Features:
  * Absolute axes: X, Y, Z, RX, RY, RZ, Hat 0 X, Hat 0 Y, 
    [3F 00 03 00 00 00 00 00 ]
  * Relative axes: 
    [00 00 ]
  * Force feedback effects types: Periodic, Rumble, Gain, 
    Force feedback periodic effects: Square, Triangle, Sine, 
    [00 00 00 00 00 00 00 00 00 00 03 07 01 00 00 00 ]
  * Number of simultaneous effects: 16

Setting master gain to 75% ... OK
Uploading effect #0 (Periodic sinusoidal) ... OK (id 0)
Uploading effect #1 (Constant) ... Error: Invalid argument
Uploading effect #2 (Spring) ... Error: Invalid argument
Uploading effect #3 (Damper) ... Error: Invalid argument
Uploading effect #4 (Strong rumble, with heavy motor) ... OK (id 1)
Uploading effect #5 (Weak rumble, with light motor) ... OK (id 2)
Enter effect number, -1 to exit

If you get a Permission Denied error, it may mean the device does not have a compatible driver installed or the driver doesn't support force feedback. You shouldn't need to run this command as root to test force feedback.

I'm not familiar with the SHANWAN Android Gamepad. I have one SHANWAN device that works on Linux, is it the same?

https://www.amazon.com/Gaming-Controller-Gamepad-Windows-Android/dp/B00OAYHIRA

IIRC, the vibration features require that the device is in XInput mode which allows it to work with the xpad kernel driver. If your gamepad has a similar mode-switching feature, try the XInput mode.

Once you've verified that the force feedback features are working for your device, you should be able to send FF commands with python-evdev:

http://python-evdev.readthedocs.io/en/latest/

nondebug
  • 761
  • 3
  • 5
  • It seems your game controller is quite similar to mine. The color is different, the design is very similar. Thank you for all this information! Great! Btw: There is no mode switch on my device. fftest had no effect: vibration motors are not getting active so the output of fftest is very very similar to yours. Next weekend I'll try to experiment a bit more. – Regis May Apr 24 '18 at 14:35
  • If that one isn't working I'd recommend getting one you know will work. The one I linked works wonderfully in Linux because it misbehaves and pretends to be a genuine Xbox 360 wired gamepad (vendor:045e, product:028e). Because of how Linux detects gamepads, the exact vendor/product pair needs to be in the driver's whitelist in order to work correctly. If you're determined to get yours working, check out the xboxdrv userland driver. BTW, the mode switch on mine is activated by holding the "V+" button in the middle for a few seconds, after which it changes the color of the center LED. – nondebug Apr 25 '18 at 16:04
  • I have a "home" button instead of a "v+" button on my controller. Nevertheless it's advertised very similar: As an Android-Windows-Game-Pad. Thank you for that information, I'll try to continue on that project soon. – Regis May Apr 26 '18 at 09:09
  • I have the Xbox 360 wired to a Rpi 4B and I'm trying to control the motors. I don't really understand what do I have to do to control it in python. I did succed to make it vibrate in the console, but how can I do it in a script? – Cătălina Sîrbu Apr 20 '20 at 21:20
  • @Cătălina Sîrbu Try python-evdev – nondebug Apr 21 '20 at 23:58
  • Thank you, I succeded! – Cătălina Sîrbu Apr 22 '20 at 18:22