Skip to content

Commit 9cbd273

Browse files
committed
Path tracking working
TODO: Trajectory generation Throttle command introduction Location from GPS points
1 parent 0bb20da commit 9cbd273

File tree

5 files changed

+71
-42
lines changed

5 files changed

+71
-42
lines changed

GlobalPathCarla2ROS.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
from get_topology import *
2222

23-
plt.ion()
24-
plt.show()
23+
# plt.ion()
24+
# plt.show()
2525

2626
class globalPathServer(object):
2727
"""Global is published everytime there is a request for global path over /get_global_path topic"""
@@ -40,8 +40,18 @@ def __init__(self, world = " ", ns = " ",source=0,destination=14):
4040
self.source = source
4141
self.destination = destination
4242

43-
self.p = get_shortest_path(self.graph, self.source, self.destination)
44-
self.plot()
43+
self.p = get_shortest_path(self.graph, self.source, self.destination)
44+
45+
46+
# Because ROS Uses right handed coordinate system
47+
# and carla uses left handed coordinated system
48+
# https://math.stackexchange.com/questions/2626961/how-to-convert-a-right-handed-coordinate-system-to-left-handed
49+
50+
print "shorted path...",self.p
51+
for i in range(len(self.p)):
52+
self.p[i][1] = -self.p[i][1]
53+
print "shorted path...",self.p
54+
# self.plot()
4555
rospy.init_node('{}_path_server'.format(self.ns), anonymous = True)
4656
rospy.Subscriber('{}/get_global_path'.format(self.ns), String, self.callback_update)
4757
self.path_publisher = rospy.Publisher('{}/global_path'.format(self.ns), Path, queue_size = 10)
@@ -109,7 +119,8 @@ def plot(self):
109119

110120
@staticmethod
111121
def directionFromTwoPointsQuaternion(p1,p2):
112-
angle_ = np.arctan2(p2[1]-p1[1],p2[0]-p1[0])
122+
123+
angle_ = np.arctan2(p2[1]-p1[1],p2[0]-p1[0])
113124
q = quaternion_from_euler(0, 0, angle_)
114125
quat_msg = Quaternion(q[0], q[1], q[2], q[3])
115126
return quat_msg

Modifications

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ros-bridge/src/carla_ros_bridge/transforms.py
2+
line 33: -carla_location.y, -> carla_location.y

controller.py

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
import numpy as np
55
import time
66
import sys
7-
7+
# import cutils
88

99
import rospy
10-
from std_msgs.msg import String
10+
from std_msgs.msg import String, Float64
1111
from geometry_msgs.msg import Pose, PoseStamped, Point, Quaternion, Twist
1212
from nav_msgs.msg import Path, Odometry
1313
from tf.transformations import quaternion_from_euler, euler_from_quaternion
@@ -17,7 +17,7 @@
1717
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
1818

1919
global debug
20-
debug = 1
20+
debug = 0
2121
class closestPoint:
2222
def __init__(self, points):
2323
self.points = points
@@ -48,12 +48,12 @@ def __init__(self,points):
4848
time.sleep(0.5)
4949
# rospy.init_node("purePursuit_node",anonymous=True)
5050

51-
self.look_ahead_distance = 10
51+
self.look_ahead_distance = 5
5252

53-
self.target_speed = 1
53+
self.target_speed = 5
5454

55-
self.k = 0.1
56-
self.kp = 1
55+
self.k = 0.
56+
self.kp = 0.2
5757

5858
self.init_xy = False
5959
self.init_heading = False
@@ -72,19 +72,24 @@ def __init__(self,points):
7272
# Initialize vehicle controller message
7373
# data = rospy.wait_for_message("/carla/ego_vehicle/odometry", Odometry)
7474
# print data
75-
# rospy.Subscriber("/carla/ego_vehicle/odometry", Odometry, self.callback_odom)
75+
rospy.Subscriber("/carla/ego_vehicle/odometry", Odometry, self.callback_odom)
7676
self.cmd_pub = rospy.Publisher('/carla/ego_vehicle/ackermann_cmd', AckermannDrive, queue_size=1)
7777

78+
self.goalxpub = rospy.Publisher('/goalx',Float64, queue_size=10)
79+
self.goalypub = rospy.Publisher('/goaly',Float64, queue_size=10)
80+
7881
self.points = points
7982

8083

8184
def callback_odom(self,data):
8285

83-
rospy.loginfo(__func__)
86+
# rospy.loginfo(__func__)
8487
self.car_current_x = data.pose.pose.position.x
8588
self.car_current_y = data.pose.pose.position.y
8689

87-
self.current_speed = data.twist.twist.linear.x
90+
current_speed_x = data.twist.twist.linear.x
91+
current_speed_y = data.twist.twist.linear.y
92+
self.current_speed = np.sqrt(current_speed_x**2 + current_speed_y**2)
8893

8994
quaternion = [data.pose.pose.orientation.x,\
9095
data.pose.pose.orientation.y,\
@@ -98,22 +103,24 @@ def callback_odom(self,data):
98103
def _controller(self):
99104

100105
############################## FIX #########################################
101-
data = rospy.wait_for_message("/carla/ego_vehicle/odometry", Odometry)
106+
# data = rospy.wait_for_message("/carla/ego_vehicle/odometry", Odometry)
102107

103-
# print data
108+
# # print data
104109

105-
self.car_current_x = data.pose.pose.position.x
106-
self.car_current_y = data.pose.pose.position.y
110+
# self.car_current_x = data.pose.pose.position.x
111+
# self.car_current_y = data.pose.pose.position.y
107112

108-
self.current_speed = data.twist.twist.linear.x
113+
# current_speed_x = data.twist.twist.linear.x
114+
# current_speed_y = data.twist.twist.linear.y
115+
# self.current_speed = np.sqrt(current_speed_x**2 + current_speed_y**2)
109116

110-
quaternion = [data.pose.pose.orientation.x,\
111-
data.pose.pose.orientation.y,\
112-
data.pose.pose.orientation.z,\
113-
data.pose.pose.orientation.w]
114-
euler = euler_from_quaternion(quaternion)
115-
# euler = tf.transformations.euler_from_quaternion(quaternion)
116-
self.car_current_heading = euler[2]
117+
# quaternion = [data.pose.pose.orientation.x,\
118+
# data.pose.pose.orientation.y,\
119+
# data.pose.pose.orientation.z,\
120+
# data.pose.pose.orientation.w]
121+
# euler = euler_from_quaternion(quaternion)
122+
# # euler = tf.transformations.euler_from_quaternion(quaternion)
123+
# self.car_current_heading = euler[2]
117124
#############################################################################
118125

119126
path = closestPoint(self.points)
@@ -139,25 +146,27 @@ def _controller(self):
139146
near_x = self.points[(ind)%len(self.points)][0]
140147
near_y = self.points[(ind)%len(self.points)][1]
141148

142-
# self.goalxpub.publish(goal_x)
143-
# self.goalypub.publish(goal_y)
149+
self.goalxpub.publish(goal_x)
150+
self.goalypub.publish(goal_y)
144151

145152
distance = sqrt((self.car_current_x-goal_x)**2 + (self.car_current_y-goal_y)**2)
153+
distance_final = sqrt((self.car_current_x-self.points[-1][0])**2 + (self.car_current_y-self.points[-1][1])**2)
146154
distance2 = sqrt((near_x-goal_x)**2 + (near_y-goal_y)**2)
147-
148-
print "Car current position :: ",self.car_current_x,self.car_current_y
149-
print "Goal position :: ",goal_x,goal_y
150-
print "Nearest point on the path:: ",near_x,near_y
151-
print "Distance to goal :: ",distance
152-
print "Look ahead distance:: ",distance2
155+
print "distance_final : ",distance_final
153156

154157
desired_pose = atan2((goal_y-self.car_current_y),(goal_x-self.car_current_x))
155158

156-
print "desired_pose:: {0} ({1}) ".format(desired_pose,degrees(desired_pose))
157-
print "current heading:: {0} ({1}) ".format(self.car_current_heading,degrees(self.car_current_heading))
158159
error = (desired_pose - self.car_current_heading)
159160

160-
print "heading error:: {0} ({1}) ".format(error,degrees(error))
161+
if debug:
162+
print "Car current position :: ",self.car_current_x,self.car_current_y
163+
print "Goal position :: ",goal_x,goal_y
164+
print "Nearest point on the path:: ",near_x,near_y
165+
print "Distance to goal :: ",distance
166+
print "Look ahead distance:: ",distance2
167+
print "desired_pose:: {0} ({1}) ".format(desired_pose,degrees(desired_pose))
168+
print "current heading:: {0} ({1}) ".format(self.car_current_heading,degrees(self.car_current_heading))
169+
print "heading error:: {0} ({1}) ".format(error,degrees(error))
161170

162171
# if error < -3.14159:
163172
# # print "error is below -pi"
@@ -172,15 +181,21 @@ def _controller(self):
172181
L = 2.6
173182
ld = 10
174183
kl = 1
175-
self.throttle = self._PIDControl(self.target_speed,self.current_speed)
184+
# self.throttle = self._PIDControl(self.target_speed,self.current_speed)
176185
# self.throttle = min(self.throttle,0.2)
186+
if(distance_final < 1):
187+
self.throttle = 0;
188+
self.throttle = 5;
177189
# self.steering = np.arctan(2*L*sin(error)/(kl*self.throttle))
178190
self.steering_ratio = 1
179191
self.steering = self.steering_ratio * np.arctan2(2*L*sin(error),Lf)
180192
# self.steering = error
181193

182194

183195
def _PIDControl(self,target, current):
196+
print "\n"
197+
print "_PIDControl: ", target-current
198+
print "\n"
184199
a = self.kp * (target - current)
185200
return a
186201

@@ -191,7 +206,8 @@ def publish_(self):
191206
ai = self.throttle
192207
di = self.steering
193208

194-
print "throttle and steering angle :: ",ai,di
209+
# print "throttle and steering angle :: ",di
210+
print "speed and steering angle :: ",ai,di
195211
print "\n\n"
196212

197213
ackermann_cmd_msg = AckermannDrive()

main_carla.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ def main_loop(args):
645645
print(snode,dnode)
646646

647647
node = globalPathServer(world.world,'carla',snode,dnode)
648-
node.plot()
648+
# node.plot()
649649
r = rospy.Rate(10)
650650
# while not rospy.is_shutdown():
651651
while True:

navigate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def PlotData(self, x, y):
7171
self.xdata.append(x)
7272
self.ydata.append(y)
7373
self.x = x
74-
self.y = y
74+
self.y = -y
7575
self.lines.set_data(self.xdata,self.ydata)
7676
self.ax.relim()
7777
self.ax.autoscale_view()

0 commit comments

Comments
 (0)