Recording Acceleration with a Wiimote
Introduction
The Wiimote (Wii remote) has become a very
popular toy without even using it with the Wii. A funny
story... My wife
went to the store for some items and I asked her to pick up a
Wiimote, since I was currently borrowing a friend's to see if I could get it
to work. When she asked for help to get the Wiimote from
behind the glass, the associate asked if she wanted to buy a special
game pack for $10. My wife told them that we didn't actually
have a Wii, but her husband was going to use the Wiimote with the
computer. After using smelling salts to revive him
from the
shock, she explained a little of what I was planning to do.
There are much better hacks of the Wiimote out there like a
virtual white board by Johnny Lee. My project is just
a simple program that uses Python to connect to the Wiimote and record
acceleration data. This program also has the following
features:
- Calibration - gets an initial reading when starting the
Wiimote to zero the data.
- Data smoothing - averaging every 3 points
- Records and plots acceleration, velocity (integrated from
acceleration), and position (integrated from velocity) vs. time
- In order to get better results for velocity and position,
a tolerance on acceleration is used. So if acceleration
changes below a specified threshhold, velocity is kept constant.
This provides better results, but the error in velocity and
position is still noticeable and quickly adds up.
- The program is controlled completely by the Wiimote
- Data is recorded about every hundredth of a second
There are a few useful examples out there performing similar
functions. I just wanted to provide another example that
others can start with and that may spark ideas for other uses.
REQUIREMENTS
Hardware
Wiimote
Bluetooth USB Adapter (or an internal bluetooth device)
Software
Linux
Python
- the programming language
- Cwiid - Python Wii
connecting module
- Matplotlib - a plotting
library
(There are other
modules required that the above need, but Synaptic will find those
dependencies or you just read the documentation.
wiimote_record_accel_vel_pos.py
- a program that grabs the accelerometer data, calculates velocity and position, and plots the results.
plot them just using the wiimote
wiimote_record_ir_pos_and_accel.py - a program that grabs the y position of a IR LED, calculates velocity and acceleration, and plots the results.
INSTRUCTIONS
Just run the program and follow the on screen
instructions. The Wiimote controls the program. Here are
what the Wiimote buttons do:
| Buttons |
Description |
| 1 & 2 simultaneously | Press both buttons at the same time when the program starts, so that it can find the Wiimote. |
| Home | This calibrates the Wiimote by recording data for 2 seconds and getting
the average point. All of your data will subtract this value before
recording and plotting. |
| + | Start recording data |
| - | Stop recording data and then plots your results |
DOCUMENTATION - Accelerometer
The Wiimote provides acceleration data on 3 axis (x,y,z). The acceleration data provided from the Python Cwiid module is an integer and not a direct m/s2 or g value. I have looked a little bit online, but have not utilized a conversion factor yet, so this program just displays the output without any units.
Data is gathered about every hundredth of a second and averaged across 3 points. So the plot resolution is about 0.03 seconds. Velocity is calculated using simple numerical integration:
vi = vi-1 + ai(ti - ti-1)
(where a is acceleration, v is velocity, t is time, i is the current data point, i-1 is the previous data point)
To improve the accuracy of the velocity integration, the program ignores changes in acceleration below a specified threshhold. So if acceleration changes less than "x", the velocity equals the previous data points velocity.
Position is calculated similar to velocity. It is the integration of velocity.
pi = pi-1 + vi(ti - ti-1)
(where p is position, v is velocity, t is time, i is the current data point, i-1 is the previous data point)
To test the data analysis I came up with a simple experiment. The Wiimote starts out at rest on my desk. I then proceed to lift it up about 2 feet and then bring it back down to the desk, and again let it sit at rest. I tried not to rotate the Wiimote or alter the x or y positions. Here are the results:
Notice that the position does not get back to 0 even though I set the Wiimote back on my desk. This has to do with the summation of errors from integration. You can also see that even though the Wiimote is at rest at the end, the position is moving up slightly. This is due to a velocity slightly over 0. The acceleration data is good but the position and velocity data are not good enough for a real experiment, but can provide a ballpark answer. Searching online about this topic, someone mentioned that the Wii designers added the sensor bar to account for this inaccuracy.
DOCUMENTATION - IR Sensor Position
The Wiimote also has an infrared (IR) camera and the Wii sensor bar is actually just 2 infrared LEDs. Using the Python Cwiid library, you can get the IR (infrared sensor) data from the sensor bar or another IR device (like a IR LED). This provides a better measurement of position. The Wiimote provides position and intensity values for up to 4 IR sources and the Cwiid Python module can provide this data for us also.
For this experiment I did not actually have a Wii Sensorbar, so I just used a IR LED that I purchased at RadioShack. It is a high intensity IR LED with a forward current of 100 mA and a forward voltage of 1.2V. Hooking it up to a 5 volt source, I used a 47 ohm resistor in series.
Velocity is calculated using numerical differentiation:
yi+1 - yi
------------
ti+1 - ti
Acceleration is calculated as the second derivative of position with numerical methods:
yi+2 - 2yi+1 + yi
-----------------------------
(ti+2 - ti+1)(ti+1 - ti)
The following plot shows the raw output of the y position of the IR LED and also the calculated velocity and acceleration using differentiation.
References
Wiimote interfacing using the cwiid python module
Matplotlib
Johnny Lee's Wiimote projects
Click here to
send comments or questions about this project.