import os
import pychrono as chrono
import pychrono.vehicle as veh
import pychrono.irrlicht as irr
import math
# =============================================================================
# SETUP SIMULATION
# =============================================================================
def main():
# Set data path (replace with your vehicle JSON directory)
veh.SetDataPath(chrono.GetChronoDataPath() + "vehicle/")
# Create Chrono system
system = chrono.ChSystemNSC()
system.SetCollisionSystemType(chrono.ChCollisionSystem.Type_BULLET)
system.SetGravitationalAcceleration(chrono.ChVector3d(0, 0, -9.81))
# =========================================================================
# 1. VEHICLE DEFINITION (JSON-based)
# =========================================================================
# Load vehicle from JSON (replace with your file)
vehicle_file = "D:/CHRONO-engine_demos/chrono-9.0.0/chrono-9.0.0/data/vehicle/hmmwv/vehicle/HMMWV_Vehicle.json"
#vehicle_file = "D:/Hangups on Crossings/Vehicle Models/Scania-old-Charlie/vehicle.json"
vehicle = veh.WheeledVehicle(system, vehicle_file)
vehicle.Initialize(chrono.ChCoordsysd(chrono.ChVector3d(0, 0, 1.5), chrono.ChQuaterniond(1, 0, 0, 0)))
# Add this debug print immediately after
print(f"Vehicle reference frame height: {vehicle.GetChassis().GetPos().z}")
for axle in vehicle.GetAxles():
tireL = veh.ReadTireJSON("D:/Hangups on Crossings/Vehicle Models/hmmwv/tire/HMMWV_TMeasyTire.json") # Replace with your tire file
tireR = veh.ReadTireJSON("D:/Hangups on Crossings/Vehicle Models/hmmwv/tire/HMMWV_TMeasyTire.json")
vehicle.InitializeTire(tireL, axle.m_wheels[0], veh.VisualizationType_MESH)
vehicle.InitializeTire(tireR, axle.m_wheels[1], veh.VisualizationType_MESH)
#tireL.EnableCollision(veh.ChTire.CollisionType_MESH)
#tireR.EnableCollision(veh.ChTire.CollisionType_MESH)
vehicle.SetWheelVisualizationType(veh.VisualizationType_MESH)
#debug
print(f"Vehicle has {vehicle.GetNumberAxles()} axles") # Should print 2 for HMMWV
#print(f"Initialized {len(vehicle.GetTires())} tires") # Should print 4 for HMMWV
#for i, tire in enumerate(vehicle.GetTires()):
# print(f"Tire {i} collision enabled: {tire.IsCollisionEnabled()}")
# Add this check before loading tires:
#tire_path = veh.GetDataFile("D:/Hangups on Crossings/Vehicle Models/hmmwv/tire/HMMWV_TMeasyTire.json")
tire_path = "D:/Hangups on Crossings/Vehicle Models/hmmwv/tire/HMMWV_TMeasyTire.json"
tireL = veh.ReadTireJSON(tire_path)
tireL.Initialize(axle.m_wheels[0])
tireR.Initialize(axle.m_wheels[1])
if not os.path.exists(tire_path):
print(f"ERROR: Tire file not found at {tire_path}")
else:
print(f"Found tire file at {tire_path}")
# Set visualization modes
vehicle.SetChassisVisualizationType(veh.VisualizationType_MESH)
vehicle.SetSuspensionVisualizationType(veh.VisualizationType_PRIMITIVES)
vehicle.SetWheelVisualizationType(veh.VisualizationType_MESH)
vehicle.SetTireVisualizationType(veh.VisualizationType_MESH)
# =========================================================================
# 2. TERRAIN
# =========================================================================
patch_mat = chrono.ChContactMaterialNSC()
patch_mat.SetFriction(0.9)
patch_mat.SetRestitution(0.1)
terrain = veh.RigidTerrain(system)
patch = terrain.AddPatch(patch_mat, chrono.CSYSNORM, 100, 100)
patch.SetTexture(veh.GetDataFile("terrain/textures/tile4.jpg"), 200, 200)
terrain.Initialize()
# =========================================================================
# 3. VISUALIZATION (Irrlicht)
# =========================================================================
vis = veh.ChWheeledVehicleVisualSystemIrrlicht()
vis.SetWindowTitle("Custom Vehicle Demo")
vis.SetWindowSize(1280, 1024)
vis.SetChaseCamera(chrono.ChVector3d(0.0, 0.0, 1.75), 6.0, 0.5)
vis.Initialize()
vis.AddLogo(chrono.GetChronoDataFile("logo_pychrono_alpha.png"))
vis.AddSkyBox()
vis.AddLightDirectional()
vis.AttachVehicle(vehicle)
# =========================================================================
# 4. DRIVER CONTROLS
# =========================================================================
# Option A: Interactive driver (keyboard controls)
driver = veh.ChInteractiveDriverIRR(vis)
driver.SetSteeringDelta(0.02)
driver.SetThrottleDelta(0.02)
driver.SetBrakingDelta(0.06)
driver.Initialize()
# Option B: Path-follower (uncomment to use)
#path = veh.StraightLinePath(chrono.ChVector3d(0, 0, 0), chrono.ChVector3d(100, 0, 0))
#driver = veh.ChPathFollowerDriver(vehicle, path, "my_path", 10.0) # 10 m/s target speed
#driver.Initialize()
# Debug print vehicle components
print("\n=== Vehicle Status ===")
print(f"Chassis pos: {vehicle.GetChassisBody().GetPos()}")
for i, axle in enumerate(vehicle.GetAxles()):
print(f"Axle {i}:")
print(f" Left wheel pos: {axle.m_wheels[0].GetPos()}")
print(f" Right wheel pos: {axle.m_wheels[1].GetPos()}")
#if i < len(vehicle.GetTires()):
#print(f" Left tire contact: {vehicle.GetTires()[2*i].IsInContact()}")
#print(f" Right tire contact: {vehicle.GetTires()[2*i+1].IsInContact()}")
# Pause briefly to see debug output
import time
time.sleep(3)
# =========================================================================
# 5. SIMULATION LOOP
# =========================================================================
step_size = 0.002
while vis.Run():
time = system.GetChTime()
# Render scene
vis.BeginScene()
vis.Render()
vis.EndScene()
# Get driver inputs
driver_inputs = driver.GetInputs()
# Update modules
driver.Synchronize(time)
terrain.Synchronize(time)
vehicle.Synchronize(time, driver_inputs, terrain)
vis.Synchronize(time, driver_inputs)
# Advance simulation
driver.Advance(step_size)
terrain.Advance(step_size)
vehicle.Advance(step_size)
vis.Advance(step_size)
# =============================================================================
# RUN SIMULATION
# =============================================================================
#if __name__ == "__main__":print(f"Vehicle has {vehicle.GetNumberAxles()} axles") # Should print 2 for HMMWV
#print(f"Initialized {len(vehicle.GetTires())} tires") # Should print 4 for HMMWV
#for i, tire in enumerate(vehicle.GetTires()):
# print(f"Tire {i} collision enabled: {tire.IsCollisionEnabled()}")
# main()
if __name__ == "__main__":
main()