Page 1 of 1

Speed and Distance Measuring

Posted: Sat Dec 05, 2015 2:32 pm
by Kratos
I looking for help to concoct a program that will do the following things with the sense hat:
1. Output speed in MPH
2. Output distance in feet

My code (not accurate):

Code: Select all

#Description :This program will print your speed and distance usng the sense hat
#Author: Kratos
#Version: 12.5.15
#Notes: Output is in meters and meters per second
# Speed = distance / time
#distance = 0.5 * acceleration * (time * time)

import time
from sense_hat import SenseHat

sense = SenseHat()
green = 0, 255, 0
print_on_hat = False #set to True if you want the values dislpayed on the hat
ti = time.time()
init_speed = 0

while True:
    new_time = time.time()
    raw = sense.get_accelerometer_raw()
    y = raw['y']
    distance = (init_speed + 0.5*y*((ti - time.time())*(ti - time.time()))*3.2808)
    speed = distance / new_time
    init_speed = speed
    if print_on_hat == False:
        print('Your distance: %s' %distance)
        print('Your speed: %s' %speed)
    if print_on_hat == True:
        sense.show_message('D: %s' %distance, text_colour=green)
        sense.show_message('S: %s' %speed, text_colour=green)
Kratos

Re: Speed and Distance Measuring

Posted: Sat Dec 05, 2015 10:33 pm
by TideMan
Unfortunately, calculating velocity and displacement from acceleration is fraught with difficulty, not because of the mathematics which is straightforward, but because in the process of numerically integrating a noisy signal, spurious low frequency noise gets amplified. The effect of this is that the displacement can wander all over the place or head off to + or - infinity.

If you sample the accelerations from the sense hat when it is just sitting at rest and plot them, you will see how noisy the signal is. I've attached an example. The low frequency noise is quite evident with a peak at 2 sec dropping to a trough at 4 s. This spurious noise would be amplified by integration, doubly so for displacement..

The solution is to high-pass filter the accelerations before integrating to get velocity, then to high-pass filter the velocities before integrating to get displacement. Of course this is pretty difficult in real time. IMHO, the best way to do this high-pass filtering is by orthogonal wavelets - there's a toolbox for Python called pywt - but there are lots of simpler ways.

Re: Speed and Distance Measuring

Posted: Sat Dec 05, 2015 11:54 pm
by Kratos
Interesting...thanks for the graph! I posted my code because I thought it was correct, but the values kept wandering.

Kratos

Re: Speed and Distance Measuring

Posted: Tue Jun 14, 2016 10:44 am
by masalinas
Hello Kratos,

Could you resolve the problem of applying this filters inside your code to obtain the velocity from acceleration in your raspberry??

Regards.

Re: Speed and Distance Measuring

Posted: Sun Mar 18, 2018 1:15 pm
by ctatos
Hi, did anyone manage to filter the accelerometer to measure speed? My values are also wandering off... :cry: