Linear Movement of a Ball
Posted: Sat Jan 11, 2025 3:21 am
Hi all,
I am new to OpenSim and trying to update the OpenSim model using Python. I am trying to add a ball and move it linearly. I can successfully add a ball to the model, but I am having difficulty moving it linearly. While I can add the ball, it remains stationary. I have checked various sources and examples, but I still can't find the answer to my question. Any help would be appreciated.
You can find the related code below:
# Add Ball to the Model
ball = opensim.Body('target', 0.0010 , opensim.Vec3(0), opensim.Inertia(1,1,.0001,0,0,0) );
ball.setName('Ball');
ball.setMass(1);
ball.setInertia( opensim.Inertia(1,1,1,0,0,0) );
ballGeometry = opensim.Ellipsoid(0.1, 0.1, 0.1);
ballGeometry.setColor( opensim.Vec3(0.8, 0.1, 0.1) );
target_joint = opensim.PlanarJoint('target-joint',
model.getGround(), # PhysicalFrame
opensim.Vec3(3, 0.1, 0),
opensim.Vec3(0, 0, 0),
ball, # PhysicalFrame
opensim.Vec3(0, 0, -0.1),#Vec3(0, 0, -0.25)
opensim.Vec3(0, 0, 0))
ball.attachGeometry(ballGeometry);
model.addJoint(target_joint)
model.addBody(ball)
model.addController(brain)#Add a Controller to the Model.
model_state = model.initSystem()
BallLocation = ball.getPositionInGround(model_state)
target_joint.getCoordinate(0).setLocked(state, False)
target_joint.getCoordinate(1).setLocked(state, False)
target_joint.getCoordinate(2).setLocked(state, True)
istep = 0
stepsize = 0.01
def set_state(state):
state = state
istep = int(state.getTime() / stepsize)
i = 3
while i >= 0:
#model.getStateVariableValue(state, "target-joint")
state = opensim.State(state)
target_joint.getCoordinate(1).setLocked(state, False)
target_joint.getCoordinate(1).setValue(state, -i, False)
target_joint.getCoordinate(1).setLocked(state, True)
set_state(state)
i -= 1;
NOTE: I am getting error when I add "model.getStateVariableValue(state, "target-joint")" code and couldn't find how to use it.
I am new to OpenSim and trying to update the OpenSim model using Python. I am trying to add a ball and move it linearly. I can successfully add a ball to the model, but I am having difficulty moving it linearly. While I can add the ball, it remains stationary. I have checked various sources and examples, but I still can't find the answer to my question. Any help would be appreciated.
You can find the related code below:
# Add Ball to the Model
ball = opensim.Body('target', 0.0010 , opensim.Vec3(0), opensim.Inertia(1,1,.0001,0,0,0) );
ball.setName('Ball');
ball.setMass(1);
ball.setInertia( opensim.Inertia(1,1,1,0,0,0) );
ballGeometry = opensim.Ellipsoid(0.1, 0.1, 0.1);
ballGeometry.setColor( opensim.Vec3(0.8, 0.1, 0.1) );
target_joint = opensim.PlanarJoint('target-joint',
model.getGround(), # PhysicalFrame
opensim.Vec3(3, 0.1, 0),
opensim.Vec3(0, 0, 0),
ball, # PhysicalFrame
opensim.Vec3(0, 0, -0.1),#Vec3(0, 0, -0.25)
opensim.Vec3(0, 0, 0))
ball.attachGeometry(ballGeometry);
model.addJoint(target_joint)
model.addBody(ball)
model.addController(brain)#Add a Controller to the Model.
model_state = model.initSystem()
BallLocation = ball.getPositionInGround(model_state)
target_joint.getCoordinate(0).setLocked(state, False)
target_joint.getCoordinate(1).setLocked(state, False)
target_joint.getCoordinate(2).setLocked(state, True)
istep = 0
stepsize = 0.01
def set_state(state):
state = state
istep = int(state.getTime() / stepsize)
i = 3
while i >= 0:
#model.getStateVariableValue(state, "target-joint")
state = opensim.State(state)
target_joint.getCoordinate(1).setLocked(state, False)
target_joint.getCoordinate(1).setValue(state, -i, False)
target_joint.getCoordinate(1).setLocked(state, True)
set_state(state)
i -= 1;
NOTE: I am getting error when I add "model.getStateVariableValue(state, "target-joint")" code and couldn't find how to use it.