How to obtain the coordinates of the right hand using forward kinematics

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
joyce Glatzer
Posts: 2
Joined: Fri May 19, 2023 12:49 pm

How to obtain the coordinates of the right hand using forward kinematics

Post by joyce Glatzer » Fri May 19, 2023 1:05 pm

Hi everyone,

I am currently trying to find a way using the OpenSim and python, to calculate the x,y,z position of the right and of a given model and motion. The motion data I got by using the IK tool. The code I came up with so fare give me the position and orientation of the right hand, but it doesnot change overtime, so im wondering what goes wrong. Since, simulating the motion in open sim clearly shows a movement.

The code:

Code: Select all

model = osim.Model(model_file)
motion = osim.Storage(motion_file)

# Set up ForwardTool and run simulation
dt = motion.getMinTimeStep()
final_time = motion.getLastTime()
tool = osim.ForwardTool()
tool.setModel(model)
tool.setControlsFileName(motion_file)
tool.setResultsDir("path/to/results/")
tool.setInitialTime(0)
tool.setFinalTime(final_time)
tool.setMinDT(dt)
tool.run()

# Get the hand bodypart by name
hand = model.getBodySet().get("hand_r")

# Create a Vec3 object to specify the point on the body

point = hand.getPositionInGround(state)
print(point)

# Iterate over the frames of the motion data
for i in range(motion.getSize()):
    state = model.initSystem()

    # Set the model's state to match the motion frame
    time = motion.getStateVector(i).getTime()
    state.setTime(time)

    # Get the global position of the wrist joint
    hand_pos = osim.Vec3()  # Create an empty Vec3 object to store the result
    model.getSimbodyEngine().getPosition(state, hand, point, hand_pos)
    model.getFrameList
    # Extract the x, y, and z coordinates
    x_coord = hand_pos.get(0)
    y_coord = hand_pos.get(1)
    z_coord = hand_pos.get(2)

    # Print or store the coordinates
    print("Frame {}: Time={}, Hand_r Coordinates: X={}, Y={}, Z={}".format(i, time, x_coord, y_coord, z_coord))
    

or another try of mine:

Code: Select all

for t in time_steps:

    #time = motion.getStateVector(t).getTime()
    state.setTime(t)

    # Get the body transform for "hand_r" in the updated state
    hand_r = model.getBodySet().get("hand_r")
    body_transform = hand_r.getTransformInGround(state)
    
    # Get the position and orientation
    position = body_transform.p()
    orientation = body_transform.R().convertRotationToQuaternion()

    print("Time:", t)
    print("Position:", position)
    print("Orientation:", orientation)
    print()
either code does not change their output over time.

I hope anyone could help.
best regards,
joy

Tags:

User avatar
Carmichael Ong
Posts: 378
Joined: Fri Feb 24, 2012 11:50 am

Re: How to obtain the coordinates of the right hand using forward kinematics

Post by Carmichael Ong » Mon May 22, 2023 10:58 am

The ForwardTool will output the results out to file, so you would have to load the results from the file back into MATLAB using the API to see how the values are changing over time, such as using a Storage or StatesTrajectory object.

One other way to do this is to use the Manager class to integrate your model in a loop and check the values as you integrate forward. Examples of how to run the loop and calculate certain values can be found in the test cases: https://github.com/opensim-org/opensim- ... r.cpp#L114

User avatar
joyce Glatzer
Posts: 2
Joined: Fri May 19, 2023 12:49 pm

Re: How to obtain the coordinates of the right hand using forward kinematics

Post by joyce Glatzer » Tue May 23, 2023 1:16 pm

Thank you so much.
So the manager is responsable for the forward kinematics?
Do you know how the forward kinematics is implemented ?

POST REPLY