webOS Nation Forums >  webOS apps and software >  webOS development > AccelBall - Practical Accelerometer Demo
AccelBall - Practical Accelerometer Demo

  Reply
 
LinkBack Thread Tools Display Modes
Old 07/26/2009, 02:36 PM   #1 (permalink)
Member
 
Join Date: Jul 2009
Location: Lebanon, Oregon, USA
Posts: 41
Likes Received: 0
Thanks: 6
Thanked 10 Times in 5 Posts
Default AccelBall - Practical Accelerometer Demo

Here's some code for folks who want to get started playing with raw data from the accelerometer. Very, very basic. It just lets you hold your Pre flat and tip it back and forth to roll a ball around. Figured it would be something fun to play around with for coders new to the Pre.

Just one event handler and a tiny bit of math pulls off the heavy lifting:

Code:
MainAssistant.prototype.handleAcceleration = function(event) {
	this.driftX += (event.accelX * this.scale);
	this.driftY += (event.accelY * this.scale);
	this.driftZ += (event.accelZ * this.scale);
}
And a PeriodicalExecuter to move the ball around once every 30th of a second:

Code:
MainAssistant.prototype.setup = function() {
...
	new PeriodicalExecuter(function(pe) {
		obj.moveDot();
	}, (1/30));
...
}
And the moveDot function:

Code:
MainAssistant.prototype.moveDot = function(){
	// Move that dot! Note that the Z axis isn't used in this demo...
	this.theDot.style.left = (parseInt(this.theDot.style.left) + this.driftX) + 'px';
	this.theDot.style.top = (parseInt(this.theDot.style.top) - this.driftY) + 'px';

	// And there's more code to keep the ball within the viewport, just snag the source...
}
Attached Files
File Type: zip accelball-source.zip (5.3 KB, 46 views) Email Attachment
File Type: ipk com.randallagordon.accelball_0.0.0_all.ipk (3.8 KB, 48 views) Email Attachment
Download URL:
randallagordon is offline   Reply With Quote
Old 07/26/2009, 02:50 PM   #2 (permalink)
Member
 
Join Date: Jun 2009
Posts: 19
Likes Received: 0
Thanks: 19
Thanked 5 Times in 3 Posts
Default

just installed, works great!
jamesonrw is offline   Reply With Quote
Old 07/26/2009, 03:13 PM   #3 (permalink)
Member
 
Join Date: Jun 2009
Posts: 127
Likes Received: 0
Thanks: 52
Thanked 10 Times in 9 Posts
Default

I thought there was a 4Hz sample rate limitation from the accel API?
Shane112358 is offline   Reply With Quote
Old 07/26/2009, 04:49 PM   #4 (permalink)
Member
 
Dtom2444's Avatar
 
Join Date: Jun 2009
Location: Stamford Bridge
Posts: 122
Likes Received: 0
Thanks: 19
Thanked 29 Times in 16 Posts
Default

There's an app for the iPhone/iPod Touch like this but with obstacles and a Goal at the end of a maze. It's my favorite time-waster for the format.

I hope someone further expands on this. The only suggestion I could think of would be to fix the speed. It seems to move too much with little effort. I suppose it kinda makes sense though. The ball is small so it's "lighter". A bigger ball would be "heavier" so it wouldn't move as much.

Thanks again!
Dtom2444 is offline   Reply With Quote
Old 07/26/2009, 09:27 PM   #5 (permalink)
Member
 
Join Date: Jul 2009
Location: Lebanon, Oregon, USA
Posts: 41
Likes Received: 0
Thanks: 6
Thanked 10 Times in 5 Posts
Default

Quote:
Originally Posted by Shane112358 View Post
I thought there was a 4Hz sample rate limitation from the accel API?
Indeed there is, and the "drift" variables are getting updated at 4Hz, however, the data can still be used 30 times a second (or more for that matter). I have a hunch that 4Hz was chosen simply to keep the API from getting flooded with "acceleration" events, lest the Pre be brought to it's kness any time an app wanted to put the accelerometer to work.

At this point I am attempting to track down a method for bumping the polling interval up. Unfortunately it doesn't appear to be quite as simple as some of the other hardware hacks. Simply changing the value in "/sys/class/input/input5/poll_interval" (hopefully I got that typed out right...) doesn't do a damn thing. But, it does contain a value of "250" and 250ms = 4Hz, so that's certainly on the right track. Any info anyone has would be greatly appreciated!

Quote:
Originally Posted by Dtom2444 View Post
There's an app for the iPhone/iPod Touch like this but with obstacles and a Goal at the end of a maze. It's my favorite time-waster for the format.

I hope someone further expands on this. The only suggestion I could think of would be to fix the speed. It seems to move too much with little effort. I suppose it kinda makes sense though. The ball is small so it's "lighter". A bigger ball would be "heavier" so it wouldn't move as much.
I actually intend to code just that, a maze app. Dunno if I will get around to it or not, but if I do, it'll end up in the Homebrew section!

The only reason this is so spartan is to show off how the code works, not to create an end user app/game. For that purpose, I wanted to add as few bells & whistles as possible. Hence why it is in the development forum and not homebrew.

And personally, I feel it is still too slow. I haven't taken the time to add the math to lock this in to "real physics" yet, however I feel it is still slower than gravity. I want to get it to the point where "dropping" a ball from the top of the screen will result in it accelerating at 9.82 m/s˛, the real acceleration due to gravity. (As in, calculating out the physical width of a pixel and going from there...)

Think of it this way. With the scaling set the way it is now, if you put a marble on the screen of your Pre and rolled it around along with this ball, the marble would still roll around faster than the on screen ball... But, snag the code and change the scale variable around so you can get a feel for what is going on, it really helps to solidify understanding.
randallagordon is offline   Reply With Quote
Thanked By: cashen
Old 07/26/2009, 10:59 PM   #6 (permalink)
Member
 
Colonel Kernel's Avatar
 
Join Date: Apr 2009
Location: Boston, MA
Posts: 773
Likes Received: 0
Thanks: 191
Thanked 101 Times in 58 Posts
Default

If this qualifies as an app, get it into the Homebrew section.
Colonel Kernel is offline   Reply With Quote
Old 07/27/2009, 01:45 AM   #7 (permalink)
Member
 
Join Date: Jul 2009
Location: Lebanon, Oregon, USA
Posts: 41
Likes Received: 0
Thanks: 6
Thanked 10 Times in 5 Posts
Default

Quote:
Originally Posted by Colonel Kernel View Post
If this qualifies as an app, get it into the Homebrew section.
It doesn't really...it doesn't do anything useful at this point. It simply exists to give programmers some code to start from when using the accelerometer. If I get around to adding some other physics goodies to it, then yeah, possibly should end up over in Homebrew.
randallagordon is offline   Reply With Quote
Old 07/27/2009, 01:55 AM   #8 (permalink)
Member
 
Dtom2444's Avatar
 
Join Date: Jun 2009
Location: Stamford Bridge
Posts: 122
Likes Received: 0
Thanks: 19
Thanked 29 Times in 16 Posts
Default

Quote:
And personally, I feel it is still too slow.
Well, I showed this off to my brother and he immediately agreed with my previous statement. The responsiveness is okay, but it feels like it continues to roll for a split second too long, hence the "unnatural" feel.

But good luck with that possible app! Can't wait to see what develops!
__________________

"We all follow the Chelsea!!!"
Dtom2444 is offline   Reply With Quote
Old 07/27/2009, 01:58 AM   #9 (permalink)
Member
 
Join Date: Jul 2009
Location: Lebanon, Oregon, USA
Posts: 41
Likes Received: 0
Thanks: 6
Thanked 10 Times in 5 Posts
Default

Quote:
Originally Posted by Dtom2444 View Post
Well, I showed this off to my brother and he immediately agreed with my previous statement. The responsiveness is okay, but it feels like it continues to roll for a split second too long, hence the "unnatural" feel.
Ah, yes, inertia. Yes, that would actually be taken care of by speeding it up. The "lag" is because it is responding slower than gravity actually would. Slow it down further and that "unnatural feel" gets magnified like you wouldn't believe.
randallagordon is offline   Reply With Quote
Old 07/27/2009, 09:45 AM   #10 (permalink)
Member
 
Join Date: Jul 2009
Posts: 40
Likes Received: 0
Thanks: 1
Thanked 5 Times in 5 Posts
Default

Randall. Thank you for having such a concise example of using the accelerometer. I can't wait to play with this tonight. I've been working on a mythtv controller app, and this will add a few nifty whizbang features like jog control in recordings (turn the pre like a knob), and navigation of media lists by tilting the device 'up' and 'down'.

Not killer features, I know. But the tv has such a larger screen, when you're in front of it
dennisharrison is offline   Reply With Quote
Old 07/27/2009, 10:11 AM   #11 (permalink)
Member
 
Join Date: Jul 2009
Location: Lebanon, Oregon, USA
Posts: 41
Likes Received: 0
Thanks: 6
Thanked 10 Times in 5 Posts
Default

Quote:
Originally Posted by dennisharrison View Post
Randall. Thank you for having such a concise example of using the accelerometer. I can't wait to play with this tonight. I've been working on a mythtv controller app, and this will add a few nifty whizbang features like jog control in recordings (turn the pre like a knob), and navigation of media lists by tilting the device 'up' and 'down'.

Not killer features, I know. But the tv has such a larger screen, when you're in front of it
Excellent! People being able to get a quick start towards being innovative and creative is exactly what I was hoping for.

The idea of using the Pre as a "knob" is great!
randallagordon is offline   Reply With Quote
Old 07/27/2009, 11:04 AM   #12 (permalink)
Member
 
Join Date: Jun 2009
Location: Pensacola, FL
Posts: 28
Likes Received: 0
Thanks: 6
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by randallagordon View Post
I actually intend to code just that, a maze app. Dunno if I will get around to it or not, but if I do, it'll end up in the Homebrew section!
Please code it! I love apps that actually utilize the accelerometer!
sixshooterz is offline   Reply With Quote
Old 08/10/2009, 03:12 PM   #13 (permalink)
Member
 
mjsmiley's Avatar
 
Join Date: Jul 2009
Location: Plano, TX
Posts: 90
Likes Received: 0
Thanks: 32
Thanked 1 Time in 1 Post
Default Homebrew it

Quote:
Originally Posted by randallagordon View Post
It doesn't really...it doesn't do anything useful at this point. It simply exists to give programmers some code to start from when using the accelerometer. If I get around to adding some other physics goodies to it, then yeah, possibly should end up over in Homebrew.
First, this is probably one of the neatest apps amongst the homebrew apps. I think if you put this into the homebrew apps as a demo, it would get your code seen a lot more and maybe spark more development with it. This little red ball still shows more accelerometer promise than any other app that I've spotted-so I'm rooting for it getting mass attention on this forum. If you wanted to blow everyone's mind (or at least mine), add the ability to tap on the screan and create a small round fixed peg obstical.
mjsmiley is offline   Reply With Quote
Old 08/10/2009, 03:34 PM   #14 (permalink)
Developer
 
Join Date: Jul 2009
Posts: 1,400
Likes Received: 3
Thanks: 17
Thanked 1,540 Times in 376 Posts
Default

Quote:
Originally Posted by randallagordon View Post
At this point I am attempting to track down a method for bumping the polling interval up. Unfortunately it doesn't appear to be quite as simple as some of the other hardware hacks. Simply changing the value in "/sys/class/input/input5/poll_interval" (hopefully I got that typed out right...) doesn't do a damn thing. But, it does contain a value of "250" and 250ms = 4Hz, so that's certainly on the right track. Any info anyone has would be greatly appreciated!
I don't know if you are keeping up with this, but changing this value does indeed work. I have also created a service for apps to use so they can dynamically decide what value to use for the poll frequency.

Application:AccelService - WebOS Internals


I've started work on a Labyrinth game, and setting the poll frequency to 1KHZ (poll_interval = 1ms) doesn't seem to bog down the application at all. There are brief hiccups every once in a while, but I've seen those at 4HZ as well.

Labyrinth - WebOS Internals
egaudet is offline   Reply With Quote
Old 08/10/2009, 05:54 PM   #15 (permalink)
Member
 
Join Date: Jul 2009
Posts: 498
Likes Received: 1
Thanks: 3
Thanked 94 Times in 71 Posts
Default

You can easily back that down to 20-40 Hz for a labyrinth type of game and save some resources (power, etc). 1KHz is waaay overkill! But, nice work!
s219 is offline   Reply With Quote
Old 08/10/2009, 06:02 PM   #16 (permalink)
Member
 
Join Date: Jul 2009
Location: Lebanon, Oregon, USA
Posts: 41
Likes Received: 0
Thanks: 6
Thanked 10 Times in 5 Posts
Default

Quote:
Originally Posted by emoney_33 View Post
I don't know if you are keeping up with this, but changing this value does indeed work. I have also created a service for apps to use so they can dynamically decide what value to use for the poll frequency.

Application:AccelService - WebOS Internals


I've started work on a Labyrinth game, and setting the poll frequency to 1KHZ (poll_interval = 1ms) doesn't seem to bog down the application at all. There are brief hiccups every once in a while, but I've seen those at 4HZ as well.

Labyrinth - WebOS Internals
Excellent news, indeed! I've been busy with other projects lately, so I've fallen behind on developments. Although, your link appears to be broken and searching on the site doesn't reveal the app either...

Good to know high polling rates work (1KHz is awesome!), because that's precisely what I need for the app I have in mind... Going to have to toy with this tonight.
randallagordon is offline   Reply With Quote
Old 08/10/2009, 06:04 PM   #17 (permalink)
Member
 
Join Date: Jul 2009
Location: Lebanon, Oregon, USA
Posts: 41
Likes Received: 0
Thanks: 6
Thanked 10 Times in 5 Posts
Default

Quote:
Originally Posted by randallagordon View Post
Although, your link appears to be broken and searching on the site doesn't reveal the app either...
I do see the source on the git, however, so I'll snag it there. Thanks, much!
randallagordon is offline   Reply With Quote
Old 08/10/2009, 06:16 PM   #18 (permalink)
Developer
 
Join Date: Jul 2009
Posts: 1,400
Likes Received: 3
Thanks: 17
Thanked 1,540 Times in 376 Posts
Default

Quote:
Originally Posted by randallagordon View Post
I do see the source on the git, however, so I'll snag it there. Thanks, much!

Yes I believe webos-internals.org is down right now for upgrades. Let me know if you have trouble or need help installing the AccelService.

Or come over to #webos-internals irc channel (freenode.net)
egaudet is offline   Reply With Quote
Old 08/10/2009, 08:48 PM   #19 (permalink)
Member
 
Nightburn's Avatar
 
Join Date: Jun 2009
Location: Portland, OR
Posts: 469
Likes Received: 22
Thanks: 186
Thanked 110 Times in 80 Posts
Default

I installed this and opened it with pandora, the web browser, and my emailed opened at the same time . It ran great !! I can't wait to see more. Good job and thanks!
Nightburn is offline   Reply With Quote
Reply

 

Thread Tools
Display Modes



 


Content Relevant URLs by vBSEO 3.6.0