sensorUserFunctionTest.py
You can view and download this file on Github: sensorUserFunctionTest.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: Test sensor with user function
5#
6# Author: Johannes Gerstmayr
7# Date: 2021-02-18
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.itemInterface import *
15
16from math import pi, atan2
17
18useGraphics = True #without test
19#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
20#you can erase the following lines and all exudynTestGlobals related operations if this is not intended to be used as TestModel:
21try: #only if called from test suite
22 from modelUnitTests import exudynTestGlobals #for globally storing test results
23 useGraphics = exudynTestGlobals.useGraphics
24except:
25 class ExudynTestGlobals:
26 pass
27 exudynTestGlobals = ExudynTestGlobals()
28#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
30SC = exu.SystemContainer()
31mbs = SC.AddSystem()
32node = mbs.AddNode(NodePoint(referenceCoordinates = [1,0,0],
33 initialCoordinates=[0,0,0],
34 initialVelocities=[0,1,0]))
35mbs.AddObject(MassPoint(nodeNumber = node, physicsMass=1))
36
37sNode = mbs.AddSensor(SensorNode(nodeNumber=node,
38 fileName='solution/sensorTestPos.txt',
39 writeToFile = useGraphics, #no output needed
40 outputVariableType=exu.OutputVariableType.Position))
41
42def UFsensor(mbs, t, sensorNumbers, factors, configuration):
43 val = mbs.GetSensorValues(sensorNumbers[0]) #x,y,z
44 phi = atan2(val[1],val[0]) #compute angle from x,y: atan2(y,x)
45 #print("phi=", factors[0]*phi)
46 #print("x,y,z", val)
47 return [factors[0]*phi] #return angle in degree
48
49sUser = mbs.AddSensor(SensorUserFunction(sensorNumbers=[sNode], factors=[180/pi],
50 storeInternal=True,#fileName='solution/sensorTestPhi.txt',
51 writeToFile = useGraphics,
52 sensorUserFunction=UFsensor))
53
54#assemble and solve system for default parameters
55mbs.Assemble()
56
57simulationSettings = exu.SimulationSettings() #takes currently set values or default values
58simulationSettings.solutionSettings.writeSolutionToFile = False
59
60mbs.SolveDynamic(simulationSettings)
61
62#evaluate final (=current) output values
63u = mbs.GetSensorValues(sUser)
64exu.Print('sensor=',u)
65
66exudynTestGlobals.testResult = u #should be 45 degree finally
67
68#+++++++++++++++++++++++++++++++++++++++++++++++++++++
69if useGraphics:
70
71 mbs.PlotSensor([sNode, sNode, sUser], [0, 1, 0])