forked from djhedges/exit_speed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlap_lib.py
executable file
·128 lines (107 loc) · 4.05 KB
/
lap_lib.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
#!/usr/bin/python3
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=anomalous-backslash-in-string
"""Functions related to lap time calculations.
point_c |\
| \
| \
| \
|____\______ start/finish
| /
| /
| /
|B/
point_b |/
|
|
|
|
|
|
|
|
point_a |
"""
# pylint: enable=anomalous-backslash-in-string
import common_lib
import gps_pb2
import math
def GetPriorUniquePoint(lap: gps_pb2.Lap,
point_c: gps_pb2.Point) -> gps_pb2.Point:
"""Avoids a divsion by zero if the two points have the same time.
Older logged data had multiple points at the same time.
"""
index = -1
point = lap.points[-1]
while point.time.ToNanoseconds() == point_c.time.ToNanoseconds():
index -= 1
point = lap.points[index]
return point
def SolvePointBAngle(point_b, point_c) -> float:
"""Returns the angle of B."""
dist_b_c = common_lib.PointDelta(point_b, point_c)
# cos(B) = (c² + a² - b²)/2ca https://rb.gy/pgi7zm
a = point_b.start_finish_distance
b = point_c.start_finish_distance
c = dist_b_c
return math.degrees(math.acos((c**2 + a**2 - b**2)/(2*c*a)))
def CalcAcceleration(point_b: gps_pb2.Point, point_c: gps_pb2.Point) -> float:
"""a = Δv/Δt"""
return (((point_b.speed - point_c.speed) /
(point_b.time.ToNanoseconds() - point_c.time.ToNanoseconds())) /
1e-09) # Nanoseconds > Seconds.
def PerpendicularDistanceToFinish(point_b_angle: float,
point_b: gps_pb2.Point) -> float:
"""
cos(B) = Adjacent / Hypotenuse
https://www.mathsisfun.com/algebra/trig-finding-side-right-triangle.html
"""
return math.cos(math.radians(point_b_angle)) * point_b.start_finish_distance
def SolveTimeToCrossFinish(point_b: gps_pb2.Point,
perp_dist_b: float,
accelration: float):
"""
https://physics.stackexchange.com/questions/134771/deriving-time-from-acceleration-displacement-and-initial-velocity
"""
sqrt = math.sqrt(point_b.speed ** 2 + 2 * accelration * perp_dist_b)
return (point_b.speed * -1 + sqrt) / accelration
def GetTimeDelta(first_point, last_point) -> float:
return last_point.time.ToNanoseconds() - first_point.time.ToNanoseconds()
def CalcTimeAfterFinish(lap: gps_pb2.Point) -> float:
"""Returns how many seconds between crossing start/finish and the last point.
This assumes the first/last points of a lap are just past start/finish.
"""
point_c = lap.points[-1]
point_b = GetPriorUniquePoint(lap, point_c)
point_b_angle = SolvePointBAngle(point_b, point_c)
accelration = CalcAcceleration(point_b, point_c)
perp_dist_b = PerpendicularDistanceToFinish(point_b_angle, point_b)
time_to_fin = SolveTimeToCrossFinish(point_b, perp_dist_b, accelration)
delta = GetTimeDelta(point_b, point_c)
return delta - time_to_fin
def CalcLastLapDuration(session: gps_pb2.Session) -> float:
"""Calculates the last lap duration for the given session."""
if len(session.laps) == 1:
first_point = session.laps[0].points[0]
last_point = session.laps[0].points[-1]
return GetTimeDelta(first_point, last_point)
prior_lap = session.laps[-2]
current_lap = session.laps[-1]
first_point = current_lap.points[0]
last_point = current_lap.points[-1]
delta = GetTimeDelta(first_point, last_point)
prior_after = CalcTimeAfterFinish(prior_lap)
current_after = CalcTimeAfterFinish(current_lap)
return int(delta - current_after * 1e9 + prior_after * 1e9)