-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
92 lines (80 loc) · 2.18 KB
/
utils.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
import time
import os
import numpy as np
from matplotlib import pyplot as plt
def normalize_angles(th):
return np.arctan2(np.sin(th), np.cos(th))
def tic():
return time.time()
def toc(tic):
return time.time() - tic
TIME = -1
def delay(HZ):
global TIME
PREV_TIME = TIME
TIME = tic()
elapsed = TIME - PREV_TIME
dt = max(0, 1/HZ - elapsed)
time.sleep(dt)
def save_plot(name, t, data):
plt.figure()
if type(data) == dict:
for k, v in data.items():
plt.plot(t, v, label=k)
plt.legend()
else:
plt.plot(t, data)
if not os.path.exists('plots'):
os.makedirs('plots')
plt.savefig('plots/%s.png' % name)
print('save plots/%s.png' % name)
def save_traj(xs, ys):
plt.figure()
plt.plot(xs, ys)
plt.axis('square')
if not os.path.exists('plots'):
os.makedirs('plots')
plt.savefig('plots/traj.png')
print('save plots/traj.png')
def cap_value_at(d, big):
if abs(d) > big:
d = np.sign(d) * big
return d
def abs_to_rel_pose(rob_pose, rel_pose):
"""
rob_pose wrt to world
rel_pose wrt to world
return rel_pose wrt to rob
"""
T = np.array(
[
[np.cos(rob_pose[2]), -np.sin(rob_pose[2]), rob_pose[0]],
[np.sin(rob_pose[2]), np.cos(rob_pose[2]), rob_pose[1]],
[0, 0, 1],
]
)
xy = np.linalg.inv(T) @ np.array([rel_pose[0], rel_pose[1], 1])
th = normalize_angles(rel_pose[2] - rob_pose[2])
rel_pose = np.array([float(xy[0]), float(xy[1]), th])
return rel_pose
def rel_to_abs_pose(rob_pose, rel_pose):
"""
rob_pose wrt to world
rel_pose wrt to robot
return rel_pose wrt to world
"""
T = np.array(
[
[np.cos(rob_pose[2]), -np.sin(rob_pose[2]), rob_pose[0]],
[np.sin(rob_pose[2]), np.cos(rob_pose[2]), rob_pose[1]],
[0, 0, 1],
]
)
xy = T @ np.array([rel_pose[0], rel_pose[1], 1])
th = normalize_angles(rel_pose[2] + rob_pose[2])
abs_pose = np.array([float(xy[0]), float(xy[1]), th])
return abs_pose
def angle_between(dxdy):
return np.arctan2(dxdy[1], dxdy[0])
def to_rad(deg):
return deg / 180 * np.pi