Skip to content

Commit 590d86f

Browse files
committed
fix bug in control thread
1 parent 6b11579 commit 590d86f

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

ROS_Core/src/Labs/Lab1/cfg/planner.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ from dynamic_reconfigure.parameter_generator_catkin import *
55

66
gen = ParameterGenerator()
77

8-
gen.add("latency", double_t, 0, "Latency compensation for control", 0.05, -1, 1)
8+
gen.add("latency", double_t, 0, "Latency compensation for control", 0.25, -1, 1)
99

1010
exit(gen.generate("racecar_planner", "racecar_planner", "planner"))

ROS_Core/src/Labs/Lab1/scripts/traj_planner.py

+20-20
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def control_thread(self):
222222
'''
223223
Main control thread to publish control command
224224
'''
225-
rate = rospy.Rate(50)
225+
rate = rospy.Rate(40)
226226
u_queue = queue.Queue()
227227

228228
# values to keep track of the previous control command
@@ -234,7 +234,7 @@ def dyn_step(x, u, dt):
234234
dx = np.array([x[2]*np.cos(x[3]),
235235
x[2]*np.sin(x[3]),
236236
u[0],
237-
x[2]*np.tan(u[1]*1.05)/0.257,
237+
x[2]*np.tan(u[1]*1.1)/0.257,
238238
0
239239
])
240240
x_new = x + dx*dt
@@ -258,10 +258,11 @@ def dyn_step(x, u, dt):
258258
# check if there is new state available
259259
if self.control_state_buffer.new_data_available:
260260
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()
262262

263263
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:
265266
u = u_queue.get() # remove old control commands
266267

267268
# get the state from the odometry message
@@ -277,27 +278,26 @@ def dyn_step(x, u, dt):
277278
psi,
278279
u[1]
279280
])
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
286283
for i in range(u_queue.qsize()):
287284
u_next = u_queue.queue[i]
288-
dt = u_next[-1] - t_prev
285+
dt = u_next[-1] - u[-1]
289286
state_cur = dyn_step(state_cur, u, dt)
290287
u = u_next
291-
t_prev = u[-1]
292288

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)
295295

296296
# if there is no new state available, we do one step forward integration to predict the state
297297
elif prev_state is not None:
298298
t_prev = prev_u[-1]
299299
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
301301
state_cur = dyn_step(prev_state, prev_u, dt)
302302

303303
# Generate control command from the policy
@@ -310,20 +310,20 @@ def dyn_step(x, u, dt):
310310

311311
if state_ref is not None:
312312
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))
314314
else:
315315
# reset the policy buffer if the policy is not valid
316316
rospy.logwarn("Try to retrieve a policy beyond the horizon! Reset the policy buffer!")
317317
self.policy_buffer.reset()
318318

319319
# 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:
324321
# If we are using robot,
325322
# the throttle and steering angle needs to convert to PWM signal
326323
throttle_pwm, steer_pwm = self.pwm_converter.convert(accel, steer, state_cur[2])
324+
else:
325+
throttle_pwm = accel
326+
steer_pwm = steer
327327

328328
# publish control command
329329
servo_msg = ServoMsg()

ROS_Core/src/Labs/Lab1/scripts/utils/generate_pwm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def convert(self, accel: float, steer: float, v: float):
4545
v_bounded = v
4646

4747
# negative pwm means turn left (positive steering angle)
48-
steer_pwm = -np.clip(steer/0.35, -1, 1)
48+
steer_pwm = -np.clip(steer/0.37, -1, 1)
4949
accel_bounded = np.sign(accel)*min(abs(accel), 2+v)
5050

5151
# Generate Input vector

0 commit comments

Comments
 (0)