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