Wiimote Gyro to mouse movements

Official forum for open source FreePIE discussion and development.
Post Reply
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

UPDATE: I have officially located the bug. See my updates for the solution.

How can I improve gyroscope tracking for the Wiimote when increased speed is applied to the movements? Right now I have pretty good tracking if I move slowly. But the moment I move too fast from one direction to the other, the cursor will track poorly, causing large, random jumps in mouse cursor movements. Often times the jumps will express itself as sharp vertical movements when the wiimote is moved horizontal. The opposite happens as well.

I tried applying a deadband to motionplus.pitch_left and motionplus.yaw_down, but I cannot solve the inaccuracies caused by fast movements. I also tried the deadband on ahrs.yaw and ahrs.pitch, but it seems they behave more or less similarly with fast movements.

the code I use for mouse tracking with the wiimote is:

Code: Select all

	
	yaw = filters.deadband((wiimote[0].motionplus.yaw_down), 0.15) 
	pitch = filters.deadband((wiimote[0].motionplus.pitch_left), 0.15) 

	if wiimote[0].buttons.button_down(WiimoteButtons.A): 
		mouse.deltaX = yaw/-9
		mouse.deltaY = pitch/9
		
Last edited by MeteorFalling2 on Sun Dec 25, 2016 8:55 pm, edited 4 times in total.
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Re: Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

So I did some diagnostics.debug(mouse.Delta(X and Y)) and reviewed the output as controlled by the wiimote. I found some interesting things out:

The x movement in either direction had very little noticeable changes in the values while swinging left or right.

The y movements always had a dip and rise depending on the angle of the wiimote. When the wiimote is swung with a slight tilt up, the movements of the mouse would look like an upside down U. if the wiimote is tilted down, the movements would look like a U.

What's interesting, is that I have seen this movement in some GlovePIE scripts, although they where smoothed out a bit more and appeared more like arcs.

in my current GlovePIE script that I use, I cannot even detect the arcs at all. Something is done with with the reading that remove it. I have no idea how it was done as some of the commands used are not in FreePie, and I don't know what they mean anyways.
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Re: Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

I am stuck. It seems like I cannot get FreePIE to be an acceptable solution for Wiimote mouse emulation. I have finally cracked the code on my favorite GlovePIE script and ported it into FreePIE at a 1:1 accuracy(or so I believe). Unfortunately, I do not get the same results from FreePIE as I do with GlovePIE.

GlovePIE removes unwanted Pitch movements when swinging the Wiimote in the Yaw. FreePIE on the other hand does not. Despite using the same formula, I still get the insane Pitch movements.

I believe the differences lie with the underlying code, specifically with the initial calibration. I do not know enough to be able to open the hood and implement the correct code myself, however. It's really unfortunate too, as glovePIE is really sluggish in some of my games at medium to high settings. FreePIE is blazing fast, allowing me to play at max settings. I just cannot use it as the jumps and dips in my camera view are a deal breaker.

I will leave both codes here for anyone that is interested in comparing the scripts from GlovePIE and FreePIE.


GlovePIE

Code: Select all

GlovePIE.FrameRate = 125hz

//125 = the framerate of GlovePIE. Can use RemoveUnits(PIE.FrameRate) instead.

if wiimote.a {

    Mouse.DirectInputX += sin(atan2(wiimote.MotionPlus.RawYawSpeed/ 125, -wiimote.MotionPlus.RawPitchSpeed/125)) * |( wiimote.MotionPlus.RawYawSpeed/ 125)| * 30 // |(x)| is abs value.
    Mouse.DirectInputY += cos(atan2(wiimote.MotionPlus.RawYawSpeed/ 125, -wiimote.MotionPlus.RawPitchSpeed/125)) * |(-wiimote.MotionPlus.RawPitchSpeed/125)| * 30


}

FreePIE

Code: Select all

def update():

	if wiimote[0].buttons.button_down(WiimoteButtons.A) or wiimote[0].buttons.button_down(WiimoteButtons.Home):
	
		mouse.deltaX = -math.sin(math.atan2(wiimote[0].motionplus.yaw_down/125, wiimote[0].motionplus.pitch_left/125))* abs(wiimote[0].motionplus.yaw_down/125)*30 
		mouse.deltaY = math.cos(math.atan2(wiimote[0].motionplus.yaw_down/125, wiimote[0].motionplus.pitch_left/125))* abs(-wiimote[0].motionplus.pitch_left/125)*30 
		
if starting:

	system.setThreadTiming(TimingTypes.HighresSystemTimer)
	system.threadExecutionInterval = 8
	wiimote[0].motionplus.update += update
	wiimote[0].enable(WiimoteCapabilities.MotionPlus)
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Re: Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

alas, I have reduced my GlovePIE code even further. It's as simple as " Mouse.DirectInputX += wiimote.MotionPlus.RawYawSpeed/10 ". Now I know the calibration is at fault in FreePIE. This is the same as saying "mouse.deltaX = wiimote[0].motionplus.yaw_down/10. " which is completely a mess to use.

Is there a way to calibrate the Wiimote in FreePIE? It looks like there is a motionplus.calibrated option, but I have no idea what do with at.
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Re: Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

Here are the peaks from Pitch while moving Yaw. The top graph is motionplus gyro data, and the bottom graph is the accelerator readings.

Image
Last edited by MeteorFalling2 on Wed Dec 21, 2016 3:39 pm, edited 1 time in total.
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Re: Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

It turns out that after testing the other gyros that the spikes happen in them all. This image shows a spike in Yaw values when Roll is rotating:
Image
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Re: Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

After much testing I figured out one of the processes that causes gyro spikes in data.

In FreePIE, there is a specific reference to Motionplus slow and fast modes. According to Wiibrew, the Wiimote sends two modes of data for the gyro movements. When in slow mode (wiimote moving slowly), the wiimote will travel more units per degree/s than when in fast mode (Wiimote moving quickly). So they state that in order to account for these differences, you must apply a multiplier of (2000/440) to the fast mode. This multiplier is applied in FreePIE, and what I did was build from a modified source with the multiplier removed.

This change solved the spikes in Pitch values, but created a different problem. The slow and fast mode toggle is easily seen. If the Wiimote rotation speed becomes too great, the slow and fast modes toggle, causing disparity between the distance covered. It is similar to the effect that the Windows Enhance Pointer Precision does to mouse movements, except with only two speeds and more obvious.

A work-around that will somewhat smooth out these differences is to use the Filters.EnsureMapRange() function. That way you can set an upper limit that would toggle the fast-mode preventing the amount of distance it traveled. It is not an ideal solution however, since it doesn't prevent the the speed mode from working within the map range, such as moving in the opposite direction on the axis.

Something else that I have been playing with is removing only the Pitch multiplier. Moving Pitch fast happens less often than moving Yaw. This allows the normal function of Yaw movements, without the jarring speed differences, and removes the dips and rises of Pitch.

I am still trying to isolate the problem. I am still not sure why having a multiplier on the modes creates a problem. More testing is needed.



Data on slow and fast modes: http://wiibrew.org/wiki/Wiimote/Extensi ... otion_Plus
location of the multiplier that I removed: https://github.com/AndersMalmgren/FreeP ... ata.cs#L70
Last edited by MeteorFalling2 on Wed Dec 21, 2016 3:42 pm, edited 3 times in total.
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Re: Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

I did some more testing, and I am noticing the slow/fast mode switch sometimes becomes reversed. I am not sure why it does this, but my guess is it has to do with a delay in input movements. When I was testing with the multipliers removes on all axis, I saw noticeable input delay gradually increase over time.
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Re: Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

I made a new discovery about the slow/fast modes in FreePIE. FreePIE has the Roll and Pitch reversed. 0x4(the fast mode reading) is assigned to the gyro.Value.y which should be 0x2. After keeping the order of code, and just replacing the 0x4 with the 0x2, I was able to remove the large spikes in Yaw data. See here for the program entries I am speaking of: https://github.com/AndersMalmgren/FreeP ... ata.cs#L71

I believe what was happening was the roll fast mode and its multiplier(4-5) was being applied to Pitch. The reason it was applying was because of the slight tilt of hand gestures along the Roll axis. To test this theory, I opened the official FreePIE program, and placed the Wiimote on a flat surface to control Roll rotation and moved back and forth Yaw. I saw no large spikes.

Unfortunately or fortunately, fixing this mistake in the coding lead me to isolate the real issue with FreePIE. The slow and fast mode compensation aren't working correctly. Here is the Yaw comparison between the official FreePIE program and a modified version with the multiplier removed. The peaks with a sharp decline, then a plateau is the transition from slow to fast mode.

Official FreePIE with source code multiplier:
Image
Modified FreePIE with multiplier removed.:
Image

But to make things more frustrating, the mode switches sometimes do come on, however, it doesn't seem to be consistent, with it triggering when it is not desired.
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Re: Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

I have successfully fixed the problem. It turns out that for my Wiimote, Yaw(x) is 0X4, Pitch(y) is 0x2, and Roll(z) is 0x1. This is in contrast to the assignments given on the FreePIE git: https://github.com/AndersMalmgren/FreeP ... DLL.cs#L30

In addition, I verified my finding by recording my movements over time. In the graph, I started with isolated movements to Yaw, then Pitch, and finished with Roll. As you can see, the peaks have no inverse spikes followed by a plateau as can be seen in my other tests. This means that the fast mode multiplier is being applied to the correct axis.

I am not sure why the assignments were off. My guess is that it might be because my Wiimote is an older one with a Motionplus attachment. I will test to see if my theory is correct when I have access the the newer Wiimotes with it built in.

Image
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Re: Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

Okay, I had the chance to test two other Wiimotes with the Motionplus attachment. They both are Nintendo RVL-CNT-01-TR versions. They both have the fast modes wrongly assigned just like the RVL-CNT-01 version(M+ attachment).

The only discernible difference between the two versions beside the attachments, are Yaw, Pitch, and Roll. Their axis values are switched. Other than that, all three Wiimotes need the fast modes corrected as I outlined. 0x4 = x, 0x2 = y, and 0x1 = z (changed from 0x1 = x, 0x2 = y, 0x4 = z).
frnckdsrt
One Eyed Hopeful
Posts: 1
Joined: Fri Jun 09, 2017 10:36 am

Re: Wiimote Gyro to mouse movements

Post by frnckdsrt »

Hi MeteorFalling2,

I'm looking at your graphs and it seems to me you got something very clean after all the updates you've made.
I just discovered Freepie as I've coded a python script to get orientation infos from wiimote but the filter I've used (a recuperated/customised karman) doesn't give me satisfaction.
I've tried to do some tests on Freepie with the lines of code you provide on this topic but I'm not sure I can deal with it as it seems to be parts. (+ I'm not yet understanding all the way Freepie works)
Could you reveal the script from which you had these results ?
Otherwise you've pointed on the Freepie Github some code you've modified, does it mean you're a contributor and that the last version of Freepie contains these modifications ?
MeteorFalling2
One Eyed Hopeful
Posts: 42
Joined: Thu Dec 08, 2016 10:23 pm

Re: Wiimote Gyro to mouse movements

Post by MeteorFalling2 »

@frnckdsrt

Hi, so what I did was download the file from github and altered the files myself in the highlighted location: https://github.com/AndersMalmgren/FreeP ... ata.cs#L70. All I did was switched around the 0x1,0x2,0x4 to match the correct axis and then recompiled it.

I am not a Freepie contributor. I was merely showing how the programs definitions are wrong for each axis according to my tests. It would be great if someone could alter the files to fix this, but I do not know github well. I don't even know how to make an installer exe with the compiler, so I just run a portable version of the program instead. The only problem I run into, is that I need to install the base program so that the portable version works correctly.
Post Reply

Return to “FreePIE”