4
4
import numpy as np
5
5
import time
6
6
import sys
7
-
7
+ # import cutils
8
8
9
9
import rospy
10
- from std_msgs .msg import String
10
+ from std_msgs .msg import String , Float64
11
11
from geometry_msgs .msg import Pose , PoseStamped , Point , Quaternion , Twist
12
12
from nav_msgs .msg import Path , Odometry
13
13
from tf .transformations import quaternion_from_euler , euler_from_quaternion
17
17
logging .basicConfig (level = logging .DEBUG , format = '%(asctime)s - %(levelname)s - %(message)s' )
18
18
19
19
global debug
20
- debug = 1
20
+ debug = 0
21
21
class closestPoint :
22
22
def __init__ (self , points ):
23
23
self .points = points
@@ -48,12 +48,12 @@ def __init__(self,points):
48
48
time .sleep (0.5 )
49
49
# rospy.init_node("purePursuit_node",anonymous=True)
50
50
51
- self .look_ahead_distance = 10
51
+ self .look_ahead_distance = 5
52
52
53
- self .target_speed = 1
53
+ self .target_speed = 5
54
54
55
- self .k = 0.1
56
- self .kp = 1
55
+ self .k = 0.
56
+ self .kp = 0.2
57
57
58
58
self .init_xy = False
59
59
self .init_heading = False
@@ -72,19 +72,24 @@ def __init__(self,points):
72
72
# Initialize vehicle controller message
73
73
# data = rospy.wait_for_message("/carla/ego_vehicle/odometry", Odometry)
74
74
# print data
75
- # rospy.Subscriber("/carla/ego_vehicle/odometry", Odometry, self.callback_odom)
75
+ rospy .Subscriber ("/carla/ego_vehicle/odometry" , Odometry , self .callback_odom )
76
76
self .cmd_pub = rospy .Publisher ('/carla/ego_vehicle/ackermann_cmd' , AckermannDrive , queue_size = 1 )
77
77
78
+ self .goalxpub = rospy .Publisher ('/goalx' ,Float64 , queue_size = 10 )
79
+ self .goalypub = rospy .Publisher ('/goaly' ,Float64 , queue_size = 10 )
80
+
78
81
self .points = points
79
82
80
83
81
84
def callback_odom (self ,data ):
82
85
83
- rospy .loginfo (__func__ )
86
+ # rospy.loginfo(__func__)
84
87
self .car_current_x = data .pose .pose .position .x
85
88
self .car_current_y = data .pose .pose .position .y
86
89
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 )
88
93
89
94
quaternion = [data .pose .pose .orientation .x ,\
90
95
data .pose .pose .orientation .y ,\
@@ -98,22 +103,24 @@ def callback_odom(self,data):
98
103
def _controller (self ):
99
104
100
105
############################## FIX #########################################
101
- data = rospy .wait_for_message ("/carla/ego_vehicle/odometry" , Odometry )
106
+ # data = rospy.wait_for_message("/carla/ego_vehicle/odometry", Odometry)
102
107
103
- # print data
108
+ # # print data
104
109
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
107
112
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)
109
116
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]
117
124
#############################################################################
118
125
119
126
path = closestPoint (self .points )
@@ -139,25 +146,27 @@ def _controller(self):
139
146
near_x = self .points [(ind )% len (self .points )][0 ]
140
147
near_y = self .points [(ind )% len (self .points )][1 ]
141
148
142
- # self.goalxpub.publish(goal_x)
143
- # self.goalypub.publish(goal_y)
149
+ self .goalxpub .publish (goal_x )
150
+ self .goalypub .publish (goal_y )
144
151
145
152
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 )
146
154
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
153
156
154
157
desired_pose = atan2 ((goal_y - self .car_current_y ),(goal_x - self .car_current_x ))
155
158
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 ))
158
159
error = (desired_pose - self .car_current_heading )
159
160
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 ))
161
170
162
171
# if error < -3.14159:
163
172
# # print "error is below -pi"
@@ -172,15 +181,21 @@ def _controller(self):
172
181
L = 2.6
173
182
ld = 10
174
183
kl = 1
175
- self .throttle = self ._PIDControl (self .target_speed ,self .current_speed )
184
+ # self.throttle = self._PIDControl(self.target_speed,self.current_speed)
176
185
# self.throttle = min(self.throttle,0.2)
186
+ if (distance_final < 1 ):
187
+ self .throttle = 0 ;
188
+ self .throttle = 5 ;
177
189
# self.steering = np.arctan(2*L*sin(error)/(kl*self.throttle))
178
190
self .steering_ratio = 1
179
191
self .steering = self .steering_ratio * np .arctan2 (2 * L * sin (error ),Lf )
180
192
# self.steering = error
181
193
182
194
183
195
def _PIDControl (self ,target , current ):
196
+ print "\n "
197
+ print "_PIDControl: " , target - current
198
+ print "\n "
184
199
a = self .kp * (target - current )
185
200
return a
186
201
@@ -191,7 +206,8 @@ def publish_(self):
191
206
ai = self .throttle
192
207
di = self .steering
193
208
194
- print "throttle and steering angle :: " ,ai ,di
209
+ # print "throttle and steering angle :: ",di
210
+ print "speed and steering angle :: " ,ai ,di
195
211
print "\n \n "
196
212
197
213
ackermann_cmd_msg = AckermannDrive ()
0 commit comments