-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMirrorChoreo.py
83 lines (62 loc) · 2.22 KB
/
MirrorChoreo.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
import json
import os
field_width = 57.573 / 3.281
field_height = 26.417 / 3.281
choreoDirectory = "./src/main/deploy/choreo"
def load_traj(file_path):
with open(file_path, 'r') as f:
traj = json.load(f)
return traj
def mirror_snap_point(point):
#Mirror a point across the y centerline of the field making sure to rotate accordingly
point = {
"x": point["x"],
"y": field_height - point["y"],
"heading": -point["heading"],
"intervals": point["intervals"],
"split": point["split"],
"fixTranslation": point["fixTranslation"],
"fixHeading": point["fixHeading"],
"overrideIntervals": point["overrideIntervals"]
}
return point
def flipExpression(expression):
exp = expression["exp"]
val = expression["val"]
#add - to the exp value if it is not already there
if exp[0] != "-":
exp = "-" + exp
else:
exp = exp[1:]
val = -val
return {
"exp": exp,
"val": val
}
def mirror_param_point(point):
pYVal = field_height - point["y"]["val"]
pYExp = str(pYVal) + " m"
point["y"]["val"] = pYVal
point["y"]["exp"] = pYExp
pHeadingVal = -point["heading"]["val"]
pHeadingExp = str(pHeadingVal) + " rad"
point["heading"]["val"] = pHeadingVal
point["heading"]["exp"] = pHeadingExp
return point
def main():
choreoFiles = os.listdir(choreoDirectory)
#Find only .traj files that do not contain the word "mirrored"
trajFiles = [file for file in choreoFiles if file.endswith(".traj") and "mirrored" not in file and "Center" not in file and "Right" not in file]
for file in trajFiles:
traj = load_traj(choreoDirectory + "\\" + file)
snapshot_waypoints = traj["snapshot"]["waypoints"]
param_waypoints = traj["params"]["waypoints"]
for i, point in enumerate(snapshot_waypoints):
snapshot_waypoints[i] = mirror_snap_point(point)
for i, point in enumerate(param_waypoints):
param_waypoints[i] = mirror_param_point(point)
#save the mirrored traj
with open(choreoDirectory + "\\" + "mirrored_" + file, 'w') as f:
json.dump(traj, f, indent=4)
if __name__ == '__main__':
main()