## Speed and Distance Measuring

Kratos
Posts: 394
Joined: Sun Apr 12, 2015 12:41 pm

### Speed and Distance Measuring

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:
if print_on_hat == True:
sense.show_message('D: %s' %distance, text_colour=green)
sense.show_message('S: %s' %speed, text_colour=green)
``````
Kratos
I have posted mostly with a Pi 2 running either Ubuntu MATE, or Raspbian.

TideMan
Posts: 213
Joined: Fri Jun 22, 2012 8:08 am
Location: ChCh, NZ

### Re: Speed and Distance Measuring

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.
Attachments
Vert Accel. Sense Hat at rest
AtRest.png (42.81 KiB) Viewed 6306 times

Kratos
Posts: 394
Joined: Sun Apr 12, 2015 12:41 pm

### Re: Speed and Distance Measuring

Interesting...thanks for the graph! I posted my code because I thought it was correct, but the values kept wandering.

Kratos
I have posted mostly with a Pi 2 running either Ubuntu MATE, or Raspbian.

masalinas
Posts: 4
Joined: Fri Jun 03, 2016 8:26 am

### Re: Speed and Distance Measuring

Hello Kratos,

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

Regards.

ctatos
Posts: 1
Joined: Sun Mar 18, 2018 1:12 pm

### Re: Speed and Distance Measuring

Hi, did anyone manage to filter the accelerometer to measure speed? My values are also wandering off...