def AddContainerWall(body, mat, size, pos, visible=True): hsize = chrono.ChVectorD(0.5 * size.x, 0.5 * size.y, 0.5 * size.z) body.GetCollisionModel().AddBox(mat, hsize.x, hsize.y, hsize.z, pos) if visible: box = chrono.ChBoxShape() box.GetBoxGeometry().Pos = pos box.GetBoxGeometry().Size = hsize body.AddAsset(box)
def AddFallingItems(sys): # Shared contact materials for falling objects mat = chrono.ChMaterialSurfaceSMC() # Create falling rigid bodies (spheres and boxes etc.) for ix in range(-2, 3): for iz in range(-2, 3): # add spheres mass = 1 radius = 1.1 body = chrono.ChBody() comp = (2.0 / 5.0) * mass * radius**2 body.SetInertiaXX(chrono.ChVectorD(comp, comp, comp)) body.SetMass(mass) body.SetPos(chrono.ChVectorD(4.0 * ix + 0.1, 4.0, 4.0 * iz)) body.GetCollisionModel().ClearModel() body.GetCollisionModel().AddSphere(mat, radius) body.GetCollisionModel().BuildModel() body.SetCollide(True) sphere = chrono.ChSphereShape() sphere.GetSphereGeometry().rad = radius body.AddAsset(sphere) texture = chrono.ChTexture() texture.SetTextureFilename( chrono.GetChronoDataFile("textures/bluewhite.png")) body.AddAsset(texture) sys.AddBody(body) # add boxes mass = 1 hsize = chrono.ChVectorD(0.75, 0.75, 0.75) body = chrono.ChBody() body.SetMass(mass) body.SetPos(chrono.ChVectorD(4.0 * ix, 6.0, 4.0 * iz)) body.GetCollisionModel().ClearModel() body.GetCollisionModel().AddBox(mat, hsize.x, hsize.y, hsize.z) body.GetCollisionModel().BuildModel() body.SetCollide(True) box = chrono.ChBoxShape() box.GetBoxGeometry().Size = hsize body.AddAsset(box) texture = chrono.ChTexture() texture.SetTextureFilename( chrono.GetChronoDataFile("textures/pinkwhite.png")) body.AddAsset(texture) sys.AddBody(body)
def createGround(self): # Create ground self.ground = chrono.ChBody() self.mysystem.AddBody(self.ground) self.ground.SetIdentifier(-1) self.ground.SetPos(chrono.ChVectorD(0,-1,0)) self.ground.SetName("ground") self.ground.SetBodyFixed(True) self.box_ground = chrono.ChBoxShape() self.box_ground.GetBoxGeometry().Size = chrono.ChVectorD(5,0.01,5) self.ground.AddAsset(self.box_ground)
def createBody(self): # Create a fixed rigid body self.mbody1 = chrono.ChBody() self.mbody1.SetBodyFixed(False) self.mbody1.SetIdentifier(1) self.mbody1.SetPos(chrono.ChVectorD(0, 0, -0.2)) self.mbody1.SetRot(chrono.ChQuaternionD(0, 0, 0, 1)) self.mysystem.Add(self.mbody1) self.mboxasset = chrono.ChBoxShape() self.mboxasset.GetBoxGeometry().Size = chrono.ChVectorD(0.2, 0.5, 0.1) self.mbody1.AddAsset(self.mboxasset)
def add_boxShape(MiroSystem, size_x, size_y, size_z, pos, texture='test.jpg', scale=[4,3], Collide=True, Fixed=True, rotX=0, rotY=0, rotZ=0, rotOrder=['x','y','z'], rotAngle=0, rotAxis=[1,0,0], rotDegrees=True, mass=False, density=1000, dynamic=False, color=[0.5, 0.5, 0.5]): '''system, size_x, size_y, size_z, pos, texture, scale = [5,5], hitbox = True/False''' # Convert position to chrono vector, supports using chvector as input as well ChPos = ChVecify(pos) ChRotAxis = ChVecify(rotAxis) # Create a box body_box = chrono.ChBody() body_box.SetBodyFixed(Fixed) body_box.SetPos(ChPos) if not mass: mass = density * size_x * size_y * size_z inertia_brick_xx = (size_y**2 + size_z**2)*mass/3 inertia_brick_yy = (size_x**2 + size_z**2)*mass/3 inertia_brick_zz = (size_x**2 + size_y**2)*mass/3 body_box.SetMass(mass) body_box.SetInertiaXX(chrono.ChVectorD(inertia_brick_xx, inertia_brick_yy, inertia_brick_zz)) # Collision shape body_box.GetCollisionModel().ClearModel() body_box.GetCollisionModel().AddBox(size_x/2, size_y/2, size_z/2) # hemi sizes body_box.GetCollisionModel().BuildModel() body_box.SetCollide(Collide) # Visualization shape body_box_shape = chrono.ChBoxShape() body_box_shape.GetBoxGeometry().Size = chrono.ChVectorD(size_x/2, size_y/2, size_z/2) body_box_shape.SetColor(chrono.ChColor(0.4,0.4,0.5)) body_box.GetAssets().push_back(body_box_shape) # Body texture if texture: # Filter 'textures/' out of the texture name, it's added later if len(texture) > len('textures/'): if texture[0:len('textures/')] == 'textures/': texture = texture[len('textures/'):] body_box_texture = chrono.ChTexture() body_box_texture.SetTextureFilename(chrono.GetChronoDataFile('textures/'+texture)) body_box_texture.SetTextureScale(scale[0], scale[1]) body_box.GetAssets().push_back(body_box_texture) rotateBody(body_box, rotX, rotY, rotZ, rotOrder, rotAngle, rotAxis, rotDegrees) if MiroSystem: ChSystem = MiroSystem.Get_APIsystem()[0] ChSystem.Add(body_box) return body_box
# Create the first shaft body # --------------------------- shaft_1 = chrono.ChBody() mysystem.AddBody(shaft_1) shaft_1.SetIdentifier(1) shaft_1.SetBodyFixed(False) shaft_1.SetCollide(False) shaft_1.SetMass(1) shaft_1.SetInertiaXX(chrono.ChVectorD(1, 1, 0.2)) shaft_1.SetPos(chrono.ChVectorD(0, 0, -hl)) shaft_1.SetRot(chrono.ChQuaternionD(1, 0, 0, 0)) # Add visualization assets to represent the shaft (a box) and the arm of the # universal joint's cross associated with this shaft (a cylinder) box_1 = chrono.ChBoxShape() box_1.GetBoxGeometry().Size = chrono.ChVectorD(0.15, 0.15, 0.9 * hl) shaft_1.AddAsset(box_1) cyl_2 = chrono.ChCylinderShape() cyl_2.GetCylinderGeometry().p1 = chrono.ChVectorD(-0.2, 0, hl) cyl_2.GetCylinderGeometry().p2 = chrono.ChVectorD(0.2, 0, hl) cyl_2.GetCylinderGeometry().rad = 0.05 shaft_1.AddAsset(cyl_2) col = chrono.ChColorAsset() col.SetColor(chrono.ChColor(0.9, 0.4, 0.1)) shaft_1.AddAsset(col) # Create the second shaft body # ----------------------------
ground.SetBodyFixed(True) ground.GetCollisionModel().ClearModel() ground.GetCollisionModel().AddBox(ground_mat, 5.0, 1.0, 5.0, chrono.ChVectorD(0, -1, 0)) ground.GetCollisionModel().AddBox(ground_mat, 0.1, 1.0, 5.1, chrono.ChVectorD(-5, 0, 0)) ground.GetCollisionModel().AddBox(ground_mat, 0.1, 1.0, 5.1, chrono.ChVectorD(5, 0, 0)) ground.GetCollisionModel().AddBox(ground_mat, 5.1, 1.0, 0.1, chrono.ChVectorD(0, 0, -5)) ground.GetCollisionModel().AddBox(ground_mat, 5.1, 1.0, 0.1, chrono.ChVectorD(0, 1, 5)) ground.GetCollisionModel().BuildModel() vshape_1 = chrono.ChBoxShape() vshape_1.GetBoxGeometry().SetLengths(chrono.ChVectorD(10, 2, 10)) vshape_1.GetBoxGeometry().Pos = chrono.ChVectorD(0, -1, 0) ground.AddAsset(vshape_1) vshape_2 = chrono.ChBoxShape() vshape_2.GetBoxGeometry().SetLengths(chrono.ChVectorD(0.2, 2, 10.2)) vshape_2.GetBoxGeometry().Pos = chrono.ChVectorD(-5, 0, 0) ground.AddAsset(vshape_2) vshape_3 = chrono.ChBoxShape() vshape_3.GetBoxGeometry().SetLengths(chrono.ChVectorD(0.2, 2, 10.2)) vshape_3.GetBoxGeometry().Pos = chrono.ChVectorD(5, 0, 0) ground.AddAsset(vshape_3) vshape_4 = chrono.ChBoxShape()
# --------------------------------------------------------------------- # # Create the simulation system and add items # mysystem = chrono.ChSystemNSC() # Create a fixed rigid body mbody1 = chrono.ChBody() mbody1.SetBodyFixed(True) mbody1.SetPos(chrono.ChVectorD(0, 0, -0.2)) mysystem.Add(mbody1) mboxasset = chrono.ChBoxShape() mboxasset.GetBoxGeometry().Size = chrono.ChVectorD(0.2, 0.5, 0.1) mbody1.AddAsset(mboxasset) # Create a swinging rigid body mbody2 = chrono.ChBody() mbody2.SetBodyFixed(False) mysystem.Add(mbody2) mboxasset = chrono.ChBoxShape() mboxasset.GetBoxGeometry().Size = chrono.ChVectorD(0.2, 0.5, 0.1) mbody2.AddAsset(mboxasset) mboxtexture = chrono.ChTexture() mboxtexture.SetTextureFilename('../../../data/textures/concrete.jpg')
def AddContainer(sys): # The fixed body (5 walls) fixedBody = chrono.ChBody() fixedBody.SetMass(1.0) fixedBody.SetBodyFixed(True) fixedBody.SetPos(chrono.ChVectorD()) fixedBody.SetCollide(True) # Contact material for container fixed_mat = chrono.ChMaterialSurfaceSMC() fixedBody.GetCollisionModel().ClearModel() AddContainerWall(fixedBody, fixed_mat, chrono.ChVectorD(20, 1, 20), chrono.ChVectorD(0, -5, 0)) AddContainerWall(fixedBody, fixed_mat, chrono.ChVectorD(1, 10, 20.99), chrono.ChVectorD(-10, 0, 0)) AddContainerWall(fixedBody, fixed_mat, chrono.ChVectorD(1, 10, 20.99), chrono.ChVectorD(10, 0, 0)) AddContainerWall(fixedBody, fixed_mat, chrono.ChVectorD(20.99, 10, 1), chrono.ChVectorD(0, 0, -10), False) AddContainerWall(fixedBody, fixed_mat, chrono.ChVectorD(20.99, 10, 1), chrono.ChVectorD(0, 0, 10)) fixedBody.GetCollisionModel().BuildModel() texture = chrono.ChTexture() texture.SetTextureFilename( chrono.GetChronoDataFile("textures/concrete.jpg")) fixedBody.AddAsset(texture) sys.AddBody(fixedBody) # The rotating mixer body rotatingBody = chrono.ChBody() rotatingBody.SetMass(10.0) rotatingBody.SetInertiaXX(chrono.ChVectorD(50, 50, 50)) rotatingBody.SetPos(chrono.ChVectorD(0, -1.6, 0)) rotatingBody.SetCollide(True) # Contact material for mixer body rot_mat = chrono.ChMaterialSurfaceSMC() hsize = chrono.ChVectorD(5, 2.75, 0.5) rotatingBody.GetCollisionModel().ClearModel() rotatingBody.GetCollisionModel().AddBox(rot_mat, hsize.x, hsize.y, hsize.z) rotatingBody.GetCollisionModel().BuildModel() box = chrono.ChBoxShape() box.GetBoxGeometry().Size = hsize rotatingBody.AddAsset(box) rotatingBody.AddAsset(texture) sys.AddBody(rotatingBody) # A motor between the two my_motor = chrono.ChLinkMotorRotationSpeed() my_motor.Initialize( rotatingBody, fixedBody, chrono.ChFrameD(chrono.ChVectorD(0, 0, 0), chrono.Q_from_AngAxis(chrono.CH_C_PI_2, chrono.VECT_X))) mfun = chrono.ChFunction_Const(chrono.CH_C_PI / 2.0) # speed w=90°/s my_motor.SetSpeedFunction(mfun) sys.AddLink(my_motor) return rotatingBody
body_brick.SetMass(mass_brick) body_brick.SetInertiaXX( chrono.ChVectorD(inertia_brick, inertia_brick, inertia_brick)) # set collision surface properties body_brick.SetMaterialSurface(brick_material) # Collision shape body_brick.GetCollisionModel().ClearModel() body_brick.GetCollisionModel().AddBox(size_brick_x / 2, size_brick_y / 2, size_brick_z / 2) # must set half sizes body_brick.GetCollisionModel().BuildModel() body_brick.SetCollide(True) # Visualization shape, for rendering animation body_brick_shape = chrono.ChBoxShape() body_brick_shape.GetBoxGeometry().Size = chrono.ChVectorD( size_brick_x / 2, size_brick_y / 2, size_brick_z / 2) if iy % 2 == 0: body_brick_shape.SetColor(chrono.ChColor( 0.65, 0.65, 0.6)) # set gray color only for odd bricks body_brick.GetAssets().push_back(body_brick_shape) my_system.Add(body_brick) # Create the room floor: a simple fixed rigid body with a collision shape # and a visualization shape body_floor = chrono.ChBody() body_floor.SetBodyFixed(True) body_floor.SetPos(chrono.ChVectorD(0, -2, 0))
def run_sim(traits, trial_num, gen_num, difficulty_level): my_system = chrono.ChSystemNSC() # Set the default outward/inward shape margins for collision detection chrono.ChCollisionModel.SetDefaultSuggestedEnvelope(0.001) chrono.ChCollisionModel.SetDefaultSuggestedMargin(0.001) # Sets simulation precision my_system.SetMaxItersSolverSpeed(70) # Create a contact material (surface property)to share between all objects. rollfrict_param = 0.5 / 10.0 * 0.05 brick_material = chrono.ChMaterialSurfaceNSC() brick_material.SetFriction(0.5) brick_material.SetDampingF(0.2) brick_material.SetCompliance(0.0000001) brick_material.SetComplianceT(0.0000001) brick_material.SetRollingFriction(rollfrict_param) brick_material.SetSpinningFriction(0.00000001) brick_material.SetComplianceRolling(0.0000001) brick_material.SetComplianceSpinning(0.0000001) # Create the set of bricks in a vertical stack, along Y axis block_bodies = [] # visualizes bodies block_shapes = [] # geometry purposes current_y = 0 for block_index in range(0, 4): size_brick_x = traits[block_index][0] size_brick_z = traits[block_index][1] size_brick_y = traits[block_index][2] if size_brick_y < settings.MIN_DIMENSIONS_THRESHOLD or size_brick_x < settings.MIN_DIMENSIONS_THRESHOLD or size_brick_z < settings.MIN_DIMENSIONS_THRESHOLD: return [-50, traits] mass_brick = settings.BLOCK_MASS inertia_brick_xx = 1 / 12 * mass_brick * (pow(size_brick_z, 2) + pow(size_brick_y, 2)) inertia_brick_yy = 1 / 12 * mass_brick * (pow(size_brick_x, 2) + pow(size_brick_z, 2)) inertia_brick_zz = 1 / 12 * mass_brick * (pow(size_brick_x, 2) + pow(size_brick_y, 2)) body_brick = chrono.ChBody() body_brick.SetPos(chrono.ChVectorD(0, current_y + 0.5 * size_brick_y, 0)) # set initial position current_y += size_brick_y # set tower block positions # setting mass properties body_brick.SetMass(mass_brick) body_brick.SetInertiaXX(chrono.ChVectorD(inertia_brick_xx, inertia_brick_yy, inertia_brick_zz)) # set collision surface properties body_brick.SetMaterialSurface(brick_material) # Collision shape body_brick.GetCollisionModel().ClearModel() body_brick.GetCollisionModel().AddBox(size_brick_x / 2, size_brick_y / 2, size_brick_z / 2) # must set half sizes body_brick.GetCollisionModel().BuildModel() body_brick.SetCollide(True) # Visualization shape, for rendering animation body_brick_shape = chrono.ChBoxShape() body_brick_shape.GetBoxGeometry().Size = chrono.ChVectorD(size_brick_x / 2, size_brick_y / 2, size_brick_z / 2) if block_index % 2 == 0: body_brick_shape.SetColor(chrono.ChColor(0.65, 0.65, 0.6)) # set gray color only for odd bricks body_brick.GetAssets().push_back(body_brick_shape) my_system.Add(body_brick) block_bodies.append(body_brick) block_shapes.append(body_brick_shape); # Create the room floor body_floor = chrono.ChBody() body_floor.SetBodyFixed(True) body_floor.SetPos(chrono.ChVectorD(0, -2, 0)) body_floor.SetMaterialSurface(brick_material) # Floor's collision shape body_floor.GetCollisionModel().ClearModel() body_floor.GetCollisionModel().AddBox(3, 1, 3) # hemi sizes default: 3,1,3 body_floor.GetCollisionModel().BuildModel() body_floor.SetCollide(True) # Visualization shape body_floor_shape = chrono.ChBoxShape() body_floor_shape.GetBoxGeometry().Size = chrono.ChVectorD(3, 1, 3) body_floor.GetAssets().push_back(body_floor_shape) body_floor_texture = chrono.ChTexture() # body_floor_texture.SetTextureFilename(chrono.GetChronoDataPath() + 'concrete.jpg') body_floor.GetAssets().push_back(body_floor_texture) my_system.Add(body_floor) # Create the shaking table, as a box size_table_x = 1 size_table_y = 0.2 size_table_z = 1 body_table = chrono.ChBody() body_table.SetPos(chrono.ChVectorD(0, -size_table_y / 2, 0)) body_table.SetMaterialSurface(brick_material) # Collision shape body_table.GetCollisionModel().ClearModel() body_table.GetCollisionModel().AddBox(size_table_x / 2, size_table_y / 2, size_table_z / 2) # hemi sizes body_table.GetCollisionModel().BuildModel() body_table.SetCollide(True) # Visualization shape body_table_shape = chrono.ChBoxShape() body_table_shape.GetBoxGeometry().Size = chrono.ChVectorD(size_table_x / 2, size_table_y / 2, size_table_z / 2) body_table_shape.SetColor(chrono.ChColor(0.4, 0.4, 0.5)) body_table.GetAssets().push_back(body_table_shape) body_table_texture = chrono.ChTexture() # body_table_texture.SetTextureFilename(chrono.GetChronoDataPath() + 'concrete.jpg') body_table.GetAssets().push_back(body_table_texture) my_system.Add(body_table) # Makes the table shake link_shaker = chrono.ChLinkLockLock() # link_shaker.SetMotion_X() link_shaker.Initialize(body_table, body_floor, chrono.CSYSNORM) my_system.Add(link_shaker) # ..create the function for imposed y vertical motion, etc. mfunY = chrono.ChFunction_Sine(0, settings.TABLE_FREQ_Y, settings.TABLE_AMP_Y) # phase, frequency, amplitude # ..create the function for imposed z horizontal motion, etc. mfunZ = chrono.ChFunction_Sine(0, settings.TABLE_FREQ_Z, settings.TABLE_AMP_Z) # phase, frequency, amplitude # ..create the function for imposed x horizontal motion, etc. mfunX = chrono.ChFunction_Sine(2, 0, 0) # phase, frequency, amplitude print("Sim env_level " + str(difficulty_level)) if difficulty_level == settings.SHAKE_IN_X_AXIS_LEVEL: mfunX = chrono.ChFunction_Sine(2, settings.TABLE_FREQ_X, settings.TABLE_AMP_X) # phase, frequency, amplitude elif difficulty_level >= settings.SHAKE_IN_X_AND_Z_AXIS_LEVEL: increment = 0.25 * difficulty_level mfunX = chrono.ChFunction_Sine(2, settings.TABLE_FREQ_X + increment, settings.TABLE_AMP_X) # phase, frequency, amplitude mfunZ = chrono.ChFunction_Sine(0, settings.TABLE_FREQ_Z + increment, settings.TABLE_AMP_Z) # phase, frequency, amplitude link_shaker.SetMotion_Y(mfunY) link_shaker.SetMotion_Z(mfunZ) link_shaker.SetMotion_X(mfunX) # --------------------------------------------------------------------- # # Create an Irrlicht application to visualize the system window_name = "Tower Trial: " + str(trial_num) + " Gen: " + str(gen_num) if trial_num == -1: window_name = "Initializing Population..." app = chronoirr.ChIrrApp(my_system, window_name, chronoirr.dimension2du(settings.SCREEN_WIDTH, settings.SCREEN_HEIGHT)) app.AddTypicalSky() app.AddTypicalLogo(chrono.GetChronoDataPath() + 'logo_pychrono_alpha.png') app.AddTypicalCamera(chronoirr.vector3df(settings.CAMERA_X, settings.CAMERA_Y, settings.CAMERA_Z)) app.AddLightWithShadow(chronoirr.vector3df(2, 4, 2), # point chronoirr.vector3df(0, 0, 0), # aimpoint 9, # radius (power) 1, 9, # near, far 30) # Committing visualization app.AssetBindAll() app.AssetUpdateAll(); app.AddShadowAll(); # --------------------------------------------------------------------- # # Run the simulation. This is where all of the constraints are set # app.SetTimestep(settings.SPEED) app.SetTryRealtime(True) app.GetDevice().run() fitness = 0 brick1_init = block_bodies[0].GetPos().y brick2_init = block_bodies[1].GetPos().y brick3_init = block_bodies[2].GetPos().y brick4_init = block_bodies[3].GetPos().y # Highest while True: brick1_curr_height = block_bodies[0].GetPos().y brick2_curr_height = block_bodies[1].GetPos().y brick3_curr_height = block_bodies[2].GetPos().y brick4_curr_height = block_bodies[3].GetPos().y # Highest # Break conditions if my_system.GetChTime() > settings.SIMULATION_RUNTIME: break if my_system.GetChTime() > settings.SIMULATION_RUNTIME / 2: mfunX = chrono.ChFunction_Sine(2, 0, 0) mfunZ = chrono.ChFunction_Sine(2, 0, 0) link_shaker.SetMotion_Z(mfunZ) link_shaker.SetMotion_X(mfunX) # If the blocks fall out of line if brick1_init - brick1_curr_height > settings.CANCEL_SIM_THRESHOLD or \ brick2_init - brick2_curr_height > settings.CANCEL_SIM_THRESHOLD or \ brick3_init - brick3_curr_height > settings.CANCEL_SIM_THRESHOLD or \ brick4_init - brick4_curr_height > settings.CANCEL_SIM_THRESHOLD: break # Record fitness every 1/1000 of the runtime if 0.01 > my_system.GetChTime() % ((1 / 1000) * settings.SIMULATION_RUNTIME) > 0: if settings.FITNESS_FUNCTION == settings.Fitness.SumLengths: # Sum of size_y fitness += block_shapes[0].GetBoxGeometry().GetLengths().y + \ block_shapes[1].GetBoxGeometry().GetLengths().y + \ block_shapes[2].GetBoxGeometry().GetLengths().y + \ block_shapes[3].GetBoxGeometry().GetLengths().y elif settings.FITNESS_FUNCTION == settings.Fitness.MaxPosition: # Max of y positions fitness += max(block_bodies[0].GetPos().y, block_bodies[1].GetPos().y, block_bodies[2].GetPos().y, block_bodies[3].GetPos().y) elif settings.FITNESS_FUNCTION == settings.Fitness.MaxPositionSumLengths: # Max * sum of sizes fitness += max(block_bodies[0].GetPos().y, block_bodies[1].GetPos().y, block_bodies[2].GetPos().y, block_bodies[3].GetPos().y) * \ block_shapes[0].GetBoxGeometry().GetLengths().y + \ block_shapes[1].GetBoxGeometry().GetLengths().y + \ block_shapes[2].GetBoxGeometry().GetLengths().y + \ block_shapes[3].GetBoxGeometry().GetLengths().y app.BeginScene() app.DrawAll() for substep in range(0, 5): app.DoStep() app.EndScene() app.GetDevice().closeDevice() print("Fitness: " + str(fitness) + " Gen: " + str(gen_num)) return [fitness, traits]