1#------------------------------------------------------------------------------
2# Name:        pychrono example
3# Purpose:
4#
5# Author:      Lijing Yang
6#
7# Created:     6/12/2020
8# Copyright:   (c) ProjectChrono 2019
9#------------------------------------------------------------------------------
10
11
12import pychrono.core as chrono
13import pychrono.irrlicht as chronoirr
14
15print ("Example: demonstration of using friction models")
16
17# The path to the Chrono data directory containing various assets (meshes, textures, data files)
18# is automatically set, relative to the default location of this demo.
19# If running from a different directory, you must change the path to the data directory with:
20# chrono.SetChronoDataPath('relative/path/to/data/directory/')
21
22# ---------------------------------------------------------------------
23#
24#  Create the simulation system and add items
25#
26
27mphysicalSystem      = chrono.ChSystemNSC()
28
29# Create all the rigid bodies.
30mradius = 0.5
31density = 1000
32
33# Create a texture asset. It can be shared between bodies.
34textureasset = chrono.ChTexture(chrono.GetChronoDataFile("textures/bluewhite.png"))
35
36# Create some spheres that roll horizontally, with increasing rolling friction values
37for bi in range(10):
38    mat = chrono.ChMaterialSurfaceNSC()
39    mat.SetFriction(0.4)
40    mat.SetRollingFriction((float(bi) / 10.) * 0.05)
41
42    msphereBody = chrono.ChBodyEasySphere(mradius,  # radius size
43                                          1000,     # density
44                                          True,     # visualization?
45                                          True,     # collision?
46                                          mat)      # contact material
47
48    # Set some properties
49    msphereBody.SetPos(chrono.ChVectorD(-7, mradius - 0.5, -5 + bi * mradius * 2.5))
50    msphereBody.AddAsset(textureasset)  # assets can be shared
51
52    # Set initial speed: rolling in horizontal direction
53    initial_angspeed = 10
54    initial_linspeed = initial_angspeed * mradius
55    msphereBody.SetWvel_par(chrono.ChVectorD(0, 0, -initial_angspeed))
56    msphereBody.SetPos_dt(chrono.ChVectorD(initial_linspeed, 0, 0))
57
58    # Add to the system
59    mphysicalSystem.Add(msphereBody)
60
61# Create some spheres that spin on place, for a 'drilling friction' case, with increasing spinning friction values
62for bi in range(10):
63    mat = chrono.ChMaterialSurfaceNSC()
64    mat.SetFriction(0.4)
65    mat.SetSpinningFriction((float(bi) / 10) * 0.02)
66
67    msphereBody = chrono.ChBodyEasySphere(mradius,  # radius size
68                                          1000,     # density
69                                          True,     # visualization?
70                                          True,     # collision?
71                                          mat)      # contact material
72    # Set some properties
73    msphereBody.SetPos(chrono.ChVectorD(-8, 1 + mradius - 0.5, -5 + bi * mradius * 2.5))
74    msphereBody.AddAsset(textureasset)  # assets can be shared
75
76    # Set initial speed: spinning in vertical direction
77    msphereBody.SetWvel_par(chrono.ChVectorD(0, 20, 0))
78
79    # Add to the system
80    mphysicalSystem.Add(msphereBody)
81
82    # Notes:
83    # - setting nonzero spinning friction and/or setting nonzero rolling friction
84    #   affects the speed of the solver (each contact eats 2x of CPU time repsect to the
85    #   case of simple sliding/staic contact)
86    # - avoid using zero spinning friction with nonzero rolling friction.
87
88# Create a container fixed to ground
89bin = chrono.ChBody()
90bin.SetPos(chrono.ChVectorD(0, -1, 0))
91bin.SetBodyFixed(True)
92bin.SetCollide(True)
93
94# Set rolling and spinning friction coefficients for the container.
95# By default, the composite material will use the minimum value for an interacting collision pair.
96bin_mat = chrono.ChMaterialSurfaceNSC()
97bin_mat.SetRollingFriction(1)
98bin_mat.SetSpinningFriction(1)
99
100# Add collision geometry and visualization shapes for the floor and the 4 walls
101bin.GetCollisionModel().ClearModel()
102bin.GetCollisionModel().AddBox(bin_mat, 20. / 2., 1. / 2., 20. / 2., chrono.ChVectorD(0, 0, 0))
103bin.GetCollisionModel().AddBox(bin_mat, 1. / 2.,  2. / 2., 20.99/2., chrono.ChVectorD(-10, 1, 0))
104bin.GetCollisionModel().AddBox(bin_mat, 1. / 2.,  2. / 2., 20.99/2., chrono.ChVectorD( 10, 1, 0))
105bin.GetCollisionModel().AddBox(bin_mat, 20.99/2., 2. / 2., 1. / 2., chrono.ChVectorD(0, 1, -10))
106bin.GetCollisionModel().AddBox(bin_mat, 20.99/2., 2. / 2., 1. / 2., chrono.ChVectorD(0, 1,  10))
107bin.GetCollisionModel().BuildModel()
108
109vshape_1 = chrono.ChBoxShape()
110vshape_1.GetBoxGeometry().SetLengths(chrono.ChVectorD(20, 1, 20))
111vshape_1.GetBoxGeometry().Pos = chrono.ChVectorD(0, 0, 0)
112bin.AddAsset(vshape_1)
113
114vshape_2 = chrono.ChBoxShape()
115vshape_2.GetBoxGeometry().SetLengths(chrono.ChVectorD(1, 2, 20.99))
116vshape_2.GetBoxGeometry().Pos = chrono.ChVectorD(-10, 1, 0)
117bin.AddAsset(vshape_2)
118
119vshape_3 = chrono.ChBoxShape()
120vshape_3.GetBoxGeometry().SetLengths(chrono.ChVectorD(1, 2, 20.99))
121vshape_3.GetBoxGeometry().Pos = chrono.ChVectorD(10, 1, 0)
122bin.AddAsset(vshape_3)
123
124vshape_4 = chrono.ChBoxShape()
125vshape_4.GetBoxGeometry().SetLengths(chrono.ChVectorD(20.99, 2, 1))
126vshape_4.GetBoxGeometry().Pos = chrono.ChVectorD(0, 1, -10)
127bin.AddAsset(vshape_4)
128
129vshape_5 = chrono.ChBoxShape()
130vshape_5.GetBoxGeometry().SetLengths(chrono.ChVectorD(20.99, 2, 1))
131vshape_5.GetBoxGeometry().Pos = chrono.ChVectorD(0, 1, 10)
132bin.AddAsset(vshape_5)
133bin.AddAsset(chrono.ChTexture(chrono.GetChronoDataFile("textures/blue.png")))
134
135mphysicalSystem.Add(bin)
136
137# ---------------------------------------------------------------------
138#
139#  Create an Irrlicht application to visualize the system
140#
141
142myapplication = chronoirr.ChIrrApp(mphysicalSystem, 'PyChrono example: Friction', chronoirr.dimension2du(1024,768))
143
144myapplication.AddTypicalSky()
145myapplication.AddTypicalLogo(chrono.GetChronoDataFile('logo_pychrono_alpha.png'))
146myapplication.AddTypicalCamera(chronoirr.vector3df(0, 14, -20))
147myapplication.AddTypicalLights(chronoirr.vector3df(30., 100., 30.),
148                               chronoirr.vector3df(-30, 100., 30.))
149
150# ==IMPORTANT!== Use this function for adding a ChIrrNodeAsset to all items
151# in the system. These ChIrrNodeAsset assets are 'proxies' to the Irrlicht meshes.
152# If you need a finer control on which item really needs a visualization proxy in
153# Irrlicht, just use application.AssetBind(myitem) on a per-item basis.
154
155myapplication.AssetBindAll()
156
157# ==IMPORTANT!== Use this function for 'converting' into Irrlicht meshes the assets
158# that you added to the bodies into 3D shapes, they can be visualized by Irrlicht!
159
160myapplication.AssetUpdateAll()
161
162
163# ---------------------------------------------------------------------
164#
165#  Run the simulation
166#
167
168mphysicalSystem.SetSolverType(chrono.ChSolver.Type_APGD)
169myapplication.SetTimestep(0.005)
170myapplication.SetTryRealtime(True)
171
172while(myapplication.GetDevice().run()):
173    myapplication.BeginScene(True, True, chronoirr.SColor(255, 140, 161, 192))
174    myapplication.DrawAll()
175    myapplication.DoStep()
176    myapplication.EndScene()
177
178
179
180
181
182