fourBarMechanism3D.py
You can view and download this file on Github: fourBarMechanism3D.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: A simple 3D four bar mechanism #read full text output!
5# 1) regular case does not work (redundant constraints/overconstrained joints; jacobian singluar)
6# 2) use simulationSettings.linearSolverSettings.ignoreSingularJacobian = True
7# 3) remove redundant constraints: change flags for GenericJoint at last joint [1,1,0,0,0,0] to obtain well defined mbs
8#
9# Author: Johannes Gerstmayr
10# Date: 2021-08-05
11#
12# 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.
13#
14#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
16import exudyn as exu
17from exudyn.itemInterface import *
18from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
19import exudyn.graphics as graphics #only import if it does not conflict
20import numpy as np
21from math import pi, sin, cos
22
23
24useGraphics = True
25
26casesText = ['redundant constraints', 'redundant constraints with improved solver', 'non-redundant constraints']
27cases = [0,1,2]
28
29for case in cases:
30 caseText = casesText[case]
31 print('\n\n************************************************')
32 print('run four bar mechanism with case:\n '+caseText)
33 print('************************************************')
34
35 SC = exu.SystemContainer()
36 mbs = SC.AddSystem()
37
38
39 #%%++++++++++++++++++++++++++++++++++++++++++++++++++++
40 #physical parameters
41 g = [0.1,-9.81,0] #gravity + disturbance
42 L = 1 #length
43 w = 0.1 #width
44 bodyDim=[L,w,w] #body dimensions
45 # p0 = [0,0,0]
46 pMid0 = np.array([0,L*0.5,0]) #center of mass, body0
47 pMid1 = np.array([L*0.5,L,0]) #center of mass, body1
48 pMid2 = np.array([L,L*0.5,0]) #center of mass, body2
49
50 #ground body
51 graphicsCOM0 = graphics.Basis(origin=[0,0,0], length=4*w)
52 oGround = mbs.AddObject(ObjectGround(visualization=VObjectGround(graphicsData=[graphicsCOM0])))
53 markerGround0 = mbs.AddMarker(MarkerBodyRigid(bodyNumber=oGround, localPosition=[0,0,0]))
54 markerGround1 = mbs.AddMarker(MarkerBodyRigid(bodyNumber=oGround, localPosition=[L,0,0]))
55
56 #%%++++++++++++++++++++++++++++++++++++++++++++++++++++
57 #first link:
58 iCube0 = InertiaCuboid(density=5000, sideLengths=bodyDim)
59
60 #graphics for body
61 graphicsBody0 = graphics.RigidLink(p0=[-0.5*L,0,0],p1=[0.5*L,0,0],
62 axis0=[0,0,1], axis1=[0,0,1], radius=[0.5*w,0.5*w],
63 thickness = w, width = [1.2*w,1.2*w], color=graphics.color.red)
64 graphicsBody1 = graphics.RigidLink(p0=[-0.5*L,0,0],p1=[0.5*L,0,0],
65 axis0=[0,0,1], axis1=[0,0,1], radius=[0.5*w,0.5*w],
66 thickness = w, width = [1.2*w,1.2*w], color=graphics.color.green)
67 graphicsBody2 = graphics.RigidLink(p0=[-0.5*L,0,0],p1=[0.5*L,0,0],
68 axis0=[0,0,1], axis1=[0,0,1], radius=[0.5*w,0.5*w],
69 thickness = w, width = [1.2*w,1.2*w], color=graphics.color.steelblue)
70
71 [n0,b0]=AddRigidBody(mainSys = mbs,
72 inertia = iCube0, #includes COM
73 nodeType = exu.NodeType.RotationEulerParameters,
74 position = pMid0,
75 rotationMatrix = RotationMatrixZ( 0.5*pi),
76 gravity = g,
77 graphicsDataList = [graphicsBody0])
78
79 markerBody0J0 = mbs.AddMarker(MarkerBodyRigid(bodyNumber=b0, localPosition=[-0.5*L,0,0]))
80 markerBody0J1 = mbs.AddMarker(MarkerBodyRigid(bodyNumber=b0, localPosition=[ 0.5*L,0,0]))
81
82 [n1,b1]=AddRigidBody(mainSys = mbs,
83 inertia = iCube0, #includes COM
84 nodeType = exu.NodeType.RotationEulerParameters,
85 position = pMid1,
86 rotationMatrix = RotationMatrixZ(0.),
87 gravity = g,
88 graphicsDataList = [graphicsBody1])
89 markerBody1J0 = mbs.AddMarker(MarkerBodyRigid(bodyNumber=b1, localPosition=[-0.5*L,0,0]))
90 markerBody1J1 = mbs.AddMarker(MarkerBodyRigid(bodyNumber=b1, localPosition=[ 0.5*L,0,0]))
91
92 [n2,b2]=AddRigidBody(mainSys = mbs,
93 inertia = iCube0, #includes COM
94 nodeType = exu.NodeType.RotationEulerParameters,
95 position = pMid2,
96 rotationMatrix = RotationMatrixZ(-0.5*pi),
97 gravity = g,
98 graphicsDataList = [graphicsBody2])
99
100 markerBody2J0 = mbs.AddMarker(MarkerBodyRigid(bodyNumber=b2, localPosition=[-0.5*L,0,0]))
101 markerBody2J1 = mbs.AddMarker(MarkerBodyRigid(bodyNumber=b2, localPosition=[ 0.5*L,0,0]))
102
103
104 #revolute joint option 1:
105 mbs.AddObject(GenericJoint(markerNumbers=[markerGround0, markerBody0J0],
106 constrainedAxes=[1,1,1,1,1,0],
107 visualization=VObjectJointGeneric(axesRadius=0.2*w, axesLength=1.4*w)))
108
109 mbs.AddObject(GenericJoint(markerNumbers=[markerBody0J1, markerBody1J0],
110 constrainedAxes=[1,1,1,1,1,0],
111 visualization=VObjectJointGeneric(axesRadius=0.2*w, axesLength=1.4*w)))
112
113 mbs.AddObject(GenericJoint(markerNumbers=[markerBody1J1, markerBody2J0],
114 constrainedAxes=[1,1,1,1,1,0],
115 visualization=VObjectJointGeneric(axesRadius=0.2*w, axesLength=1.4*w)))
116
117 constrainedAxes3 = [1,1,1,1,1,0]
118 if case == 2:
119 constrainedAxes3 = [1,1,0,0,0,0] #only these constraints are needed for closing loop!
120 print('use non-redundant constraints for last joint:', constrainedAxes3)
121
122 mbs.AddObject(GenericJoint(markerNumbers=[markerBody2J1, markerGround1],
123 constrainedAxes=constrainedAxes3,
124 visualization=VObjectJointGeneric(axesRadius=0.2*w, axesLength=1.4*w)))
125
126 #position sensor at tip of body1
127 sens1=mbs.AddSensor(SensorBody(bodyNumber=b1, localPosition=[0,0,0.5*L],
128 fileName='solution/sensorPos.txt',
129 outputVariableType = exu.OutputVariableType.Position))
130
131 #%%++++++++++++++++++++++++++++++++++++++++++++++++++++++
132 #assemble system before solving
133 mbs.Assemble()
134 if False:
135 mbs.systemData.Info() #show detailed information
136 if False:
137 #from exudyn.utilities import DrawSystemGraph
138 mbs.DrawSystemGraph(useItemTypes=True) #draw nice graph of system
139
140 simulationSettings = exu.SimulationSettings() #takes currently set values or default values
141
142 tEnd = 10 #simulation time
143 h = 2e-3 #step size
144 simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
145 simulationSettings.timeIntegration.endTime = tEnd
146 simulationSettings.timeIntegration.verboseMode = 1
147 #simulationSettings.timeIntegration.simulateInRealtime = True
148 #simulationSettings.timeIntegration.realtimeFactor = 4
149
150 if case == 1:
151 simulationSettings.linearSolverSettings.ignoreSingularJacobian = True #for redundant constraints
152
153 simulationSettings.timeIntegration.newton.useModifiedNewton = True
154 simulationSettings.solutionSettings.writeSolutionToFile = False
155 #simulationSettings.solutionSettings.solutionWritePeriod = 0.005 #store every 5 ms
156
157 SC.visualizationSettings.window.renderWindowSize=[1200,1024]
158 SC.visualizationSettings.openGL.multiSampling = 4
159 SC.visualizationSettings.general.autoFitScene = False
160
161 SC.visualizationSettings.nodes.drawNodesAsPoint=False
162 SC.visualizationSettings.nodes.showBasis=True
163
164 if useGraphics:
165 exu.StartRenderer()
166 if 'renderState' in exu.sys: #reload old view
167 SC.SetRenderState(exu.sys['renderState'])
168
169 mbs.WaitForUserToContinue() #stop before simulating
170
171 try: #solver will raise exception in case 1
172 mbs.SolveDynamic(simulationSettings = simulationSettings)
173 except:
174 pass
175
176 # mbs.SolveDynamic(simulationSettings = simulationSettings,
177 # solverType=exu.DynamicSolverType.TrapezoidalIndex2)
178 if useGraphics:
179 SC.WaitForRenderEngineStopFlag() #stop before closing
180 exu.StopRenderer() #safely close rendering window!
181
182 #check redundant constraints and DOF:
183 mbs.ComputeSystemDegreeOfFreedom(verbose=useGraphics)
184
185
186if False:
187 sol = LoadSolutionFile('coordinatesSolution.txt')
188
189 mbs.SolutionViewer(sol)
190
191if False:
192
193 mbs.PlotSensor([sens1],[1])