Friday, August 14, 2009

Programming with the Balance Board

This post covers how to program with the balance board via devkitPRO's libogc.

But before we program the Wii Balance Board, we should understand how the board itself works. The balance board has four sensors in it which can detect pressure applied to them. The sensors will be referred to in this post as the top left sensor, the bottom left sensor, the top right sensor, and the bottom right sensor. Unlike Wiimotes which can be set to one of four channels, the balance board is always automatically set to a fifth channel.

devkitPRO reads the data from this board initially as raw interpolated data from each sensor. To the untrained eye, this data looks like a bunch of random numbers. devkitPRO then applies a formula to the raw interpolated data and can determine the approximate amount of pressure applied to any of the four sensors. devkitPRO also calculates an X and Y coordinate based on the difference in data between all of the sensors. As programmer's, we have access to all of this data at any time we wish.

So now that we have a basic overview of how the balance board works and how devkitPRO interprets the data read from it, let's get straight to the actual programming:

First, include the balance board code from devkitPRO:
#include (wiiuse/wpad.h)//replace "(" with "<" and ")" with ">"

Initialize the balance board:
struct expansion_t exp;
exp.type = WPAD_EXP_WIIBOARD;


For every turn of a loop that needs to access data from the balance board, be sure to have the computer read the data coming from the balance board:
WPAD_ScanPads();
WPAD_Expansion(WPAD_BALANCE_BOARD, &exp);


You'll then have access to the following values from the expansion_t struct exp:
  • exp.wb.tl - The approximate pressure on the top left sensor
  • exp.wb.tr - The approximate pressure on the top right sensor
  • exp.wb.bl - The approximate pressure on the bottom left sensor
  • exp.wb.br - The approximate pressure on the bottom right sensor
  • exp.wb.rtl - The raw data from the top left sensor
  • exp.wb.rtr - The raw data from the top right sensor
  • exp.wb.rbl - The raw data from the bottom left sensor
  • exp.wb.rbr - The raw data from the bottom right sensor
  • exp.wb.x - The calculated X value
  • exp.wb.y - The calculated Y value

How you use these values is entirely up to you. I highly reccomend basing the majority of your code around the X and Y values if possible. I'd now like to elaborate on how these values are used:
  • If X is less than 0, then there is more pressure on the left half of the board
  • If X is greater than 0, then there is more pressure on the right half of the board
  • If Y is less than 0, then there is more pressre on the top half of the board
  • If Y is greater than 0, then there is more pressure on the bottom half of the board

Hopefully this post gave you a good introduction on how to program with the Wii Balance Board. Finding the right way to use the values from the balance board is mostly up to how you interpret the specific needs of your program.

Kudos to Ave who originally wrote a patch that implemented the balance board in libogc. Without you, this tutorial wouldn't exist.

1 comment:

  1. Thanks for the post. Do you know if it is possible to get data from the balance board while it is hooked up to the Wii and playing "normal" games. Thanks for your help :)

    ReplyDelete