@@ -222,7 +222,7 @@ def control_thread(self):
222
222
'''
223
223
Main control thread to publish control command
224
224
'''
225
- rate = rospy .Rate (50 )
225
+ rate = rospy .Rate (40 )
226
226
u_queue = queue .Queue ()
227
227
228
228
# values to keep track of the previous control command
@@ -234,7 +234,7 @@ def dyn_step(x, u, dt):
234
234
dx = np .array ([x [2 ]* np .cos (x [3 ]),
235
235
x [2 ]* np .sin (x [3 ]),
236
236
u [0 ],
237
- x [2 ]* np .tan (u [1 ]* 1.05 )/ 0.257 ,
237
+ x [2 ]* np .tan (u [1 ]* 1.1 )/ 0.257 ,
238
238
0
239
239
])
240
240
x_new = x + dx * dt
@@ -258,10 +258,11 @@ def dyn_step(x, u, dt):
258
258
# check if there is new state available
259
259
if self .control_state_buffer .new_data_available :
260
260
odom_msg = self .control_state_buffer .readFromRT ()
261
- t_prev = odom_msg .header .stamp .to_sec ()
261
+ t_slam = odom_msg .header .stamp .to_sec ()
262
262
263
263
u = np .zeros (3 )
264
- while not u_queue .empty () and u_queue .queue [0 ][- 1 ] < t_prev :
264
+ u [- 1 ] = t_slam
265
+ while not u_queue .empty () and u_queue .queue [0 ][- 1 ] < t_slam :
265
266
u = u_queue .get () # remove old control commands
266
267
267
268
# get the state from the odometry message
@@ -277,27 +278,26 @@ def dyn_step(x, u, dt):
277
278
psi ,
278
279
u [1 ]
279
280
])
280
-
281
- # update the state buffer for the planning thread
282
- plan_state = np .append (state_cur , t_act )
283
- self .plan_state_buffer .writeFromNonRT (plan_state )
284
-
285
- # update the current state use past control command
281
+
282
+ # predict the current state use past control command
286
283
for i in range (u_queue .qsize ()):
287
284
u_next = u_queue .queue [i ]
288
- dt = u_next [- 1 ] - t_prev
285
+ dt = u_next [- 1 ] - u [ - 1 ]
289
286
state_cur = dyn_step (state_cur , u , dt )
290
287
u = u_next
291
- t_prev = u [- 1 ]
292
288
293
- # update the cur state with the most recent control command
294
- state_cur = dyn_step (state_cur , u , t_act - t_prev )
289
+ # predict the cur state with the most recent control command
290
+ state_cur = dyn_step (state_cur , u , t_act - u [- 1 ])
291
+
292
+ # update the state buffer for the planning thread
293
+ plan_state = np .append (state_cur , t_act )
294
+ self .plan_state_buffer .writeFromNonRT (plan_state )
295
295
296
296
# if there is no new state available, we do one step forward integration to predict the state
297
297
elif prev_state is not None :
298
298
t_prev = prev_u [- 1 ]
299
299
dt = t_act - t_prev
300
- # predict the state when the control command is executed
300
+ # predict the state using the last control command is executed
301
301
state_cur = dyn_step (prev_state , prev_u , dt )
302
302
303
303
# Generate control command from the policy
@@ -310,20 +310,20 @@ def dyn_step(x, u, dt):
310
310
311
311
if state_ref is not None :
312
312
accel , steer_rate = self .compute_control (state_cur , state_ref , u_ref , K )
313
- steer = max (- 0.35 , min (0.35 , prev_u [1 ] + steer_rate * dt ))
313
+ steer = max (- 0.37 , min (0.37 , prev_u [1 ] + steer_rate * dt ))
314
314
else :
315
315
# reset the policy buffer if the policy is not valid
316
316
rospy .logwarn ("Try to retrieve a policy beyond the horizon! Reset the policy buffer!" )
317
317
self .policy_buffer .reset ()
318
318
319
319
# generate control command
320
- if self .simulation :
321
- throttle_pwm = accel
322
- steer_pwm = steer
323
- else :
320
+ if not self .simulation and state_cur is not None :
324
321
# If we are using robot,
325
322
# the throttle and steering angle needs to convert to PWM signal
326
323
throttle_pwm , steer_pwm = self .pwm_converter .convert (accel , steer , state_cur [2 ])
324
+ else :
325
+ throttle_pwm = accel
326
+ steer_pwm = steer
327
327
328
328
# publish control command
329
329
servo_msg = ServoMsg ()
0 commit comments