Welcome! Log In Create A New Profile

Advanced

Motion Sensing help

Posted by blutderengel 
Motion Sensing help
February 23, 2009 12:23AM
I've just begun programming with devkitpro and the wiiuse library. I started off with the template that comes with devkitpro and expanded on that. So far I have all the buttons on the wiimote working for pressed, released, and held, as well as the nunchuck buttons and the analog stick. Also, I've gotten then IR to work properly.

I'm now interested in using the accelerometer in the wiimote and I've been trying to gather the data from it, but I only get 0's out of it. I can't figure out what I've done wrong, but I feel like I need to initialize the accelerometer or something.

I have the code set to display the roll, pitch, and yaw only when you hold the B button, and the acceleration is displayed when holding the Minus button.

Any help would be very much appreciated.

Here is my code:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

static u32 *xfb;
static GXRModeObj *rmode;

void Initialise() {

	VIDEO_Init();
	WPAD_Init();

	rmode = VIDEO_GetPreferredMode(NULL);

	xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
	console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);

	VIDEO_Configure(rmode);
	VIDEO_SetNextFramebuffer(xfb);
	VIDEO_SetBlack(FALSE);
	VIDEO_Flush();
	VIDEO_WaitVSync();
	if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
}

int main() {

	Initialise();
	
	printf("\x1b[2;0H");
	printf("Hello World!\n");
	
	struct expansion_t exp;
	struct orient_t orient;
	struct vec3w_t accel;

	while(1) {
		
		WPAD_ScanPads();

		u16 buttonsDown = WPAD_ButtonsDown(0);
                u16 buttonsHeld = WPAD_ButtonsHeld(0);

		
		WPAD_Expansion(WPAD_CHAN_0, &exp);
		WPAD_Orientation(WPAD_CHAN_0, &orient);
		WPAD_Accel(WPAD_CHAN_0, &accel);
		
		if( buttonsDown & WPAD_BUTTON_A ) {
			printf("Button A pressed.\n");
		}
//.... a bunch of similar code checking the buttons presses and holdings.

if (buttonsHeld & WPAD_BUTTON_B ) {
			printf("\x1b[2;0H");
			printf("Wiimote yaw  : %f\n"
					"Wiimote pitch: %f\n"
					"Wiimote roll : %f\n", orient.yaw, orient.pitch, orient.roll);
		}
		
		if (buttonsHeld & WPAD_BUTTON_MINUS ) {
			printf("\x1b[2;0H");
			printf("Wiimote acceleration:\n"
					"X:  %f\n"
					"Y:  %f\n"
					"Z:  %f\n", accel.x, accel.y, accel.z);
		}

		if (buttonsDown & WPAD_BUTTON_HOME) {
			exit(0);
		}
	}

	return 0;
}

Re: Motion Sensing help
February 23, 2009 12:42AM
You're missing the init to get that data, put this after WPAD_Init()

WPAD_SetDataFormat(WPAD_CHAN_ALL,WPAD_FMT_BTNS_ACC_IR);
WPAD_SetVRes(WPAD_CHAN_ALL,screenwidth, screenheight);

where screenwidth/height are whatever width/height you want (eg: 640x480)
Re: Motion Sensing help
February 23, 2009 12:49AM
Also, I see this a lot - using all those WPAD functions to get data. You can use one function to get all your WPAD data, do something like so:

WPADData wpad;

while(1)
{
memcpy(&wpad, WPAD_Data(0), sizeof(WPADData));

// use the data
if (wpad.btns_d & WPAD_BUTTON_B ) {

printf("Wiimote acceleration:\n"
"X: %f\n"
"Y: %f\n"
"Z: %f\n", wpad.accel.x, wpad.accel.y, wpad.accel.z);
}
}
Re: Motion Sensing help
February 23, 2009 01:03PM
Thanks for the reply Tantric.

I took your suggested code and the orientation works great now, but the acceleration still displays 0's. Any ideas why that might be?
Sorry, only registered users may post in this forum.

Click here to login