:py:mod:`multirotor.simulation` =============================== .. py:module:: multirotor.simulation Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: multirotor.simulation.Propeller multirotor.simulation.Motor multirotor.simulation.Battery multirotor.simulation.Multirotor .. py:class:: Propeller(params: multirotor.vehicle.PropellerParams, simulation: multirotor.vehicle.SimulationParams) Models the thrust and aerodynamics of the propeller blades spinning at a certain rate. .. py:property:: state :type: float .. py:property:: rpm :type: float .. py:attribute:: speed :type: float :value: 0.0 Radians per second .. py:method:: reset() .. py:method:: apply_speed(u: float, **kwargs) -> float Calculate the actual speed of the propeller after the speed signal is given. This method is *pure* and does not change the state of the propeller. It is used by the multirotor's dxdt_* methods to calculate derivatives. Parameters ---------- u : float Radians per second speed command Returns ------- float The actual speed .. py:method:: step(u: float, **kwargs) -> float Step through the speed command. This method changes the state of the propeller. Parameters ---------- u : float Speed command in radians per second. Returns ------- float The actual speed achieved. .. py:method:: _thrust_constant(speed, airstream_velocity: numpy.ndarray = np.zeros(3)) -> float .. py:method:: _thrust_physics(speed, airstream_velocity: numpy.ndarray = np.zeros(3)) -> float .. py:class:: Motor(params: multirotor.vehicle.MotorParams, simulation: multirotor.vehicle.SimulationParams) Models the electronic and mechanical characterists of the rotation of the propeller shaft. .. py:attribute:: speed :type: float :value: 0.0 Radians per second .. py:method:: reset() -> float .. py:method:: current_average(max_voltage: float) -> float Average current consumption given the duty cycle of the speed controller. Duty cycle depends on max voltage which causes 100% duty cycle. Parameters ---------- max_voltage : float The peak voltage corresponding to 100% duty cycle. Returns ------- float Current consumption .. py:method:: apply_speed(u: float, max_voltage: float = np.inf) -> float Apply a voltage speed signal to the motor. This method is pure and doesn't change the state of the motor. Parameters ---------- u : float Voltage signal. max_voltage : float, optional The maximum voltage supply from power source. By default infinite. Returns ------- float The speed of the motor (rad /s) .. py:method:: step(u: float, max_voltage: float = np.inf) -> float Apply a voltage speed signal to the motor. This method changes the state of the motor. Parameters ---------- u : float Speed signal (rad/s). max_voltage : float, optional The maximum voltage supply from power source. By default infinite. Returns ------- float The speed of the motor (rad /s) .. py:class:: Battery(params: multirotor.vehicle.BatteryParams, simulation: multirotor.vehicle.SimulationParams) Models the state of charge of the battery of the Multirotor. .. py:property:: state :type: float .. py:method:: reset() .. py:method:: step() .. py:class:: Multirotor(params: multirotor.vehicle.VehicleParams, simulation: multirotor.vehicle.SimulationParams) The multirotor class models dynamics and control allocation of a vehicle. .. py:property:: position Position in the inertial frame. .. py:property:: velocity :type: numpy.ndarray Body-frame velocity .. py:property:: inertial_velocity :type: numpy.ndarray Velocity in the intertial frame. .. py:property:: acceleration :type: numpy.ndarray Body-frame acceleration .. py:property:: inertial_acceleration :type: numpy.ndarray Acceleration in the intertial frame. .. py:property:: orientation :type: numpy.ndarray Euler rotations (roll, pitch, yaw) .. py:property:: angular_rate :type: numpy.ndarray Angular rate of body frame axes (not same as rate of roll, pitch, yaw) .. py:property:: euler_rate :type: numpy.ndarray Euler rate of vehicle d(roll, pitch, yaw)/dt .. py:property:: weight :type: float .. py:property:: current_average :type: float Duty-cycle adjusted currrent draw from battery. .. py:property:: speeds :type: numpy.ndarray .. py:method:: reset() -> numpy.ndarray Reset the state of the vehicle. This includes resetting each propeller and re-calculating inertia and allocation matrices. Can simulate dynamics with propellers with/out motors. Returns ------- np.ndarray The state of the vehicle. .. py:method:: get_forces_torques(speeds: numpy.ndarray, state: numpy.ndarray) -> Tuple[numpy.ndarray, numpy.ndarray] Calculate the forces and torques acting on the vehicle's center of gravity given its current state and speed of propellers. Parameters ---------- speeds : np.ndarray Propeller speeds (rad/s) state : np.ndarray State of the vehicle (position, velocity, orientation, angular rate) Returns ------- Tuple[np.ndarray, np.ndarray] The forces and torques acting on the body. .. py:method:: dxdt_dynamics(t: float, x: numpy.ndarray, u: numpy.ndarray, params=None) Calculate the rate of change of state given the dynamics (forces, torques) acting on the system. Parameters ---------- t : float Time. Currently this function is time invariant. x : np.ndarray State of the vehicle. u : np.ndarray A 6-vector of forces and torques. Returns ------- np.ndarray The rate of change of state. .. py:method:: dxdt_speeds(t: float, x: numpy.ndarray, u: numpy.ndarray, disturb_forces: numpy.ndarray = 0.0, disturb_torques: numpy.ndarray = 0.0, params=None) Calculate the rate of change of state given the propeller speeds on the system (rad/s). Parameters ---------- t : float Time. Currently this function is time invariant. x : np.ndarray State of the vehicle. u : np.ndarray A p-vector of propeller speeds (rad/s), where p=number of propellers. disturb_forces : np.ndarray, optional Disturbinng x,y,z forces in the vehicle's local frame, by default 0. disturb_torques : np.ndarray, optional Disturbing x,y,z torques in the vehicle's local frame, by default 0. Returns ------- np.ndarray The rate of change of state. .. py:method:: step_dynamics(u: numpy.ndarray) -> numpy.ndarray Given the 6-vector of x,y,z-forces and roll,pitch,yaw-torques, calculate the next state of the vehicle. Parameters ---------- u : np.ndarray The 6-vector, where the first 3 elements are forces (N) and the next 3 elements are the torques (Nm) Returns ------- np.ndarray The new state of the vehicle. .. py:method:: step_speeds(u: numpy.ndarray, disturb_forces: numpy.ndarray = 0.0, disturb_torques: numpy.ndarray = 0.0) -> numpy.ndarray Given the n-vector of propeller speed signals, calculate the next state of the vehicle. Where n is number of propellers. Parameters ---------- u : np.ndarray The speed signals to be sent to each propeller's step() method. Can be the actual speed (rad/s) or the voltage signal (V) if a motor is used and MotorParams.speed_voltage_scaling constant is set. disturb_forces : np.ndarray, optional Disturbinng x,y,z forces in the vehicle's local frame, by default 0. disturb_torques : np.ndarray, optional Disturbing x,y,z torques in the vehicle's local frame, by default 0. Returns ------- np.ndarray The new state of the vehicle. .. py:method:: allocate_control(thrust: float, torques: numpy.ndarray) -> numpy.ndarray Allocate control to propellers by converting prescribed forces and torqes into propeller speeds. Uses the control allocation matrix. Parameters ---------- thrust : float The thrust in the body z-direction. torques : np.ndarray The roll, pitch, yaw torques required about (x, y, z) axes. Returns ------- np.ndarray The prescribed propeller speeds (rad /s) .. py:method:: nonlinear_dynamics_controls_system(linearize: bool = False, perturbation: float = 0.1, about_state: numpy.ndarray = 0, about_action: numpy.ndarray = 0) Create a system representation using the python controls library. The system takes net forces and torques as input. Parameters ---------- linearize : bool, optional Whether to linearize the system about a state/action, by default False perturbation : float, optional The change in time to use to calculate dx/dt, by default 1e-1 about_state : np.ndarray, optional The state about which to linearize, by default 0 about_action : np.ndarray, optional The action about which to linearize, by default 0 Returns ------- Union[control.LinearIOSystem, control.NonlinearIOSystem] The system object .. py:method:: nonlinear_speeds_controls_system(linearize: bool = False, perturbation: float = 0.1, about_state: numpy.ndarray = 0, about_action: numpy.ndarray = 0) Create a system representation using the python controls library. The system takes propeller speed signals as input. Parameters ---------- linearize : bool, optional Whether to linearize the system about a state/action, by default False perturbation : float, optional The change in time to use to calculate dx/dt, by default 1e-1 about_state : np.ndarray, optional The state about which to linearize, by default 0 about_action : np.ndarray, optional The action about which to linearize, by default 0 Returns ------- Union[control.LinearIOSystem, control.NonlinearIOSystem] The system object