:py:mod:`multirotor.controller` =============================== .. py:module:: multirotor.controller Submodules ---------- .. toctree:: :titlesonly: :maxdepth: 1 pid/index.rst scurves/index.rst Package Contents ---------------- Classes ~~~~~~~ .. autoapisummary:: multirotor.controller.PIDController multirotor.controller.PosController multirotor.controller.VelController multirotor.controller.AttController multirotor.controller.RateController multirotor.controller.AltController multirotor.controller.AltRateController multirotor.controller.Controller multirotor.controller.SCurveController .. py:class:: PIDController Proportional Integral Derivative controller. Tracks a reference signal and outputs a control signal to minimize output. The equation is: err = reference - measurement u = k_p * err + k_d * d(err)/dt + k_i * int(err . dt) Can control a single or an array of signals, given float or array PID constants. .. py:property:: state :type: numpy.ndarray .. py:attribute:: k_p :type: numpy.ndarray Proportional constant .. py:attribute:: k_i :type: numpy.ndarray Integral constant .. py:attribute:: k_d :type: numpy.ndarray Derivative constant .. py:attribute:: max_err_i :type: numpy.ndarray Maximum accumulated error .. py:method:: __post_init__() .. py:method:: reset() .. py:method:: set_params(**params: Dict[str, Union[numpy.ndarray, bool, float, int]]) .. py:method:: get_params() -> Dict[str, numpy.ndarray] .. py:method:: step(reference: numpy.ndarray, measurement: numpy.ndarray, dt: float = 1.0, ref_is_error: bool = False, persist: bool = True) -> numpy.ndarray Calculate the output, based on the current measurement and the reference signal. Parameters ---------- reference : np.ndarray The reference signal(s) to track. Can be a number or an array. measurement : np.ndarray The actual measurement(s). ref_is_error: bool Whether to interpret the reference input as the error. persist: bool Whether to store the current state for the next step. Returns ------- np.ndarray The action signal. .. py:class:: PosController Bases: :py:obj:`PIDController` Position controller. Convert xy-waypoint into required xy-velocity in the body-frame. .. py:property:: leash :type: float .. py:attribute:: vehicle :type: multirotor.simulation.Multirotor .. py:attribute:: max_velocity :type: float :value: 7.0 Maximum velocity in m/s .. py:attribute:: max_acceleration :type: float :value: 3.0 Maximum acceleration in m/s/s .. py:attribute:: max_jerk :type: float :value: 100.0 Maximum jerk in m/s/s/s .. py:attribute:: square_root_scaling :type: bool :value: True Whether to scale P-gain with the square root of the error .. py:attribute:: leashing :type: bool :value: True Whether to limit proportional position error .. py:method:: __post_init__() .. py:method:: step(reference, measurement, dt, persist: bool = True) Calculate the output, based on the current measurement and the reference signal. Parameters ---------- reference : np.ndarray The reference signal(s) to track. Can be a number or an array. measurement : np.ndarray The actual measurement(s). ref_is_error: bool Whether to interpret the reference input as the error. persist: bool Whether to store the current state for the next step. Returns ------- np.ndarray The action signal. .. py:class:: VelController Bases: :py:obj:`PIDController` Velocity controller. Takes reference x/y body-frame velocity and outputs reference pitch and roll angles for x and y motion, respectively. Compares against measured velocity. The deficit is used to change pitch and roll angles to increase and decrease velocity. Able to limit tilt angles when tracking reference waypoints. .. py:attribute:: vehicle :type: multirotor.simulation.Multirotor .. py:attribute:: max_tilt :type: float Maximum tilt angle in radians .. py:method:: __post_init__() .. py:method:: step(reference, measurement, dt, persist: bool = True) Calculate the output, based on the current measurement and the reference signal. Parameters ---------- reference : np.ndarray The reference signal(s) to track. Can be a number or an array. measurement : np.ndarray The actual measurement(s). ref_is_error: bool Whether to interpret the reference input as the error. persist: bool Whether to store the current state for the next step. Returns ------- np.ndarray The action signal. .. py:class:: AttController Bases: :py:obj:`PIDController` Attitude controller. Convert velocity reference into angular rate control signal. .. py:attribute:: vehicle :type: multirotor.simulation.Multirotor .. py:attribute:: max_acceleration :type: float :value: 0.2 Maximum acceleration in rad/s/s .. py:attribute:: max_jerk :type: float :value: 100.0 Maximum jerk in rad/s/s/s .. py:attribute:: square_root_scaling :type: bool :value: False Whether to scale P-gain with the square root of the error .. py:method:: __post_init__() .. py:method:: step(reference, measurement, dt, persist: bool = True) Calculate the output, based on the current measurement and the reference signal. Parameters ---------- reference : np.ndarray The reference signal(s) to track. Can be a number or an array. measurement : np.ndarray The actual measurement(s). ref_is_error: bool Whether to interpret the reference input as the error. persist: bool Whether to store the current state for the next step. Returns ------- np.ndarray The action signal. .. py:class:: RateController Bases: :py:obj:`PIDController` Attitude rate controller. Tracks reference roll, pitch, yaw rates and outputs the necessary moments about each x,y,z axes to achieve them. Uses change in orientation from measured to reference as approximate reference angular rate. Compares against measured angular rate. Outputs required change in angular rate (angular acceleration) as moments. .. py:attribute:: vehicle :type: multirotor.simulation.Multirotor .. py:attribute:: max_acceleration :type: float :value: 0.2 Maximum acceleration in rad/s/s .. py:method:: __post_init__() .. py:method:: step(reference, measurement, dt, persist: bool = True) Calculate the output, based on the current measurement and the reference signal. Parameters ---------- reference : np.ndarray The reference signal(s) to track. Can be a number or an array. measurement : np.ndarray The actual measurement(s). ref_is_error: bool Whether to interpret the reference input as the error. persist: bool Whether to store the current state for the next step. Returns ------- np.ndarray The action signal. .. py:class:: AltController Bases: :py:obj:`PIDController` Altitude Controller. Tracks z-position and outputs velocity. Uses change in z-position as approximate vertical velocity. Compares against measured velocity. Outputs the change in velocity (acceleration) as thrust force, given orientation of vehicle. .. py:attribute:: vehicle :type: multirotor.simulation.Multirotor .. py:attribute:: max_velocity :type: float :value: 5 .. py:method:: __post_init__() .. py:method:: step(reference: numpy.ndarray, measurement: numpy.ndarray, dt: float = 1, persist: bool = True) -> numpy.ndarray Calculate the output, based on the current measurement and the reference signal. Parameters ---------- reference : np.ndarray The reference signal(s) to track. Can be a number or an array. measurement : np.ndarray The actual measurement(s). ref_is_error: bool Whether to interpret the reference input as the error. persist: bool Whether to store the current state for the next step. Returns ------- np.ndarray The action signal. .. py:class:: AltRateController Bases: :py:obj:`PIDController` Climb rate controller. Tracks z-velocity and outputs thrust force needed. .. py:attribute:: vehicle :type: multirotor.simulation.Multirotor .. py:method:: __post_init__() .. py:method:: step(reference, measurement, dt, persist: bool = True) Calculate the output, based on the current measurement and the reference signal. Parameters ---------- reference : np.ndarray The reference signal(s) to track. Can be a number or an array. measurement : np.ndarray The actual measurement(s). ref_is_error: bool Whether to interpret the reference input as the error. persist: bool Whether to store the current state for the next step. Returns ------- np.ndarray The action signal. .. py:class:: Controller(ctrl_p: PosController, ctrl_v: VelController, ctrl_a: AttController, ctrl_r: RateController, ctrl_z: AltController, ctrl_vz: AltRateController, period_p: float = 0.1, period_a: float = 0.01, period_z: float = 0.1, feedforward_weight: float = 0.0) The cascaded PID controller. Tracks position and yaw, and outputs thrust and moments needed. (x,y) --> Position --> Velocity --> Attitude --> Rate --> (Moments) (z) --> Altitude --> Velocity --> (Forces) .. py:property:: state :type: numpy.ndarray .. py:property:: vehicle :type: multirotor.simulation.Multirotor .. py:method:: reset() .. py:method:: set_params(**params) .. py:method:: get_params() -> Dict[str, Dict[str, numpy.ndarray]] .. py:method:: step(reference: numpy.ndarray, measurement=None, ref_is_error: bool = False, feed_forward_velocity: numpy.ndarray = None, persist: bool = True) .. py:method:: predict(ref, deterministic=True) .. py:method:: make_for(vehicle: multirotor.simulation.Multirotor) -> Controller :classmethod: Make a `Controller` with p-only parameters for a vehicle. The parameters can be learned using the `optimize` function. Parameters ---------- vehicle : Multirotor The vehicle to control. Returns ------- Controller The controller with p-only parameters. .. py:class:: SCurveController(ctrl: multirotor.controller.pid.Controller) .. py:property:: action .. py:property:: reference .. py:property:: feedforward_weight .. py:method:: get_params() .. py:method:: set_params(**params: Dict[str, Union[numpy.ndarray, bool, float, int]]) .. py:method:: reset() .. py:method:: step(reference: numpy.ndarray, ref_is_error=False)