-
Notifications
You must be signed in to change notification settings - Fork 9
/
utils.py
80 lines (62 loc) · 2.27 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
#!/usr/bin/env python
from sensor_msgs.msg import LaserScan
from nav_msgs.msg import Odometry
from tf.transformations import euler_from_quaternion, quaternion_from_euler
import numpy as np
def lidar_scan(msgScan):
"""
Convert LaserScan msg to array
"""
distances = np.array([])
angles = np.array([])
information = np.array([])
for i in range(len(msgScan.ranges)):
# angle calculation
ang = i * msgScan.angle_increment
# distance calculation
if ( msgScan.ranges[i] > msgScan.range_max ):
dist = msgScan.range_max
elif ( msgScan.ranges[i] < msgScan.range_min ):
dist = msgScan.range_min
else:
dist = msgScan.ranges[i]
# smaller the distance, bigger the information (measurement is more confident)
inf = ((msgScan.range_max - dist) / msgScan.range_max) ** 2
distances = np.append(distances, dist)
angles = np.append(angles, ang)
information = np.append(information, inf)
# distances in [m], angles in [radians], information [0-1]
return ( distances, angles, information )
def lidar_scan_xy(distances, angles, x_odom, y_odom, theta_odom):
"""
Lidar measurements in X-Y plane
"""
distances_x = np.array([])
distances_y = np.array([])
for (dist, ang) in zip(distances, angles):
distances_x = np.append(distances_x, x_odom + dist * np.cos(ang + theta_odom))
distances_y = np.append(distances_y, y_odom + dist * np.sin(ang + theta_odom))
return (distances_x, distances_y)
def transform_orientation(orientation_q):
"""
Transform theta to [radians] from [quaternion orientation]
"""
orientation_list = [ orientation_q.x, orientation_q.y, orientation_q.z, orientation_q.w]
(roll, pitch, yaw) = euler_from_quaternion(orientation_list)
if yaw < 0:
yaw = 2 * np.pi + yaw # 0->360 degrees >> 0->2pi
return yaw
def get_odom_orientation(msgOdom):
""""
Get theta from Odometry msg in [radians]
"""
orientation_q = msgOdom.pose.pose.orientation
theta = transform_orientation(orientation_q)
return theta
def get_odom_position(msgOdom):
"""
Get (x,y) coordinates from Odometry msg in [m]
"""
x = msgOdom.pose.pose.position.x
y = msgOdom.pose.pose.position.y
return (x, y)