-
Notifications
You must be signed in to change notification settings - Fork 1
/
guibraketest.py
149 lines (118 loc) · 5.45 KB
/
guibraketest.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 14 10:15:10 2022
@author: RTB
"""
import tkinter
import tkinter.ttk
from collections import deque
import statistics
import constants
import math
import csv
#-brake test
#- initial speed, final speed, distance
#fdp
#'dist_traveled'
#fdp.speed
#fdp.brake
#consider deriving distance with: 0.5 * start speed
class GUIBraketest:
WAIT = 0
BRAKE = 1
DISPLAY = 2
firstrow = ['timestamp', 'brake%', 'speed', 'deaccel', 'long_g', 'distance', 'slipratioFL', 'slipratioFR', 'slipratioRL', 'slipratioRR']
def __init__(self, logger, *args, **kwargs):
self.logger = logger
self.initial_speed = 0
self.final_speed = 0
self.distance = 0
self.brake_time = 0
self.speed_var = tkinter.StringVar()
self.distance_var = tkinter.StringVar()
self.write_var = tkinter.IntVar()
self.write_var.set(0)
self.log_var = tkinter.IntVar()
self.log_var.set(0)
self.state = GUIBraketest.WAIT
self.x = 0
self.y = 0
self.z = 0
#timestamp, brake%, speed, deacceleration, longtitudal g, slipratioFL/FR/RL/RR, distance
self.brakedata = []
self.reset()
def writedata_to_csv(self):
with open('braking.csv', 'w', newline='') as rawcsv:
csvobject = csv.writer(rawcsv, delimiter='\t', quotechar='', quoting=csv.QUOTE_NONE)
csvobject.writerow(GUIBraketest.firstrow)
csvobject.writerows(self.brakedata)
self.logger.info(f"Written {len(self.brakedata)} rows to file")
def display(self):
self.speed_var.set(f'{self.initial_speed*3.6:6.1f} to {self.final_speed*3.6:6.1f}')
self.distance_var.set(f'{self.distance:6.1f} ({self.brake_time:4.0f}ms)')
def add_distance(self, x, y, z):
self.distance += math.sqrt((x - self.x)**2 + (y - self.y)**2 + (z - self.z)**2)
self.x = x
self.y = y
self.z = z
def add_datapoint(self, fdp):
row = [fdp.timestamp_ms - self.initial_timestamp,
fdp.brake/255.0,
fdp.speed,
fdp.acceleration_z,
fdp.acceleration_z/9.81,
self.distance,
fdp.tire_slip_ratio_FL,
fdp.tire_slip_ratio_FR,
fdp.tire_slip_ratio_RL,
fdp.tire_slip_ratio_RR ]
self.brakedata.append(row)
def update(self, fdp):
if self.state == GUIBraketest.WAIT and fdp.brake == 0 and fdp.speed > 3:
self.initial_speed = fdp.speed
self.x = fdp.position_x
self.y = fdp.position_y
self.z = fdp.position_z
elif self.state == GUIBraketest.WAIT and fdp.brake > 0 and fdp.speed > 3: #initial braking state
self.state = GUIBraketest.BRAKE
self.initial_timestamp = fdp.timestamp_ms
# self.logger.info(f"We braking {self.initial_speed*3.6:.1f}")
if self.state == GUIBraketest.BRAKE and not (fdp.brake == 0 or fdp.speed < 0.05): #steady braking state
self.add_distance(fdp.position_x, fdp.position_y, fdp.position_z)
self.add_datapoint(fdp)
elif self.state == GUIBraketest.BRAKE and (fdp.brake == 0 or fdp.speed < 0.05):
self.final_speed = fdp.speed
self.add_distance(fdp.position_x, fdp.position_y, fdp.position_z)
self.add_datapoint(fdp)
self.brake_time = fdp.timestamp_ms - self.initial_timestamp
if self.log_var.get() == 1:
self.logger.info(f"We braked. {self.initial_speed*3.6:.1f} {self.final_speed*3.6:.1f} {self.distance:.1f} ({self.brake_time:4.0f}ms)")
if self.write_var.get() == 1:
self.writedata_to_csv()
self.display()
self.state = GUIBraketest.WAIT
self.distance = 0
def set_canvas(self, frame):
self.frame = tkinter.Frame(frame, border=0, bg=constants.background_color, relief="groove",
highlightthickness=True, highlightcolor=constants.text_color)
opts = {'bg':constants.background_color, 'fg':constants.text_color}
tkinter.Label(self.frame, text="Brake\nSpeed (km/h)", **opts,
font=('Helvetica 15 bold')).grid(row=0, sticky='W', rowspan=2)
tkinter.Label(self.frame, textvariable=self.speed_var, **opts, justify=tkinter.LEFT, anchor="w", width=12,
font=('Helvetica 20 bold')).grid(row=2, sticky='W', columnspan=2)
tkinter.Label(self.frame, text="Brake distance (m)", **opts,
font=('Helvetica 15 bold')).grid(row=3, sticky='W', columnspan=2)
tkinter.Label(self.frame, textvariable=self.distance_var, **opts, justify=tkinter.LEFT, anchor="w", width=12,
font=('Helvetica 20 bold')).grid(row=4, sticky='W', columnspan=2)
tkinter.Checkbutton(self.frame, text='Write', variable=self.write_var, **opts).grid(row=0, column=1, sticky='W')
tkinter.Checkbutton(self.frame, text='Log', variable=self.log_var, **opts).grid(row=1, column=1, sticky='W')
def reset(self):
self.initial_speed = 0
self.final_speed = 0
self.distance = 0
self.brake_time = 0
self.x = 0
self.y = 0
self.z = 0
self.brakedata.clear()
self.display()