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.SetInertiaXX( chrono.ChVectorD(10,10,10) )
25 print (my_shbodyA.GetInertia() )
26 my_shbodyA.SetPos(chrono.ChVectorD(1,-1,0))
27 my_shbodyA.GetCollisionModel().AddBox(10,1,10)
28 my_shbodyA.SetBodyFixed(True)
29 my_shbodyA.SetCollide(True)
30 
31 my_shbodyB = chrono.ChBody()
32 my_shbodyB.SetPos(chrono.ChVectorD(0,2,0))
33 my_shbodyB.GetCollisionModel().AddBox(1,1,1)
34 my_shbodyB.SetCollide(True)
35 
36 my_shmarker = chrono.ChMarker()
37 my_funct = chrono.ChFunction_Sine(0,0.5,3)
38 my_shmarker.SetMotion_X(my_funct)
39 my_shmarker.SetPos(chrono.ChVectorD(1,2,3))
40 my_shbodyB.AddMarker(my_shmarker)
41 
42 my_system.Add(my_shbodyA)
43 my_system.Add(my_shbodyB)
44 
45 # Define surface material(s)
46 my_shmaterial = chrono.ChMaterialSurfaceNSC()
47 my_shmaterial.SetFriction(0.3)
48 my_shmaterial.SetCompliance(0)
49 my_shbodyA.SetMaterialSurface(my_shmaterial)
50 my_shbodyB.SetMaterialSurface(my_shmaterial)
51 
52 
53 # Add Contact callback (TO FIX!!)
54 ##class MyContactCallback(chrono.ChCustomCollisionPointCallbackP):
55 ## def __init__(self):
56 ## chrono.ChCustomCollisionPointCallbackP.__init__(self)
57 ## def ContactCallback(self,collinfo,matcouple):
58 ## print (' add contact: ' , collinfo.distance, matcouple.static_friction)
59 ##
60 ##my_call = MyContactCallback()
61 ##my_system.SetCustomCollisionPointCallback(my_call)
62 
63 # Report Contact callback
64 class MyReportContactCallback(chrono.ChReportContactCallbackP):
65  def __init__(self):
66  chrono.ChReportContactCallbackP.__init__(self)
67  def OnReportContact(self,vA,vB,cA,dist,rad,force,torque,modA,modB):
68  print (' contact: point A=' , vA, ' dist=',dist)
69  return True # return False to stop reporting contacts
70 
71 my_rep = MyReportContactCallback()
72 
73 
74 
75 # Simulation loop
76 my_system.SetChTime(0)
77 while (my_system.GetChTime() < 1.2) :
78 
79  my_system.DoStepDynamics(0.01)
80 
81  print ('time=', my_system.GetChTime(), ' bodyB y=', my_shbodyB.GetPos().y)
82 
83  my_system.GetContactContainer().ReportAllContacts(my_rep)
84 
85 
86 # Iterate over added bodies (Python style)
87 print ('Positions of all bodies in the system:')
88 for abody in my_system.Get_bodylist():
89  print (' body pos=', abody.GetPos() )
90 
91 
92 # Move a body, using a ChFrame
93 my_displacement = chrono.ChFrameMovingD(chrono.ChVectorD(5,1,0));
94 my_shbodyA %= my_displacement
95 # ..also as:
96 # my_shbody.ConcatenatePreTransformation(my_displacement)
97 
98 print ('Moved body pos=', my_shbodyA.GetPos() )
99 
100 
101 # Use a body with an auxiliary reference (REF) that does not correspond
102 # to the center of gravity (COG)
103 body_1= chrono.ChBodyAuxRef()
104 body_1.SetName('Parte1-1')
105 body_1.SetPos(chrono.ChVectorD(-0.0445347481124079,0.0676266363930238,-0.0230808979433518))
106 body_1.SetRot(chrono.ChQuaternionD(1,0,0,0))
107 body_1.SetMass(346.17080777653)
108 body_1.SetInertiaXX(chrono.ChVectorD(48583.2418823358,526927.118351673,490689.966726565))
109 body_1.SetInertiaXY(chrono.ChVectorD(1.70380722975012e-11,1.40840344485366e-11,-2.31869065456271e-12))
110 body_1.SetFrame_COG_to_REF(chrono.ChFrameD(chrono.ChVectorD(68.9923703887577,-60.1266363930238,70.1327223302498),chrono.ChQuaternionD(1,0,0,0)))
111 myasset = chrono.ChObjShapeFile()
112 myasset.SetFilename("shapes/test.obj")
113 body_1.GetAssets().push_back(myasset)
114 
115 print ('Done...')