Skip to content

Commit eebe8ee

Browse files
committed
Controller working... check driving mode
1 parent 84a46c9 commit eebe8ee

File tree

6 files changed

+150
-14
lines changed

6 files changed

+150
-14
lines changed

GlobalPathCarla2ROS.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
from get_topology import *
2222

23+
plt.ion()
24+
plt.show()
2325

2426
class globalPathServer(object):
2527
"""Global is published everytime there is a request for global path over /get_global_path topic"""
@@ -102,7 +104,8 @@ def plot(self):
102104
plt.plot(dest[0],dest[1],'ro--', linewidth=2, markersize=12)
103105

104106
plt.plot(self.p[:,0],self.p[:,1])
105-
plt.show()
107+
plt.draw()
108+
plt.pause(0.001)
106109

107110
@staticmethod
108111
def directionFromTwoPointsQuaternion(p1,p2):

controller.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,24 @@ def __init__(self,points):
8080

8181
def callback_odom(self,data):
8282

83-
83+
rospy.loginfo(__func__)
8484
self.car_current_x = data.pose.pose.position.x
8585
self.car_current_y = data.pose.pose.position.y
8686

87-
self.current_speed = data.twist.linear.x
87+
self.current_speed = data.twist.twist.linear.x
8888

89-
quaternion = data.pose.orientation
90-
euler = tf.transformations.euler_from_quaternion(quaternion)
89+
quaternion = [data.pose.pose.orientation.x,\
90+
data.pose.pose.orientation.y,\
91+
data.pose.pose.orientation.z,\
92+
data.pose.pose.orientation.w]
93+
euler = euler_from_quaternion(quaternion)
94+
# euler = tf.transformations.euler_from_quaternion(quaternion)
9195
self.car_current_heading = euler[2]
9296

9397

9498
def _controller(self):
9599

96-
############################### FIX #########################################
100+
############################## FIX #########################################
97101
data = rospy.wait_for_message("/carla/ego_vehicle/odometry", Odometry)
98102

99103
# print data
@@ -110,7 +114,7 @@ def _controller(self):
110114
euler = euler_from_quaternion(quaternion)
111115
# euler = tf.transformations.euler_from_quaternion(quaternion)
112116
self.car_current_heading = euler[2]
113-
##############################################################################
117+
#############################################################################
114118

115119
path = closestPoint(self.points)
116120
ind = path.closest_node((self.car_current_x,self.car_current_y))

get_topology.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,18 @@ def get_topology(map):
5959
x = j.transform.location.x
6060
y = j.transform.location.y
6161
z = j.transform.location.z
62+
63+
#logging
64+
print x,y,z
6265
xs.append(x)
6366
ys.append(y)
6467
zs.append(z)
6568
xs = np.array(xs).reshape((len(xs),1))
6669
ys = np.array(ys).reshape((len(ys),1))
6770
zs = np.array(zs).reshape((len(zs),1))
6871

72+
# logging
73+
# print ys,zs
6974

7075
waypoints = np.hstack((xs,ys,zs))
7176

main_carla.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
from __future__ import print_function
4242

43-
# import rospy
43+
import rospy
4444

4545
import carla
4646
from carla import ColorConverter as cc
@@ -641,9 +641,9 @@ def main_loop(args):
641641

642642
print(snode,dnode)
643643

644-
# node = globalPathServer(world.world,'carla',snode,dnode)
645-
# node.plot()
646-
# r = rospy.Rate(10)
644+
node = globalPathServer(world.world,'carla',snode,dnode)
645+
node.plot()
646+
r = rospy.Rate(10)
647647
# while not rospy.is_shutdown():
648648
while True:
649649
clock = pygame.time.Clock()
@@ -654,7 +654,7 @@ def main_loop(args):
654654
world.tick(clock)
655655
world.render(display)
656656
pygame.display.flip()
657-
# r.sleep()
657+
r.sleep()
658658

659659
finally:
660660

navigate.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import rospy
66
from std_msgs.msg import String
77
from geometry_msgs.msg import Pose, PoseStamped, Point, Quaternion, Twist
8-
from nav_msgs.msg import Path
8+
from nav_msgs.msg import Path,Odometry
99

1010
import numpy as np
1111

1212
import controller as cn
13+
import matplotlib.pyplot as plt
14+
plt.ion()
15+
plt.show()
1316

1417
class Navigation(object):
1518
"""docstring for Navigation"""
@@ -41,17 +44,60 @@ def get_points(self):
4144
self.global_path_pub.publish("abc")
4245
return np.array(self.points)
4346

44-
47+
class DynamicUpdate():
48+
def __init__(self,points):
49+
global i
50+
self.xdata=[]
51+
self.ydata=[]
52+
self.x = None
53+
self.y = None
54+
self.points = points
55+
56+
# print self.points
57+
self.fig, self.ax = plt.subplots(1, 1)
58+
self.ax.axis('equal')
59+
self.ax.plot(self.points[:,0],self.points[:,1])
60+
if(bool(len(self.xdata))):
61+
self.ax.plot(self.xdata[-1],self.ydata[-1],'ro')
62+
self.lines, = self.ax.plot(self.xdata,self.ydata,color='g', linewidth=2.0)
63+
self.ax.set_autoscaley_on(True)
64+
self.ax.set_xlabel('X (m)')
65+
self.ax.set_ylabel('Y (m)')
66+
# self.ax.set_ylim([-40.0, 40.0])
67+
# self.ax.set_xlim([-40.0, 40.0])
68+
self.ax.grid()
69+
70+
def PlotData(self, x, y):
71+
self.xdata.append(x)
72+
self.ydata.append(y)
73+
self.x = x
74+
self.y = y
75+
self.lines.set_data(self.xdata,self.ydata)
76+
self.ax.relim()
77+
self.ax.autoscale_view()
78+
#We need to draw *and* flush
79+
self.fig.canvas.draw()
80+
self.fig.canvas.flush_events()
4581
def main(argv):
4682

4783
# namespace - 'carla'
4884
node = Navigation('carla')
4985
points = node.get_points()
86+
plt.plot(points[:,0],points[:,1])
87+
88+
# plt.draw()
89+
# plt.pause(0.001)
5090
# print points
5191
controller_object = cn.purePursuit(points)
92+
plot = DynamicUpdate(points)
5293
r = rospy.Rate(50)
5394

5495
while not rospy.is_shutdown():
96+
pos=rospy.wait_for_message("/carla/ego_vehicle/odometry",Odometry)
97+
98+
x=pos.pose.pose.position.x
99+
y=pos.pose.pose.position.y
100+
plot.PlotData(x,y)
55101
controller_object.publish_()
56102
r.sleep()
57103

plot2d.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#!/usr/bin/env python
2+
3+
import matplotlib.pyplot as plt
4+
import mpl_toolkits.mplot3d.axes3d as p3
5+
import rospy
6+
from nav_msgs.msg import Odometry
7+
#from nav_msgs.msg import Odometry
8+
import numpy as np
9+
import rospy
10+
from std_msgs.msg import String, Float64
11+
from geometry_msgs.msg import Twist, Vector3Stamped
12+
13+
points = []
14+
15+
x = 0
16+
while x<=2*np.pi:
17+
# points.append((40*((1-np.sin(x))*np.cos(x)),40*(1-np.sin(x))))
18+
points.append((20*np.sin(x),20*np.cos(x)))
19+
x += 0.05
20+
21+
22+
i=0
23+
points = np.array(points)
24+
plt.ion()
25+
class DynamicUpdate():
26+
def __init__(self,points):
27+
global i
28+
self.xdata=[]
29+
self.ydata=[]
30+
self.x = None
31+
self.y = None
32+
self.points = points
33+
34+
# print self.points
35+
self.fig, self.ax = plt.subplots(1, 1)
36+
self.ax.axis('equal')
37+
self.ax.plot(self.points[:,0],self.points[:,1])
38+
if(bool(len(self.xdata))):
39+
self.ax.plot(self.xdata[-1],self.ydata[-1],'ro')
40+
self.lines, = self.ax.plot(self.xdata,self.ydata,color='g', linewidth=2.0)
41+
self.ax.set_autoscaley_on(True)
42+
self.ax.set_xlabel('X (m)')
43+
self.ax.set_ylabel('Y (m)')
44+
# self.ax.set_ylim([-40.0, 40.0])
45+
# self.ax.set_xlim([-40.0, 40.0])
46+
self.ax.grid()
47+
48+
def PlotData(self, x, y):
49+
global i
50+
i+=1
51+
self.xdata.append(x)
52+
self.ydata.append(y)
53+
self.x = x
54+
self.y = y
55+
self.lines.set_data(self.xdata,self.ydata)
56+
self.ax.relim()
57+
self.ax.autoscale_view()
58+
#We need to draw *and* flush
59+
self.fig.canvas.draw()
60+
self.fig.canvas.flush_events()
61+
62+
63+
64+
rospy.init_node("plot",anonymous=True)
65+
position_vector = rospy.wait_for_message("/vehicle/perfect_gps/utm",Vector3Stamped)
66+
67+
init_x = position_vector.vector.x
68+
init_y = position_vector.vector.y
69+
points = points + np.array([init_x,init_y])
70+
plot = DynamicUpdate(points)
71+
72+
73+
while not rospy.is_shutdown():
74+
pos=rospy.wait_for_message("/vehicle/perfect_gps/utm",Vector3Stamped)
75+
76+
x=pos.vector.x
77+
y=pos.vector.y
78+
plot.PlotData(x,y)

0 commit comments

Comments
 (0)