# Quickie: Which way does gravity point? Everyone knows a compass always points north, and most people know it’s because of magnetic fields present on Earth’s surface.  There’s another force here on Earth directed to a central point, and that’s gravity.  Humans are quite adept at sensing gravity thanks to equilibrioception, where  fluid contained in structures in our inner ear provide feedback to help us stay balanced.

But machines, too, can detect gravity thanks to the simple accelerometer.  Already present in most smartphones today, accelerometers react to gravity with tiny springs, creating a voltage difference that we can measure and turn into meaningful units.

On Android, we can easily read the accelerometer data:

```SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor accel = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorManager.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);

...

public void onSensorChanged(SensorEvent event) {
float x, y, z;
x = event.values;
y = event.values;
z = event.values;
...}```

### Using accelerometers to emulate human’s perception of gravity

I’d like to show how we can use an Android phone (even my dusty old Droid Eris) to visualize the force of gravity.  To save time, we’re only going to use two dimensions, x and y, but the technique used here can easily be extended into 3D.

Let’s represent gravity the same way students in a high school physics class would — with an arrow pointing down.  The goal would be the ability to rotate the phone (changing the x and y position), while still having that arrow point down, illustrating the direction of gravity.

The first thing we’ll need to do is convert the rectangular coordinates given to us (x and y) to a polar system (r, θ), where extracting an angle is much easier.

Thinking back to high school geometry, the inverse tangent will provide that angle directly.  Java has a built-in method, atan2(), which even gracefully handles the divide-by-zero case when x = 0. Because the image rotation I’m using is based on degrees (more on that in a moment), we can convert the radian angle to a common degree (0-360°).

```double theta = Math.atan2(y, x);
double degree = ((theta * -180.0) / 3.14159) + 180;  // +180 to keep 0 on the right```

That gives us the degree rotation of the phone in 2D.  We’re almost there.  To determine the degree that we would like the gravity arrow to point, we need to offset that degree, modulo 360 to keep us within the range (0-360°):

`float rotateDegree = (float) ((degree + 270.0) % 360.0);`

Now it’s just a matter of re-drawing the arrow image on the screen.  Android offers some fancy animation techniques, but for this quickie project, I chose to use a matrix rotation:

```Matrix matrix = new Matrix();
matrix.setRotate(rotateDegree);
Bitmap rotated = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(),matrix, true);
arrowImage.setImageBitmap(rotated);```

With that code in place, we can finally visualize the force of gravity, at least in two dimensions:

This project was a quick one (writing this blog entry actually took longer than the code itself), but I think it’s important to show how we can figuratively “teach” a device a human trait and give them a new skill.  For instance, with a faster refresh rate and perhaps a little more accuracy, a robot can use this technique to keep itself balanced, much like humans use information from gravitational forces to stay balanced.

Github available here.