-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGlobalPathCarla2ROS.py
154 lines (110 loc) · 3.98 KB
/
GlobalPathCarla2ROS.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import rospy
from std_msgs.msg import String
from geometry_msgs.msg import Pose, PoseStamped, Point, Quaternion, Twist
from nav_msgs.msg import Path
from tf.transformations import quaternion_from_euler
import carla
import time
import random
import sys
import threading
import matplotlib.pyplot as plt
import numpy as np
import time
import networkx as nx
import math
from get_topology import *
# plt.ion()
# plt.show()
class globalPathServer(object):
"""Global is published everytime there is a request for global path over /get_global_path topic"""
def __init__(self, world = " ", ns = " ",source=0,destination=14):
# super(GlobalPathServer, self).__init__()
self.ns = ns
# Get topology from the map
_map = world.get_map()
# Build waypoint graph
topology,waypoints = get_topology(_map)
self.graph,self.id_map = build_graph(topology)
# get source and destination location from user (invoke while creating the object)
self.source = source
self.destination = destination
self.p = get_shortest_path(self.graph, self.source, self.destination)
for i in range(self.p.shape[0]):
wp = carla.Location(self.p[i][0],self.p[i][1],self.p[i][2])
world.debug.draw_point(wp, size=0.1, color=carla.Color(0, 255, 0), life_time=300.0,persistent_lines=True)
# print(wp)
# Because ROS Uses right handed coordinate system
# and carla uses left handed coordinated system
# https://math.stackexchange.com/questions/2626961/how-to-convert-a-right-handed-coordinate-system-to-left-handed
print "shorted path...",self.p
for i in range(len(self.p)):
self.p[i][1] = -self.p[i][1]
print "shorted path...",self.p
# self.plot()
rospy.init_node('{}_path_server'.format(self.ns), anonymous = True)
rospy.Subscriber('{}/get_global_path'.format(self.ns), String, self.callback_update)
self.path_publisher = rospy.Publisher('{}/global_path'.format(self.ns), Path, queue_size = 10)
self.path = Path()
def makePathMessage(self):
carla_path = self.p
# posestamped required header to be set...
# Sequence Number - increase every time this function is called
# time stamp - ros current time
# frame_id - global
poses = []
for i in range(len(carla_path)-1):
# posestamped required header to be set...
# Sequence Number - increase every time this function is called
# time stamp - ros current time
# frame_id - global
posestamped = PoseStamped()
pose = Pose()
pose.position.x = carla_path[i][0]
pose.position.y = carla_path[i][1]
pose.position.z = carla_path[i][1]
pose.orientation = self.directionFromTwoPointsQuaternion(carla_path[i],carla_path[i+1])
posestamped.pose = pose
poses.append(posestamped)
posestamped = PoseStamped()
pose = Pose()
pose.position.x = carla_path[-1][0]
pose.position.y = carla_path[-1][1]
pose.position.z = 0;
pose.orientation = self.directionFromTwoPointsQuaternion(carla_path[-1],carla_path[-1])
posestamped.pose = pose
poses.append(posestamped)
self.path.poses = poses
def callback_update(self, data):
self.makePathMessage()
self.path_publisher.publish(self.path)
def plot(self):
mapk = self.id_map.keys()
srcind = self.id_map.values().index(self.source)
destind = self.id_map.values().index(self.destination)
source = mapk[srcind]
dest = mapk[destind]
plt.plot(source[0],source[1],'go--', linewidth=2, markersize=12)
plt.plot(dest[0],dest[1],'ro--', linewidth=2, markersize=12)
plt.plot(self.p[:,0],self.p[:,1])
plt.draw()
plt.pause(0.001)
@staticmethod
def directionFromTwoPointsQuaternion(p1,p2):
angle_ = np.arctan2(p2[1]-p1[1],p2[0]-p1[0])
q = quaternion_from_euler(0, 0, angle_)
quat_msg = Quaternion(q[0], q[1], q[2], q[3])
return quat_msg
def main(argv):
client = carla.Client('localhost',2000)
client.set_timeout(2.0)
world = client.get_world()
# namespace - 'carla'
node = globalPathServer(world,'carla')
while not rospy.is_shutdown():
rospy.spin()
if __name__ == '__main__':
try:
main(sys.argv[1:])
except rospy.ROSInterruptException:
pass