1# =============================================================================
2# PROJECT CHRONO - http://projectchrono.org
3#
4# Copyright (c) 2014 projectchrono.org
5# All rights reserved.
6#
7# Use of this source code is governed by a BSD-style license that can be found
8# in the LICENSE file at the top level of the distribution and at
9# http://projectchrono.org/license-chrono.txt.
10#
11# =============================================================================
12# Authors: Radu Serban
13# =============================================================================
14#
15# Demonstration program for M113 vehicle on rigid terrain.
16#
17# The vehicle reference frame has Z up, X towards the front of the vehicle, and
18# Y pointing to the left.
19#
20# =============================================================================
21
22import pychrono as chrono
23import pychrono.vehicle as veh
24import pychrono.irrlicht as irr
25import os
26import math as m
27
28
29# =============================================================================
30
31def main():
32    #print("Copyright (c) 2017 projectchrono.org\nChrono version: ", CHRONO_VERSION , "\n\n")
33
34    #  Create the M113 vehicle
35    # ------------------------
36
37    vehicle = veh.M113_Vehicle(False,
38                               veh.TrackShoeType_SINGLE_PIN,
39                               veh.DrivelineTypeTV_BDS,
40                               veh.BrakeType_SIMPLE,
41                               chrono.ChContactMethod_SMC,
42                               veh.CollisionType_NONE)
43
44    vehicle.Initialize(chrono.ChCoordsysD(initLoc, initRot))
45
46    vehicle.SetChassisVisualizationType(veh.VisualizationType_PRIMITIVES)
47    vehicle.SetSprocketVisualizationType(veh.VisualizationType_MESH);
48    vehicle.SetIdlerVisualizationType(veh.VisualizationType_MESH);
49    vehicle.SetRoadWheelAssemblyVisualizationType(veh.VisualizationType_MESH);
50    vehicle.SetRoadWheelVisualizationType(veh.VisualizationType_MESH);
51    vehicle.SetTrackShoeVisualizationType(veh.VisualizationType_MESH);
52
53    # Create the powertrain system
54    # ----------------------------
55
56    powertrain = veh.M113_SimpleCVTPowertrain("Powertrain")
57    vehicle.InitializePowertrain(powertrain)
58
59    # Create the terrain
60    # ------------------
61
62    terrain = veh.RigidTerrain(vehicle.GetSystem())
63    if (contact_method == chrono.ChContactMethod_NSC):
64        patch_mat = chrono.ChMaterialSurfaceNSC()
65        patch_mat.SetFriction(0.9)
66        patch_mat.SetRestitution(0.01)
67    elif (contact_method == chrono.ChContactMethod_SMC):
68        patch_mat = chrono.ChMaterialSurfaceSMC()
69        patch_mat.SetFriction(0.9)
70        patch_mat.SetRestitution(0.01)
71        patch_mat.SetYoungModulus(2e7)
72    patch = terrain.AddPatch(patch_mat,
73                             chrono.ChVectorD(0, 0, 0), chrono.ChVectorD(0, 0, 1),
74                             terrainLength, terrainWidth)
75    patch.SetTexture(veh.GetDataFile("terrain/textures/tile4.jpg"), 200, 200)
76    patch.SetColor(chrono.ChColor(0.5, 0.8, 0.5))
77    terrain.Initialize()
78
79    # Create the vehicle Irrlicht interface
80    # -------------------------------------
81
82    app = veh.ChTrackedVehicleIrrApp(vehicle, 'M113', irr.dimension2du(1000,800))
83
84    app.SetSkyBox()
85    app.AddTypicalLights(irr.vector3df(30, -30, 100), irr.vector3df(30, 50, 100), 250, 130)
86    app.AddTypicalLogo(chrono.GetChronoDataFile('logo_pychrono_alpha.png'))
87    app.SetChaseCamera(trackPoint, 6.0, 0.5)
88    app.SetTimestep(step_size)
89    app.AssetBindAll()
90    app.AssetUpdateAll()
91
92    # Create the interactive driver system
93    # ------------------------------------
94
95    driver = veh.ChIrrGuiDriver(app)
96
97    # Set the time response for steering and throttle keyboard inputs.
98    steering_time = 0.5  # time to go from 0 to +1 (or from 0 to -1)
99    throttle_time = 1.0  # time to go from 0 to +1
100    braking_time = 0.3   # time to go from 0 to +1
101    driver.SetSteeringDelta(render_step_size / steering_time)
102    driver.SetThrottleDelta(render_step_size / throttle_time)
103    driver.SetBrakingDelta(render_step_size / braking_time)
104
105    driver.Initialize()
106
107    # Simulation loop
108    # ---------------
109
110    # Inter-module communication data
111    shoe_forces_left = veh.TerrainForces(vehicle.GetNumTrackShoes(veh.LEFT))
112    shoe_forces_right = veh.TerrainForces(vehicle.GetNumTrackShoes(veh.RIGHT))
113
114    # Number of simulation steps between miscellaneous events
115    render_steps = m.ceil(render_step_size / step_size)
116
117    # Initialize simulation frame counter and simulation time
118    step_number = 0
119
120    realtime_timer = chrono.ChRealtimeStepTimer()
121    while (app.GetDevice().run()):
122        time = vehicle.GetSystem().GetChTime()
123
124        app.BeginScene(True, True, irr.SColor(255, 140, 161, 192))
125        app.DrawAll()
126        app.EndScene()
127
128        # Get driver inputs
129        driver_inputs = driver.GetInputs()
130
131        # Update modules (process inputs from other modules)
132        driver.Synchronize(time)
133        terrain.Synchronize(time)
134        vehicle.Synchronize(time, driver_inputs, shoe_forces_left, shoe_forces_right)
135        app.Synchronize("", driver_inputs)
136
137        # Advance simulation for one timestep for all modules
138        driver.Advance(step_size)
139        terrain.Advance(step_size)
140        vehicle.Advance(step_size)
141        app.Advance(step_size)
142
143        # Increment frame number
144        step_number += 1
145
146        # Spin in place for real time to catch up
147        realtime_timer.Spin(step_size)
148
149    return 0
150
151
152# The path to the Chrono data directory containing various assets (meshes, textures, data files)
153# is automatically set, relative to the default location of this demo.
154# If running from a different directory, you must change the path to the data directory with:
155#chrono.SetChronoDataPath('path/to/data')
156
157veh.SetDataPath(chrono.GetChronoDataPath() + 'vehicle/')
158
159# Initial vehicle location and orientation
160initLoc = chrono.ChVectorD(0, 0, 1.1)
161initRot = chrono.ChQuaternionD(1, 0, 0, 0)
162
163# Collision type for chassis (PRIMITIVES, MESH, or NONE)
164chassis_collision_type = veh.CollisionType_NONE
165
166# Rigid terrain
167terrainHeight = 0;      # terrain height (FLAT terrain only)
168terrainLength = 100.0;  # size in X direction
169terrainWidth = 100.0;   # size in Y direction
170
171# Point on chassis tracked by the camera
172trackPoint = chrono.ChVectorD(0.0, 0.0, 0.0)
173
174# Contact method
175contact_method = chrono.ChContactMethod_SMC
176
177# Simulation step sizes
178step_size = 5e-4;
179
180# Time interval between two render frames
181render_step_size = 1.0 / 60;  # FPS = 60
182
183main()
184