2 """Translate equidistant path to equitemporal (isochronic) trajectory."""
3 from math import sin, atan, cos, tan, pi, acos
11 return (0 < x) - (x < 0)
15 return ((p2[0] - p1[0])**2 + (p2[1] - p1[1])**2)**0.5
22 def normalize_path(path):
28 while path[0] == path[1]:
33 def check_limits(x, max_x):
42 def set_max(x, max_x):
49 def lines_intersect(li1, li2):
58 deno = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4)
61 t = (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)
63 u = (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)
66 if t < 0 or t > 1 or u < 0 or u > 1:
68 return x1 + t * (x2 - x1), y1 + t * (y2 - y1)
71 def point_on_right_side_of(p, li):
78 if sgn((x3 - x1) * (y2 - y1) - (y3 - y1) * (x2 - x1)) < 0:
84 def min_angle_between(p1, pp, p2):
89 dot = d1x * d2x + d1y * d2y
90 d1 = (d1x * d1x + d1y * d1y)**0.5
91 d2 = (d2x * d2x + d2y * d2y)**0.5
92 delta = acos(dot / (d1 * d2))
93 return min(delta, pi - delta)
97 ctc = 11.2531 # this is guess resulting to max_wa ~= 35.99998076387121
101 df = 2.895 + 0.9 # this is guess
102 dr = 4.918 - df # length - df
104 max_sp = 3 / 3.6 # mps
107 mtr = ((ctc / 2)**2 - wb**2)**0.5 - ft/2
108 max_wa = atan(wb / mtr)
110 br = max_sp**2 / max_acc / 2 # braking path length
112 def __init__(self, x=0, y=0, h=0):
113 self.x = x # x, y is center of rear axle
117 self.wa = 0 # wheel angle
122 """Euclidean distance from self to pose in 2D."""
123 return ed(self.pose(), pose)
126 return (self.x, self.y, self.h, self.wa, self.sp, self.acc)
128 def poses_to(self, pose):
129 """Return tuple of poses toward pose.
131 see https://math.stackexchange.com/questions/1794943/how-to-calculate-the-distance-between-two-points-on-a-circle-in-degrees (error in answer, it's theta/2)
132 see https://math.stackexchange.com/questions/285866/calculating-circle-radius-from-two-points-on-circumference-for-game-movement
133 see https://en.wikipedia.org/wiki/Arc_length
139 while self.sp != 0 and d < s:
142 poses.append(self.pose())
147 self.sp += self.acc * DT
148 self.sp = check_limits(self.sp, self.max_sp)
150 if self.acc < 0 and self.sp < 0:
153 elif self.acc > 0 and self.sp > 0:
156 self.h += self.sp / self.wb * tan(self.wa) * DT
161 self.x += DT * self.sp * cos(self.h)
162 self.y += DT * self.sp * sin(self.h)
164 def brake_within(self, d):
166 self.acc = check_limits(-1 * bd * self.sp**2 / 2 / d, self.max_acc)
169 def wa_to(self, pose):
172 self.wa = self.max_wa
173 # s = self.mtr * asin(self.ed(pose) / 2 / self.mtr)
175 self.wa = -self.max_wa
176 # s = self.mtr * asin(self.ed(pose) / 2 / self.mtr)
181 def find_cusps(path):
184 for i in range(1, len(path) - 1):
186 if sgn(path[i-1][3]) != sgn(path[i+1][3]):
189 if sgn(path[i][3]) != sgn(path[i+1][3]) and path[i+1][3] != 0:
194 if __name__ == "__main__":
196 with open(f"orig-{PLAN_FILE}.json", "r") as f:
198 nnpath = list(plan["path"])
199 path = normalize_path(nnpath)
200 if PLAN_FILE in ["pa1", "pa3", "pe1"]:
203 if PLAN_FILE == "pa2":
204 path = path[:30] + path[32:]
205 if PLAN_FILE == "pe1":
207 if PLAN_FILE == "pe2":
208 path = path[:24] + path[27:45]
209 cusps = find_cusps(path)
210 c = BicycleCar(*path[0][:3])
212 c.acc = c.max_acc # start move forward
221 d += ed(path[j], path[j + 1])
223 if len(path) == 1: # last move
224 lpose = list(plan["ispath"][-1])
226 if PLAN_FILE.startswith("pe"):
230 c.brake_within(c.ed(lpose))
231 traj += c.poses_to(lpose)
236 traj.append(c.pose())
239 traj += c.poses_to(path[0])
242 while abs(c.sp - 0) > 1e-3:
244 traj.append(c.pose())
250 traj.append(c.pose())
251 c.acc = sgn(path[1][3]) * c.max_acc
254 traj.append(c.pose())
257 print(f"left {len(path)} waypoints")
258 plan["nnpath"] = nnpath
261 with open(f"{PLAN_FILE}.json", "w") as f:
262 json.dump(plan, f, indent="\t")
263 with open(f"{PLAN_FILE}.h", "w") as f:
264 f.write("#ifndef COMMANDER_COMMANDS_H\n")
265 f.write("#define COMMANDER_COMMANDS_H\n")
266 f.write(f"#define COMMANDS_COUNT {len(traj)}\n")
267 f.write("struct command { double wa; double sp; double acc; };\n")
268 f.write("struct command commands[] = {\n")
270 f.write(f" {{{to_deg(p[3])}, {p[4]}, {p[5]}}},\n")
272 f.write("#endif // COMMANDER_COMMANDS_H\n")