movingGroundRobotTest.py
You can view and download this file on Github: movingGroundRobotTest.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: Test Example of a serial robot with redundant coordinates
5# NOTE: more efficient version in serialRobotTSD.py and serialRobotKinematicTree.py using TorsionalSpringDamper
6#
7# Author: Johannes Gerstmayr
8# Date: 2020-02-16
9# Revised: 2021-07-09
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 * #includes itemInterface and rigidBodyUtilities
17import exudyn.graphics as graphics #only import if it does not conflict
18from exudyn.rigidBodyUtilities import *
19from exudyn.robotics import *
20from exudyn.robotics.motion import Trajectory, ProfileConstantAcceleration
21
22import numpy as np
23from numpy import linalg as LA
24
25useGraphics = True #without test
26#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
28try: #only if called from test suite
29 from modelUnitTests import exudynTestGlobals #for globally storing test results
30 useGraphics = exudynTestGlobals.useGraphics
31except:
32 class ExudynTestGlobals:
33 pass
34 exudynTestGlobals = ExudynTestGlobals()
35#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
36
37SC = exu.SystemContainer()
38mbs = SC.AddSystem()
39
40#useGraphics = False
41sensorWriteToFile = False
42
43jointWidth=0.1
44jointRadius=0.06
45linkWidth=0.1
46
47graphicsBaseList = [graphics.Brick([0,0,-0.15], [0.4,0.4,0.1], graphics.color.grey)]
48graphicsBaseList +=[graphics.Cylinder([0,0,0], [0.5,0,0], 0.0025, graphics.color.red)]
49graphicsBaseList +=[graphics.Cylinder([0,0,0], [0,0.5,0], 0.0025, graphics.color.green)]
50graphicsBaseList +=[graphics.Cylinder([0,0,0], [0,0,0.5], 0.0025, graphics.color.blue)]
51graphicsBaseList +=[graphics.Cylinder([0,0,-jointWidth], [0,0,jointWidth], linkWidth*0.5, graphics.colorList[0])] #belongs to first body
52
53ty = 0.03
54tz = 0.04
55zOff = -0.05
56toolSize= [0.05,0.5*ty,0.06]
57graphicsToolList = [graphics.Cylinder(pAxis=[0,0,zOff], vAxis= [0,0,tz], radius=ty*1.5, color=graphics.color.red)]
58graphicsToolList+= [graphics.Brick([0,ty,1.5*tz+zOff], toolSize, graphics.color.grey)]
59graphicsToolList+= [graphics.Brick([0,-ty,1.5*tz+zOff], toolSize, graphics.color.grey)]
60
61
62#changed to new robot structure July 2021:
63newRobot = Robot(gravity=[0,0,9.81],
64 base = RobotBase(visualization=VRobotBase(graphicsData=graphicsBaseList)),
65 tool = RobotTool(HT=HTtranslate([0,0,0.1]), visualization=VRobotTool(graphicsData=graphicsToolList)),
66 referenceConfiguration = []) #referenceConfiguration created with 0s automatically
67
68# illegal Ixx and Izz: newRobot.AddLink(RobotLink(mass=20, COM=[0,0,0], inertia=np.diag([1e-8,0.35,1e-8]), localHT = StdDH2HT([0,0,0,np.pi/2]), visualization=VRobotLink(linkColor=graphics.colorList[0])))
69newRobot.AddLink(RobotLink(mass=20, COM=[0,0,0], inertia=np.diag([0.18,0.35,0.18]), localHT = StdDH2HT([0,0,0,np.pi/2]), visualization=VRobotLink(linkColor=graphics.colorList[0]))) #0.18 is assumed and has no influence
70newRobot.AddLink(RobotLink(mass=17.4, COM=[-0.3638, 0.006, 0.2275], inertia=np.diag([0.13,0.524,0.539]), localHT = StdDH2HT([0,0,0.4318,0]), visualization=VRobotLink(linkColor=graphics.colorList[1])))
71newRobot.AddLink(RobotLink(mass=4.8, COM=[-0.0203,-0.0141,0.07], inertia=np.diag([0.066,0.086,0.0125+0.0075]), localHT = StdDH2HT([0,0.15,0.0203,-np.pi/2]), visualization=VRobotLink(linkColor=graphics.colorList[2]))) #+0.0075 is added to make inertia parameters physically sound
72newRobot.AddLink(RobotLink(mass=0.82, COM=[0,0.019,0], inertia=np.diag([0.0018,0.0013,0.0018]), localHT = StdDH2HT([0,0.4318,0,np.pi/2]), visualization=VRobotLink(linkColor=graphics.colorList[3])))
73newRobot.AddLink(RobotLink(mass=0.34, COM=[0,0,0], inertia=np.diag([0.0003,0.0004,0.0003]), localHT = StdDH2HT([0,0,0,-np.pi/2]), visualization=VRobotLink(linkColor=graphics.colorList[4])))
74newRobot.AddLink(RobotLink(mass=0.09, COM=[0,0,0.032], inertia=np.diag([0.00015,0.00015,4e-5]), localHT = StdDH2HT([0,0,0,0]), visualization=VRobotLink(linkColor=graphics.colorList[5])))
75
76
77# newRobot = Robot(gravity=[0,0,-9.81], referenceConfiguration = [])
78# newRobot.BuildFromDictionary(myRobot) #this allows conversion from old structure
79
80#assumption, as the bodies in the mbs have their COM at the reference position
81#for link in robot['links']:
82# link['COM']=[0,0,0]
83
84#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
85#configurations and trajectory
86q0 = [0,0,0,0,0,0] #zero angle configuration
87
88q1 = [0, np.pi/8, np.pi*0.25, 0,np.pi/8,0] #configuration 1
89q2 = [np.pi/2,-np.pi/8,-np.pi*0.125, 0,np.pi/4,0] #configuration 2
90#q2 = [np.pi*0.45,-np.pi*0.35,-np.pi*0.25, np.pi*0.10,np.pi*0.2,np.pi*0.3] #configuration 2
91#q2 = [pi/2,-pi/8,-pi*0.125,0,pi/4,pi/2] #configuration 2
92
93trajectory=Trajectory(q0, 0)
94if False: #tests for static torque compensation
95 #trajectory.Add(ProfileConstantAcceleration(q0, 1)) #V1.2.37: torques at tEnd= 0.3773259482700045
96 trajectory.Add(ProfileConstantAcceleration(q2, 0.25))
97 trajectory.Add(ProfileConstantAcceleration(q2, 0.75))
98else: #standard test case:
99 trajectory.Add(ProfileConstantAcceleration(q1, 0.25))
100 trajectory.Add(ProfileConstantAcceleration(q2, 0.25))
101 trajectory.Add(ProfileConstantAcceleration(q0, 0.5 ))
102 trajectory.Add(ProfileConstantAcceleration(q0, 1e6))
103
104#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
105#test robot model
106#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
107#control parameters, per joint:
108Pcontrol = np.array([40000, 40000, 40000, 100, 100, 10])
109Dcontrol = np.array([400, 400, 100, 1, 1, 0.05]) #last value 0.05 gives less oscillations as compared to earlier values of 0.1!
110# Pcontrol = 0.01*Pcontrol #soft behavior
111
112
113#++++++++++++++++++++++++++++++++++++++++++++++++
114#base, graphics, object and marker:
115graphicsBaseList = [graphics.Brick([0,0,-0.2], [0.4,0.4,0.1], graphics.color.grey)]
116graphicsBaseList +=[graphics.Cylinder([0,0,0], [0.5,0,0], 0.0025, graphics.color.red)]
117graphicsBaseList +=[graphics.Cylinder([0,0,0], [0,0.5,0], 0.0025, graphics.color.green)]
118graphicsBaseList +=[graphics.Cylinder([0,0,0], [0,0,0.5], 0.0025, graphics.color.blue)]
119#oGround = mbs.AddObject(ObjectGround(referencePosition=list(HT2translation(Tcurrent)),
120objectGround = mbs.AddObject(ObjectGround(referencePosition=HT2translation(newRobot.GetBaseHT()),
121 visualization=VObjectGround(graphicsData=graphicsBaseList)))
122
123#baseMarker; could also be a moving base!
124baseMarker = mbs.AddMarker(MarkerBodyRigid(bodyNumber=objectGround, localPosition=[0,0,0]))
125
126
127
128#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
129#build mbs robot model:
130robotDict = newRobot.CreateRedundantCoordinateMBS(mbs, baseMarker=baseMarker)
131
132# !!!!!IMPORTANT!!!!!:
133jointList = robotDict['jointList'] #must be stored there for the load user function
134
135unitTorques0 = robotDict['unitTorque0List'] #(left body)
136unitTorques1 = robotDict['unitTorque1List'] #(right body)
137
138loadList0 = robotDict['jointTorque0List'] #(left body)
139loadList1 = robotDict['jointTorque1List'] #(right body)
140#print(loadList0, loadList1)
141#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
142#control robot
143compensateStaticTorques = True
144def ComputeMBSstaticRobotTorques(newRobot):
145 q=[]
146 for joint in jointList:
147 q += [mbs.GetObjectOutput(joint, exu.OutputVariableType.Rotation)[2]] #z-rotation
148 HT=newRobot.JointHT(q)
149 return newRobot.StaticTorques(HT)
150
151#user function which is called only once per step, speeds up simulation drastically
152def PreStepUF(mbs, t):
153 if compensateStaticTorques:
154 staticTorques = ComputeMBSstaticRobotTorques(newRobot)
155 else:
156 staticTorques = np.zeros(len(jointList))
157 #compute load for joint number
158 for i in range(len(jointList)):
159 joint = i
160 phi = mbs.GetObjectOutput(jointList[joint], exu.OutputVariableType.Rotation)[2] #z-rotation
161 omega = mbs.GetObjectOutput(jointList[joint], exu.OutputVariableType.AngularVelocityLocal)[2] #z-angular velocity
162
163 #[u,v,a] = MotionInterpolator(t, robotTrajectory, joint) #OLD, until V1.1.135
164 [u,v,a] = trajectory.EvaluateCoordinate(t, joint)
165
166
167 torque = -(Pcontrol[joint]*(phi-u) + Dcontrol[joint]*(omega-v)) #negative sign in feedback control!
168 # torque = (Pcontrol[joint]*(phi+u) + Dcontrol[joint]*(omega+v)) #until V1.2.; but static Torque compensation is wrong in this case!
169 torque -= staticTorques[joint] #add static torque compensation
170
171 load0 = torque * unitTorques0[i] #includes sign and correct unit-torque vector
172 load1 = torque * unitTorques1[i] #includes sign and correct unit-torque vector
173
174 # #write updated torque to joint loads, applied to left and right body
175 mbs.SetLoadParameter(loadList0[i], 'loadVector', list(load0))
176 mbs.SetLoadParameter(loadList1[i], 'loadVector', list(load1))
177
178 return True
179
180mbs.SetPreStepUserFunction(PreStepUF)
181
182
183
184#add sensors:
185sJointRot = []
186sJointAngVel = []
187sJointTorque = []
188cnt = 0
189for jointLink in jointList:
190 sJointRot += [mbs.AddSensor(SensorObject(objectNumber=jointLink,
191 storeInternal=True,#fileName="solution/joint" + str(cnt) + "Rot.txt",
192 outputVariableType=exu.OutputVariableType.Rotation,
193 writeToFile = sensorWriteToFile))]
194 sJointAngVel += [mbs.AddSensor(SensorObject(objectNumber=jointLink,
195 storeInternal=True,#fileName="solution/joint" + str(cnt) + "AngVel.txt",
196 outputVariableType=exu.OutputVariableType.AngularVelocityLocal,
197 writeToFile = sensorWriteToFile))]
198 cnt+=1
199
200cnt = 0
201for load0 in robotDict['jointTorque0List']:
202 sJointTorque += [mbs.AddSensor(SensorLoad(loadNumber=load0,storeInternal=True,# fileName="solution/jointTorque" + str(cnt) + ".txt",
203 writeToFile = sensorWriteToFile))]
204 cnt+=1
205
206
207
208mbs.Assemble()
209#mbs.systemData.Info()
210
211SC.visualizationSettings.connectors.showJointAxes = True
212SC.visualizationSettings.connectors.jointAxesLength = 0.02
213SC.visualizationSettings.connectors.jointAxesRadius = 0.002
214
215SC.visualizationSettings.nodes.showBasis = True
216SC.visualizationSettings.nodes.basisSize = 0.1
217SC.visualizationSettings.loads.show = False
218
219SC.visualizationSettings.openGL.multiSampling=4
220
221tEnd = 0.2 #0.2 for testing
222h = 0.001
223
224if useGraphics:
225 tEnd = 0.2
226 #tEnd = 1 #shows exactly static torques ComputeMBSstaticRobotTorques(newRobot) and desired angles (q2) at end
227
228#SC.renderer.DoIdleTasks()
229simulationSettings = exu.SimulationSettings() #takes currently set values or default values
230
231simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
232simulationSettings.timeIntegration.endTime = tEnd
233simulationSettings.solutionSettings.solutionWritePeriod = h
234simulationSettings.solutionSettings.sensorsWritePeriod = h
235simulationSettings.solutionSettings.writeSolutionToFile = useGraphics
236# simulationSettings.timeIntegration.simulateInRealtime = True
237# simulationSettings.timeIntegration.realtimeFactor = 0.25
238
239simulationSettings.timeIntegration.verboseMode = 1
240simulationSettings.displayComputationTime = False
241simulationSettings.displayStatistics = False
242simulationSettings.linearSolverType = exu.LinearSolverType.EigenSparse
243
244#simulationSettings.timeIntegration.newton.useModifiedNewton = True
245simulationSettings.timeIntegration.generalizedAlpha.useIndex2Constraints = False
246simulationSettings.timeIntegration.generalizedAlpha.useNewmark = simulationSettings.timeIntegration.generalizedAlpha.useIndex2Constraints
247simulationSettings.timeIntegration.generalizedAlpha.spectralRadius = 0.5 #0.6 works well
248
249simulationSettings.timeIntegration.generalizedAlpha.computeInitialAccelerations=True
250
251mbs.SolveDynamic(simulationSettings)
252
253if useGraphics:
254 SC.visualizationSettings.general.autoFitScene=False
255 SC.renderer.Start()
256 if 'renderState' in exu.sys:
257 SC.renderer.SetState(exu.sys['renderState'])
258
259
260 mbs.SolutionViewer()
261 SC.renderer.Stop()
262
263lastRenderState = SC.renderer.GetState() #store model view
264
265#compute final torques:
266measuredTorques=[]
267
268for cnt, sensorNumber in enumerate(sJointTorque):
269 if useGraphics:
270 exu.Print('sensor torque',cnt, '=', mbs.GetSensorValues(sensorNumber))
271 measuredTorques += [1e-2*mbs.GetSensorValues(sensorNumber)[2]]
272
273if useGraphics:
274 for cnt, sensorNumber in enumerate(sJointRot):
275 exu.Print('sensor rot ',cnt, '=', mbs.GetSensorValues(sensorNumber))
276
277
278fact=0.01 #to reach desired accuracy
279exu.Print("torques at tEnd=", fact*VSum(measuredTorques))
280
281#add larger test tolerance for 32/64bits difference
282exudynTestGlobals.testError = (fact*VSum(measuredTorques) - 0.007681798995944785) #until 2026-01-28 (unphysical inertia) #until 2022-04-21: 7680031232063571; until 2021-09-10: 76.8003123206452; until 2021-08-19 (changed robotics.py): 76.80031232091771; old controller: 77.12176106978085) #OLDER results: up to 2021-06-28: 0.7712176106955341; 2020-08-25: 77.13193176752571 (32bits), 2020-08-24: (64bits)77.13193176846507
283exudynTestGlobals.testResult = fact*VSum(measuredTorques)
284
285#exu.Print('error=', exudynTestGlobals.testError)
286
287if useGraphics:
288
289
290 mbs.PlotSensor(sJointTorque, components=2, closeAll=True, yLabel='joint torques (Nm)', title='joint torques')
291
292 mbs.PlotSensor(sJointRot, components=2, yLabel='joint angles (rad)', title='joint angles')
293
294 #V1.2.40, P39: with D[-1]=0.05: since 2022-04-22:
295 # sensor torque 0 = [ 0. 0. -12.68901871]
296 # sensor torque 1 = [-0. -0. 76.45031947]
297 # sensor torque 2 = [-0. -0. 12.97010176]
298 # sensor torque 3 = [ 0.00000000e+00 0.00000000e+00 -8.56924875e-05]
299 # sensor torque 4 = [-0. -0. 0.08725323]
300 # sensor torque 5 = [ 0.0000000e+00 0.0000000e+00 -9.5769297e-07]
301 # sensor rot 0 = [-1.04899069e-15 3.05694259e-19 -3.19543456e-04]
302 # sensor rot 1 = [-4.57411886e-14 -1.77599905e-14 3.63829923e-01]
303 # sensor rot 2 = [ 4.68252006e-14 -1.78150803e-14 7.25569701e-01]
304 # sensor rot 3 = [-1.07990283e-13 1.95010674e-13 2.42757634e-07]
305 # sensor rot 4 = [ 1.07852866e-13 -2.06184741e-13 3.63658987e-01]
306 # sensor rot 5 = [ 1.84235149e-16 -6.26054764e-13 -5.07549126e-08]
307 # torques at tEnd= 0.7681856909852399
308
309 #V1.2.40, P39: with D[-1]=0.1:
310 # sensor torque 0 = [ 0. 0. -12.68901812]
311 # sensor torque 1 = [-0. -0. 76.4503195]
312 # sensor torque 2 = [-0. -0. 12.97010177]
313 # sensor torque 3 = [ 0.0000000e+00 0.0000000e+00 -8.9097007e-05]
314 # sensor torque 4 = [-0. -0. 0.08725323]
315 # sensor torque 5 = [-0.00000000e+00 -0.00000000e+00 1.09612342e-05]
316 # sensor rot 0 = [-1.04899069e-15 3.32799313e-19 -3.19543454e-04]
317 # sensor rot 1 = [-4.59632332e-14 -1.77794520e-14 3.63829923e-01]
318 # sensor rot 2 = [ 4.71643413e-14 -1.78734434e-14 7.25569701e-01]
319 # sensor rot 3 = [-1.07609037e-13 1.95343741e-13 2.42965135e-07]
320 # sensor rot 4 = [ 1.07471637e-13 -2.06279100e-13 3.63658987e-01]
321 # sensor rot 5 = [ 1.84106879e-16 -6.25874352e-13 8.28833899e-08]
322 # torques at tEnd= 0.7681857824086907
323
324 #V1.2.37, P37: ==> uses wrong static torque compensation
325 # sensor torque 0 = [ 0. 0. -12.67938332]
326 # sensor torque 1 = [-0. -0. 76.42613004]
327 # sensor torque 2 = [-0. -0. 12.96641046]
328 # sensor torque 3 = [ 0.00000000e+00 0.00000000e+00 -8.93114211e-05]
329 # sensor torque 4 = [-0. -0. 0.08723331]
330 # sensor torque 5 = [-0.0000000e+00 -0.0000000e+00 1.1143982e-05]
331 # sensor rot 0 = [ 1.60812265e-16 -2.71050543e-20 3.19257214e-04]
332 # sensor rot 1 = [ 5.06049354e-14 -1.91882065e-14 -3.63432530e-01]
333 # sensor rot 2 = [-4.07000942e-14 3.58791681e-14 -7.25182770e-01]
334 # sensor rot 3 = [ 9.91554982e-13 4.63740157e-13 -2.41708605e-07]
335 # sensor rot 4 = [-9.26617234e-13 3.52539928e-13 -3.63103888e-01]
336 # sensor rot 5 = [ 9.21913969e-18 3.74700271e-16 -8.34127775e-08]
337 # torques at tEnd= 0.7680031232065901