Пример #1
0
 def __init__(self, parameters=PendulumParameters()):
     super(Pendulum, self).__init__()
     self.origin = np.array([0.0, 0.0])
     self.theta = 0.0
     self.dtheta = 0.0
     self.parameters = parameters
     return
Пример #2
0
def exercise1b():
    """ Exercise 1  """
    biolog.info("Executing Lab 4 : Exercise 1")
    parameters = PendulumParameters()
    ### With damping
    t_start = 0.0
    t_stop = 10.0
    dt = 0.05

    biolog.warning("Using large time step dt={}".format(dt))
    time = np.arange(t_start, t_stop, dt)

    parameters.b1 = 0.5
    parameters.b2 = 0.5
    x0 = [np.pi / 3, 0]
    biolog.info(parameters.showParameters())
    res = integrate(pendulum_integration, x0, time, args=(parameters, ))

    res.plot_state("State")
Пример #3
0
def exercise1():
    """ Exercise 1  """
    biolog.info("Executing Lab 4 : Exercise 1")
    parameters = PendulumParameters()
    biolog.info(
        "Find more information about Pendulum Parameters in SystemParameters.py"
    )
    biolog.info("Loading default pendulum parameters")
    biolog.info(parameters.showParameters())

    # Simulation Parameters
    t_start = 0.0
    t_stop = 10.0
    dt = 0.001
    biolog.warning("Using large time step dt={}".format(dt))
    time0 = np.arange(t_start, t_stop / 2., dt)
    x0 = [np.pi / 2.,
          0.3]  # x0[0] = initial position in rad, x0[1] = initial velocity

    time1 = np.arange(t_stop / 2., t_stop, dt)
    x1 = [1.0,
          0.7]  # x0[0] = initial position in rad, x0[1] = initial velocity

    res0 = integrate(pendulum_integration, x0, time0, args=(parameters, ))
    res1 = integrate(pendulum_integration, x1, time1, args=(parameters, ))

    res0.plot_phase("Phase")
    res1.plot_phase("Phase")
    res0.plot_state("State")
    res1.plot_state("State")

    if DEFAULT["save_figures"] is False:
        plt.show()
        font = {'family': 'normal', 'weight': 'normal', 'size': 16}
        plt.rc('font', **font)
        plt.title(
            " Init_Pos_0 = $\pi/2$, Init_Vel_0 = {}, Init_Pos_1 = {}, Init_Vel_1 = {},\nK1 = {}, K2 = {}, theta_ref_1 = {}, theta_ref_2 = {}"
            .format(x0[1], x1[0], x1[1], parameters.k1, parameters.k2,
                    parameters.s_theta_ref1, parameters.s_theta_ref2))
        plt.minorticks_on()
        plt.grid(which='major', linestyle='-', linewidth='0.5', color='black')
        plt.grid(which='minor', linestyle=':', linewidth='0.5', color='black')
    return
Пример #4
0
def pendulum_equation(theta, dtheta, parameters=PendulumParameters()):
    """ Pendulum equation d2theta = -g/L*sin(theta)

    with:
        - theta: Angle [rad]
        - dtheta: Angular velocity [rad/s]
        - g: Gravity constant [m/s**2]
        - L: Length [m]
        - sin: np.sin
        - k1 : Spring constant of spring 1 [N/rad]
        - k2 : Spring constant of spring 2 [N/rad]
        - s_theta_ref1 : Spring 1 reference angle [rad]
        - s_theta_ref2 : Spring 2 reference angle [rad]
        - b1 : Damping constant damper 1 [N-s/rad]
        - b2 : Damping constant damper 2 [N-s/rad]
    """
    g, L, sin, k1, k2, s_theta_ref1, s_theta_ref2, b1, b2 = (
        parameters.g,
        parameters.L,
        parameters.sin,
        parameters.k1,
        parameters.k2,
        parameters.s_theta_ref1,
        parameters.s_theta_ref2,
        parameters.b1,
        parameters.b2
    )
    
    delta_theta1 = s_theta_ref1-theta
    delta_theta2 = s_theta_ref2-theta
    
    if s_theta_ref1 - theta > 0:
        delta_theta1 = 0
    if s_theta_ref2 - theta < 0:
        delta_theta2 = 0
        
    d2theta = k1*delta_theta1/L + k2*delta_theta2/L - g*sin(theta)/L - b1*dtheta - b2*dtheta
    
    return d2theta
Пример #5
0
def pendulum_equation(theta, dtheta, parameters=PendulumParameters()):
    """ Pendulum equation d2theta = -g/L*sin(theta)

    with:
        - theta: Angle [rad]
        - dtheta: Angular velocity [rad/s]
        - g: Gravity constant [m/s**2]
        - L: Length [m]
        - sin: np.sin
        - k1 : Spring constant of spring 1 [N/rad]
        - k2 : Spring constant of spring 2 [N/rad]
        - s_theta_ref1 : Spring 1 reference angle [rad]
        - s_theta_ref2 : Spring 2 reference angle [rad]
        - b1 : Damping constant damper 1 [N-s/rad]
        - b2 : Damping constant damper 2 [N-s/rad]
    """
    g, L, sin, k1, k2, s_theta_ref1, s_theta_ref2, b1, b2 = (
        parameters.g, parameters.L, parameters.sin, parameters.k1,
        parameters.k2, parameters.s_theta_ref1, parameters.s_theta_ref2,
        parameters.b1, parameters.b2)
    dampers = False
    if dampers == False:
        # 1a to 1c
        return (-g / L) * (
            np.sin(theta)) + k1 * (s_theta_ref1 - theta) * np.heaviside(
                -s_theta_ref1 + theta,
                0.5) + k2 * (s_theta_ref2 - theta) * np.heaviside(
                    s_theta_ref2 - theta, 0.5
                )  # returns theta-point-point, which is integrated afterwards
    else:
        # 1d and finish
        return (-g / L) * (np.sin(theta)) + (
            k1 * (s_theta_ref1 - theta) -
            b1 * dtheta) * np.heaviside(-s_theta_ref1 + theta, 0.5) + (
                k2 * (s_theta_ref1 - theta) - b2 * dtheta) * np.heaviside(
                    s_theta_ref2 - theta, 0.5
                )  # returns theta-point-point, which is integrated afterwards
Пример #6
0
def pendulum_system(theta, dtheta, parameters=PendulumParameters()):
    """ Pendulum """
    return np.array([
        [dtheta],
        [pendulum_equation(theta, dtheta, parameters)]  # d2theta
    ])
Пример #7
0
def exercise3():
    """ Main function to run for Exercise 3.

    Parameters
    ----------
        None

    Returns
    -------
        None
    """

    # Define and Setup your pendulum model here
    # Check Pendulum.py for more details on Pendulum class
    P_params = PendulumParameters()  # Instantiate pendulum parameters
    P_params.L = .5  # To change the default length of the pendulum
    P_params.mass = 5.  # To change the default mass of the pendulum
    pendulum = Pendulum(P_params)  # Instantiate Pendulum object

    #### CHECK OUT PendulumSystem.py to ADD PERTURBATIONS TO THE MODEL #####

    biolog.info('Pendulum model initialized \n {}'.format(
        pendulum.parameters.showParameters()))

    # Define and Setup your pendulum model here
    # Check MuscleSytem.py for more details on MuscleSytem class
    M1_param = MuscleParameters()  # Instantiate Muscle 1 parameters
    M1_param.f_max = 1500  # To change Muscle 1 max force
    M2_param = MuscleParameters()  # Instantiate Muscle 2 parameters
    M2_param.f_max = 1500  # To change Muscle 2 max force
    M1 = Muscle(M1_param)  # Instantiate Muscle 1 object
    M2 = Muscle(M2_param)  # Instantiate Muscle 2 object
    # Use the MuscleSystem Class to define your muscles in the system
    muscles = MuscleSytem(M1, M2)  # Instantiate Muscle System with two muscles
    biolog.info('Muscle system initialized \n {} \n {}'.format(
        M1.parameters.showParameters(), M2.parameters.showParameters()))
    ###
    ########################################## 3a ########################################
    #
    #    # Define a huge mass, such that the pendulum goes up to pi/2 and up t -pi/2
    #    P_params.mass = 15000.
    #    # Defines 3 different origins and 3 different insertions for each muscle
    #    origins1 = [-0.01, -0.05, -0.10, -0.15, -0.2]
    #    origins2 = map(lambda x: -(x), origins1)
    #    insertions1 = [-0.15, -0.20, -0.25, -0.30]
    #    insertions2 = insertions1[:] # Just for visibility
    #    legendsertion =np.array(['Insertion at -15 cm', 'Insertion at -20 cm', 'Insertion at -25 cm', 'Insertion at -30 cm'])
    #    legleg = np.array(['Insertion at -15 cm', 'Insertion at -15 cm', 'Insertion at -20 cm', 'Insertion at -20 cm', 'Insertion at -25 cm', 'Insertion at -25 cm', 'Insertion at -30 cm', 'Insertion at -30 cm'])
    #    cols = ('blue', 'red', 'olive', 'purple')
    ##    length1 = np.array()
    #
    #    for o1,o2 in zip(origins1, origins2):
    #        fig, (ax1, ax2) = plt.subplots(1,2, sharex = True)
    #        fig.suptitle('Origin of muscles: o1 = {}, o2 = {}'.format(o1,o2), fontsize='26')
    #        for i,j in enumerate(insertions1):
    #            # Define Muscle Attachment points
    #            m1_origin = np.array([o1, 0.0])  # Origin of Muscle 1
    #            m1_insertion = np.array([0.0, j])  # Insertion of Muscle 1
    #
    #            m2_origin = np.array([o2, 0.0])  # Origin of Muscle 2
    #            m2_insertion = np.array([0.0, j])  # Insertion of Muscle 2
    #
    #            # Attach the muscles
    #            muscles.attach(np.array([m1_origin, m1_insertion]),
    #                           np.array([m2_origin, m2_insertion]))
    #
    #            # Create a system with Pendulum and Muscles using the System Class
    #            # Check System.py for more details on System class
    #            sys = System()  # Instantiate a new system
    #            sys.add_pendulum_system(pendulum)  # Add the pendulum model to the system
    #            sys.add_muscle_system(muscles)  # Add the muscle model to the system
    #
    #            ##### Time #####
    #
    #            dt=0.01
    #            t_max = 1.  # Maximum simulation time
    #            time = np.arange(0., t_max, dt)  # Time vector
    #
    #
    #
    #            ##### Model Initial Conditions #####
    #            x0_P = np.array([np.pi/2. - 0.00001,0.])  # Pendulum initial condition
    #
    #            # Muscle Model initial condition
    #            x0_M = np.array([0., M1.l_CE, 0., M2.l_CE])
    #
    #            x0 = np.concatenate((x0_P, x0_M))  # System initial conditions
    #
    #            ##### System Simulation #####
    #            # For more details on System Simulation check SystemSimulation.py
    #            # SystemSimulation is used to initialize the system and integrate
    #            # over time
    #
    #            sim = SystemSimulation(sys)  # Instantiate Simulation object
    #
    #            # Add muscle activations to the simulation
    #            # Here you can define your muscle activation vectors
    #            # that are time dependent
    #
    #            act1=np.ones((len(time),1))*0.05
    #            act2=np.ones((len(time),1))*0.05
    #
    #            activations = np.hstack((act1, act2))
    #
    #            # Method to add the muscle activations to the simulation
    #
    #            sim.add_muscle_activations(activations)
    #
    #            # Simulate the system for given time
    #
    #            sim.initalize_system(x0, time)  # Initialize the system state
    #
    #            # Integrate the system for the above initialized state and time
    #            sim.simulate()
    #
    #            # Obtain the states of the system after integration
    #            # res is np.array [time, states]
    #            # states vector is in the same order as x0
    #            res = sim.results()
    #
    #            # In order to obtain internal paramters of the muscle
    #            # Check SystemSimulation.py results_muscles() method for more information
    #            res_muscles = sim.results_muscles()
    #            length1 = np.array(map(lambda x: np.sqrt(o1**2. + j**2. + 2.*o1*j*np.sin(x)), res[:,1])) # muscle 1 length
    #            length2 = np.array(map(lambda x: np.sqrt(o2**2. + j**2. + 2.*o2*j*np.sin(x)), res[:,1])) # muscle 2 length
    #            # moment arm of muscles calculation
    #            h1lambda = np.array(map(lambda x: np.abs(o1)*np.abs(j)*np.cos(x), res[:,1]))
    #            h2lambda = np.array(map(lambda x: np.abs(o2)*np.abs(j)*np.cos(x), res[:,1]))
    #            h1 = h1lambda/length1
    #            h2 = h2lambda/length2
    #            # Plotting the result for the current origins
    #            ax1.plot(res[:, 1], length1)
    #            ax2.plot(res[:, 1], h1)
    #            if o1 == -0.1:
    #                plt.figure('Muscles\' moment arm for insertions at {} and {}'.format(o1, o2))
    #                if j == -0.15:
    #                    temp = 0.0
    #                    plt.plot(res[:, 1], h1, color=(1.0, temp, 0.0), linestyle = ':')
    #                    plt.plot(res[:, 1], h2, color=(temp, .5, 1.), linestyle = ':')
    #                elif j == -0.2:
    #                    temp = 0.3
    #                    plt.plot(res[:, 1], h1, color=(1.0, temp, 0.0), linestyle = '--')
    #                    plt.plot(res[:, 1], h2, color=(temp, .5, 1.), linestyle = '--')
    #                elif j == -0.25:
    #                    temp = 0.7
    #                    plt.plot(res[:, 1], h1, color=(1.0, temp, 0.0), linestyle = '-.')
    #                    plt.plot(res[:, 1], h2, color=(temp, .5, 1.), linestyle = '-.')
    #                else:
    #                    temp = 1.0
    #                    plt.plot(res[:, 1], h1, color=(1.0, temp, 0.0))
    #                    plt.plot(res[:, 1], h2, color=(temp, .5, 1.))
    #                plt.legend(legleg)
    #                plt.xlabel('Pendulum position [rad]', fontsize = '18')
    #                plt.ylabel('Muscle moment arm [m]', fontsize = '18')
    #                plt.title('Muscles\' moment arm for insertions at {} and {}.\nRedish = muscle 1 (left), Blueish = muscle 2 (right)'.format(o1, o2), fontsize='26')
    #                plt.grid('ON')
    #
    #        ax1.legend(legendsertion)
    #        ax2.legend(legendsertion)
    #        ax1.set_xlabel('Position [rad]', fontsize='18')
    #        ax2.set_xlabel('Position [rad]', fontsize='18')
    #        ax1.set_ylabel('Muscle length [m]', fontsize='18') # Here we computed length 1 but muscle 2 has the same behaviour for identical params
    #        ax2.set_ylabel('Muscle moment arm [m]', fontsize='18')
    #        ax1.set_title('Muscle-tendon unit length in terms of pendulum position', fontsize='20')
    #        ax2.set_title('Muscle\'s moment arm', fontsize='20')
    #        ax1.grid()
    #        ax2.grid()
    #
    ######################################## End OF 3a ########################################
    ##
    ########################################## 3b ########################################
    #
    #    # Define a huge mass, such that the pendulum goes up to pi/2 and up t -pi/2
    #    P_params.mass = 1500.
    #    # Defines 3 different origins and 3 different insertions for each muscle
    #    origins1 = [-0.01, -0.05, -0.10, -0.15, -0.2]
    #    origins2 = map(lambda x: -(x), origins1)
    #    insertions1 = [-0.15, -0.20, -0.25, -0.30]
    #    insertions2 = insertions1[:] # Just for visibility
    #    legendsertion =np.array(['Insertion at -15 cm', 'Insertion at -20 cm', 'Insertion at -25 cm', 'Insertion at -30 cm'])
    #    legleg = np.array(['Insertion at -15 cm', 'Insertion at -15 cm', 'Insertion at -20 cm', 'Insertion at -20 cm', 'Insertion at -25 cm', 'Insertion at -25 cm', 'Insertion at -30 cm', 'Insertion at -30 cm'])
    #    cols = ('blue', 'red', 'olive', 'purple')
    ##    length1 = np.array()
    #
    #    for o1,o2 in zip(origins1, origins2):
    #        fig, (ax1, ax2) = plt.subplots(1,2, sharex = True)
    #        fig.suptitle('Origin of muscles - passive: o1 = {}, o2 = {}'.format(o1,o2), fontsize='26')
    #        for i,j in enumerate(insertions1):
    #            # Define Muscle Attachment points
    #            m1_origin = np.array([o1, 0.0])  # Origin of Muscle 1
    #            m1_insertion = np.array([0.0, j])  # Insertion of Muscle 1
    #
    #            m2_origin = np.array([o2, 0.0])  # Origin of Muscle 2
    #            m2_insertion = np.array([0.0, j])  # Insertion of Muscle 2
    #
    #            # Attach the muscles
    #            muscles.attach(np.array([m1_origin, m1_insertion]),
    #                           np.array([m2_origin, m2_insertion]))
    #
    #            # Create a system with Pendulum and Muscles using the System Class
    #            # Check System.py for more details on System class
    #            sys = System()  # Instantiate a new system
    #            sys.add_pendulum_system(pendulum)  # Add the pendulum model to the system
    #            sys.add_muscle_system(muscles)  # Add the muscle model to the system
    #
    #            ##### Time #####
    #
    #            dt=0.01
    #            t_max = 1.  # Maximum simulation time
    #            time = np.arange(0., t_max, dt)  # Time vector
    #
    #
    #
    #            ##### Model Initial Conditions #####
    #            x0_P = np.array([np.pi/2. - 0.00001,0.])  # Pendulum initial condition
    #
    #            # Muscle Model initial condition
    #            x0_M = np.array([0., M1.l_CE, 0., M2.l_CE])
    #
    #            x0 = np.concatenate((x0_P, x0_M))  # System initial conditions
    #
    #            ##### System Simulation #####
    #            # For more details on System Simulation check SystemSimulation.py
    #            # SystemSimulation is used to initialize the system and integrate
    #            # over time
    #
    #            sim = SystemSimulation(sys)  # Instantiate Simulation object
    #
    #            # Add muscle activations to the simulation
    #            # Here you can define your muscle activation vectors
    #            # that are time dependent
    #
    #            act1=np.ones((len(time),1))*0.00 # Passive muscles, unactivated.
    #            act2=np.ones((len(time),1))*0.00
    #
    #            activations = np.hstack((act1, act2))
    #
    #            # Method to add the muscle activations to the simulation
    #
    #            sim.add_muscle_activations(activations)
    #
    #            # Simulate the system for given time
    #
    #            sim.initalize_system(x0, time)  # Initialize the system state
    #
    #            # Integrate the system for the above initialized state and time
    #            sim.simulate()
    #
    #            # Obtain the states of the system after integration
    #            # res is np.array [time, states]
    #            # states vector is in the same order as x0
    #            res = sim.results()
    #
    #            # In order to obtain internal paramters of the muscle
    #            # Check SystemSimulation.py results_muscles() method for more information
    #            res_muscles = sim.results_muscles()
    #            length1 = np.array(map(lambda x: np.sqrt(o1**2. + j**2. + 2.*o1*j*np.sin(x)), res[:,1])) # muscle 1 length
    #            length2 = np.array(map(lambda x: np.sqrt(o2**2. + j**2. + 2.*o2*j*np.sin(x)), res[:,1])) # muscle 2 length
    #            # moment arm of muscles calculation
    #            h1lambda = np.array(map(lambda x: np.abs(o1)*np.abs(j)*np.cos(x), res[:,1]))
    #            h2lambda = np.array(map(lambda x: np.abs(o2)*np.abs(j)*np.cos(x), res[:,1]))
    #            h1 = h1lambda/length1
    #            h2 = h2lambda/length2
    #            # Plotting the result for the current origins
    #            ax1.plot(res[:, 1], length1)
    #            ax2.plot(res[:, 1], h1)
    #            if o1 == -0.1:
    #                plt.figure('Muscles\' moment arm for insertions at {} and {} - passive'.format(o1, o2))
    #                if j == -0.15:
    #                    temp = 0.0
    #                    plt.plot(res[:, 1], h1, color=(1.0, temp, 0.0), linestyle = ':')
    #                    plt.plot(res[:, 1], h2, color=(temp, .5, 1.), linestyle = ':')
    #                elif j == -0.2:
    #                    temp = 0.3
    #                    plt.plot(res[:, 1], h1, color=(1.0, temp, 0.0), linestyle = '--')
    #                    plt.plot(res[:, 1], h2, color=(temp, .5, 1.), linestyle = '--')
    #                elif j == -0.25:
    #                    temp = 0.7
    #                    plt.plot(res[:, 1], h1, color=(1.0, temp, 0.0), linestyle = '-.')
    #                    plt.plot(res[:, 1], h2, color=(temp, .5, 1.), linestyle = '-.')
    #                else:
    #                    temp = 1.0
    #                    plt.plot(res[:, 1], h1, color=(1.0, temp, 0.0))
    #                    plt.plot(res[:, 1], h2, color=(temp, .5, 1.))
    #                plt.legend(legleg)
    #                plt.xlabel('Pendulum position [rad]', fontsize = '18')
    #                plt.ylabel('Muscle moment arm [m]', fontsize = '18')
    #                plt.title('Muscles\' moment arm for insertions at {} and {} - passive.\nRedish = muscle 1 (left), Blueish = muscle 2 (right)'.format(o1, o2), fontsize='26')
    #                plt.grid('ON')
    #
    #        ax1.legend(legendsertion)
    #        ax2.legend(legendsertion)
    #        ax1.set_xlabel('Position [rad]', fontsize='18')
    #        ax2.set_xlabel('Position [rad]', fontsize='18')
    #        ax1.set_ylabel('Muscle length [m]', fontsize='18') # Here we computed length 1 but muscle 2 has the same behaviour for identical params
    #        ax2.set_ylabel('Muscle moment arm [m]', fontsize='18')
    #        ax1.set_title('Muscle-tendon unit length in terms of pendulum position', fontsize='20')
    #        ax2.set_title('Muscle\'s moment arm', fontsize='20')
    #        ax1.grid()
    #        ax2.grid()
    #
    ######################################## End OF 3b ########################################
    ######################################## Part 3c,d,e,f ##########################################

    LC = True  # Limit Cycle ON => LC True, Limit Cyle OFF => LC False

    if LC == True:
        P_params.mass = 10.
    else:
        P_params.mass = 100.

    # For point 3f, we changed max forces up here (line 55 and 57)

    # Define Muscle Attachment points
    m1_origin = np.array([-0.17, 0.0])  # Origin of Muscle 1
    m1_insertion = np.array([0.0, -0.17])  # Insertion of Muscle 1

    m2_origin = np.array([0.17, 0.0])  # Origin of Muscle 2
    m2_insertion = np.array([0.0, -0.17])  # Insertion of Muscle 2

    # Attach the muscles
    muscles.attach(np.array([m1_origin, m1_insertion]),
                   np.array([m2_origin, m2_insertion]))

    # Create a system with Pendulum and Muscles using the System Class
    # Check System.py for more details on System class
    sys = System()  # Instantiate a new system
    sys.add_pendulum_system(pendulum)  # Add the pendulum model to the system
    sys.add_muscle_system(muscles)  # Add the muscle model to the system

    ##### Time #####

    dt = 0.01
    t_max = 5.  # Maximum simulation time
    time = np.arange(0., t_max, dt)  # Time vector

    ##### Model Initial Conditions #####
    x0_P = np.array([2 * np.pi / 6., 0.])  # Pendulum initial condition

    # Muscle Model initial condition
    x0_M = np.array([0., M1.l_CE, 0., M2.l_CE])

    x0 = np.concatenate((x0_P, x0_M))  # System initial conditions

    ##### System Simulation #####
    # For more details on System Simulation check SystemSimulation.py
    # SystemSimulation is used to initialize the system and integrate
    # over time

    sim = SystemSimulation(sys)  # Instantiate Simulation object

    # Add muscle activations to the simulation
    # Here you can define your muscle activation vectors
    # that are time dependent

    act1 = np.arange(0., t_max, dt)
    act2 = np.arange(0., t_max, dt)

    if LC == True:
        act1 = (np.sin(2. * np.pi / t_max * 5. * act1) +
                1.) * .5  # acts = time
        act2 = (
            -np.sin(2. * np.pi / t_max * 5. * act2) + 1.
        ) * .5  # by increasing frequency, we don't let the time to gravity to make its office and contribute to the system's equilibrium, so the amplitude of the pendulum diminues.
        act1 = act1.reshape(len(act1), 1)
        act2 = act2.reshape(len(act2), 1)
    else:
        act1 = np.ones((len(time), 1)) * 0.25
        act2 = np.ones((len(time), 1)) * 0.25

    activations = np.hstack((act1, act2))
    # Method to add the muscle activations to the simulation

    sim.add_muscle_activations(activations)

    # Simulate the system for given time

    sim.initalize_system(x0, time)  # Initialize the system state

    # Integrate the system for the above initialized state and time
    sim.simulate()

    # Obtain the states of the system after integration
    # res is np.array [time, states]
    # states vector is in the same order as x0
    res = sim.results()

    # In order to obtain internal paramters of the muscle
    # Check SystemSimulation.py results_muscles() method for more information
    res_muscles = sim.results_muscles()

    # Plotting the results
    plt.figure('Pendulum')
    #    plt.title('Pendulum Phase')
    plt.title('Limit cycle behaviour for a max force of {} N'.format(M1.F_max),
              fontsize='22')
    #    plt.plot(res[:, 1], res[:, 2])
    plt.plot(time, res[:, 1])
    #    plt.xlabel('Position [rad]')
    #    plt.ylabel('Velocity [rad.s]')
    plt.xlabel('Time [s]')
    plt.ylabel('Pendulum position [rad]')
    plt.grid()

    # Plotting activation
    legact = ("Muscle 1 - left", "Muscle 2 - right")
    plt.figure('Activations')
    #    plt.title('Pendulum Phase')
    plt.title('Muscle activation patterns', fontsize='22')
    #    plt.plot(res[:, 1], res[:, 2])
    plt.plot(time, act1, color='red')
    plt.plot(time, act2, color='green')
    #    plt.xlabel('Position [rad]')
    #    plt.ylabel('Velocity [rad.s]')
    plt.xlabel('Time [s]')
    plt.ylabel('Activation [-]')
    plt.legend(legact)
    plt.grid()

    if DEFAULT["save_figures"] is False:
        plt.show()
    else:
        figures = plt.get_figlabels()
        biolog.debug("Saving figures:\n{}".format(figures))
        for fig in figures:
            plt.figure(fig)
            save_figure(fig)
            plt.close(fig)

    # To animate the model, use the SystemAnimation class
    # Pass the res(states) and systems you wish to animate
    simulation = SystemAnimation(res, pendulum, muscles)
    # To start the animation
    simulation.animate()
Пример #8
0
def exercise1a():
    """ Exercise 1  """
    biolog.info("Executing Lab 4 : Exercise 1")

    def period_(dt, state):
        zero = [0.0, 0.0]
        j = 0
        for i in range(1, len(state)):
            if state[i] * state[i - 1] <= 0:
                zero[j] = i
                j += 1
            if j == 2:
                break
        period = (zero[1] - zero[0]) * dt
        return period

    def different_initial_conditions(position, parameters, time):
        for position_ in position:
            x0 = [position_, 0]
            res = integrate(pendulum_integration,
                            x0,
                            time,
                            args=(parameters, ))
            res.plot_state("State")
            plt.title('State')
            res.plot_phase("Phase")
            plt.title('Phase')
        plt.show()
        return None

    def influence_of_k(k, parameters, time, x0, k_test):
        maxposition = np.ones(len(k))
        maxvelocity = np.ones(len(k))
        period = np.ones(len(k))
        ''' the change of amplitude and the period in function of k '''

        for i in range(0, len(k)):
            parameters.k1 = k[i]
            parameters.k2 = k[i]
            res = integrate(pendulum_integration,
                            x0,
                            time,
                            args=(parameters, ))

            maxposition[i] = max(res.state[:, 0])
            maxvelocity[i] = max(res.state[:, 1])
            period[i] = period_(dt, res.state[:, 1])

        plt.plot(k, maxposition)
        plt.plot(k, maxvelocity)
        plt.plot(k, period)
        plt.grid()
        plt.title(
            'The change of the amplitude and the period in function of k')
        plt.legend([
            'amplitude of position(rad)', 'amplitude of velocity(rad/s)',
            'period of movement(s)'
        ])
        plt.show()
        ''' plot position and velocity seperately for 2 k'''
        for i in range(0, 2):
            for k_ in k_test:
                parameters.k1 = k_
                parameters.k2 = k_
                res = integrate(pendulum_integration,
                                x0,
                                time,
                                args=(parameters, ))
                plt.plot(time, res.state[:, i])
            if i == 0:
                plt.title('Position-time')
            else:
                plt.title('Velocity-time')

            plt.legend(['k1 = %s' % (k_test[0]), 'k2 = %s' % (k_test[1])])
            #            plt.legend(['k = s'])
            plt.grid()
            plt.show()
        return None

    def influence_of_theta0(theta0, parameters, time, x0, theta0_test):

        maxposition = np.ones(len(theta0))
        minposition = np.ones(len(theta0))
        maxvelocity = np.ones(len(k))
        period = np.ones(len(k))
        ''' the change of amplitude, the range of motion, and the period in function of k '''
        for i in range(0, len(theta0)):
            parameters.s_theta_ref1 = theta0[i]
            parameters.s_theta_ref2 = theta0[i]
            res = integrate(pendulum_integration,
                            x0,
                            time,
                            args=(parameters, ))

            maxposition[i] = max(res.state[:, 0])
            minposition[i] = min(res.state[:, 0])
            maxvelocity[i] = max(res.state[:, 1])
            period[i] = period_(dt, res.state[:, 1])

        plt.plot(theta0, maxposition)
        plt.plot(theta0, minposition)
        plt.plot(theta0, maxvelocity)
        plt.plot(theta0, period)
        plt.title(
            'The change of the amplitude, the range of motion, and the period in function of k'
        )
        plt.legend([
            'amplitude of right position(rad)',
            'amplitude of left position(rad)', 'amplitude of velocity(rad/s)',
            'period of movement(s)'
        ])
        plt.grid()
        plt.show()

        for i in range(0, 2):
            for theta0_ in theta0_test:
                parameters.s_theta_ref1 = theta0_
                parameters.s_theta_ref2 = theta0_
                res = integrate(pendulum_integration,
                                x0,
                                time,
                                args=(parameters, ))
                plt.plot(time, res.state[:, i])

            if i == 0:
                plt.title('Position-time')
            else:
                plt.title('Velocity-time')

            plt.legend([
                'theta01 = %s' % (theta0_test[0]),
                'theta02 = %s' % (theta0_test[1])
            ])
            plt.grid()
            plt.show()

        return None

    def different_k1_k2(k1, k2, parameters, time, x0):
        parameters.k1 = k1
        parameters.k2 = k2
        res = integrate(pendulum_integration, x0, time, args=(parameters, ))
        res.plot_state("State")
        plt.title('State with k1 = %s, k2 = %s' % (k1, k2))
        plt.show()
        return None

    def different_theta01_theta02(theta01, theta02, parameters, time, x0):
        parameters.s_theta_ref1 = theta01
        parameters.s_theta_ref2 = theta02
        res = integrate(pendulum_integration, x0, time, args=(parameters, ))
        res.plot_state("State")
        plt.title('State with theta01 = %s, theta02 = %s' % (theta01, theta02))
        plt.show()
        return None

    # Simulation Parameters
    parameters = PendulumParameters()
    t_start = 0.0
    t_stop = 10.0
    dt = 0.05

    biolog.warning("Using large time step dt={}".format(dt))
    time = np.arange(t_start, t_stop, dt)

    # no damping
    parameters.b1 = 0
    parameters.b2 = 0

    x0 = [np.pi / 3, 0]

    position = [-np.pi / 2, 0, np.pi / 4]
    different_initial_conditions(position, parameters, time)

    k = np.linspace(1, 60, 40)
    k_test = [20, 50]
    influence_of_k(k, parameters, time, x0, k_test)

    theta0 = np.linspace(-np.pi / 2, np.pi / 2, 40)
    theta0_test = [np.pi / 6, np.pi / 3]
    influence_of_theta0(theta0, parameters, time, x0, theta0_test)

    ### different k1 and k2
    k1 = 10
    k2 = 30
    different_k1_k2(k1, k2, parameters, time, x0)

    ### different theta01 and theta02
    theta01 = 1.5
    theta02 = 1
    different_theta01_theta02(theta01, theta02, parameters, time, x0)
Пример #9
0
def exercise4():
    """ Main function to run for Exercise 3.

    Parameters
    ----------
        None

    Returns
    -------
        None
    """
    # Define and Setup your pendulum model here
    # Check Pendulum.py for more details on Pendulum class
    P_params = PendulumParameters()  # Instantiate pendulum parameters
    P_params.L = 0.5  # To change the default length of the pendulum
    P_params.mass = 1.  # To change the default mass of the pendulum
    pendulum = Pendulum(P_params)  # Instantiate Pendulum object

    #### CHECK OUT Pendulum.py to ADD PERTURBATIONS TO THE MODEL #####

    biolog.info('Pendulum model initialized \n {}'.format(
        pendulum.parameters.showParameters()))

    # Define and Setup your pendulum model here
    # Check MuscleSytem.py for more details on MuscleSytem class
    M1_param = MuscleParameters()  # Instantiate Muscle 1 parameters
    M1_param.f_max = 1500  # To change Muscle 1 max force
    M2_param = MuscleParameters()  # Instantiate Muscle 2 parameters
    M2_param.f_max = 1500  # To change Muscle 2 max force
    M1 = Muscle(M1_param)  # Instantiate Muscle 1 object
    M2 = Muscle(M2_param)  # Instantiate Muscle 2 object
    # Use the MuscleSystem Class to define your muscles in the system
    muscles = MuscleSytem(M1, M2)  # Instantiate Muscle System with two muscles
    biolog.info('Muscle system initialized \n {} \n {}'.format(
        M1.parameters.showParameters(),
        M2.parameters.showParameters()))

    # Define Muscle Attachment points
    m1_origin = np.array([-0.10, 0.0])  # Origin of Muscle 1
    m1_insertion = np.array([0.0, -0.17])  # Insertion of Muscle 1

    m2_origin = np.array([0.17, 0.0])  # Origin of Muscle 2
    m2_insertion = np.array([0.0, -0.10])  # Insertion of Muscle 2

    # Attach the muscles
    muscles.attach(np.array([m1_origin, m1_insertion]),
                   np.array([m2_origin, m2_insertion]))

    ##### Neural Network #####
    
    # The network consists of four neurons
    N_params = NetworkParameters()  # Instantiate default network parameters
    N_params.D = 1.  # To change a network parameter
    # Similarly to change w -> N_params.w = (4x4) array
    N_params.w=[[.0,-5.,-5.0,0.],
              [-5.,0.,.0,-5.0],
              [3.0,-1.0,.0,.0],
              [-1.0,3.,.0,.0]]
    
    N_params.b=[3.0,3.0,-3.0,-3.]
    N_params.tau=[.02,.02,.1,.1]
    biolog.info(N_params.showParameters())

    # Create a new neural network with above parameters
    neural_network = NeuralSystem(N_params)

    # Create system of Pendulum, Muscles and neural network using SystemClass
    # Check System.py for more details on System class
    sys = System()  # Instantiate a new system
    sys.add_pendulum_system(pendulum)  # Add the pendulum model to the system
    sys.add_muscle_system(muscles)  # Add the muscle model to the system
    # Add the neural network to the system
    sys.add_neural_system(neural_network)

    ##### Time #####
    t_max = 20.  # Maximum simulation time
    time = np.arange(0., t_max, 0.001)  # Time vector

    ##### Model Initial Conditions #####
    x0_P = np.array([0., 0.])  # Pendulum initial condition

    # Muscle Model initial condition
    x0_M = np.array([0., M1.l_CE, 0., M2.l_CE])

    x0_N = np.array([-0.5, 1, 0.5, 1])  # Neural Network Initial Conditions

    x0 = np.concatenate((x0_P, x0_M, x0_N))  # System initial conditions

    ##### System Simulation #####
    # For more details on System Simulation check SystemSimulation.py
    # SystemSimulation is used to initialize the system and integrate
    # over time

    sim = SystemSimulation(sys)  # Instantiate Simulation object

    # Add external inputs to neural network
    heaviside=np.heaviside(time - np.mean(time),0.5)
    h4=np.zeros((len(time),4))
    
    h4[:,0]=heaviside
    h4[:,1]=heaviside
    h4[:,2]=heaviside
    h4[:,3]=heaviside
    sim.add_external_inputs_to_network(h4) # Activates the external drive to its max (1)
    # sim.add_external_inputs_to_network(ext_in)

    sim.initalize_system(x0, time)  # Initialize the system state

    # Integrate the system for the above initialized state and time
    sim.simulate()

    # Obtain the states of the system after integration
    # res is np.array [time, states]
    # states vector is in the same order as x0
    res = sim.results()

    # In order to obtain internal paramters of the muscle
    # Check SystemSimulation.py results_muscles() method for more information
    res_muscles = sim.results_muscles()

    # Plotting the results
    plt.figure('Pendulum')
    plt.title('Pendulum Phase',fontsize = '18')
    plt.plot(res[:, 1], res[:, 2])
    plt.xlabel('Position [rad]',fontsize = '16')
    plt.ylabel('Velocity [rad/s]', fontsize = '16')
    plt.grid()

    if DEFAULT["save_figures"] is False:
        plt.show()
    else:
        figures = plt.get_figlabels()
        biolog.debug("Saving figures:\n{}".format(figures))
        for fig in figures:
            plt.figure(fig)
            save_figure(fig)
            plt.close(fig)

    # To animate the model, use the SystemAnimation class
    # Pass the res(states) and systems you wish to animate
    simulation = SystemAnimation(res, pendulum, muscles, neural_network)
    # To start the animation
    simulation.animate()
    
        # Plotting the results
    plt.figure('Time & Phase')
    plt.subplot(2,1,1)
    plt.plot(res[:,0],res[:,1])
#    plt.xlabel('Time [s]')
    plt.grid()
    plt.ylabel('Pos. [rad]',fontsize = '16')
    plt.title('Position',fontsize = '18')
    plt.subplot(2,1,2)
    plt.title('Velocity',fontsize = '18')
    plt.plot(res[:,0],res[:,2])
    plt.xlabel('Time [s]',fontsize = '16')
    plt.ylabel('Vel [rad/s]',fontsize = '16')
    plt.grid()
    plt.show()