import os import time import smbus import numpy as np import csv import serial import pynmea2 import warnings import pytz import datetime import tkinter as tk # from pynput import keyboard from tkinter import ttk from threading import Thread from imusensor.MPU9250 import MPU9250 from imusensor.filters import kalman from vpython import * result = 0 root = tk.Tk() root.geometry('300x200') root.resizable(False,False) root.title('Sarsıntı Butonu') def btn(arg): global root def btnclck(): global result if result == 0: result = 1 btn1.state(['pressed']) btn1['text'] = 'Sarsıntı Var' else : result = 0 btn1.state(['!pressed']) btn1['text'] = 'Sarsıntı Yok' btn1 = ttk.Button( root, text='Sarsıntı Yok', command=btnclck ) btn1.pack( ipadx=50, ipady=50, expand=True ) # scene.x=0 # scene.y=0 # scene.witdh=600 # scene.height=600 # scene.title='MPU9250' # # marrw = arrow(axis=vector(1,0,0), shaftwidth = 0.01, fixedwidth=1) # mlbl = label(text='acc', pos=marrw.axis) # xarrw = arrow(color=color.red, axis=vector(1,0,0), shaftwidth = 0.01, fixedwidth=1) # label(text='x', pos=xarrw.axis) # yarrw = arrow(color=color.green, axis=vector(0,1,0), shaftwidth = 0.01, fixedwidth=1) # label(text='y', pos=yarrw.axis) # zarrw = arrow(color=color.blue, axis=vector(0,0,1), shaftwidth = 0.01, fixedwidth=1) # label(text='z', pos=zarrw.axis) warnings.filterwarnings('ignore') tz = pytz.timezone("Turkey") # def on_press(key): # if key.char == ('d'): # global result # result = 1 # # def on_release(key): # if key.char == ('d'): # global result # result = 0 # # listener = keyboard.Listener( # on_press=on_press, # on_release=on_release) # listener.start() # def port_setup(port): # ser = serial.Serial(port, baudrate=9600, timeout=2) # return ser # def parseGPSdata(ser): # keywords = ["$GPRMC","$GPGGA"] # gps_data = ser.readline() # gps_data = gps_data.decode("utf-8") # # if len(gps_data) > 5: # if gps_data[0:6] in keywords: # gps_msg = pynmea2.parse(gps_data) # lat = gps_msg.latitude # lng = gps_msg.longitude # today = datetime.datetime.now(tz=pytz.utc) # if gps_msg.timestamp is not None: # localT = datetime.datetime(today.year, today.month, today.day, gps_msg.timestamp.hour, gps_msg.timestamp.minute, gps_msg.timestamp.second, tzinfo=pytz.utc) # date = localT.astimezone(tz).isoformat() # else: # date = today # return (date,lat,lng) # else: # return None # else: # return None def myMainLoop(): # gps_port = "/dev/serial0" # ser = port_setup(gps_port) address = 0x68 bus = smbus.SMBus(1) imu = MPU9250.MPU9250(bus, address) imu.begin() imu.loadCalibDataFromFile("/home/pi/Desktop/sarsinti/calib.json") sensorfusion = kalman.Kalman() imu.readSensor() imu.computeOrientation() sensorfusion.roll = imu.roll sensorfusion.pitch = imu.pitch sensorfusion.yaw = imu.yaw count = 0 currTime = time.time() #os.remove('/home/pi/Desktop/sarsinti/data.csv') f = open('/home/pi/Desktop/sarsinti/data.csv', 'w') writer = csv.writer(f) row = ['roll','pitch','date','lat','lon','result'] writer.writerow(row) f.close() while True: try: imu.readSensor() imu.computeOrientation() newTime = time.time() dt = newTime - currTime currTime = newTime sensorfusion.computeAndUpdateRollPitchYaw(imu.AccelVals[0], imu.AccelVals[1], imu.AccelVals[2], imu.GyroVals[0], imu.GyroVals[1], imu.GyroVals[2],\ imu.MagVals[0], imu.MagVals[1], imu.MagVals[2], dt) # marrw.axis=vector(imu.AccelVals[1], imu.AccelVals[0], imu.AccelVals[2]) # mlbl.text="{0} {1} {2}".format(imu.AccelVals[1], imu.AccelVals[0], imu.AccelVals[2]) # mlbl.pos=marrw.axis # gps_coords = parseGPSdata(ser) row = [sensorfusion.roll, sensorfusion.pitch, result] f = open('/home/pi/Desktop/sarsinti/data.csv', 'a') writer = csv.writer(f) writer.writerow(row) f.close() ##print(imu.roll, imu.pitch, imu.yaw) print("KalmanRoll:{0} KalmanPitch:{1} KalmanYaw:{2} Result: {3}".format(sensorfusion.roll, sensorfusion.pitch, sensorfusion.yaw, result)) # if gps_coords is not None: # row = [sensorfusion.roll, sensorfusion.pitch, gps_coords[0], gps_coords[1], gps_coords[2], result] # f = open('/home/pi/Desktop/sarsinti/data.csv', 'a') # writer = csv.writer(f) # writer.writerow(row) # f.close() ##print(imu.roll, imu.pitch, imu.yaw) # print("KalmanRoll:{0} KalmanPitch:{1} KalmanYaw:{2} DateUTC: {3} GPSLat: {4} GPSLon: {5} Result: {6}".format(sensorfusion.roll, sensorfusion.pitch, sensorfusion.yaw, gps_coords[0], gps_coords[1], gps_coords[2], result)) time.sleep(0.01) except KeyboardInterrupt as e: f.close() break if __name__ == "__main__": mthrd = Thread(target = myMainLoop) bthrd = Thread(target = btn, args=(12,)) mthrd.start() bthrd.start() root.mainloop()