Matthew C Good : Musician, Software Engineer, Hobbyist.

What A Water Change Looks Like to the FishApp

One of the goals of the FishApp is to have automatic water change detection available in phase 1. In order to do this, I have a Ping))) ultrasonic distance sensor pointed down at my fishtank. This little guy works by producing a sound above human hearing range, and listening for it to bounce back. If you know the speed of sound, you can calculate how far away the object was that caused the reflection. The sensor I am using is mounted above the tank in a piece of 1/4 inch wood to help shield it from the moisture, and samples the water level at predetermined intervals, sending its data over a serial connection to a host computer via an Arduino controller.

The host computer gets this stream of numbers, and has to have some way of determining when I’ve done a water change, and how much water I’ve changed. I got the feeling that random variation (noise) in the data from the sensor could throw off whatever method I use to compute all of this, so I needed to figure out exactly what the data looks like coming in to the computer, preferably saving it so I can test my algorithms against it without having to do a water change after each revision – that would be a heck of a lot worse than just waiting for the code to recompile.

So I fired up Arduino and Python did a water change, saving the raw data from the Ping))) sensor to a file. Without further ado, this is what a water change looks like to a computer:

Pretty cool, huh?  When I do a water change, I siphon water out of the tank into a 3 gallon bucket, and empty it a bucket at a time, until I’ve taken out as much as I like, and then I re-fill the think 3 gallons at a time.  You end up with the very visible “steps” on the graph.  While the data looks mostly consistent, you can see some wonkiness in some of the steps – which almost looks like thick lines.  The sensor isn’t quite reading the distance regularly in this case.  This looks to me like the kind of data which could really throw of my detection algorithm.  So I had the bright idea of taking a median of 5 samples for each data point and using that series for detection.  Here’s what a median-of-five graph looks like:

Median of Five Graph

Median of Five Graph

You should notice two things: 1) there are fewer datapoints by an order of five, because of the median, and 2) the curve is smoother.  I could prove this by computing the standard deviation on some of those trouble spots from above, but I don’t think it’s necessary: it’s plain to see when graphed.  The data could still be better though – look at the jaggies around 100.  We’ve got plenty of data, so we should be able to create a very smooth line and still have enough resolution to see each step, etc.

I’ll spare you all the gory details, but suffice it to say that the larger the median used, the better.  A median of 10 was better, but still not good.  A median of 15 was nearly perfect, but there was still just a little weirdness.  A median of 20 was perfect:

Median of 20 graph

Median of 20 graph

That’s more like it.  We’ve still got enough data there to see the water change in detail, but smoothed out all the ugliness that could throw off the computer.  Cool stuff.

More details on the detection algorithm’s implementation to follow.  Charts generated with my new favorite tool, Python.

Tags: , , , , ,

Leave a Reply