iOS — Relative altimeter, empowered

Guntis T
3 min readSep 25, 2022

--

TL;DR;

I used relative altimeter, to be able to automatically open the apartment building gates, whenever I went away, or came home.

Full story and code:

So, you might have seen this article of mine, about automated recording of car drives.

As I already have an app, that is able to detect each car drive — I am also able to extend this feature to open my apartment building gate automatically.

(once the app detects a start of a drive, it automatically calls via Twilio service to a certain phone number, to open the gates).

That works great, but I was wondering.. can I also make it so that, when I am walking in the direction of the gate, could they also open for me, automatically?

The issue is — whenever there is no active drive, then the app turns off gps to save battery.

Then I thought, could I use the altimeter to figure this out?

The problems with the relative altimeter is that — it gives proper pressure, but the height above the sea level is relative (uses pressure, but starts at 0 once the altimeter is started).

I also noticed, that the pressure is in “constant flux”, it changes every second, minute, and over the course of hours and days can fluctuate very drasticly.

How I solved this one

Luckily I am living in an apartment building 4th floor.
I calculated, that from 4th floor and ground floor there are at least 10 meters in altitude, and so I wrote a little helper function that simply stores each received measurement in a stack (size 70).

Each time I receive a value, I then compare stack's first and last value. If the altitude has changed at least 10 meters, then I am either on a ground floor, or inside my apartment.

1.) If the altitude has decreased 10 meters — I am now on ground level, so I turn on the GPS, until I leave apartment location circle, or if I go up to the apartment again, or maybe 10 minutes have passed.

2.) If the altitude increases 10 meters, — I am now in the apartment, so, turn off active GPS

3.) If I am outside apartment location circle, turn off GPS

Problems

1. On the first app launch time, I can only detect, whether I am in apartment location circle or not. But as it is relative altitude, then I cannot be sure, if I am inside apartment or on ground level. Thus I have that 10 minute interval, until I turn active GPS off. (within 10 minutes I would have left the apartment location anyways).

2. If I am descending the stairs verrry slowly, then this altimeter will not triger, that I am now on ground level / apartment level. But that mostly depends on the person itself. (I have only once encountered such scenario, when I ascended the stairs and saw something outside, and stopped to look, and it took too long, and when I ascended to the apartment floor, it did not register that I was now in apartment).

Here is a gist of that class. Maybe it will help you as well.

As always, please Follow, for some other interesting articles!

--

--