V-REP for Windows – Integrate Xbox 360 Controller input via plugin

Hi all!

I’ve always wanted to learn some robotics. Finally, in 2014 I spent four months browsing tons of Arduino forums and tutorials, learning lots of things.
However, each time I was about to buy some generic starter kit, I started wondering if it was the right thing to do: in some way I was worried to lose drive and motivation buying something “physical” without having a well defined project to build, so I was kind of stuck.
Fortunatelly, few months ago I discovered V-REP, a really well documented, intuitive and stable robot simulator. These qualities makes it a perfect match for a beginner like me! Without a clear “big” idea to implement and free from every hardware costs, I could begin learning some basic robot programming techniques with a powerful visual feedback.

I started studying the embedded LUA scripting system when I looked at the Xbox 360 Controller sitting on my desktop and…
idea

In this post I’ll share a simple and efficient way to integrate Xbox 360 Controller input in V-REP for Windows.

After reading the fine written Katy’s tutorial “XInput Tutorial Part 1: Adding gamepad support to your Windows game”, I decided to write a V-REP plugin.
Following the official “Plugin tutorial” I implemented a DLL to extend the internal V-REP LUA scripting with the following functions:

boolean isConnected = simExtXbox360Controller_isConnected()

number port = simExtXbox360Controller_getPort()
boolean refreshState = simExtXbox360Controller_refreshState()

boolean isStartPressed = simExtXbox360Controller_isStartPressed()
boolean isBackPressed = simExtXbox360Controller_isBackPressed()

boolean isAPressed = simExtXbox360Controller_isAPressed()
boolean isBPressed = simExtXbox360Controller_isBPressed()
boolean isXPressed = simExtXbox360Controller_isXPressed()
boolean isYPressed = simExtXbox360Controller_isYPressed()

boolean isDPadLeftPressed = simExtXbox360Controller_isDPadLeftPressed()
boolean isDPadRightPressed = simExtXbox360Controller_isDPadRightPressed()
boolean isDPadUpPressed = simExtXbox360Controller_isDPadUpPressed()
boolean isDPadDownPressed = simExtXbox360Controller_isDPadDownPressed()

boolean isLeftShoulderPressed = simExtXbox360Controller_isLeftShoulderPressed()
boolean isRightShoulderPressed = simExtXbox360Controller_isRightShoulderPressed()

boolean isLeftThumbStickPressed = simExtXbox360Controller_isLeftThumbStickPressed()
boolean isRightThumbStickPressed = simExtXbox360Controller_isRightThumbStickPressed()

-- return table_2 (x, y) . x, y in [-1, 1]
table_2 leftThumbStickCoords = simExtXbox360Controller_getLeftThumbStickCoords()
table_2 rightThumbStickCoords = simExtXbox360Controller_getRightThumbStickCoords()

-- returns float x in [0, 1]
float leftTriggerPressure = simExtXbox360Controller_getLeftTriggerPressure()
float rightTriggerPressure = simExtXbox360Controller_getRightTriggerPressure()

The logic behind those functions is to check if the required module was loaded correctly during the script initialization:

moduleName=0
moduleVersion=0
index=0
xbox360ControllerModuleNotFound=true
while moduleName do
	moduleName,moduleVersion=simGetModuleName(index)
	if (moduleName=='Xbox360Controller') then
		xbox360ControllerModuleNotFound=false
	end
	index=index+1
end
if (xbox360ControllerModuleNotFound) then
	simAddStatusbarMessage("ERROR! - Xbox360Controller plugin was not found. (v_repExtXbox360Controller.dll)&&nSimulation will not run properly.")
else
	simAddStatusbarMessage("OK! - Xbox360Controller plugin FOUND. (v_repExtXbox360Controller.dll)")
end

During the actuation phase “simExtXbox360Controller_getXXX” functions can be called, after “simExtXbox360Controller_refreshState”, to read the state of each button, trigger and stick of the Xbox 360 controller.

-- reload controller state
simExtXbox360Controller_refreshState()

-- read last loaded controller state
if (simExtXbox360Controller_isConnected()) then

	if (simExtXbox360Controller_isStartPressed()) then
		simAddStatusbarMessage("Start pressed!")
	end

	if (simExtXbox360Controller_isBackPressed()) then
		simAddStatusbarMessage("Back pressed!")
	end

	if (simExtXbox360Controller_isAPressed()) then
		simAddStatusbarMessage("A pressed!")
	end

	if (simExtXbox360Controller_isBPressed()) then
		simAddStatusbarMessage("B pressed!")
	end

	if (simExtXbox360Controller_isXPressed()) then
		simAddStatusbarMessage("X pressed!")
	end

	if (simExtXbox360Controller_isYPressed()) then
		simAddStatusbarMessage("Y pressed!")
	end

	if (simExtXbox360Controller_isDPadLeftPressed()) then
		simAddStatusbarMessage("DPad Left pressed!")
	end

	if (simExtXbox360Controller_isDPadRightPressed()) then
		simAddStatusbarMessage("DPad Right pressed!")
	end

	if (simExtXbox360Controller_isDPadUpPressed()) then
		simAddStatusbarMessage("DPad Up pressed!")
	end

	if (simExtXbox360Controller_isDPadDownPressed()) then
		simAddStatusbarMessage("DPad Down pressed!")
	end

	if (simExtXbox360Controller_isLeftShoulderPressed()) then
		simAddStatusbarMessage("Left Shoulder pressed!")
	end

	if (simExtXbox360Controller_isRightShoulderPressed()) then
		simAddStatusbarMessage("Right Shoulder pressed!")
	end

	if (simExtXbox360Controller_isLeftThumbStickPressed()) then
		simAddStatusbarMessage("Left Thumb Stick pressed!")
	end

	if (simExtXbox360Controller_isRightThumbStickPressed()) then
		simAddStatusbarMessage("Right Thumb Stick pressed!")
	end

	if (simExtXbox360Controller_isStartPressed()) then

		port = simExtXbox360Controller_getPort() -- return integer x in [1, 4]
		simAddStatusbarMessage(string.format("Port: %d", port))

		leftThumbStickCoords = simExtXbox360Controller_getLeftThumbStickCoords() -- returns table_2 (x, y) . x, y in [-1, 1]
		simAddStatusbarMessage(string.format("Left Thumb Stick Coords: (%f, %f)", leftThumbStickCoords[1], leftThumbStickCoords[2]))

		rightThumbStickCoords = simExtXbox360Controller_getRightThumbStickCoords() -- returns table_2 (x, y) . x, y in [-1, 1]
		simAddStatusbarMessage(string.format("Right Thumb Stick Coords: (%f, %f)", rightThumbStickCoords[1], rightThumbStickCoords[2]))

	end

	if (simExtXbox360Controller_isBackPressed()) then

		leftTriggerPressure = simExtXbox360Controller_getLeftTriggerPressure() -- returns float x in [0, 1]
		simAddStatusbarMessage(string.format("Left Trigger Pressure: %f", leftTriggerPressure))

		rightTriggerPressure = simExtXbox360Controller_getRightTriggerPressure() -- returns float x in [0, 1]
		simAddStatusbarMessage(string.format("Right Trigger Pressure: %f", rightTriggerPressure))

	end

else
	simAddStatusbarMessage("ERROR! - Xbox 360 Controller not connected!")
end

In the following video there’s an example of the plugin in action:

Thanks for reading! 😉

UPDATE – July 21, 2016

GitHub repositories:

 

UPDATE – January 29, 2017

Visual Studio Project with x64 compiled dll: download.

 

6 thoughts on “V-REP for Windows – Integrate Xbox 360 Controller input via plugin

  1. Very interesting post! Would you mind us having a link to your page? Also, could you provide us with your full name, so that we can correctly acknowledge your work?

    Marc (from the V-REP team)

    Like

Leave a comment