Analog and digital speedometer

Problems with Canvas, OpenGL, etc...

Analog and digital speedometer

Postby x_byte » Tue Sep 13, 2011 2:06 pm

Hi

i'm looking to develop an app that shows the rpm and speed of a car. I am retrieving all the data through a can network over bluetooth, but i want show them in a fancy speedometer, that shows the rpm of the car with the needle and the velocity in km/h on a "digital screen" at the bottom of the speedometer.

I searched on the web and i found that i could eventually do that with adobe air.

Has anyone another shot to do that without flash ?

Best regards
x_byte
Freshman
Freshman
 
Posts: 4
Joined: Tue May 24, 2011 2:19 am

Top

Re: Analog and digital speedometer

Postby MichaelEGR » Tue Sep 13, 2011 4:52 pm

It sounds like you may not do much coding, so yeah there is a bit involved of course, but here is some essential details on a way to implement what you are trying to do. Unfortunately I don't have much time to help more.

The easiest way to accomplish this is to create a native app with a custom graphic component for the fancy speedometer with the Android 2D graphics API. At its simplest you could have an image provide the background for the speedometer then simply draw a line for the needle. You could also conceivably use an image for the needle and rotate the image accordingly.

If the speedometer is linear from 0 to max speed things are fairly easy to determine the rotation of the needle. Let's say it will rotate on the top half of a circle for speeds 0 to 200 km/h. 0 is PI (radians angle) and 200 km / h is 0 (radians). You can determine the point along the unit circle in screen coordinates (0, 0; upper left corner - 2D API is oriented this way) by applying x = cos(<angle in radians>); y = -sin(<angle in radians>). You can treat this (x, y) as a unit vector / normalized direction. You can multiply it by a scalar value to increase the length.

For a linear mapping of speed to angle in radians for rotation around the top half of a circle you'd calculate PI - PI * (speed / max speed).

For a review of general trig and a picture of the unit circle and how radians relate check this out:
http://library.thinkquest.org/20991/alg2/trig.html

Good luck.. :)
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Re: Analog and digital speedometer

Postby roopa » Fri Sep 23, 2011 8:06 am

he analog speedo in my 1971 Datsun 1200 was notoriously unreliable, and the hotter the ambient temperature, the higher it would read!

After scouring the web for digital speedo schematics, I found one, and set about constructing it. After sorting out some teething issues with the electronics, and redesigning parts of the circuit, I built a prototype, and installed it in my Datsun. It functioned perfectly in my Datsun for about 5 years, and was removed when I sold the Datsun.
roopa
Developer
Developer
 
Posts: 27
Joined: Wed Sep 14, 2011 6:46 am

Re: Analog and digital speedometer

Postby millerni456 » Thu Oct 13, 2011 3:14 am

I have a working analog like joystick in my application and here's how I did it.

I have two Opengl Squares that render two textures. The base of the analog and the thumb or actual stick of the analog. I enable GL_BLEND and pass the GL_SRC_ALPHA_MINUS_ONE for the blend function like this:

gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_SRC_ALPHA_MINUS_ONE);

this enables transparency in textures.

After that I used a little trigonometry to position the thumb:

First I need the mouse clicks position and the middle of the analog base.

P1 = mouseLoc; P2 = analogBaseCenterPoint.

Then I need to find the distance from these using the distance formula:

P1.y - P2.y divided by P1.x - P2.x = distance.
This distance will determine the value(speed) of the analog.

I then need to find the change in X and Y separately to create a vector.
The same points will be used and I'll create a vector which the two points

Vector( changeInX, changeInY);
Then I normalize it, so its coordinates will divide to be within 0 to 1.
Normalizing can be done by dividing each component by the vectors magnitude ( or length).

So now i have normalized coordinates.

Using trig I can now plug these into arcsin or arccos functions to get the angle (or direction or velocity of the analog)

I also use this and the distance to determine where to draw/translate the analog thumb.

So... I use this method for finding the angle:
where X = the normalized X coord
and Y = the normalized Y coord.
The angle will be the angle of direction. (between 0 and 360 degrees).

float angle =0;
if(x>0&&y>0)
{//QUADRANT ONE
angle = (float) Math.acos(x);
}
else if(x<0&&y>0)
{//QUADRANT TWO
angle = (float) Math.acos(x);
}
else if(x<0&&y<0)
{//QUADRANT THREE
angle = (float) (Math.abs(Math.asin(y)) + Math.toRadians(180));
}
else if(x>=0&&y<0)
{//QUADRANT FOUR
angle = (float) (Math.asin(y) + Math.toRadians(360));
}

So... now that I'ved got the angle I need to create a 3-component velocity vector.
I do this again using trig and using the sin and cos functions

float velX = (float) Math.cos(angle) * distance; //multiply by distance to tell how much. cos functions return a value up to 1.
float velZ = (float) Math.sin(angle) * distance; //same as cos function but for Z value. (character moves along xz plane).

Create my final movement vector:

Vector( velX , 0, velZ) //y = 0 because character doesn't move up(jump) from the analog.




Hope this helps and is relevant to what your are trying to achieve. Good Luck and Happy Programming!
millerni456
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Oct 05, 2011 12:39 am

Re: Analog and digital speedometer

Postby loosi007 » Sun Jan 01, 2012 3:49 pm

Nice post thanks for sharing........
loosi007
Freshman
Freshman
 
Posts: 6
Joined: Sun Jan 01, 2012 3:29 pm

Re: Analog and digital speedometer

Postby samstephan9 » Fri Jan 06, 2012 12:20 pm

I am also interested to know that please help us I will make an speedometer too.
samstephan9
Junior Developer
Junior Developer
 
Posts: 20
Joined: Wed Jul 13, 2011 12:11 pm
Location: California

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 2 guests