Linear Velocity Constraint or Goal

OpenSim Moco is a software toolkit to solve optimal control problems with musculoskeletal models defined in OpenSim using the direct collocation method.
POST REPLY
User avatar
cedric attias
Posts: 11
Joined: Tue Jan 10, 2023 8:22 am

Linear Velocity Constraint or Goal

Post by cedric attias » Tue Aug 20, 2024 10:39 am

I am attempting to constrain the motion of my model so that the final velocity of the hand is a certain linear speed as such:

problem.setStateInfo('/bodyset/hand/speed', [0,50], 0, 10);

I am realizing that the setStatInfo does not recognize a body, and requires a joint speed to constrain. The problem is that I am interested in segment velocity, not joint velocity.

How can I go about constraining a segment in my model to start and end at a certain linear velocity? Is this better achieved through a cost function? If so, which cost?

Thank you in advance!

User avatar
Nicholas Bianco
Posts: 1044
Joined: Thu Oct 04, 2012 8:09 pm

Re: Linear Velocity Constraint or Goal

Post by Nicholas Bianco » Wed Aug 21, 2024 3:38 pm

Hi Cedric,

You can use MocoFinalOutputGoal in endpoint constraint mode to achieve what you're looking for:

Code: Select all

finalHandVelocity = MocoFinalOutputGoal('final_hand_velocity);
finalHandVelocity.setOutputPath('/bodyset/hand|linear_velocity');
finalHandVelocity.setMode('endpoint_constraint');
finalHandVelocity.setEndpointConstraintBounds(MocoBounds(10));
problem.addGoal(finalHandVelocity);
Best,
Nick

User avatar
cedric attias
Posts: 11
Joined: Tue Jan 10, 2023 8:22 am

Re: Linear Velocity Constraint or Goal

Post by cedric attias » Fri Aug 23, 2024 8:35 am

Thank you Nick!

I am having an issue with the following line:

setEndpointConstraintBounds(MocoBounds(10));

Where it says;
Incorrect number or types of inputs or outputs for function setEndpointConstraintBounds.

From my understanding, setEndpointConstraintBounds requires a vector with the length equal to the number of outputs for this goal. This goal only has one output (linear velocity), so I am unsure why MocoBounds(10) is insufficient.

User avatar
Nicholas Bianco
Posts: 1044
Joined: Thu Oct 04, 2012 8:09 pm

Re: Linear Velocity Constraint or Goal

Post by Nicholas Bianco » Fri Aug 23, 2024 9:07 am

Ah yes, apologies.

Here is the correct snippet:

Code: Select all

finalHandVelocity = MocoFinalOutputGoal('final_hand_velocity);
finalHandVelocity.setOutputPath('/bodyset/hand|linear_velocity');
finalHandVelocity.setMode('endpoint_constraint');
bounds = StdVectorMocoBounds();
bounds.append(MocoBounds(10)); 
finalHandVelocity.setEndpointConstraintBounds(bounds);
problem.addGoal(finalHandVelocity);

User avatar
cedric attias
Posts: 11
Joined: Tue Jan 10, 2023 8:22 am

Re: Linear Velocity Constraint or Goal

Post by cedric attias » Fri Aug 23, 2024 9:13 am

Nick, you are a lifesaver!

But unfortunately, append seems to require a text input. I used add instead of append and that seemed to work, but wanted to check and confirm if that was a viable solution.

Thanks again,

Cedric
Last edited by cedric attias on Fri Aug 23, 2024 9:16 am, edited 1 time in total.

User avatar
Nicholas Bianco
Posts: 1044
Joined: Thu Oct 04, 2012 8:09 pm

Re: Linear Velocity Constraint or Goal

Post by Nicholas Bianco » Fri Aug 23, 2024 9:15 am

Try ".add()" instead.

(Appending to a std::vector in Matlab and Python works differently and I can never remember which one to use.)

User avatar
cedric attias
Posts: 11
Joined: Tue Jan 10, 2023 8:22 am

Re: Linear Velocity Constraint or Goal

Post by cedric attias » Mon Aug 26, 2024 9:25 am

Thanks for all the help Nick.

I see your responses across a number of forum posts, and the efforts are very much appreciated.

Regarding my question, is there a way to specify a direction for my linear velocity? For example, if I want the hand to reach a final velocity along the positive x-axis?

Thanks

Cedric

User avatar
Nicholas Bianco
Posts: 1044
Joined: Thu Oct 04, 2012 8:09 pm

Re: Linear Velocity Constraint or Goal

Post by Nicholas Bianco » Tue Aug 27, 2024 3:33 pm

Hi Cedric,

You can use "setOutputIndex" to specific the component to minimize or constraint:

Code: Select all

finalHandVelocity = MocoFinalOutputGoal('final_hand_velocity);
finalHandVelocity.setOutputPath('/bodyset/hand|linear_velocity');
finalHandVelocity.setMode('endpoint_constraint');
// assuming here that the 0th index corresponds to the x-direction
finalHandVelocity.setOutputIndex(0);
// change the sign on the bounds to specific the direction
bounds = StdVectorMocoBounds();
bounds.append(MocoBounds(10)); 
finalHandVelocity.setEndpointConstraintBounds(bounds);
problem.addGoal(finalHandVelocity);

User avatar
cedric attias
Posts: 11
Joined: Tue Jan 10, 2023 8:22 am

Re: Linear Velocity Constraint or Goal

Post by cedric attias » Thu Oct 17, 2024 10:25 am

Sorry to resurface an old post, but I cannot seem to identify the units for speed in Moco.

User avatar
Nicholas Bianco
Posts: 1044
Joined: Thu Oct 04, 2012 8:09 pm

Re: Linear Velocity Constraint or Goal

Post by Nicholas Bianco » Sat Oct 19, 2024 9:42 am

Hi Cedric,

OpenSim (and Moco) uses SI units: meters, radians, Newtons, etc. Speed is measured in meters per second (m/s).

Best,
Nick

POST REPLY