Introduction to Python (2)

Basic creation of a physical system and rigid bodies, using PyChrono.

  • create a ChSystem
  • create and add rigid bodies
  • iterate on created contacts
  • iterate on added rigid bodies using the Python syntax
1 #------------------------------------------------------------------------------
2 # Name: pychrono example
3 # Purpose:
4 #
5 # Author: Alessandro Tasora
6 #
7 # Created: 1/01/2019
8 # Copyright: (c) ProjectChrono 2019
9 #------------------------------------------------------------------------------
10 
11 print ("Second tutorial: create and populate a physical system");
12 
13 
14 # Load the Chrono::Engine core module!
15 import pychrono as chrono
16 
17 
18 # Create a physical system,
19 my_system = chrono.ChSystemNSC()
20 
21 # Add two bodies
22 my_shbodyA = chrono.ChBody()
23 my_shbodyA.SetMass(20)
24 my_shbodyA.SetName('BodyA')
25 my_shbodyA.SetInertiaXX( chrono.ChVectorD(10,10,10) )
26 print (my_shbodyA.GetInertia() )
27 my_shbodyA.SetPos(chrono.ChVectorD(1,-1,0))
28 my_shbodyA.GetCollisionModel().AddBox(10,1,10)
29 my_shbodyA.SetBodyFixed(True)
30 my_shbodyA.SetCollide(True)
31 
32 my_shbodyB = chrono.ChBody()
33 my_shbodyB.SetName('BodyB')
34 my_shbodyB.SetPos(chrono.ChVectorD(0,2,0))
35 my_shbodyB.GetCollisionModel().AddBox(1,1,1)
36 my_shbodyB.SetCollide(True)
37 
38 my_shmarker = chrono.ChMarker()
39 my_funct = chrono.ChFunction_Sine(0,0.5,3)
40 my_shmarker.SetMotion_X(my_funct)
41 my_shmarker.SetPos(chrono.ChVectorD(1,2,3))
42 my_shbodyB.AddMarker(my_shmarker)
43 
44 my_system.Add(my_shbodyA)
45 my_system.Add(my_shbodyB)
46 
47 # Define surface material(s)
48 my_shmaterial = chrono.ChMaterialSurfaceNSC()
49 my_shmaterial.SetFriction(0.3)
50 my_shmaterial.SetCompliance(0)
51 my_shbodyA.SetMaterialSurface(my_shmaterial)
52 my_shbodyB.SetMaterialSurface(my_shmaterial)
53 
54 
55 # Add Contact callback (TO FIX!!)
56 ##class MyContactCallback(chrono.ChCustomCollisionPointCallbackP):
57 ## def __init__(self):
58 ## chrono.ChCustomCollisionPointCallbackP.__init__(self)
59 ## def ContactCallback(self,collinfo,matcouple):
60 ## print (' add contact: ' , collinfo.distance, matcouple.static_friction)
61 ##
62 ##my_call = MyContactCallback()
63 ##my_system.SetCustomCollisionPointCallback(my_call)
64 
65 # Report Contact callback
66 class MyReportContactCallback(chrono.ReportContactCallback):
67  def __init__(self):
68  chrono.ReportContactCallback.__init__(self)
69  def OnReportContact(self,vA,vB,cA,dist,rad,force,torque,modA,modB):
70  bodyUpA = chrono.CastContactableToChBody(modA)
71  nameA = bodyUpA.GetName()
72  bodyUpB = chrono.CastContactableToChBody(modB)
73  nameB = bodyUpB.GetName()
74  print (' contact: point A=' , vA, ' dist=',dist, 'Body A:', nameA, 'Body B:', nameB)
75  return True # return False to stop reporting contacts
76 
77 my_rep = MyReportContactCallback()
78 
79 
80 
81 # Simulation loop
82 my_system.SetChTime(0)
83 while (my_system.GetChTime() < 1.2) :
84 
85  my_system.DoStepDynamics(0.01)
86 
87  print ('time=', my_system.GetChTime(), ' bodyB y=', my_shbodyB.GetPos().y)
88 
89  my_system.GetContactContainer().ReportAllContacts(my_rep)
90 
91 
92 # Iterate over added bodies (Python style)
93 print ('Positions of all bodies in the system:')
94 for abody in my_system.Get_bodylist():
95  print (' body pos=', abody.GetPos() )
96 
97 
98 # Move a body, using a ChFrame
99 my_displacement = chrono.ChFrameMovingD(chrono.ChVectorD(5,1,0));
100 my_shbodyA %= my_displacement
101 # ..also as:
102 # my_shbody.ConcatenatePreTransformation(my_displacement)
103 
104 print ('Moved body pos=', my_shbodyA.GetPos() )
105 
106 
107 # Use a body with an auxiliary reference (REF) that does not correspond
108 # to the center of gravity (COG)
109 body_1= chrono.ChBodyAuxRef()
110 body_1.SetName('Parte1-1')
111 body_1.SetPos(chrono.ChVectorD(-0.0445347481124079,0.0676266363930238,-0.0230808979433518))
112 body_1.SetRot(chrono.ChQuaternionD(1,0,0,0))
113 body_1.SetMass(346.17080777653)
114 body_1.SetInertiaXX(chrono.ChVectorD(48583.2418823358,526927.118351673,490689.966726565))
115 body_1.SetInertiaXY(chrono.ChVectorD(1.70380722975012e-11,1.40840344485366e-11,-2.31869065456271e-12))
116 body_1.SetFrame_COG_to_REF(chrono.ChFrameD(chrono.ChVectorD(68.9923703887577,-60.1266363930238,70.1327223302498),chrono.ChQuaternionD(1,0,0,0)))
117 myasset = chrono.ChObjShapeFile()
118 myasset.SetFilename("shapes/test.obj")
119 body_1.GetAssets().push_back(myasset)
120 
121 print ('Done...')