Skip to content

Commit

Permalink
Fix issue that car can't full stop if brake (#786)
Browse files Browse the repository at this point in the history
Fix issue that car can't full stop if brake=-1
  • Loading branch information
pengzhenghao authored Dec 15, 2024
1 parent b908149 commit 2b6f5d3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
15 changes: 13 additions & 2 deletions metadrive/component/vehicle/base_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,19 @@ def _apply_throttle_brake(self, throttle_brake):
self.system.applyEngineForce(max_engine_force * throttle_brake, wheel_index)
self.system.setBrake(0, wheel_index)
else:
self.system.applyEngineForce(0.0, wheel_index)
self.system.setBrake(abs(throttle_brake) * max_brake_force, wheel_index)
DEADZONE = 0.01

# Speed m/s in car's heading:
heading = self.heading
velocity = self.velocity
speed_in_heading = velocity[0] * heading[0] + velocity[1] * heading[1]

if speed_in_heading < DEADZONE:
self.system.applyEngineForce(0.0, wheel_index)
self.system.setBrake(2, wheel_index)
else:
self.system.applyEngineForce(0.0, wheel_index)
self.system.setBrake(abs(throttle_brake) * max_brake_force, wheel_index)

"""---------------------------------------- vehicle info ----------------------------------------------"""

Expand Down
43 changes: 43 additions & 0 deletions metadrive/tests/test_policy/test_full_stop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from metadrive.envs import MetaDriveEnv


def test_full_stop():
config = {
"use_render": False,
"map": "y",
"vehicle_config": {
"spawn_position_heading": [[10, 10], 0],
},
}

env = MetaDriveEnv(config)
env.reset()

try:
# Driving forward phase
for step in range(30): # Drive forward for 30 steps
env.step([0, 1]) # Full throttle, no steering
# print("Speed: ", env.agent.speed)
# env.render(mode="topdown")

success = False
print("Starting Brake Phase")
for i in range(20): # Continue braking until the car stops
env.step([0.0, -1.0]) # No throttle, apply brake
# print(i, "Speed: ", env.agent.speed)
# env.render(mode="topdown")

if env.agent.speed <= 0.01: # Stop if speed is effectively zero
print("Car has stopped.")
success = True
break

if not success:
raise ValueError("Car did not stop after 20 steps")

finally:
env.close()


if __name__ == '__main__':
test_full_stop()

0 comments on commit 2b6f5d3

Please sign in to comment.