In [ ]:
from racepi_database_handler import *
from racepi_can_decoder import *
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from datetime import datetime
from collections import defaultdict
from ipywidgets import interact
import numpy as np
import math

from bokeh.io import push_notebook, show, output_notebook
from bokeh.plotting import figure
output_notebook()

In [49]:
def calc_gear_difference(trimmed_data):
    CLIP_SPEED = 26.3754 # 59 mph
    added_time = 0.0
    for p in trimmed_data:
        if p[2] > CLIP_SPEED:
            speed_ratio = CLIP_SPEED/p[2] 
            new_time = p[1] / speed_ratio
            added_time += new_time - p[1]
            p[5] = CLIP_SPEED
        p[6] = added_time
    return added_time

In [14]:
def flatten_data(data):
    can_data = [x for x in data.get('can') if x.arbitration_id == 0x80]

    tps = [[x.timestamp, focus_rs_tps_converter.convert_frame(CanFrame("999", x.msg))] for x in can_data]
    gps = data.get('gps')

    def current_tps_value(timestamp, t, ti):
        try:
            while t[0] < timestamp:
                t = ti.__next__()
        except StopIteration:
            pass
        return t
    
    flat_data = []
    t = tps[0]
    ti = iter(tps)
    for i, g in enumerate(gps):
        delta_speed=0.0
        time_delta=0.0
        if i>0:
            delta_speed = g.speed-gps[i-1].speed
            time_delta = g.timestamp-gps[i-1].timestamp
        t = current_tps_value(g.timestamp, t, ti)
        p = [g.timestamp, time_delta, g.speed, delta_speed, t[1], g.speed, 0.0]
        flat_data.append(p)
    return flat_data

def clip_flat_data(flat_data):
    TPS_MAX_THRESHOLD=65.0

    ###############################################
    # find start and end times of data and trim off

    # mark start by first time at significant speed
    start = 0
    while start < len(flat_data) and flat_data[start][2] < 10:
        start += 1

    # mark the end of the run as the last MAX TPS
    end = len(flat_data)-1
    while end>start and flat_data[end][4] < TPS_MAX_THRESHOLD:
        end -= 1;

    trimmed_data = flat_data[start:end]
    return trimmed_data

In [36]:
def get_session_data(db, session_id):
    data = defaultdict(list)
    data['gps'] = db.query(GPSData).filter(GPSData.session_id == session_id).order_by(GPSData.timestamp).all()
    data['can'] = db.query(CANData).filter(CANData.session_id == session_id).order_by(CANData.timestamp).all()
    data['imu'] = db.query(IMUData).filter(IMUData.session_id == session_id).order_by(IMUData.timestamp).all()
    return data

def load_dbfile(filename):
    engine = create_engine('sqlite:///' + filename)
    Base.metadata.bind = engine
    sm = sessionmaker()
    sm.bind = engine
    s = sm()
    return s

s = load_dbfile("/home/donour/test.db")
si = s.query(SessionInfo).filter(SessionInfo.max_speed >20).filter(SessionInfo.num_data_samples > 7000).all()

In [56]:
def plot(td, name):
    p = figure(title=str(name), plot_height=300, plot_width=900)
    x = [x[0]-td[0][0] for x in td]
    r1 = p.line(x, [x[2]*2.23694 for x in td], color="#2222aa", line_width=1, legend="Speed")
    r1 = p.line(x, [x[5]*2.23694 for x in td], color="#aa2222", line_width=1, legend="New Speed")
    r1 = p.line(x, [x[6]*100 for x in td], color="#aaaa22", line_width=1, legend="Time Lost (1/100)")
    r1 = p.line(x, [x[4] for x in td], color="#22aa22", line_width=1, legend="TPS")
    show(p, notebook_handle=True)

def process_session(session):    
    session_date = datetime.fromtimestamp(session.start_time_utc)
    data = get_session_data(s, session.session_id)
    fd = flatten_data(data)
    td = clip_flat_data(fd)
    v = calc_gear_difference(td)
    #plot(td, session_date)
    if v > 0.01:
        print(v, session_date, session.session_id)

for session in si[-11:-8]:
    process_session(session)

for session in si:
    #if session.session_id == "d9367540-9255-11e7-8aae-b827eb8021e9":
    process_session(session)


0.17543803161896718 2017-09-06 09:57:26.880405 56dffd80-930b-11e7-b17a-b827eb8021e9
0.09950102612987402 2017-09-06 10:04:42.813655 5abb0d54-930c-11e7-b17a-b827eb8021e9
0.22145490291763562 2017-09-06 10:32:31.721642 3d7fbd4e-9310-11e7-b64c-b827eb8021e9
0.04107057901176771 2017-07-03 09:07:17.770438 906e2854-5ff0-11e7-ac41-b827eb8021e9
0.015589244992468165 2017-07-03 09:30:13.535832 c47f0610-5ff3-11e7-ac41-b827eb8021e9
0.03172822701032213 2017-08-06 10:21:02.943766 81e11c88-7ab2-11e7-a812-b827eb8021e9
0.05619473683756072 2017-08-06 11:50:43.961730 0775d3b4-7abf-11e7-864d-b827eb8021e9
0.06273461805023839 2017-08-06 12:10:14.770670 c154d4e0-7ac1-11e7-864d-b827eb8021e9
0.06385867581589182 2017-08-06 12:31:38.638697 be968e80-7ac4-11e7-bc3b-b827eb8021e9
0.03904649030911239 2017-08-06 12:50:10.430994 5540b94e-7ac7-11e7-bc3b-b827eb8021e9
0.046453984678607424 2017-08-06 13:01:16.333618 e21be25c-7ac8-11e7-bc3b-b827eb8021e9
0.057081519305894585 2017-08-06 13:17:56.627831 36546b3a-7acb-11e7-bc3b-b827eb8021e9
0.04816078122412039 2017-08-19 11:58:00.323504 32eaa4a6-84f7-11e7-abe2-b827eb8021e9
0.014031224648636903 2017-08-19 12:16:17.020240 c0963cd2-84f9-11e7-abe2-b827eb8021e9
0.02280583475626749 2017-08-19 12:31:31.728153 e1d3b1e8-84fb-11e7-abe2-b827eb8021e9
0.08667094163297262 2017-08-19 12:45:22.338687 d0e50b5a-84fd-11e7-abe2-b827eb8021e9
0.09829304429760669 2017-08-19 15:00:27.659425 b00c5b5a-8510-11e7-a009-b827eb8021e9
0.1002432861533646 2017-08-19 15:18:24.070983 31a83f74-8513-11e7-a009-b827eb8021e9
0.10488557134340336 2017-08-19 15:31:31.834591 072c44b4-8515-11e7-a009-b827eb8021e9
0.02830243969402582 2017-09-02 11:19:55.059027 328f48d0-8ff2-11e7-a4b9-b827eb8021e9
0.12346388598592746 2017-09-02 11:27:44.396103 4a55dbd6-8ff3-11e7-a4b9-b827eb8021e9
0.06411886331637823 2017-09-02 11:30:47.560451 b77936c2-8ff3-11e7-a4b9-b827eb8021e9
0.012160728117267275 2017-09-02 13:55:03.137865 deabcdd6-9007-11e7-b2fd-b827eb8021e9
0.013546980972085829 2017-09-02 13:59:19.454479 7768a364-9008-11e7-b2fd-b827eb8021e9
0.026223909600248212 2017-09-02 14:03:18.639333 05f2b994-9009-11e7-b2fd-b827eb8021e9
0.05832788832462799 2017-09-02 16:29:29.560635 71d64cd4-901d-11e7-b640-b827eb8021e9
0.062639077710575 2017-09-02 16:33:20.260628 fb5134f6-901d-11e7-b640-b827eb8021e9
0.05031447038086677 2017-09-02 16:36:29.454085 6c217790-901e-11e7-b640-b827eb8021e9
0.03523639819008398 2017-09-02 16:40:16.850950 f3a0a998-901e-11e7-b640-b827eb8021e9
0.04943042991873246 2017-09-02 19:21:59.863140 8b21abda-9035-11e7-8d4c-b827eb8021e9
0.02944210758607113 2017-09-02 19:31:22.634703 da7fad52-9036-11e7-8d4c-b827eb8021e9
0.024160496262587036 2017-09-02 19:38:56.807269 e9444f68-9037-11e7-8d4c-b827eb8021e9
0.042921992937832304 2017-09-03 10:31:52.568900 a6df4f92-90b4-11e7-b6e0-b827eb8021e9
0.05727736196050289 2017-09-03 10:35:41.364035 2f463fd0-90b5-11e7-b6e0-b827eb8021e9
0.0646464532757166 2017-09-03 10:41:57.466606 0f6ae4f8-90b6-11e7-b6e0-b827eb8021e9
0.03352182887745017 2017-09-03 13:23:40.072566 a69f7eea-90cc-11e7-a147-b827eb8021e9
0.013894732914662158 2017-09-03 13:30:29.165966 9a745e14-90cd-11e7-a147-b827eb8021e9
0.020546454931040105 2017-09-05 10:56:12.765654 621a55cc-924a-11e7-ada8-b827eb8021e9
0.01135915578054807 2017-09-05 11:05:24.864815 ab27e986-924b-11e7-ada8-b827eb8021e9
0.013807130320754504 2017-09-05 11:36:38.566443 07f70d1e-9250-11e7-aff6-b827eb8021e9
0.09414702476486093 2017-09-05 11:44:08.871868 145da99a-9251-11e7-aff6-b827eb8021e9
0.11460268351661053 2017-09-05 12:18:17.061801 d9367540-9255-11e7-8aae-b827eb8021e9
0.020917956983121065 2017-09-05 12:25:34.758740 de0ed53e-9256-11e7-8aae-b827eb8021e9
0.03889863191509732 2017-09-05 12:32:28.113886 d4709930-9257-11e7-8aae-b827eb8021e9
0.14316438550197105 2017-09-06 09:23:24.354696 95745eec-9306-11e7-b17a-b827eb8021e9
0.04175981802783338 2017-09-06 09:30:41.869814 9a37c6a2-9307-11e7-b17a-b827eb8021e9
0.17543803161896718 2017-09-06 09:57:26.880405 56dffd80-930b-11e7-b17a-b827eb8021e9
0.09950102612987402 2017-09-06 10:04:42.813655 5abb0d54-930c-11e7-b17a-b827eb8021e9
0.22145490291763562 2017-09-06 10:32:31.721642 3d7fbd4e-9310-11e7-b64c-b827eb8021e9
0.06200869284965761 2017-09-06 10:39:24.431964 33719844-9311-11e7-b64c-b827eb8021e9

In [6]:
# for session in si:
#     session_date = datetime.fromtimestamp(session.start_time_utc)
#     data = get_session_data(s, session.session_id)
#     fd = flatten_data(data)    
#     v = calc_gear_difference(fd)
#     if v > 0.01:
#         print(v, session_date, session.session_id)
#     #print(session_date, session.session_id, session.duration)

In [ ]: