コード例 #1
0
    def __init__(
        self,
        parent,
        motor_constant,
        resistance,
        inductance,
        moment_of_inertia,
        initial_speed=None,
        initial_current=None,
    ):
        Block.__init__(self, parent)
        self.motor_constant = motor_constant
        self.resistance = resistance
        self.inductance = inductance
        self.moment_of_inertia = moment_of_inertia

        # Create the velocity and current state
        # These can also be used as signals which export the exact value of
        # the respective state.
        self.omega = State(
            self,
            derivative_function=self.omega_dt,
            initial_condition=initial_speed,
        )
        self.current = State(
            self,
            derivative_function=self.current_dt,
            initial_condition=initial_current,
        )

        # Create (input) ports for voltage and external torque load
        self.voltage = Port()
        self.external_torque = Port()
コード例 #2
0
    def __init__(
        self,
        parent,
        motor_constant,
        resistance,
        inductance,
        moment_of_inertia,
        initial_omega=0,
        initial_current=0,
    ):
        Block.__init__(self, parent)

        self.motor_constant = motor_constant
        self.resistance = resistance
        self.inductance = inductance
        self.moment_of_inertia = moment_of_inertia

        self.voltage = Port()
        self.external_torque = Port()

        self.omega = State(
            self,
            derivative_function=self.omega_dot,
            initial_condition=initial_omega,
        )
        self.current = State(
            self,
            derivative_function=self.current_dot,
            initial_condition=initial_current,
        )
コード例 #3
0
    def __init__(self, parent, thrust_coefficient, power_coefficient,
                 diameter):
        Block.__init__(self, parent)
        self.thrust_coefficient = thrust_coefficient
        self.power_coefficient = power_coefficient
        self.diameter = diameter

        # Define the input ports for propeller speed and air density
        self.speed_rps = Port()
        self.density = Port()
コード例 #4
0
ファイル: quadcopter_trim.py プロジェクト: ralfgerlich/modypy
class Engine(Block):
    """
    An engine block consisting of a propeller and a DC-motor.
    """
    def __init__(self, parent, thrust_coefficient, power_coefficient, diameter,
                 motor_constant, resistance, inductance, moment_of_inertia,
                 direction, vector, arm):
        Block.__init__(self, parent)

        # Create ports for the motor voltage and the air density
        self.voltage = Port()
        self.density = Port()

        # Create the motor
        self.dcmotor = DCMotor(self,
                               motor_constant,
                               resistance,
                               inductance,
                               moment_of_inertia,
                               initial_omega=1)

        # Create the propeller
        self.propeller = Propeller(self,
                                   thrust_coefficient=thrust_coefficient,
                                   power_coefficient=power_coefficient,
                                   diameter=diameter)

        # Create a thruster that converts the scalar thrust and torque into
        # thrust and torque vectors, considering the arm of the engine
        # relative to the center of gravity
        self.thruster = Thruster(self,
                                 direction=direction,
                                 vector=vector,
                                 arm=arm)

        # Provide output ports for the thrust and torque vectors
        self.thrust_vector = Port(shape=3)
        self.torque_vector = Port(shape=3)

        # Connect the voltage to the motor
        self.dcmotor.voltage.connect(self.voltage)
        # Connect the density to the propeller
        self.propeller.density.connect(self.density)

        # Connect the motor and propeller to each other
        self.dcmotor.external_torque.connect(self.propeller.torque)
        self.propeller.speed_rps.connect(self.dcmotor.speed_rps)

        # Provide the scalar thrust and torque to the thruster
        self.thruster.scalar_thrust.connect(self.propeller.thrust)
        self.thruster.scalar_torque.connect(self.dcmotor.torque)

        # Connect the thrust and torque vectors to the outputs
        self.thrust_vector.connect(self.thruster.thrust_vector)
        self.torque_vector.connect(self.thruster.torque_vector)
コード例 #5
0
ファイル: aerodyn.py プロジェクト: ralfgerlich/modypy
    def __init__(self,
                 parent,
                 vector,
                 arm,
                 direction=1):
        Block.__init__(self, parent)
        self.vector = vector
        self.arm = arm
        self.direction = direction

        self.scalar_thrust = Port()
        self.scalar_torque = Port()
コード例 #6
0
    def __init__(
        self,
        owner,
        mass,
        moment_of_inertia,
        initial_velocity_earth=None,
        initial_position_earth=None,
        initial_transformation=None,
        initial_angular_rates_earth=None,
    ):
        Block.__init__(self, owner)
        self.mass = mass
        self.moment_of_inertia = moment_of_inertia

        self.forces_body = Port(shape=3)
        self.moments_body = Port(shape=3)

        if initial_transformation is None:
            initial_transformation = np.eye(3)

        self.velocity_earth = State(
            self,
            shape=3,
            derivative_function=self.velocity_earth_dot,
            initial_condition=initial_velocity_earth,
        )
        self.position_earth = State(
            self,
            shape=3,
            derivative_function=self.position_earth_dot,
            initial_condition=initial_position_earth,
        )
        self.omega_earth = State(
            self,
            shape=3,
            derivative_function=self.omega_earth_dot,
            initial_condition=initial_angular_rates_earth,
        )
        self.dcm = State(
            self,
            shape=(3, 3),
            derivative_function=self.dcm_dot,
            initial_condition=initial_transformation,
        )
コード例 #7
0
ファイル: aerodyn.py プロジェクト: ralfgerlich/modypy
    def __init__(self,
                 parent,
                 thrust_coefficient,
                 power_coefficient,
                 diameter):
        Block.__init__(self, parent)
        if not callable(thrust_coefficient):
            thrust_coeff_value = thrust_coefficient
            thrust_coefficient = (lambda n: thrust_coeff_value)
        if not callable(power_coefficient):
            power_coeff_value = power_coefficient
            power_coefficient = (lambda n: power_coeff_value)

        self.thrust_coefficient = thrust_coefficient
        self.power_coefficient = power_coefficient
        self.diameter = diameter

        self.speed_rps = Port()
        self.density = Port()
コード例 #8
0
ファイル: linear.py プロジェクト: ralfgerlich/modypy
    def __init__(self, parent, channel_weights, output_size=1):
        Block.__init__(self, parent)

        self.channel_weights = np.asarray(channel_weights)
        self.output_size = output_size

        self.inputs = [
            Port(shape=self.output_size)
            for _ in range(self.channel_weights.shape[0])
        ]
        self.output = Signal(shape=self.output_size,
                             value=self.output_function)
コード例 #9
0
ファイル: discrete.py プロジェクト: ralfgerlich/modypy
    def __init__(self, owner, shape=1, initial_condition=None):
        """
        Constructor for ``ZeroOrderHold``

        Args:
            owner: The owner of the block (system or block)
            shape: The shape of the input and output signal
            initial_condition: The initial state of the sampling output
                (before the first tick of the block)
        """
        Block.__init__(self, owner)

        self.event_input = EventPort(self)
        self.event_input.register_listener(self.update_state)
        self.input = Port(shape=shape)
        self.output = State(
            self,
            shape=shape,
            initial_condition=initial_condition,
            derivative_function=None,
        )
コード例 #10
0
    def __init__(self, parent):
        Block.__init__(self, parent)

        self.dcm = Port(shape=(3, 3))
コード例 #11
0
ファイル: linear.py プロジェクト: ralfgerlich/modypy
    def __init__(
        self,
        parent,
        system_matrix,
        input_matrix,
        output_matrix,
        feed_through_matrix,
        initial_condition=None,
    ):
        Block.__init__(self, parent)

        # Determine the number of states and the shape of the state
        if np.isscalar(system_matrix):
            self.state_shape = ()
            num_states = 1
        else:
            system_matrix = np.asarray(system_matrix)
            if (system_matrix.ndim == 2 and system_matrix.shape[0] > 0
                    and system_matrix.shape[0] == system_matrix.shape[1]):
                self.state_shape = system_matrix.shape[0]
                num_states = self.state_shape
            else:
                raise InvalidLTIException("The system matrix must be a scalar "
                                          "or a non-empty square matrix")

        # Determine the number of inputs and the shape of the input signal
        if np.isscalar(input_matrix):
            if num_states > 1:
                raise InvalidLTIException("There is more than one state, but "
                                          "the input matrix is neither empty, "
                                          "nor a vector or a matrix")
            num_inputs = 1
            self.input_shape = ()
        else:
            input_matrix = np.asarray(input_matrix)
            if input_matrix.ndim == 1:
                # The input matrix is a column vector => one input
                if num_states != input_matrix.shape[0]:
                    raise InvalidLTIException(
                        "The height of the input matrix "
                        "must match the number of states")
                num_inputs = 1
            elif input_matrix.ndim == 2:
                # The input matrix is a matrix
                if num_states != input_matrix.shape[0]:
                    raise InvalidLTIException("The height of the input matrix "
                                              "does not match the number of "
                                              "states")
                num_inputs = input_matrix.shape[1]
            else:
                raise InvalidLTIException("The input matrix must be empty,"
                                          "a scalar, a vector or a matrix")
            self.input_shape = num_inputs

        # Determine the number of outputs and the shape of the output array
        if np.isscalar(output_matrix):
            if num_states > 1:
                raise InvalidLTIException("There is more than one state, but "
                                          "the output matrix is neither an "
                                          "empty, a vector nor a matrix")
            num_outputs = 1
            self.output_shape = ()
        else:
            output_matrix = np.asarray(output_matrix)
            if output_matrix.ndim == 1:
                # The output matrix is a row vector => one output
                if num_states != output_matrix.shape[0]:
                    raise InvalidLTIException("The width of the output matrix "
                                              "does not match the number of "
                                              "states")
                num_outputs = 1
            elif output_matrix.ndim == 2:
                # The output matrix is a matrix
                if num_states != output_matrix.shape[1]:
                    raise InvalidLTIException("The width of the output matrix "
                                              "does not match the number of "
                                              "states")
                num_outputs = output_matrix.shape[0]
            else:
                raise InvalidLTIException("The output matrix must be empty, a"
                                          "scalar, a vector or a matrix")
            self.output_shape = num_outputs

        if np.isscalar(feed_through_matrix):
            if not (num_inputs == 1 and num_outputs == 1):
                raise InvalidLTIException("A scalar feed-through matrix is "
                                          "only allowed for systems with "
                                          "exactly one input and one output")
        else:
            feed_through_matrix = np.asarray(feed_through_matrix)
            if feed_through_matrix.ndim == 1:
                # A vector feed_through_matrix is interpreted as row vector,
                # so there must be exactly one output.
                if num_outputs == 0:
                    raise InvalidLTIException("The feed-through matrix for a "
                                              "system without outputs must be"
                                              "empty")
                elif num_outputs > 1:
                    raise InvalidLTIException("The feed-through matrix for a "
                                              "system with more than one "
                                              "output must be a matrix")
                if feed_through_matrix.shape[0] != num_inputs:
                    raise InvalidLTIException(
                        "The width of the feed-through "
                        "matrix must match the number of "
                        "inputs")
            elif feed_through_matrix.ndim == 2:
                if feed_through_matrix.shape[0] != num_outputs:
                    raise InvalidLTIException(
                        "The height of the feed-through "
                        "matrix must match the number of "
                        "outputs")
                if feed_through_matrix.shape[1] != num_inputs:
                    raise InvalidLTIException(
                        "The width of the feed-through "
                        "matrix must match the number of "
                        "inputs")
            else:
                raise InvalidLTIException("The feed-through matrix must be "
                                          "empty, a scalar, a vector or a "
                                          "matrix")

        self.system_matrix = system_matrix
        self.input_matrix = input_matrix
        self.output_matrix = output_matrix
        self.feed_through_matrix = feed_through_matrix

        self.input = Port(shape=self.input_shape)
        self.state = State(
            self,
            shape=self.state_shape,
            derivative_function=self.state_derivative,
            initial_condition=initial_condition,
        )
        self.output = Signal(shape=self.output_shape,
                             value=self.output_function)
コード例 #12
0
ファイル: linear.py プロジェクト: ralfgerlich/modypy
    def __init__(self, parent, k):
        Block.__init__(self, parent)
        self.k = np.atleast_2d(k)

        self.input = Port(shape=self.k.shape[0])
        self.output = Signal(shape=self.k.shape[1], value=self.output_function)