def test_component_list(self): model = osim.Model(os.path.join(test_dir, "arm26.osim")) model.finalizeFromProperties() num_components = 0 for comp in model.getComponentsList(): num_components += 1 assert num_components > 0 num_bodies = 0 for body in model.getBodyList(): num_bodies += 1 body.getMass() assert num_bodies == 2 num_joints = 0 for joint in model.getJointList(): num_joints += 1 joint.numCoordinates() assert num_joints == 2 # Custom filtering. num_bodies = 0 for frame in model.getFrameList(): body = osim.Body.safeDownCast(frame) if body != None: num_bodies += 1 print(body.getName()) body.getInertia() assert num_bodies == 2 model = osim.Model() thelenMuscle = osim.Thelen2003Muscle("Darryl", 1, 0.5, 0.5, 0) millardMuscle = osim.Millard2012EquilibriumMuscle("Matt", 1, 0.5, 0.5, 0) model.addComponent(thelenMuscle) model.addComponent(millardMuscle) # Total number of muscles is 2. assert len(set(model.getMuscleList())) == 2 for muscle in model.getMuscleList(): assert (isinstance(muscle, osim.Thelen2003Muscle) or isinstance(muscle, osim.Millard2012EquilibriumMuscle)) # There is exactly 1 Thelen2003Muscle. assert len(set(model.getThelen2003MuscleList())) == 1 for muscle in model.getThelen2003MuscleList(): assert isinstance(muscle, osim.Thelen2003Muscle) # There is exactly 1 Millard2012EquilibriumMuscle. assert len(set(model.getMillard2012EquilibriumMuscleList())) == 1 for muscle in model.getMillard2012EquilibriumMuscleList(): assert isinstance(muscle, osim.Millard2012EquilibriumMuscle)
def test_muscle_helper_classes(self): # This test exists because some classes that Thelen2003Muscle used were # not accessibly in the bindings. muscle = osim.Thelen2003Muscle() fwpm = muscle.getPennationModel() fwpm.get_optimal_fiber_length() adm = muscle.getActivationModel() adm.get_activation_time_constant() muscle = osim.Millard2012EquilibriumMuscle() tendonFL = osim.TendonForceLengthCurve() muscle.setTendonForceLengthCurve(tendonFL)
elbow = osim.PinJoint( "elbow", humerus, # PhysicalFrame osim.Vec3(0, 0, 0), osim.Vec3(0, 0, 0), radius, # PhysicalFrame osim.Vec3(0, 1, 0), osim.Vec3(0, 0, 0)) # --------------------------------------------------------------------------- # Add a muscle that flexes the elbow (actuator for robotics people). # --------------------------------------------------------------------------- biceps = osim.Millard2012EquilibriumMuscle( "biceps", # Muscle name 200.0, # Max isometric force 0.6, # Optimal fibre length 0.55, # Tendon slack length 0.0) # Pennation angle biceps.addNewPathPoint("origin", humerus, osim.Vec3(0, 0.8, 0)) biceps.addNewPathPoint("insertion", radius, osim.Vec3(0, 0.7, 0)) # --------------------------------------------------------------------------- # Add a controller that specifies the excitation of the muscle. # --------------------------------------------------------------------------- brain = osim.PrescribedController() brain.addActuator(biceps) brain.prescribeControlForActuator("biceps", osim.StepFunction(0.5, 3.0, 0.3, 1.0))
def replace_thelen_muscles_with_millard(model_file, target_folder): """Replaces Thelen muscles with Millard muscles so that we can disable tendon compliance and perform MuscleAnalysis to compute normalized fiber length/velocity without spikes. """ model = opensim.Model(model_file) new_force_set = opensim.ForceSet() force_set = model.getForceSet() for i in range(force_set.getSize()): force = force_set.get(i) muscle = opensim.Muscle.safeDownCast(force) millard_muscle = opensim.Millard2012EquilibriumMuscle.safeDownCast( force) thelen_muscle = opensim.Thelen2003Muscle.safeDownCast(force) if muscle is None: new_force_set.adoptAndAppend(force.clone()) elif millard_muscle is not None: millard_muscle = millard_muscle.clone() millard_muscle.set_ignore_tendon_compliance(True) new_force_set.adoptAndAppend(millard_muscle) elif thelen_muscle is not None: millard_muscle = opensim.Millard2012EquilibriumMuscle() # properties millard_muscle.set_default_activation( thelen_muscle.getDefaultActivation()) millard_muscle.set_activation_time_constant( thelen_muscle.get_activation_time_constant()) millard_muscle.set_deactivation_time_constant( thelen_muscle.get_deactivation_time_constant()) # millard_muscle.set_fiber_damping(0) # millard_muscle.set_tendon_strain_at_one_norm_force( # thelen_muscle.get_FmaxTendonStrain()) millard_muscle.setName(thelen_muscle.getName()) millard_muscle.set_appliesForce(thelen_muscle.get_appliesForce()) millard_muscle.setMinControl(thelen_muscle.getMinControl()) millard_muscle.setMaxControl(thelen_muscle.getMaxControl()) millard_muscle.setMaxIsometricForce( thelen_muscle.getMaxIsometricForce()) millard_muscle.setOptimalFiberLength( thelen_muscle.getOptimalFiberLength()) millard_muscle.setTendonSlackLength( thelen_muscle.getTendonSlackLength()) millard_muscle.setPennationAngleAtOptimalFiberLength( thelen_muscle.getPennationAngleAtOptimalFiberLength()) millard_muscle.setMaxContractionVelocity( thelen_muscle.getMaxContractionVelocity()) # millard_muscle.set_ignore_tendon_compliance( # thelen_muscle.get_ignore_tendon_compliance()) millard_muscle.set_ignore_tendon_compliance(True) millard_muscle.set_ignore_activation_dynamics( thelen_muscle.get_ignore_activation_dynamics()) # muscle path pathPointSet = thelen_muscle.getGeometryPath().getPathPointSet() geomPath = millard_muscle.updGeometryPath() for j in range(pathPointSet.getSize()): pathPoint = pathPointSet.get(j).clone() geomPath.updPathPointSet().adoptAndAppend(pathPoint) # append new_force_set.adoptAndAppend(millard_muscle) else: raise RuntimeError('cannot handle the type of muscle: ' + force.getName()) new_force_set.printToXML(os.path.join(target_folder, 'muscle_set.xml'))