Esempio n. 1
0
    def simulate(self, inputs, dt=1):
        """Simulate IMC.

        Arguments:
        ----------
        U : `numpy.ndarray`
            2-dimensional array with two rows. First row U[0] contain a setting value,
            second row U[1] contain a return value from plant/process.
        time : `int`
            Simulation number of iterations
            Default ``time``=1
        Returns:
        --------
        y : `numpy.ndarray`
            Last controller's value
        """

        u, yplant = inputs
        u = Models.convert_2dim_array(u)
        yplant = Models.convert_2dim_array(yplant)

        ymodel = self.delay.now if self.delay else self.model.now
        super().simulate(u + ymodel - yplant, dt=dt)

        ycontrol = self.controller.simulate(inputs=super().now, dt=dt)
        self.model.simulate(inputs=self.controller.now, dt=dt)

        if self.delay:
            self.delay.simulate(inputs=self.model.now, dt=dt)

        return ycontrol
Esempio n. 2
0
    def simulate(self, inputs, dt=None):
        """Simulate IMC.

        Arguments:
        ----------
        U : `numpy.ndarray`
            2-dimensional array with two rows. First row U[0] contain a setting value,
            second row U[1] contain a return value from plant/process.
        time : `int`
            Simulation number of iterations
            Default ``time``=1
        Returns:
        --------
        y : `numpy.ndarray`
            Last controller's value
        """

        u, yplant = inputs
        u = Models.convert_2dim_array(u)
        yplant = Models.convert_2dim_array(yplant)

        yloop = Models.StateSpace.algebraic_loop_solver(
            u - yplant, self.controller, self.model)

        ymodel = self.model.simulate(inputs=yloop, dt=dt)

        super().simulate(u + ymodel - yplant, dt=dt)

        ycontrol = self.controller.simulate(inputs=super().now, dt=dt)

        return ycontrol
Esempio n. 3
0
def trapezoid_func_F(th_e):
    """Calculate value of trapezoidal waveform for a given motor electrical angle.
    
    Arguments
    ---------
    th_e : `float`
        Motor electrical angle in range [0, 2pi]
        
    Returns
    -------
    F : `float`
        Returns value of trapezoidal waveform for given angle
    """
    tau = np.pi / 6
    f = 2 / 3 * np.pi
    T = np.pi
    A = 1

    if th_e <= tau:
        F = th_e / tau
    elif th_e <= f + tau:
        F = A
    elif th_e <= T:
        F = A - A / tau * (th_e - (T - tau))
    elif th_e <= T + tau:
        F = -A / tau * (th_e - T)
    elif th_e <= 2 * T - tau:
        F = -1
    elif th_e <= 2 * T:
        F = -A + A / tau * (th_e - (2 * T - tau))
    else:
        raise Exception(
            "Could not get trapezoid value for argument th={}".format(th_e))
    return Models.convert_2dim_array(F)
Esempio n. 4
0
    def simulate(self, inputs, dt=None):
        """Simulation of a block.

        Arguments:
        ----------
        e : `float`, `numpy.ndarray`
            Input to the controller as error e=u-y, where u is a setting value, y a return value from a process.
        time : `int`
            Number of simulation iterations.
            Default ``time``=1

        Return:
        -------
        y : `float`
            Returns last value of simulation
        """
        e = Models.convert_2dim_array(inputs).reshape(-1, 1)

        yd = self.D_block.simulate(inputs=e, dt=dt) + e if self.D_block else e
        yp = self.P_block.simulate(inputs=yd, dt=dt) if self.P_block else yd
        yi = self.I_block.simulate(inputs=yp,
                                   dt=dt) + yp if self.I_block else yp

        super().simulate(yi)

        return yi
Esempio n. 5
0
    def simulate(self, inputs, dt=None):
        inputs = Models.convert_2dim_array(inputs).reshape(-1, 1)

        ea, eb, ec = self.back_Emf.now[3:6]
        uab, ubc, Tl = inputs

        U = [[uab], [ubc], [ea - eb], [eb - ec], [self.torque.now], [Tl]]
        super().simulate(inputs=U, dt=dt)
        self.back_Emf.simulate(inputs=self.now[3:5], dt=dt)  # [w, th_m]
        self.torque.simulate(inputs=[self.back_Emf.now[0:3], self.now[0:3]],
                             dt=dt)  # [Fa, Fb, Fc, ia, ib, ic]
Esempio n. 6
0
    def simulate(self, inputs, dt=None):
        """Simulate SP block.

        Arguments:
        ----------
        U : `numpy.ndarray`
            2-dimensional array with two rows. First row [0] contain a setting value,
            second [1] contain a return value from plant/process.
        dt : `float`
            Step time of simulation.
            Default ``dt``=1
        Returns:
        --------
        y : `numpy.ndarray`
            Last controller's value
        """

        u, yplant = inputs
        u = Models.convert_2dim_array(u)
        yplant = Models.convert_2dim_array(yplant)

        yfilter = yplant - self.model_delay.now
        if self.filter:
            yfilter = self.filter.simulate(yfilter, dt)

        ureg = u - yfilter

        yloop = Models.StateSpace.algebraic_loop_solver(ureg,
                                                        self.controller,
                                                        self.model,
                                                        sign=-1)
        ymodel = self.model.simulate(yloop, dt)
        ycontrol = self.controller.simulate(ureg - ymodel, dt)

        self.model_delay.simulate(ymodel, dt)

        return ycontrol
Esempio n. 7
0
    def simulate(self, inputs, dt=None):
        """Simulation of a block.

         Arguments:
         ----------
         inputs : `float`, `numpy.ndarray`
             Input to the controller as array ['e'] for 1-degree controller or ['e', 'u'] for 2-degree controller,
             where 'u' is a setting value, 'e' is an error 'e = yplant - u' with plant output 'yplant'.
         dt : `float`

         Return:
         -------
         y : `float`
             Returns last value from regulator.
         """
        inputs = Models.convert_2dim_array(inputs).reshape(-1, 1)

        if inputs.shape[0] == 1:
            e = inputs
            u = np.zeros((1, 1))
        elif inputs.shape[0] == 2:
            e, u = inputs
        else:
            raise Exception("PID: Expected max. 2 inputs. Got {}".format(
                inputs.shape))

        yp, yi, yd, yalfa, ybeta = np.zeros((5, 1, 1))

        if self.P_block: yp = self.P_block.simulate(inputs=e, dt=dt)
        if self.I_block: yi = self.I_block.simulate(inputs=e, dt=dt)
        if self.D_block: yd = self.D_block.simulate(inputs=e, dt=dt)
        if self.P2_block: yalfa = self.P2_block.simulate(inputs=u, dt=dt)
        if self.D2_block: ybeta = self.D2_block.simulate(inputs=u, dt=dt)
        y = yp + yi + yd + yalfa + ybeta
        super().simulate(y, dt=dt)

        return y