1
Fork 0

merge mean filtering from viz branch

This commit is contained in:
juliangaal 2019-04-24 15:46:33 +02:00
parent 2ed70201e5
commit 7cf5883eac
13 changed files with 0 additions and 11742 deletions

View file

@ -1,32 +0,0 @@
Python script to visualize data, e.g. roll and pitch, used for testing and [demonstration purposes]()
Accepted file format from data recording, e.g.
```
1,2
2,3
3,6
4,9
5,4
6,7
7,7
8,4
9,3
10,7
```
"rate" defines the number of steps from one data point to the next. I.e. if the sensor pumps out 100 measurements a second, it takes way too long to visualize like this. Adjust rate to e.g. 25 to do 25% faster run:
```
$ python3 viz.py 25
```
For requirements, see [requirements.txt](requirements.txt)
On linux, you need to install `tkinter` with
```
$ sudo apt-get install python3-tk
```
If you're having issues with matplotlib, install with apt
```
$ sudo apt-get install python3-matplotlib
```

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -1,59 +0,0 @@
#!/usr/bin/env python3
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def plot(filename: str, filename_2: str, num_data_points: int):
df_1 = pd.read_csv(filename, delimiter = ',', names=['roll', 'pitch'])
df_2 = pd.read_csv(filename_2, delimiter = ',', names=['roll', 'pitch'])
n = num_data_points
y = df_1['roll'].to_list()
x = np.linspace(0, n, n)
plt.plot(x, y[:n], label='roll raw: var ' + str(np.var(y)), color='black')
var_1 = np.var(y)
y = df_2['roll'].to_list()
x = np.linspace(0, n, n)
plt.plot(x, y[:n], label='roll calibrated: var ' + str(np.var(y)), color='red')
var_2 = np.var(y)
plt.ylim([-0.1, 0.1])
plt.legend()
plt.show()
if var_1 < var_2:
print("Variance of {} is smaller: {}".format(filename, var_1))
else:
print("Variance of {} is smaller: {}".format(filename_2, var_2))
y = df_1['pitch'].to_list()
x = np.linspace(0, n, n)
plt.plot(x, y[:n], label='pitch raw: var ' + str(np.var(y)), color='black')
var_1 = np.var(y)
y = df_2['pitch'].to_list()
x = np.linspace(0, n, n)
plt.plot(x, y[:n], label='pitch calibrated: var ' + str(np.var(y)), color='red')
var_2 = np.var(y)
plt.ylim([-0.1, 0.1])
plt.legend()
plt.show()
if var_1 < var_2:
print("Variance of {} is smaller: {}".format(filename, var_1))
else:
print("Variance of {} is smaller: {}".format(filename_2, var_2))
file_1 = sys.argv[1]
file_1 = sys.argv[1]
file_2 = sys.argv[2]
n = int(sys.argv[3])
plot(file_1, file_2, n)

View file

@ -1,80 +0,0 @@
#!/usr/bin/env python3
import sys
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def plot(filename: str, filename_2: str, num_data_points: int):
df_1 = pd.read_csv(filename, delimiter = ',', names=['x', 'y', 'z'])
df_2 = pd.read_csv(filename_2, delimiter = ',', names=['x', 'y', 'z'])
n = num_data_points
y = df_1['x'].to_list()
x = np.linspace(0, n, n)
plt.plot(x, y[:n], label='x raw: var ' + str(np.var(y)), color='black')
var_1 = np.var(y)
y = df_2['x'].to_list()
x = np.linspace(0, n, n)
plt.plot(x, y[:n], label='x calib: var ' + str(np.var(y)), color='red')
var_2 = np.var(y)
plt.ylim([-0.1, 0.1])
plt.legend()
plt.show()
if var_1 < var_2:
print("Variance of {} is smaller: {}".format(filename, var_1))
else:
print("Variance of {} is smaller: {}".format(filename_2, var_2))
y = df_1['y'].to_list()
x = np.linspace(0, n, n)
plt.plot(x, y[:n], label='y raw: var ' + str(np.var(y)), color='black')
var_1 = np.var(y)
y = df_2['y'].to_list()
x = np.linspace(0, n, n)
plt.plot(x, y[:n], label='y calib: var ' + str(np.var(y)), color='red')
var_2 = np.var(y)
plt.ylim([-0.1, 0.1])
plt.legend()
plt.show()
if var_1 < var_2:
print("Variance of {} is smaller: {}".format(filename, var_1))
else:
print("Variance of {} is smaller: {}".format(filename_2, var_2))
y = df_1['z'].to_list()
x = np.linspace(0, n, n)
plt.plot(x, y[:n], label='z raw: var ' + str(np.var(y)), color='black')
var_1 = np.var(y)
y = df_2['z'].to_list()
x = np.linspace(0, n, n)
plt.plot(x, y[:n], label='z calib: var ' + str(np.var(y)), color='red')
var_2 = np.var(y)
plt.ylim([-0.1, 0.1])
plt.legend()
plt.show()
if var_1 < var_2:
print("Variance of {} is smaller: {}".format(filename, var_1))
else:
print("Variance of {} is smaller: {}".format(filename_2, var_2))
file_1 = sys.argv[1]
file_1 = sys.argv[1]
file_1 = sys.argv[1]
file_2 = sys.argv[2]
n = int(sys.argv[3])
plot(file_1, file_2, n)

View file

@ -1,3 +0,0 @@
numpy
pandas
matplotlib

File diff suppressed because it is too large Load diff

View file

@ -1,95 +0,0 @@
#!/usr/bin/env python3
import numpy as np
import sys
import math
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
import time
fig = plt.figure()
iteration = 0
ax1 = plt.subplot(211)
ax2 = plt.subplot(212)
class RegrMagic(object):
"""Mock for function Regr_magic()
"""
def __init__(self):
raw_data = open("sample.txt","r").read()
self.angles = raw_data.split('\n')
self.num_data = len(self.angles)
self.run_index = 0
self.rate = 1
if len(sys.argv) > 1 and len(sys.argv) < 3:
self.rate = int(sys.argv[1])
print("Rate set to", self.rate)
def __call__(self) -> float:
if self.run_index < self.num_data-1:
x,y = self.angles[self.run_index].split(',')
self.run_index += 100
return float(x), float(y)
else:
sys.exit(0)
regr_magic = RegrMagic()
def frames():
while True:
yield regr_magic()
def get_start_end(point: (float, float), length: float, angle: float):
x, y = point
# find the end point
endx = x + length * math.cos(math.radians(angle))
endy = y + length * math.sin(math.radians(angle))
# find the start point
startx = x - length * math.cos(math.radians(angle))
starty = y - length * math.sin(math.radians(angle))
return (endx, endy, startx, starty)
def plot_line(args):
'''
angle1 - Angle you want your end point at in degrees.
length - Length of the line you want to plot.
Will plot the line on a 5 x 5 plot.
'''
global iteration
# unpack the first point
x, y = (2., 0.)
length = 5.0
roll_angle = args[0] * 180.0 / math.pi
pitch_angle = args[1] * 180.0 / math.pi
print(roll_angle, pitch_angle)
ax1.clear()
ax2.clear()
ax1.set_ylim([-2, 2]) # set the bounds to be -20, 20
ax2.set_ylim([-2, 2]) # set the bounds to be -20, 20
ax1.set_xlim([0, 4])
ax2.set_xlim([0, 4])
# plot the points
endx, endy, startx, starty = get_start_end((x,y), length, roll_angle)
ax1.plot([startx, endx], [starty, endy], c='black', linewidth='8.0')
endx, endy, startx, starty = get_start_end((x,y), length, pitch_angle)
ax2.plot([startx, endx], [starty, endy], c='black', linewidth='8.0')
ani = animation.FuncAnimation(fig, plot_line, frames=frames, interval=10)
# save the animation as an mp4. This requires ffmpeg or mencoder to be
# installed. The extra_args ensure that the x264 codec is used, so that
# the video can be embedded in html5. You may need to adjust this for
# your system: for more information, see
# http://matplotlib.sourceforge.net/api/animation_api.html
#ani.save('double_pendulum.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
plt.legend()
plt.show()

View file

@ -1,2 +0,0 @@
[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"

View file

@ -1,10 +0,0 @@
[package]
name = "viz"
version = "0.1.0"
authors = ["Julian Gaal <juliangaal@protonmail.com>"]
edition = "2018"
[dependencies]
mpu6050 = { path = "../../" }
i2cdev = "0.4.1"
linux-embedded-hal = "0.2.2"

View file

@ -1,64 +0,0 @@
use mpu6050::*;
use linux_embedded_hal::{I2cdev, Delay};
use i2cdev::linux::LinuxI2CError;
use std::io::prelude::*;
use std::fs::File;
use std::path::Path;
use std::error::Error;
fn new_file(name: &str) -> File {
let path = Path::new(name);
let display = path.display();
let _file = match File::create(&path) {
Err(why) => panic!("couldn't create {}: {}",
display,
why.description()),
Ok(file) => return file,
};
}
fn write_x_to(file: &mut File, content: String) {
match file.write_all(content.as_bytes()) {
Err(why) => {
println!("couldn't write to file: {}", why.description());
},
Ok(_) => {},
}
}
fn main() -> Result<(), Mpu6050Error<LinuxI2CError>> {
let i2c = I2cdev::new("/dev/i2c-1")
.map_err(Mpu6050Error::I2c)?;
let delay = Delay;
let mut mpu = Mpu6050::new(i2c, delay);
mpu.init()?;
mpu.soft_calib(200)?;
mpu.calc_variance(200)?;
println!("Calculated variance: {:?}", mpu.get_variance().unwrap());
let mut acc_file = new_file("acc_data.txt");
let mut gyro_file = new_file("gyro_data.txt");
let mut temp_file = new_file("temp_data.txt");
let mut angles_file = new_file("angles_data.txt");
loop {
// get roll and pitch estimate
let acc = mpu.get_acc_angles()?;
write_x_to(&mut angles_file, format!("{},{}\n", acc.0, acc.1));
// get temp
let temp = mpu.get_temp()?;
write_x_to(&mut temp_file, format!("{}\n", temp));
// get gyro data, scaled with sensitivity
let gyro = mpu.get_gyro()?;
write_x_to(&mut gyro_file, format!("{},{},{}\n", gyro.0, gyro.1, gyro.2));
// get accelerometer data, scaled with sensitivity
let acc = mpu.get_acc()?;
write_x_to(&mut acc_file, format!("{},{},{}\n", acc.0, acc.1, acc.2));
}
}