rigidBodyTutorial3.py

You can view and download this file on Github: rigidBodyTutorial3.py

  1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2# This is an EXUDYN example
  3#
  4# Details:  3D rigid body tutorial with 2 bodies and revolute joints, using new mainSystemExtension functionality
  5#
  6# Author:   Johannes Gerstmayr
  7# Date:     2023-05-16
  8#
  9# Copyright:This file is part of Exudyn. Exudyn is free software. You can redistribute it and/or modify it under the terms of the Exudyn license. See 'LICENSE.txt' for more details.
 10#
 11#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 12
 13import exudyn as exu
 14from exudyn.utilities import * #includes itemInterface, graphicsDataUtilities and rigidBodyUtilities
 15import numpy as np
 16
 17SC = exu.SystemContainer()
 18mbs = SC.AddSystem()
 19
 20
 21#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
 22#physical parameters
 23g =     [0,-9.81,0] #gravity
 24L = 1               #length
 25w = 0.1             #width
 26bodyDim=[L,w,w] #body dimensions
 27p0 =    [0,0,0]     #origin of pendulum
 28pMid0 = np.array([L*0.5,0,0]) #center of mass, body0
 29
 30#ground body, located at specific position (there could be several ground objects)
 31oGround = mbs.CreateGround(referencePosition=[0,0,0])
 32
 33#%%++++++++++++++++++++++++++++++++++++++++++++++++++++
 34#first link:
 35iCube0 = InertiaCuboid(density=5000, sideLengths=bodyDim)
 36iCube0 = iCube0.Translated([-0.25*L,0,0]) #transform COM, COM not at reference point!
 37
 38#graphics for body
 39graphicsBody0 = GraphicsDataOrthoCubePoint(centerPoint=[0,0,0],size=[L,w,w],color=color4red)
 40graphicsCOM0 = GraphicsDataBasis(origin=iCube0.com, length=2*w) #COM frame
 41
 42#create rigid node and body
 43b0=mbs.CreateRigidBody(inertia = iCube0, #includes COM
 44                       referencePosition = pMid0,
 45                       gravity = g,
 46                       graphicsDataList = [graphicsCOM0, graphicsBody0])
 47#revolute joint (free z-axis), axis and position given in global coordinates
 48#  using reference configuration
 49mbs.CreateRevoluteJoint(bodyNumbers=[oGround, b0], position=[0,0,0],
 50                        axis=[0,0,1], axisRadius=0.2*w, axisLength=1.4*w)
 51
 52
 53#%%++++++++++++++++++++++++++
 54#second link:
 55graphicsBody1 = GraphicsDataRigidLink(p0=[0,0,-0.5*L],p1=[0,0,0.5*L],
 56                                     axis0=[1,0,0], axis1=[0,0,0], radius=[0.06,0.05],
 57                                     thickness = 0.1, width = [0.12,0.12], color=color4lightgreen)
 58
 59b1=mbs.CreateRigidBody(inertia = InertiaCuboid(density=5000, sideLengths=[0.1,0.1,1]),
 60                            referencePosition = np.array([L,0,0.5*L]), #reference pos = center of mass, body1
 61                            gravity = g,
 62                            graphicsDataList = [graphicsBody1])
 63
 64#revolute joint (free x-axis), axis and position given in global coordinates,
 65#  using reference configuration
 66mbs.CreateRevoluteJoint(bodyNumbers=[b0, b1], position=[L,0,0],
 67                        axis=[1,0,0], axisRadius=0.2*w, axisLength=1.4*w)
 68
 69#forces can be added like in the following
 70force = [0,0.5,0]       #0.5N   in y-direction
 71torque = [0.1,0,0]      #0.1Nm around x-axis
 72mbs.CreateForce(bodyNumber=b1,
 73                loadVector=force,
 74                localPosition=[0,0,0.5], #at tip
 75                bodyFixed=False) #if True, direction would corotate with body
 76mbs.CreateTorque(bodyNumber=b1,
 77                loadVector=torque,
 78                localPosition=[0,0,0],   #at body's reference point/center
 79                bodyFixed=False) #if True, direction would corotate with body
 80
 81#position sensor at tip of body1
 82sens1=mbs.AddSensor(SensorBody(bodyNumber=b1, localPosition=[0,0,0.5*L],
 83                               fileName='solution/sensorPos.txt',
 84                               outputVariableType = exu.OutputVariableType.Position))
 85
 86#%%++++++++++++++++++++++++++++++++++++++++++++++++++++++
 87#assemble system before solving
 88mbs.Assemble()
 89
 90mbs.ComputeSystemDegreeOfFreedom(verbose=True) #print out DOF and further information
 91
 92simulationSettings = exu.SimulationSettings() #takes currently set values or default values
 93
 94tEnd = 4 #simulation time
 95h = 1e-3 #step size
 96simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
 97simulationSettings.timeIntegration.endTime = tEnd
 98simulationSettings.timeIntegration.verboseMode = 1
 99simulationSettings.solutionSettings.solutionWritePeriod = 0.005 #store every 5 ms
100
101SC.visualizationSettings.window.renderWindowSize=[1600,1200]
102SC.visualizationSettings.openGL.multiSampling = 4
103
104SC.visualizationSettings.nodes.showBasis=True
105
106#start solver
107mbs.SolveDynamic(simulationSettings = simulationSettings,
108                 solverType=exu.DynamicSolverType.TrapezoidalIndex2)
109
110#load solution and visualize
111mbs.SolutionViewer()
112
113
114if True:
115
116    mbs.PlotSensor(sensorNumbers=[sens1],components=[1],closeAll=True)
117
118if False:
119    mbs.DrawSystemGraph(useItemTypes=True) #draw nice graph of system