def test_init(): with pytest.raises(TypeError): m = Muscle() motor_unit_count = 120 m = Muscle(motor_unit_count) assert m.motor_unit_count == motor_unit_count
def test_fatigue(): # With fatigue off the return should always be the same max_force = 32.0 m = Muscle(max_force, apply_peripheral_fatigue=False) # As measured by fatigue fatigue_before = m.get_peripheral_fatigue() assert fatigue_before == 0.0 # As measured by output moderate_input = 0.5 moderate_output = 0.39096348 for i in range(100): output = m.step(moderate_input, 1.0) assert output == pytest.approx(moderate_output) # And again by fatigue after fatigue_after = m.get_peripheral_fatigue() assert fatigue_after == 0.0 # Fatigue ON # You should see no change with zero activation m = Muscle(max_force, apply_peripheral_fatigue=True) # As measured by fatigue fatigue_before = m.get_peripheral_fatigue() assert fatigue_before == 0.0 for i in range(100): output = m.step(0.0, 1.0) assert output == pytest.approx(0.0) # As measured by fatgue after fatigue_after = m.get_peripheral_fatigue() assert fatigue_after == 0.0 # You should see increasing fatigue and decreasing output # with non-zero inputs. # Measured by output fatigued_output = 0.29151827 for i in range(100): output = m.step(moderate_input, 1.0) assert output == pytest.approx(fatigued_output) # As measured by fatgue after expected_fatigue = 0.18028181 fatigue_after = m.get_peripheral_fatigue() assert pytest.approx(fatigue_after, expected_fatigue)
def __init__(self, apply_fatigue=False): # Set up our 2D physics simulation self._init_sim() # Add a simulated arm consisting of: # - bones (rigid bodies) # - muscle bodies (damped spring constraints) self.brach, self.tricep = self._add_arm() # Instantiate the PyMuscles self.brach_muscle = Muscle(apply_peripheral_fatigue=apply_fatigue) self.tricep_muscle = Muscle( apply_peripheral_fatigue=False # Tricep never gets tired in this env ) self.frames = 0
def __init__(self, apply_fatigue=False): # Set up our 2D physics simulation self._init_sim() # Add a simulated arm consisting of: # - bones (rigid bodies) # - muscle bodies (damped spring constraints) self.brach, self.tricep = self._add_arm() # Instantiate the PyMuscles brach_motor_unit_count = 100 self.brach_muscle = Muscle(brach_motor_unit_count, apply_fatigue) tricep_motor_unit_count = 100 self.tricep_muscle = Muscle(tricep_motor_unit_count, apply_fatigue) self.frames = 0
def test_init(): max_force = 32.0 m = Muscle(max_force) # Check calculated number of motor units assert m.motor_unit_count == 120 # Check calculated max_output max_output = 2609.0309 assert m.max_arb_output == pytest.approx(max_output) max_force = 90.0 m = Muscle(max_force) # Check calculated number of motor units assert m.motor_unit_count == 340 max_output = 7338.29062 assert m.max_arb_output == pytest.approx(max_output)
def __init__(self, apply_fatigue=False): # Instantiate the PyMuscles (not real muscle names) hamstring_motor_unit_count = 300 self.hamstring_muscle = Muscle(hamstring_motor_unit_count, apply_fatigue) thigh_motor_unit_count = 300 self.thigh_muscle = Muscle(thigh_motor_unit_count, apply_fatigue) calf_motor_unit_count = 200 self.calf_muscle = Muscle(calf_motor_unit_count, apply_fatigue) shin_motor_unit_count = 100 self.shin_muscle = Muscle(shin_motor_unit_count, apply_fatigue) self.muscles = [ self.hamstring_muscle, self.thigh_muscle, self.calf_muscle, self.shin_muscle ] # Initialize parents mujoco_env.MujocoEnv.__init__(self, 'muscled-hopper.xml', 4) utils.EzPickle.__init__(self)
def test_step(): max_force = 32.0 m = Muscle(max_force) with pytest.raises(TypeError): m.step() with pytest.raises(AssertionError): m.step(np.ones(3), 1) # No excitation output = m.step(np.zeros(m.motor_unit_count), 1.0) assert output == pytest.approx(0.0) # Moderate m = Muscle(max_force) moderate_input = 0.5 moderate_output = 0.39096348 output = m.step(np.full(m.motor_unit_count, moderate_input), 1.0) assert output == pytest.approx(moderate_output) # Moderate - single value m = Muscle(max_force) output = m.step(moderate_input, 1.0) assert output == pytest.approx(moderate_output) # Max m = Muscle(max_force) max_input = 1.0 max_output = 0.84935028 output = m.step(np.full(m.motor_unit_count, max_input), 1.0) assert output == pytest.approx(max_output) # Above m = Muscle(max_force) output = m.step(np.full(m.motor_unit_count, max_input + 40), 1.0) assert output == pytest.approx(max_output)
def test_step(): motor_unit_count = 120 m = Muscle(motor_unit_count) with pytest.raises(TypeError): m.step() with pytest.raises(AssertionError): m.step(np.ones(3), 1) # No excitation output = m.step(np.zeros(motor_unit_count), 1.0) assert output == pytest.approx(0.0) # Moderate m = Muscle(motor_unit_count) moderate_input = 40.0 moderate_output = 1311.86896 output = m.step(np.full(motor_unit_count, moderate_input), 1.0) assert output == pytest.approx(moderate_output) # Moderate - single value m = Muscle(motor_unit_count) output = m.step(moderate_input, 1.0) assert output == pytest.approx(moderate_output) # Max m = Muscle(motor_unit_count) max_input = 67.0 max_output = 2215.98114 output = m.step(np.full(motor_unit_count, 67.0), 1.0) assert output == pytest.approx(max_output) # Above m = Muscle(motor_unit_count) output = m.step(np.full(motor_unit_count, max_input + 40), 1.0) assert output == pytest.approx(max_output)
def _get_muscles( self, muscle_count, max_force=100.0 ): """ Create N muscles where N is the number of actuators specified in the model file. Note: max_force should be kept in line with the gainprm on actuators in the <xml_filename>.xml """ muscles = [] for i in range(muscle_count): muscle = Muscle(max_force) muscles.append(muscle) return muscles
from pymuscle import PotvinFuglevandMuscle as Muscle from pymuscle.vis import PotvinChart # Create a Muscle with small number of motor units. motor_unit_count = 120 muscle = Muscle(motor_unit_count) # Set up the simulation parameters sim_duration = 200 # seconds frames_per_second = 50 step_size = 1 / frames_per_second total_steps = int(sim_duration / step_size) # Use a constant level of excitation to more easily observe fatigue excitation = 40.0 total_outputs = [] outputs_by_unit = [] print("Starting simulation ...") for i in range(total_steps): # Calling step() updates the simulation and returns the total output # produced by the muscle during this step for the given excitation level. total_output = muscle.step(excitation, step_size) total_outputs.append(total_output) # You can also introspect the muscle to see the forces being produced # by each motor unit. output_by_unit = muscle.current_forces outputs_by_unit.append(output_by_unit) if (i % (frames_per_second * 10)) == 0: print("Sim time - {} seconds ...".format(int(i / frames_per_second)))
def instantiation(a, n): for _ in range(a): m = Muscle(n)
def step(a, n): moderate_input = 40.0 in_vec = np.full(n, moderate_input) m = Muscle(n) for _ in range(a): m.step(in_vec, 1.0)