Wii Motion plus Gyros on RaspberryPi


Here’s a really quick post about the Wii Motion Plus (again !) and how to make it work with the Raspberry Pi…

I’ve connected this nice little device to an Arduino (obviously ! 🙂 ), to a IOIO board, a FEZ Domino and even to an obscure TI Arm board, the LM3S8962 using CooCox… so it was only right that I connect it to my “new” Raspberry Pi board.

Connecting the Wii Motion Plus to the Raspberry Pi board

Connecting the Wii Motion Plus to the Raspberry Pi board

Before anything else, one has to enable I2C on the Raspberry Pi. There are plenty of articles on the Internet on how to do that, I’ll just remind here the main steps:

  1. edit /etc/modprobe.d/raspi-blacklist.conf and UN-blacklist i2c-bcm2708
  2. edit /etc/modules and add i2c-dev
  3. install i2c-tools and python-smbus
  4. add you user to the i2c group
  5. connect the device and check that it’s visible with i2cdetect -y 1

And here is the Python script that reads the data and displays it every 100 milliseconds


#test connection to Wii Motion plus

import smbus
import time

bus = smbus.SMBus(1)	#the RaspberryPi B V2 has the bus #1 exposed on the pins 3/SDA and 5/SCL
ADDR_INIT = 0x53	#WM+ starts out deactivated at this address (can be seen by doing i2cdtect -y 1)
ADDR = 0x52


def initGyros():
	bus.write_byte_data(ADDR_INIT, 0xFE, 0x04)
	time.sleep(0.01)

def readGyros(gyroZeroes):
	bus.write_byte(ADDR, 0x00)	#send zero before each request
	#there are 6 bytes of data sent by the WM+	
	data0 = bus.read_byte(ADDR)
	data1 = bus.read_byte(ADDR)
	data2 = bus.read_byte(ADDR)
	data3 = bus.read_byte(ADDR)
	data4 = bus.read_byte(ADDR)
	data5 = bus.read_byte(ADDR)
	
	gyroZ = ((data3 >> 2) << 8) + data0 - gyroZeroes[2]
	gyroX = ((data4 >> 2) << 8) + data1 - gyroZeroes[0]
	gyroY = ((data5 >> 2) << 8) + data2 - gyroZeroes[1]
	
	return (gyroX, gyroY, gyroZ)


def calibrateGyrosZeroes():
	tempX = 0
	tempY = 0
	tempZ = 0
	gyroX0 = 0
	gyroY0 = 0
	gyroZ0 = 0

	for i in xrange(10):
		vals = readGyros((0, 0, 0))
		tempX += vals[0]
		tempY += vals[1]
		tempZ += vals[2]
	
	# average the 10 readings
	gyroX0 = tempX / 10
	gyroY0 = tempY / 10
	gyroZ0 = tempZ / 10
	
	return (gyroX0, gyroY0, gyroZ0)


#MAIN program / loop
try:
	initGyros()
except IOError:
	print "Couldn't INIT gyros, maybe they are already initialised..."

gyroZeroes = calibrateGyrosZeroes()
print "Calibrated: " + str(gyroZeroes)


while True:
	print readGyros(gyroZeroes)
	time.sleep(0.1)
	

Advertisements

5 Responses to Wii Motion plus Gyros on RaspberryPi

  1. Pietro says:

    Hi, i’m new to all this, but i think that’s so wonderfull… even if my knowledge is near zero, I have to give it a try…

    from your picture above it seems very simple to connect the wmp with only four wires!
    Now just a stupid request: the little red board connected to the wmp is from a wii remote or something else?

    Thanks a lot…

    • trandi says:

      Hi Pietro,

      Sorry for my late answer, was quite busy…
      No the little red board is simply an adaptor to make connecting the wires to the WMP easier…
      See the picture here https://trandi.wordpress.com/2011/01/16/wii-motion-plus-and-arduino/

      Dan

      • Pietro says:

        Hi, thankyou for your answer, i wanna ask you something else because i can’t find anything elsewhere about my issue.

        I’m first trying to connect a wiimote via Bluetooth. As in your script it seems almost easy importing modules, but ‘import i2c’ give me an error:

        Import Error: no module named i2c

        I’ve installed all that’s needed (obviously all i2c libraries and other stuff like all bluez libraries), configured the blacklist and added that module (i2c-dev) in the /etc/modules

        I don’t think that library (i2c) is essential for the connection because i2c scope (if I’m not wrong) is to connect some kind of board wired to the gpio, so I put a # in front of import i2c row. This make the script running but the wiimote won’t connect. Now i don’t know how much this i2c is important in that script or if the problem is the Bluetooth (but I’m pretty sure the dongle is working well).

        Can you help me please?

        Thankyou very very very very very much….

  2. Jeremy says:

    Greetings,
    I am new to this, please bear with me. I just got a Raspberry Pi and I am also into amateur astronomy. I was wondering if the gyros in the motion plus, if mounted on a telescope, would be able to provide data where the telescope was pointing in stelarium?

    Thanks,
    Jeremy

    • trandi says:

      Hi Jeremy,
      And thank you for your comment.
      Just the gyros in the MPlus will be accurate for the short instant, but will drift over time. This is why we usually combine gyros with accelerometers which are noisy for small movement but do not drift over time.
      Both combined can give you a “relatively” stable indication of the orientation….
      However, “relatively” is the keyword here, I have no idea how much accuracy would be needed for your project, it might be orders or magnitude more….

      Dan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: