About Me

My name is Ricardo Pascal, I'm a developer and sysadmin who lives in Florianopolis/Brazil. I enjoy work with projects related to Linux, Python, Science, Web, Django and Network.

g+ I finally found a place with plenty of space to fly my f330, now I just have to lose the fear of flying... | Vaga de emprego para programador python na lett.com.br O serviço que eles oferecem parece interessante... | "The Horribly Slow Murderer with the Extremely Inefficient Weapon", it's number one in my top 10 worst... | Challenge accepted! One per night til the end of year ( minus weekends )  | +Detectify  i have to say, well done Sirs ! " tl;dr: We uploaded a malicious XML to one of Google’s... | A drug used to treat Cytomegalovirus infections can increase almost four times the survive rate of people... | Physics FTW! | Just to clarify!  | It seems that Brazil actually "chose" the best. | Good tips for dev teams. | it's like our entire body is a baby's soft spot! | Next time I will try with a lighter battery ( < 330g )  | Testing my F330 with multiwii crius | Anyone here who owns a LG 29" 29EA73 know if the full resolution(2560x1080) can be achieved in Ubuntu... | Particularly, I think(as audience) that less slides and more whiteboard is better than no slides. | It would be awesome have something like that in multiwii. #quadrotor | LMFAO!  This is sooooo true! #ComputerNerdProblems   | Good news everyone! Galaxy note 10.1 (n8000) will be updated to android 4.4 http://www.samsungmobi... | In Brazil, cops don't put people in jail. They prefer to beat you in hope to scare you away. | Have you ever wondered how much time you could save by using a 24 cores server to do a #blast that is... |

Real-time plotting of android accelerometer data

What is a accelerometer?

It's a device aimed to measure acceleration. Usually it works analyzing the gravity force against a suspended mass. Or in the case of our smartphone, it analyze what is called seismic mass. Further explanation about accelerometers can be get here and here.

What are the uses of a accelerometer?

In smartphones, the most common use is to set the screen orientation or to achieve movimentation in games. Outside the phone world, it's widely used in stabilization systems.

What I'm trying to accomplish?

The idea is to plot the accelerometer data from a smartphone(android) using python. When you run the code, you should see something like this at your computer:

Accelerometer chart

The code

import android
import time
import matplotlib.pyplot as plt

class Chart(object):

    def __init__(self):
        self.senses = 0
        self.sb, self.xbuf, self.ybuf, self.zbuf = 0, 0, 0, 0
        self.fig = plt.figure(1, figsize=(19, 6))
        plt.ylim([-20, 20])
        plt.xlim([0, 300])

    def plot(self, x, y, z):
        self.senses += 1
        plt.plot([self.sb, self.senses], [self.xbuf, x], color='r', label='X')
        plt.plot([self.sb, self.senses], [self.ybuf, y], color='g', label='Y')
        plt.plot([self.sb, self.senses], [self.zbuf, z], color='b', label='Z')
        self.sb, self.xbuf, self.ybuf, self.zbuf = self.senses, x, y, z

# Interval between sensing
dt = 100

# Number of senses
TotalToSense = 250

# Connect to android and start sensing
android_address = ("", 9999)
droid = android.Android(android_address)
droid.startSensingTimed(2, dt)

if __name__ == "__main__":
    amountSensed = 0
    chart = Chart()
    while amountSensed <= TotalToSense:
        x, y, z = droid.sensorsReadAccelerometer().result
        if x and y and z:
            x, y, z = round(x, 1), round(y, 1), round(z, 1)
            chart.plot(x, y, z)
            time.sleep(dt / 1000.0)
            amountSensed += 1

The explanation

class Chart().__init__()

1) Start the count/buffers variables; 2) plt.ion() enables interactive mode, which will allow the plot to be updated in real time; 3) Create the chart figure, and define the scale of axis Y/X;

class Chart().plot()

1) Plot the data from accelerometer; 2) Draw the chart; 3) Update the buffers;

if _name_ == __main__

1) receive data from android accelerometer; 2) round to just one decimal; 3) call the plotting stuff 4) delay before the next sensing;


  • The data is really noisy, if you put the phone on a table and play drum with your fingers, the graph will gone wild. So it's a good idea filter it to avoid trouble in a real application.

  • The "buffers" vars is a little hack to plot the data, I think it's not really necessary, but since my knowledge about maplotlib is spartan, I was not able to avoid that.

by Ricardo Pascal on Oct 25, 2012