-
Notifications
You must be signed in to change notification settings - Fork 0
/
saving MAG data for calib.py
69 lines (55 loc) · 1.56 KB
/
saving MAG data for calib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#from vpython import *
import numpy as np
import serial
import time
import sqlite3
import random
from itertools import count
from matplotlib.animation import FuncAnimation
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
ad=serial.Serial('COM6', 115200)
time.sleep(1)
tablename = 'magnetometer'
conn = sqlite3.connect('tablename.db')
c = conn.cursor()
def create_table():
try:
c.execute("CREATE TABLE IF NOT EXISTS tablename(x REAL, y REAL, z REAL)")
conn.commit()
except Exception as e:
print(str(e))
create_table()
def streaming_data():
while(True):
while (ad.inWaiting()==0):
pass
dataPacket = ad.readline()
dataPacket = str(dataPacket, 'utf-8')
splitPacket = dataPacket.split(",")
try:
x= float(splitPacket[0])
y= float(splitPacket[1])
z= float(splitPacket[2])
except:
x =np.nan
y=np.nan
z=np.nan
return x, y, z
def populatetable(x,y,z):
try:
c.execute("INSERT INTO tablename(x, y, z) VALUES (?, ?, ?)", (x, y, z))
conn.commit()
except:
print("testerro")
##Overwite question
delte_table = input("Do you want to append or overwrite table? Type 'o' for overwrite or type anything else to append: ")
if(delte_table=="o"):
c.execute("DELETE FROM tablename")
else:
print("appending")
c.execute("DELETE FROM tablename")
while True:
print(streaming_data())
populatetable(streaming_data()[0],streaming_data()[1],streaming_data()[2])