kinematicTreeConstraintTest.py
You can view and download this file on Github: kinematicTreeConstraintTest.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN python utility library
3#
4# Details: test of MarkerKinematicTreeRigid in combination with loads and joint
5#
6# Author: Johannes Gerstmayr
7# Date: 2022-05-29
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 *
15from exudyn.FEM import *
16
17import numpy as np
18
19useGraphics = True
20#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
22try: #only if called from test suite
23 from modelUnitTests import exudynTestGlobals #for globally storing test results
24 useGraphics = exudynTestGlobals.useGraphics
25except:
26 class ExudynTestGlobals:
27 pass
28 exudynTestGlobals = ExudynTestGlobals()
29#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
30
31from math import pi, sin, cos#, sqrt
32from copy import copy, deepcopy
33from exudyn.rigidBodyUtilities import Skew, Skew2Vec
34from exudyn.robotics import *
35
36SC = exu.SystemContainer()
37mbs = SC.AddSystem()
38
39# useGraphics = False
40
41useMBS = True
42useKinematicTree = True
43addForce = True #add gravity as body / link forces
44addConstraint = True #add constraint at tip of chain
45
46
47gGround = GraphicsDataCheckerBoard(point= [0,0,-2], size = 12)
48objectGround = mbs.AddObject(ObjectGround(referencePosition = [0,0,0],
49 visualization=VObjectGround(graphicsData=[gGround])))
50baseMarker = mbs.AddMarker(MarkerBodyRigid(bodyNumber=objectGround, localPosition=[0,0,0]))
51
52L = 0.5 #length
53w = 0.1 #width of links
54pControl = 20000 #we keep the motion of the prismatic joint fixed
55dControl = pControl*0.02
56
57gravity3D = [0,-9.81*0,0]
58graphicsBaseList = [GraphicsDataOrthoCubePoint(size=[L*4, 0.8*w, 0.8*w], color=color4grey)] #rail
59
60newRobot = Robot(gravity=gravity3D,
61 base = RobotBase(visualization=VRobotBase(graphicsData=graphicsBaseList)),
62 tool = RobotTool(HT=HTtranslate([0,0.5*L,0]), visualization=VRobotTool(graphicsData=[
63 GraphicsDataOrthoCubePoint(size=[w, L, w], color=color4orange)])),
64 referenceConfiguration = []) #referenceConfiguration created with 0s automatically
65
66#cart:
67Jlink = InertiaCuboid(density=5000, sideLengths=[L,w,w]) #w.r.t. reference center of mass
68link = RobotLink(Jlink.Mass(), Jlink.COM(), Jlink.InertiaCOM(),
69 jointType='Px', preHT=HT0(),
70 PDcontrol=(pControl, dControl),
71 visualization=VRobotLink(linkColor=color4lawngreen))
72newRobot.AddLink(link)
73linksList = [copy(link)]
74
75nChainLinks = 4 #5
76for i in range(nChainLinks):
77 Jlink = InertiaCuboid(density=1000, sideLengths=[w,L,w]) #w.r.t. reference center of mass
78 Jlink = Jlink.Translated([0,0.5*L,0])
79 preHT = HT0()
80 if i > 0:
81 preHT = HTtranslateY(L)
82
83 link = RobotLink(Jlink.Mass(), Jlink.COM(), Jlink.InertiaCOM(),
84 jointType='Rz', preHT=preHT,
85 PDcontrol=(pControl*0, dControl*0),
86 visualization=VRobotLink(linkColor=color4blue))
87 newRobot.AddLink(link)
88 linksList += [copy(link)]
89
90newRobot.referenceConfiguration[0] = 0.5*0
91# for i in range(nChainLinks):
92# newRobot.referenceConfiguration[i+1] = (2*pi/360) * 5
93newRobot.referenceConfiguration[1] = -(2*pi/360) * 90 #-0.5*pi
94# newRobot.referenceConfiguration[2] = (2*pi/360) * 12 #-0.5*pi
95
96# locPos = [0.1,0.2,0.3]
97locPos = [0,0,0]
98nLinks = newRobot.NumberOfLinks()
99
100sMBS = []
101if useMBS:
102 #newRobot.gravity=[0,-9.81,0]
103 robDict = newRobot.CreateRedundantCoordinateMBS(mbs=mbs, baseMarker=baseMarker, createJointTorqueLoads=False)
104 bodies = robDict['bodyList']
105
106 sMBS+=[mbs.AddSensor(SensorBody(bodyNumber=bodies[nLinks-1], localPosition=locPos, storeInternal=True,
107 outputVariableType=exu.OutputVariableType.Position))]
108
109 if addForce:
110 for i in range(len(bodies)):
111 mBody = mbs.AddMarker(MarkerBodyRigid(bodyNumber=bodies[i], localPosition=linksList[i].COM))
112 mbs.AddLoad(Force(markerNumber=mBody, loadVector=[0,-9.81*linksList[i].mass, 0]))
113
114 if addConstraint:
115 mTip = mbs.AddMarker(MarkerBodyRigid(bodyNumber=bodies[-1], localPosition=[0,L,0]))
116 mTipGround = mbs.AddMarker(MarkerBodyRigid(bodyNumber=objectGround, localPosition=[L*nChainLinks,0,0]))
117 mbs.AddObject(SphericalJoint(markerNumbers=[mTip, mTipGround], constrainedAxes=[0,1,0]))
118
119sKT = []
120if useKinematicTree:
121 #newRobot.gravity=[0,-9.81,0]
122 dKT = newRobot.CreateKinematicTree(mbs)
123 oKT = dKT['objectKinematicTree']
124
125 sKT+=[mbs.AddSensor(SensorKinematicTree(objectNumber=oKT, linkNumber=nLinks-1, localPosition=locPos, storeInternal=True,
126 outputVariableType=exu.OutputVariableType.Position))]
127
128 if addForce:
129 for i in range(nLinks):
130 mLink = mbs.AddMarker(MarkerKinematicTreeRigid(objectNumber=oKT, linkNumber=i, localPosition=linksList[i].COM))
131 mbs.AddLoad(Force(markerNumber=mLink, loadVector=[0,-9.81*linksList[i].mass, 0]))
132
133 if addConstraint:
134 mTip = mbs.AddMarker(MarkerKinematicTreeRigid(objectNumber=oKT, linkNumber=nLinks-1, localPosition=[0,L,0]))
135 mTipGround = mbs.AddMarker(MarkerBodyRigid(bodyNumber = objectGround, localPosition=[L*nChainLinks,0,0]))
136 mbs.AddObject(SphericalJoint(markerNumbers=[mTip, mTipGround], constrainedAxes=[0,1,0]))
137
138#exu.Print(mbs)
139mbs.Assemble()
140
141simulationSettings = exu.SimulationSettings()
142
143tEnd = 0.5
144h = 4*1e-3
145#tEnd = h
146
147simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
148simulationSettings.timeIntegration.endTime = tEnd
149# simulationSettings.timeIntegration.numberOfSteps = 1#int(tEnd/h)
150# simulationSettings.timeIntegration.endTime = h*1#tEnd
151simulationSettings.solutionSettings.solutionWritePeriod = 0.01*100
152simulationSettings.solutionSettings.sensorsWritePeriod = 0.001*20
153simulationSettings.timeIntegration.verboseMode = 1
154#simulationSettings.solutionSettings.solutionWritePeriod = tEnd/steps
155simulationSettings.timeIntegration.newton.useModifiedNewton=True
156
157# simulationSettings.displayComputationTime = True
158# simulationSettings.linearSolverType=exu.LinearSolverType.EigenSparse
159
160simulationSettings.timeIntegration.generalizedAlpha.spectralRadius = 0.95 #SHOULD work with 0.9 as well
161
162SC.visualizationSettings.general.autoFitScene=False
163SC.visualizationSettings.window.renderWindowSize = [1600,1200]
164SC.visualizationSettings.general.drawCoordinateSystem=True
165SC.visualizationSettings.general.drawWorldBasis=True
166SC.visualizationSettings.openGL.multiSampling=4
167SC.visualizationSettings.nodes.showBasis = True
168SC.visualizationSettings.nodes.basisSize = 0.5
169if useGraphics:
170
171 exu.StartRenderer()
172 if 'renderState' in exu.sys: SC.SetRenderState(exu.sys['renderState']) #load last model view
173
174 mbs.WaitForUserToContinue() #press space to continue
175
176# mbs.SolveDynamic(simulationSettings, solverType = exu.DynamicSolverType.ExplicitMidpoint)
177mbs.SolveDynamic(simulationSettings)
178
179if not useGraphics or True:
180 #check results for test suite:
181 u = 0.
182 for i in range(len(sMBS)):
183 v = mbs.GetSensorValues(sMBS[i])
184 exu.Print('sensor MBS '+str(i)+'=',v)
185 u += np.linalg.norm(v)
186 v = mbs.GetSensorValues(sKT[i])
187 exu.Print('sensor KT '+str(i)+' =',v)
188 u += np.linalg.norm(v)
189
190exu.Print("solution of kinematicTreeConstraintTest=", u)
191exudynTestGlobals.testResult = u #1.8135975385993548
192
193
194if False and useGraphics: #use this to reload the solution and use SolutionViewer
195 #sol = LoadSolutionFile('coordinatesSolution.txt')
196
197 mbs.SolutionViewer() #can also be entered in IPython ...
198
199if useGraphics:
200 SC.WaitForRenderEngineStopFlag()
201 exu.StopRenderer() #safely close rendering window!