## Analog and digital speedometer

Problems with Canvas, OpenGL, etc...

### Analog and digital speedometer

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

Posts: 4
Joined: Tue May 24, 2011 2:19 am

### Re: Analog and digital speedometer

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

MichaelEGR
Senior Developer

Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

### Re: Analog and digital speedometer

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

Posts: 27
Joined: Wed Sep 14, 2011 6:46 am

### Re: Analog and digital speedometer

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)
angle = (float) Math.acos(x);
}
else if(x<0&&y>0)
angle = (float) Math.acos(x);
}
else if(x<0&&y<0)
angle = (float) (Math.abs(Math.asin(y)) + Math.toRadians(180));
}
else if(x>=0&&y<0)
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

Posts: 12
Joined: Wed Oct 05, 2011 12:39 am

### Re: Analog and digital speedometer

Nice post thanks for sharing........
loosi007
Freshman

Posts: 6
Joined: Sun Jan 01, 2012 3:29 pm