AP_MotorsMulticopter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /// @file AP_MotorsMulticopter.h
  2. /// @brief Motor control class for Multicopters
  3. #pragma once
  4. #include "AP_Motors_Class.h"
  5. #ifndef AP_MOTORS_DENSITY_COMP
  6. #define AP_MOTORS_DENSITY_COMP 1
  7. #endif
  8. #define AP_MOTORS_YAW_HEADROOM_DEFAULT 200
  9. #define AP_MOTORS_THST_EXPO_DEFAULT 0.65f // set to 0 for linear and 1 for second order approximation
  10. #define AP_MOTORS_THST_HOVER_DEFAULT 0.35f // the estimated hover throttle, 0 ~ 1
  11. #define AP_MOTORS_THST_HOVER_TC 10.0f // time constant used to update estimated hover throttle, 0 ~ 1
  12. #define AP_MOTORS_THST_HOVER_MIN 0.125f // minimum possible hover throttle
  13. #define AP_MOTORS_THST_HOVER_MAX 0.6875f // maximum possible hover throttle
  14. #define AP_MOTORS_SPIN_MIN_DEFAULT 0.15f // throttle out ratio which produces the minimum thrust. (i.e. 0 ~ 1 ) of the full throttle range
  15. #define AP_MOTORS_SPIN_MAX_DEFAULT 0.95f // throttle out ratio which produces the maximum thrust. (i.e. 0 ~ 1 ) of the full throttle range
  16. #define AP_MOTORS_SPIN_ARM_DEFAULT 0.10f // throttle out ratio which produces the armed spin rate. (i.e. 0 ~ 1 ) of the full throttle range
  17. #define AP_MOTORS_BAT_VOLT_MAX_DEFAULT 0.0f // voltage limiting max default
  18. #define AP_MOTORS_BAT_VOLT_MIN_DEFAULT 0.0f // voltage limiting min default (voltage dropping below this level will have no effect)
  19. #define AP_MOTORS_BAT_CURR_MAX_DEFAULT 0.0f // current limiting max default
  20. #define AP_MOTORS_BAT_CURR_TC_DEFAULT 5.0f // Time constant used to limit the maximum current
  21. #define AP_MOTORS_BATT_VOLT_FILT_HZ 0.5f // battery voltage filtered at 0.5hz
  22. #define AP_MOTORS_SLEW_TIME_DEFAULT 0.0f // slew rate limit for thrust output
  23. // spool definition
  24. #define AP_MOTORS_SPOOL_UP_TIME_DEFAULT 0.5f // time (in seconds) for throttle to increase from zero to min throttle, and min throttle to full throttle.
  25. /// @class AP_MotorsMulticopter
  26. class AP_MotorsMulticopter : public AP_Motors {
  27. public:
  28. // Constructor
  29. AP_MotorsMulticopter(uint16_t loop_rate, uint16_t speed_hz = AP_MOTORS_SPEED_DEFAULT);
  30. // output - sends commands to the motors
  31. virtual void output() override;
  32. // output_min - sends minimum values out to the motors
  33. void output_min() override;
  34. // set_yaw_headroom - set yaw headroom (yaw is given at least this amount of pwm)
  35. void set_yaw_headroom(int16_t pwm) { _yaw_headroom = pwm; }
  36. // set_throttle_range - sets the minimum throttle that will be sent to the engines when they're not off (i.e. to prevents issues with some motors spinning and some not at very low throttle)
  37. // also sets minimum and maximum pwm values that will be sent to the motors
  38. void set_throttle_range(int16_t radio_min, int16_t radio_max);
  39. // update estimated throttle required to hover
  40. void update_throttle_hover(float dt);
  41. virtual float get_throttle_hover() const override { return _throttle_hover; }
  42. // passes throttle directly to all motors for ESC calibration.
  43. // throttle_input is in the range of 0 ~ 1 where 0 will send get_pwm_output_min() and 1 will send get_pwm_output_max()
  44. void set_throttle_passthrough_for_esc_calibration(float throttle_input);
  45. // get_lift_max - get maximum lift ratio - for logging purposes only
  46. float get_lift_max() { return _lift_max; }
  47. // get_batt_voltage_filt - get battery voltage ratio - for logging purposes only
  48. float get_batt_voltage_filt() const { return _batt_voltage_filt.get(); }
  49. // get throttle limit imposed by battery current limiting. This is a number from 0 ~ 1 where 0 means hover throttle, 1 means full throttle (i.e. not limited)
  50. float get_throttle_limit() const { return _throttle_limit; }
  51. // returns maximum thrust in the range 0 to 1
  52. float get_throttle_thrust_max() const { return _throttle_thrust_max; }
  53. // return true if spool up is complete
  54. bool spool_up_complete() const { return _spool_state == SpoolState::THROTTLE_UNLIMITED; }
  55. // output a thrust to all motors that match a given motor
  56. // mask. This is used to control tiltrotor motors in forward
  57. // flight. Thrust is in the range 0 to 1
  58. virtual void output_motor_mask(float thrust, uint8_t mask, float rudder_dt);
  59. // get_motor_mask - returns a bitmask of which outputs are being used for motors (1 means being used)
  60. // this can be used to ensure other pwm outputs (i.e. for servos) do not conflict
  61. virtual uint16_t get_motor_mask() override;
  62. // get minimum or maximum pwm value that can be output to motors
  63. int16_t get_pwm_output_min() const;
  64. int16_t get_pwm_output_max() const;
  65. // set thrust compensation callback
  66. FUNCTOR_TYPEDEF(thrust_compensation_fn_t, void, float *, uint8_t);
  67. void set_thrust_compensation_callback(thrust_compensation_fn_t callback) {
  68. _thrust_compensation_callback = callback;
  69. }
  70. // var_info for holding Parameter information
  71. static const struct AP_Param::GroupInfo var_info[];
  72. bool motor_enabled[AP_MOTORS_MAX_NUM_MOTORS]; // true if motor is enabled
  73. protected:
  74. // run spool logic
  75. void output_logic();
  76. // output_to_motors - sends commands to the motors
  77. virtual void output_to_motors() = 0;
  78. virtual void output_to_Dshot() = 0;
  79. // update the throttle input filter
  80. virtual void update_throttle_filter() override;
  81. // return current_limit as a number from 0 ~ 1 in the range throttle_min to throttle_max
  82. virtual float get_current_limit_max_throttle();
  83. // apply_thrust_curve_and_volt_scaling - returns throttle in the range 0 ~ 1
  84. float apply_thrust_curve_and_volt_scaling(float thrust) const;
  85. // update_lift_max_from_batt_voltage - used for voltage compensation
  86. void update_lift_max_from_batt_voltage();
  87. // return gain scheduling gain based on voltage and air density
  88. float get_compensation_gain() const;
  89. // convert actuator output (0~1) range to pwm range
  90. int16_t output_to_pwm(float _actuator_output);
  91. // converts desired thrust to linearized actuator output in a range of 0~1
  92. float thrust_to_actuator(float thrust_in);
  93. // adds slew rate limiting to actuator output if MOT_SLEW_TIME > 0 and not shutdown
  94. void set_actuator_with_slew(float& actuator_output, float input);
  95. // gradually increase actuator output to ground idle
  96. float actuator_spin_up_to_ground_idle() const;
  97. // apply any thrust compensation for the frame
  98. virtual void thrust_compensation(void) {}
  99. // output booster throttle, if any
  100. virtual void output_boost_throttle(void);
  101. // save parameters as part of disarming
  102. void save_params_on_disarm() override;
  103. // enum values for HOVER_LEARN parameter
  104. enum HoverLearn {
  105. HOVER_LEARN_DISABLED = 0,
  106. HOVER_LEARN_ONLY = 1,
  107. HOVER_LEARN_AND_SAVE = 2
  108. };
  109. // parameters
  110. AP_Int16 _yaw_headroom; // yaw control is given at least this pwm range
  111. AP_Float _thrust_curve_expo; // curve used to linearize pwm to thrust conversion. set to 0 for linear and 1 for second order approximation
  112. AP_Float _slew_up_time; // throttle increase slew limitting
  113. AP_Float _slew_dn_time; // throttle decrease slew limitting
  114. AP_Float _spin_min; // throttle out ratio which produces the minimum thrust. (i.e. 0 ~ 1 ) of the full throttle range
  115. AP_Float _spin_max; // throttle out ratio which produces the maximum thrust. (i.e. 0 ~ 1 ) of the full throttle range
  116. AP_Float _spin_arm; // throttle out ratio which produces the armed spin rate. (i.e. 0 ~ 1 ) of the full throttle range
  117. AP_Float _batt_voltage_max; // maximum voltage used to scale lift
  118. AP_Float _batt_voltage_min; // minimum voltage used to scale lift
  119. AP_Float _batt_current_max; // current over which maximum throttle is limited
  120. AP_Float _batt_current_time_constant; // Time constant used to limit the maximum current
  121. AP_Int8 _batt_idx; // battery index used for compensation
  122. AP_Int16 _pwm_min; // minimum PWM value that will ever be output to the motors (if 0, vehicle's throttle input channel's min pwm used)
  123. AP_Int16 _pwm_max; // maximum PWM value that will ever be output to the motors (if 0, vehicle's throttle input channel's max pwm used)
  124. AP_Float _throttle_hover; // estimated throttle required to hover throttle in the range 0 ~ 1
  125. AP_Int8 _throttle_hover_learn; // enable/disabled hover thrust learning
  126. AP_Int8 _disarm_disable_pwm; // disable PWM output while disarmed
  127. // Maximum lean angle of yaw servo in degrees. This is specific to tricopter
  128. AP_Float _yaw_servo_angle_max_deg;
  129. // time to spool motors to min throttle
  130. AP_Float _spool_up_time;
  131. // scaling for booster motor throttle
  132. AP_Float _boost_scale;
  133. // motor output variables
  134. int16_t _throttle_radio_min; // minimum PWM from RC input's throttle channel (i.e. minimum PWM input from receiver, RC3_MIN)
  135. int16_t _throttle_radio_max; // maximum PWM from RC input's throttle channel (i.e. maximum PWM input from receiver, RC3_MAX)
  136. // spool variables
  137. // spool variables
  138. float _spin_up_ratio; // throttle percentage (0 ~ 1) between zero and throttle_min
  139. // battery voltage, current and air pressure compensation variables
  140. LowPassFilterFloat _batt_voltage_filt; // filtered battery voltage expressed as a percentage (0 ~ 1.0) of batt_voltage_max
  141. float _lift_max; // maximum lift ratio from battery voltage
  142. float _throttle_limit; // ratio of throttle limit between hover and maximum
  143. float _throttle_thrust_max; // the maximum allowed throttle thrust 0.0 to 1.0 in the range throttle_min to throttle_max
  144. uint16_t _disarm_safety_timer;
  145. // vehicle supplied callback for thrust compensation. Used for tiltrotors and tiltwings
  146. thrust_compensation_fn_t _thrust_compensation_callback;
  147. // array of motor output values
  148. float _actuator[AP_MOTORS_MAX_NUM_MOTORS];
  149. };