def __init__(self, model_path, visualize, integrator_accuracy = 5e-5): self.integrator_accuracy = integrator_accuracy self.model = opensim.Model(model_path) self.model.initSystem() self.brain = opensim.PrescribedController() # Enable the visualizer self.model.setUseVisualizer(visualize) self.muscleSet = self.model.getMuscles() self.forceSet = self.model.getForceSet() self.bodySet = self.model.getBodySet() self.jointSet = self.model.getJointSet() self.markerSet = self.model.getMarkerSet() self.contactGeometrySet = self.model.getContactGeometrySet() if self.verbose: self.list_elements() # Add actuators as constant functions. Then, during simulations # we will change levels of constants. # One actuartor per each muscle for j in range(self.muscleSet.getSize()): func = opensim.Constant(1.0) self.brain.addActuator(self.muscleSet.get(j)) self.brain.prescribeControlForActuator(j, func) self.maxforces.append(self.muscleSet.get(j).getMaxIsometricForce()) self.curforces.append(1.0) self.noutput = self.muscleSet.getSize() self.model.addController(self.brain) self.model.initSystem()
def test_markAdopted2(self): a = osim.Model() # We just need the following not to cause a segfault. # Model add* pa = osim.PathActuator() pa.setName('pa') a.addForce(pa) probe = osim.Umberger2010MuscleMetabolicsProbe() probe.setName('probe') a.addProbe(probe) ma = osim.MuscleAnalysis() ma.setName('ma') a.addAnalysis(ma) pc = osim.PrescribedController() pc.setName('pc') a.addController(pc) body = osim.Body('body1', 1.0, osim.Vec3(0, 0, 0), osim.Inertia(0, 0, 0)) loc_in_parent = osim.Vec3(0, 0, 0) orient_in_parent = osim.Vec3(0, 0, 0) loc_in_body = osim.Vec3(0, 0, 0) orient_in_body = osim.Vec3(0, 0, 0) print "creating Weld Joint.." joint = osim.WeldJoint("weld_joint", a.getGround(), loc_in_parent, orient_in_parent, body, loc_in_body, orient_in_parent) print "adding a body .." a.addBody(body) print "adding a joint .." a.addJoint(joint) print "Creating a ConstantDistanceConstraint.." constr = osim.ConstantDistanceConstraint() constr.setBody1ByName("ground") constr.setBody1PointLocation(osim.Vec3(0, 0, 0)) constr.setBody2ByName("body") constr.setBody2PointLocation(osim.Vec3(1, 0, 0)) constr.setConstantDistance(1) a.addConstraint(constr) f = osim.BushingForce("bushing", "ground", "body", osim.Vec3(2, 2, 2), osim.Vec3(1, 1, 1), osim.Vec3(0, 0, 0), osim.Vec3(0, 0, 0)) a.addForce(f) f2 = osim.BushingForce() a.addForce(f2) f3 = osim.SpringGeneralizedForce() a.addForce(f3) model = osim.Model(os.path.join(test_dir, "arm26.osim")) g = osim.CoordinateActuator('r_shoulder_elev') model.addForce(g)
def test(model_path, visualize): model = opensim.Model(model_path) brain = opensim.PrescribedController() model.addController(brain) state = model.initSystem() muscleSet = model.getMuscles() for j in range(muscleSet.getSize()): brain.addActuator(muscleSet.get(j)) func = opensim.Constant(1.0) brain.prescribeControlForActuator(j, func) block = opensim.Body('block', 0.0001, opensim.Vec3(0), opensim.Inertia(1, 1, .0001, 0, 0, 0)) model.addComponent(block) pj = opensim.PlanarJoint( 'pin', model.getGround(), # PhysicalFrame opensim.Vec3(0, 0, 0), opensim.Vec3(0, 0, 0), block, # PhysicalFrame opensim.Vec3(0, 0, 0), opensim.Vec3(0, 0, 0)) model.addComponent(pj) model.initSystem() pj.getCoordinate(1)
def __init__(self, model_path, k_paths_dict, visualize, integrator_accuracy = 1e-3): # Reduced accuracy for greater speed self.integrator_accuracy = integrator_accuracy self.model = opensim.Model(model_path) self.model.initSystem() self.brain = opensim.PrescribedController() self.k_path = k_paths_dict[list(k_paths_dict.keys())[0]] self.states = pd.read_csv(self.k_path, index_col=False).drop('Unnamed: 0', axis=1) for speed in list(k_paths_dict.keys()): self.states_trajectories_dict[speed] = pd.read_csv(k_paths_dict[speed], index_col=False).drop('Unnamed: 0', axis=1) # Enable the visualizer self.model.setUseVisualizer(visualize) self.muscleSet = self.model.getMuscles() self.forceSet = self.model.getForceSet() self.bodySet = self.model.getBodySet() self.jointSet = self.model.getJointSet() self.markerSet = self.model.getMarkerSet() self.contactGeometrySet = self.model.getContactGeometrySet() self.actuatorSet = self.model.getActuators() if self.verbose: self.list_elements() for j in range(self.actuatorSet.getSize()): func = opensim.Constant(1.0) self.brain.addActuator(self.actuatorSet.get(j)) self.brain.prescribeControlForActuator(j, func) self.curforces.append(1.0) try: self.maxforces.append(self.muscleSet.get(j).getMaxIsometricForce()) except: self.maxforces.append(np.inf) self.noutput = self.actuatorSet.getSize() self.model.addController(self.brain) self.model.initSystem()
def test_markAdopted(): a = osim.Model() # We just need the following not to not cause a segfault. # Model add* a.addComponent(osim.PathActuator()) a.addProbe(osim.Umberger2010MuscleMetabolicsProbe()) a.addAnalysis(osim.MuscleAnalysis()) a.addController(osim.PrescribedController()) body = osim.Body('body', 1.0, osim.Vec3(0, 0, 0), osim.Inertia(0, 0, 0)) loc_in_parent = osim.Vec3(0, -0, 0) orient_in_parent = osim.Vec3(0, 0, 0) loc_in_body = osim.Vec3(0, 0, 0) orient_in_body = osim.Vec3(0, 0, 0) joint = osim.WeldJoint("weld_joint", a.getGroundBody(), loc_in_parent, orient_in_parent, body, loc_in_body, orient_in_parent) a.addBody(body) constr = osim.ConstantDistanceConstraint() constr.setBody1ByName("ground") constr.setBody1PointLocation(osim.Vec3(0, 0, 0)) constr.setBody2ByName("body") constr.setBody2PointLocation(osim.Vec3(1, 0, 0)) constr.setConstantDistance(1) a.addConstraint(constr) # Force requires body names. If not provided, you get a segfault. f = osim.BushingForce() f.setBody1ByName("ground") f.setBody2ByName("body") a.addForce(f) model = osim.Model(os.environ['OPENSIM_HOME'] + "/Models/Arm26/arm26.osim") g = osim.CoordinateActuator('r_shoulder_elev') model.addForce(g)
def test_PrescribedController_prescribeControlForActuator(self): # Test memory management for # PrescribedController::prescribeControlForActuator(). model = osim.Model() # Body. body = osim.Body('b1', 1.0, osim.Vec3(0, 0, 0), osim.Inertia(0, 0, 0)) model.addBody(body) # Joint. joint = osim.PinJoint('j1', model.getGround(), body) model.addJoint(joint) # Actuator. actu = osim.CoordinateActuator() actu.setName('actu') actu.setCoordinate(joint.get_coordinates(0)) model.addForce(actu) # Controller. contr = osim.PrescribedController() contr.addActuator(actu) self.assertRaises(RuntimeError, contr.prescribeControlForActuator, 1, osim.Constant(3)) # The following calls should not cause a memory leak: contr.prescribeControlForActuator(0, osim.Constant(2)) contr.prescribeControlForActuator('actu', osim.Constant(4))
def __init__(self, model_path, visualize): self.model = opensim.Model(model_path) self.model.initSystem() self.brain = opensim.PrescribedController() # Enable the visualizer self.model.setUseVisualizer(visualize) self.muscleSet = self.model.getMuscles() self.forceSet = self.model.getForceSet() self.bodySet = self.model.getBodySet() self.jointSet = self.model.getJointSet() self.contactGeometrySet = self.model.getContactGeometrySet() for j in range(self.muscleSet.getSize()): func = opensim.Constant(1.0) self.brain.addActuator(self.muscleSet.get(j)) self.brain.prescribeControlForActuator(j, func) self.maxforces.append(self.muscleSet.get(j).getMaxIsometricForce()) self.curforces.append(1.0) self.model.addController(self.brain)
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)) # --------------------------------------------------------------------------- # Build model with components created above. # --------------------------------------------------------------------------- arm.addBody(humerus) arm.addBody(radius) arm.addJoint(shoulder) # Now required in OpenSim4.0 arm.addJoint(elbow) arm.addForce(biceps) arm.addController(brain)
def test_markAdoptedSets(): # Set's. fus = osim.FunctionSet() fu1 = osim.Constant() fus.adoptAndAppend(fu1) del fus del fu1 gs = osim.GeometrySet() dg = osim.DisplayGeometry() gs.adoptAndAppend(dg) del gs del dg s = osim.ScaleSet() o = osim.Scale() s.adoptAndAppend(o) del s del o s = osim.ForceSet() o = osim.BushingForce() s.adoptAndAppend(o) del s del o cs = osim.ControllerSet() csc = osim.PrescribedController() cs.adoptAndAppend(csc) del cs del csc s = osim.ContactGeometrySet() o = osim.ContactHalfSpace() s.adoptAndAppend(o) del s del o s = osim.AnalysisSet() o = osim.MuscleAnalysis() s.adoptAndAppend(o) del s del o s = osim.ControlSet() o = osim.ControlLinear() s.adoptAndAppend(o) del s del o s = osim.MarkerSet() o = osim.Marker() s.adoptAndAppend(o) del s del o s = osim.BodySet() o = osim.Body() s.adoptAndAppend(o) del s del o s = osim.BodyScaleSet() o = osim.BodyScale() s.adoptAndAppend(o) del s del o s = osim.CoordinateSet() o = osim.Coordinate() s.adoptAndAppend(o) del s del o s = osim.JointSet() o = osim.BallJoint() s.adoptAndAppend(o) del s del o s = osim.PathPointSet() o = osim.PathPoint() s.adoptAndAppend(o) del s del o s = osim.IKTaskSet() o = osim.IKMarkerTask() s.adoptAndAppend(o) del s del o s = osim.MarkerPairSet() o = osim.MarkerPair() s.adoptAndAppend(o) del s del o s = osim.MeasurementSet() o = osim.Measurement() s.adoptAndAppend(o) del s del o # TODO # s = osim.ProbeSet() # o = osim.Umberger2010MuscleMetabolicsProbe() # s.adoptAndAppend(o) # del s # del o s = osim.ConstraintSet() a = osim.Model() body = osim.Body('body', 1.0, osim.Vec3(0, 0, 0), osim.Inertia(0, 0, 0)) loc_in_parent = osim.Vec3(0, -0, 0) orient_in_parent = osim.Vec3(0, 0, 0) loc_in_body = osim.Vec3(0, 0, 0) orient_in_body = osim.Vec3(0, 0, 0) joint = osim.WeldJoint("weld_joint", a.getGroundBody(), loc_in_parent, orient_in_parent, body, loc_in_body, orient_in_parent) a.addBody(body) constr = osim.ConstantDistanceConstraint() constr.setBody1ByName("ground") constr.setBody1PointLocation(osim.Vec3(0, 0, 0)) constr.setBody2ByName("body") constr.setBody2PointLocation(osim.Vec3(1, 0, 0)) constr.setConstantDistance(1) s.adoptAndAppend(constr)
solution = study.solve() osim.STOFileAdapter.write(solution.exportToStatesTable(), "exampleHangingMuscle_states.sto") osim.STOFileAdapter.write(solution.exportToControlsTable(), "exampleHangingMuscle_controls.sto") # Conduct an analysis using MuscleAnalysis and ProbeReporter. # Create an AnalyzeTool setup file. analyze = osim.AnalyzeTool() analyze.setName("analyze") analyze.setModelFilename("hanging_muscle.osim") analyze.setStatesFileName("exampleHangingMuscle_states.sto") analyze.updAnalysisSet().cloneAndAppend(osim.MuscleAnalysis()) analyze.updAnalysisSet().cloneAndAppend(osim.ProbeReporter()) analyze.updControllerSet().cloneAndAppend( osim.PrescribedController("exampleHangingMuscle_controls.sto")) analyze.printToXML("exampleHangingMuscle_AnalyzeTool_setup.xml") # Run the analysis. analyze = osim.AnalyzeTool("exampleHangingMuscle_AnalyzeTool_setup.xml") analyze.run() table_force = osim.TimeSeriesTable( "analyze_MuscleAnalysis_ActiveFiberForce.sto") table_velocity = osim.TimeSeriesTable( "analyze_MuscleAnalysis_FiberVelocity.sto") time = table_force.getIndependentColumn() force = table_force.getDependentColumn("muscle").to_numpy() velocity = table_velocity.getDependentColumn("muscle").to_numpy() # Plot the terms of the metabolics model, and compare the metabolics model's # mechanical work rate to the mechanical work rate computed using the
def __init__(self, model_path, visualize, integrator_accuracy = 5e-8, integrator_MaxStepSize = 1e-1, Perturbtime = 50): #Shakiba self.integrator_accuracy = integrator_accuracy self.integrator_MaxStepSize = integrator_MaxStepSize #Shakiba self.model = opensim.Model(model_path) self.model_state = self.model.initSystem() self.brain = opensim.PrescribedController() # Enable the visualizer self.model.setUseVisualizer(visualize) self.muscleSet = self.model.getMuscles() self.forceSet = self.model.getForceSet() self.bodySet = self.model.getBodySet() self.jointSet = self.model.getJointSet() self.markerSet = self.model.getMarkerSet() self.contactGeometrySet = self.model.getContactGeometrySet() if self.verbose: self.list_elements() # Add actuators as constant functions. Then, during simulations # we will change levels of constants. # One actuartor per each muscle for j in range(self.muscleSet.getSize()): func = opensim.Constant(1.0) self.brain.addActuator(self.muscleSet.get(j)) self.brain.prescribeControlForActuator(j, func) self.maxforces.append(self.muscleSet.get(j).getMaxIsometricForce()) self.curforces.append(1.0) # Shakiba # Add external forces as constant functions. Then, during simulations # we will change levels of constants. # One actuartor per each ankle PerturbSites = ['talus_r', 'talus_l'] # perturbation site external_force = {} # external forces dictionary for j in PerturbSites: external_force[j] = opensim.PrescribedForce() sim_t = 50 sim_dt = self.stepsize t_pert = np.arange(0, sim_t+sim_dt, sim_dt) # construct a spline for fx perturb = opensim.SimmSpline() sigma = 0.005 miu = Perturbtime #miu = 8.1 #fx = 0.1*(((1/(sigma*np.sqrt(2*np.pi)))*np.exp(-((t_pert-miu)**2)/(2*sigma**2)))/2) # weighted Gaussian function divided by 2 to account for each leg fx = 0.5*(((1/(sigma*np.sqrt(2*np.pi)))*np.exp(-((t_pert-miu)**2)/(2*sigma**2)))/2) # weighted Gaussian function divided by 2 to account for each leg for i in range(t_pert.shape[0]): perturb.addPoint(t_pert[i], fx[i]) for j in PerturbSites: #make sure to add the absolute path of the body frame external_force[j].setBodyName('/bodyset/{}'.format(j)) #define the point of application in body fixed frame external_force[j].setPointFunctions(opensim.Constant(0), opensim.Constant(0), opensim.Constant(0)) #apply external forces external_force[j].set_forceIsGlobal(True) external_force[j].setForceFunctions(perturb, opensim.Constant(0), opensim.Constant(0)) self.model.addForce(external_force[j]) # self.noutput = self.muscleSet.getSize() self.model.addController(self.brain) self.model_state = self.model.initSystem()
osimModel.addBody(linkage3) osimModel.addBody(linkage4) osimModel.addJoint(tip) osimModel.addJoint(ankle) osimModel.addJoint(knee) osimModel.addJoint(hip) a1 = addActivationCoordinateActuator(osimModel, "q1", "ap") a2 = addActivationCoordinateActuator(osimModel, "q2", "kp") a3 = addActivationCoordinateActuator(osimModel, "q3", "hp") a_1 = addActivationCoordinateActuator(osimModel, "q1", "am") a_2 = addActivationCoordinateActuator(osimModel, "q2", "km") a_3 = addActivationCoordinateActuator(osimModel, "q3", "hm") actcontroller = osim.PrescribedController() actcontroller.addActuator(a1) actcontroller.addActuator(a2) actcontroller.addActuator(a3) actcontroller.addActuator(a_1) actcontroller.addActuator(a_2) actcontroller.addActuator(a_3) actcontroller.prescribeControlForActuator("ap", Constant(0)) actcontroller.prescribeControlForActuator("kp", Constant(0)) actcontroller.prescribeControlForActuator("hp", Constant(0)) actcontroller.prescribeControlForActuator("am", Constant(0)) actcontroller.prescribeControlForActuator("km", Constant(0)) actcontroller.prescribeControlForActuator("hm", Constant(0)) actcontroller.set_interpolation_method(3) osimModel.addController(actcontroller)