Page 1 of 1

Error in Python Scripting

Posted: Mon Oct 22, 2018 1:54 am
by fys
Hello,
I'm a beginner in opensim and coding. I've encountered the following error in my code.

std::exception in 'SimTK::State & OpenSim::Model::initSystem()': Assigned an invalid SimTK::MobilizedBodyIndex
In Object 'skullCenter' of type PhysicalOffsetFrame.
Thrown at physicalframe.cpp:73 in setMobilizedBodyIndex().

This is the rest of the code:

import opensim as osim

import sys
# Are we running this script as a test? Users can ignore this line!
running_as_test = 'unittest' in str().join(sys.argv)

# Define global model where the neck lives.
neck = osim.Model()
if not running_as_test: neck.setUseVisualizer(True)

# ---------------------------------------------------------------------------
# Create two links, each with a mass of 1 kg, centre of mass at the body's
# origin, and moments and products of inertia of zero.
# ---------------------------------------------------------------------------

skull = osim.Body("skull",
1.0,
osim.Vec3(0, 0, 0),
osim.Inertia(0, 0, 0))
c1 = osim.Body("c1",
1.0,
osim.Vec3(0, 0, 0),
osim.Inertia(0, 0, 0))
c2c7 = osim.Body("c2c7",
1.0,
osim.Vec3(0, 0, 0),
osim.Inertia(0, 0, 0))


r_clav = osim.Body("r_clav",
1.0,
osim.Vec3(0, 0, -1),
osim.Inertia(0, 0, 0))
l_clav = osim.Body("l_clav",
1.0,
osim.Vec3(0, 0, -1),
osim.Inertia(0, 0, 0))
stern = osim.Body("stern",
1.0,
osim.Vec3(0, 0, -1),
osim.Inertia(0, 0, 0))

# ---------------------------------------------------------------------------
# Connect the bodies with pin joints. Assume each body is 1m long.
# ---------------------------------------------------------------------------
c1g = osim.PinJoint("c1g",
neck.getGround(), # PhysicalFrame
osim.Vec3(0, 0, 0),
osim.Vec3(0, 0, 0),
c1, # PhysicalFrame
osim.Vec3(0, 1, 0),
osim.Vec3(0, 0, 0))

atlanto = osim.PinJoint("atlanto",
c1, # PhysicalFrame
osim.Vec3(0, 0, 0),
osim.Vec3(0, 0, 0),
skull, # PhysicalFrame
osim.Vec3(0, 1, 0),
osim.Vec3(0, 0, 0))

rcl_ster = osim.WeldJoint("rcl_ster",
r_clav, # PhysicalFrame
osim.Vec3(0, 0, 0),
osim.Vec3(0, 0, 0),
stern, # PhysicalFrame
osim.Vec3(0, 1, 0),
osim.Vec3(0, 0, 0))
lcl_ster = osim.WeldJoint("rcl_ster",
l_clav, # PhysicalFrame
osim.Vec3(0, 0, 0),
osim.Vec3(0, 0, 0),
stern, # PhysicalFrame
osim.Vec3(0, 1, 0),
osim.Vec3(0, 0, 0))


# ---------------------------------------------------------------------------
# Add a muscle that flexes the elbow (actuator for robotics people).
# ---------------------------------------------------------------------------



# ---------------------------------------------------------------------------
# Build model with components created above.
# ---------------------------------------------------------------------------

neck.addBody(skull)
neck.addBody(c1)
neck.addBody(r_clav)
neck.addBody(l_clav)
neck.addBody(stern)
neck.addJoint(c1g) # Now required in OpenSim4.0
neck.addJoint(atlanto)
neck.addJoint(rcl_ster)
neck.addJoint(lcl_ster)

# ---------------------------------------------------------------------------
# Add a console reporter to print the muscle fibre force and elbow angle.
# ---------------------------------------------------------------------------

# We want to write our simulation results to the console.

# ---------------------------------------------------------------------------
# Add display geometry.
# ---------------------------------------------------------------------------

bodyGeometry = osim.Ellipsoid(0.1, 0.5, 0.1)
bodyGeometry.setColor(osim.Gray)
skullCenter = osim.PhysicalOffsetFrame()
skullCenter.setName("skullCenter")
skullCenter.setParentFrame(skull)
skullCenter.setOffsetTransform(osim.Transform(osim.Vec3(0, 0.5, 0)))
skull.addComponent(skullCenter)
skullCenter.attachGeometry(bodyGeometry.clone())

c1Center = osim.PhysicalOffsetFrame()
c1Center.setName("c1Center")
c1Center.setParentFrame(c1)
c1Center.setOffsetTransform(osim.Transform(osim.Vec3(0, 0.5, 0)))
c1.addComponent(c1Center)
c1Center.attachGeometry(bodyGeometry.clone())


sternCenter = osim.PhysicalOffsetFrame()
sternCenter.setName("sternCenter")
sternCenter.setParentFrame(stern)
sternCenter.setOffsetTransform(osim.Transform(osim.Vec3(0, 0.5, 0)))
stern.addComponent(sternCenter)
sternCenter.attachGeometry(bodyGeometry.clone())

r_clavCenter = osim.PhysicalOffsetFrame()
r_clavCenter.setName("r_clavCenter")
r_clavCenter.setParentFrame(r_clav)
r_clavCenter.setOffsetTransform(osim.Transform(osim.Vec3(0, 0.5, 0)))
r_clav.addComponent(r_clavCenter)
r_clavCenter.attachGeometry(bodyGeometry.clone())

l_clavCenter = osim.PhysicalOffsetFrame()
l_clavCenter.setName("l_clavCenter")
l_clavCenter.setParentFrame(l_clav)
l_clavCenter.setOffsetTransform(osim.Transform(osim.Vec3(0, 0.5, 0)))
l_clav.addComponent(l_clavCenter)
l_clavCenter.attachGeometry(bodyGeometry.clone())

# ---------------------------------------------------------------------------
# Configure the model.
# ---------------------------------------------------------------------------

state = neck.initSystem()
# Fix the shoulder at its default angle and begin with the elbow flexed.
c1g.getCoordinate().setLocked(state, True)
atlanto.getCoordinate().setValue(state, 0.5 * osim.SimTK_PI)
stern.getCoordinate().setLocked(state,True)
r_clav.getCoordinate().setLocked(state,True)
l_clav.getCoordinate().setLocked(state,True)
neck.equilibrateMuscles(state)

# ---------------------------------------------------------------------------
# Simulate.
# ---------------------------------------------------------------------------

manager = osim.Manager(neck)
state.setTime(0)
manager.initialize(state)
state = manager.integrate(10.0)

# ---------------------------------------------------------------------------
# Print/save model file
# ---------------------------------------------------------------------------

neck.printToXML("SimpleNeck.osim")

Kindly help me understand the error and also, how to assign shapes or appearance to the defined body.
I went through doxygen syntax on frames and geometry, but unfortunately it couldn't clear my doubts.regarding the python syntax.