AP_MotorsHeli_RSC.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #pragma once
  2. #include <AP_Common/AP_Common.h>
  3. #include <AP_Math/AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
  4. #include <RC_Channel/RC_Channel.h>
  5. #include <SRV_Channel/SRV_Channel.h>
  6. // default main rotor speed (ch8 out) as a number from 0 ~ 100
  7. #define AP_MOTORS_HELI_RSC_SETPOINT 70
  8. // default main rotor critical speed
  9. #define AP_MOTORS_HELI_RSC_CRITICAL 50
  10. // RSC output defaults
  11. #define AP_MOTORS_HELI_RSC_IDLE_DEFAULT 0
  12. // default main rotor ramp up time in seconds
  13. #define AP_MOTORS_HELI_RSC_RAMP_TIME 1 // 1 second to ramp output to main rotor ESC to setpoint
  14. #define AP_MOTORS_HELI_RSC_RUNUP_TIME 10 // 10 seconds for rotor to reach full speed
  15. // Throttle Curve Defaults
  16. #define AP_MOTORS_HELI_RSC_THRCRV_0_DEFAULT 25
  17. #define AP_MOTORS_HELI_RSC_THRCRV_25_DEFAULT 32
  18. #define AP_MOTORS_HELI_RSC_THRCRV_50_DEFAULT 38
  19. #define AP_MOTORS_HELI_RSC_THRCRV_75_DEFAULT 50
  20. #define AP_MOTORS_HELI_RSC_THRCRV_100_DEFAULT 100
  21. // RSC governor defaults
  22. #define AP_MOTORS_HELI_RSC_GOVERNOR_SETPNT_DEFAULT 1500
  23. #define AP_MOTORS_HELI_RSC_GOVERNOR_DISENGAGE_DEFAULT 25
  24. #define AP_MOTORS_HELI_RSC_GOVERNOR_DROOP_DEFAULT 30
  25. #define AP_MOTORS_HELI_RSC_GOVERNOR_TCGAIN_DEFAULT 90
  26. #define AP_MOTORS_HELI_RSC_GOVERNOR_RANGE_DEFAULT 100
  27. // rotor controller states
  28. enum RotorControlState {
  29. ROTOR_CONTROL_STOP = 0,
  30. ROTOR_CONTROL_IDLE,
  31. ROTOR_CONTROL_ACTIVE
  32. };
  33. // rotor control modes
  34. enum RotorControlMode {
  35. ROTOR_CONTROL_MODE_DISABLED = 0,
  36. ROTOR_CONTROL_MODE_SPEED_PASSTHROUGH,
  37. ROTOR_CONTROL_MODE_SPEED_SETPOINT,
  38. ROTOR_CONTROL_MODE_OPEN_LOOP_POWER_OUTPUT,
  39. ROTOR_CONTROL_MODE_CLOSED_LOOP_POWER_OUTPUT
  40. };
  41. class AP_MotorsHeli_RSC {
  42. public:
  43. friend class AP_MotorsHeli_Single;
  44. friend class AP_MotorsHeli_Dual;
  45. friend class AP_MotorsHeli_Quad;
  46. AP_MotorsHeli_RSC(SRV_Channel::Aux_servo_function_t aux_fn,
  47. uint8_t default_channel) :
  48. _aux_fn(aux_fn),
  49. _default_channel(default_channel)
  50. {
  51. AP_Param::setup_object_defaults(this, var_info);
  52. };
  53. // init_servo - servo initialization on start-up
  54. void init_servo();
  55. // set_control_mode - sets control mode
  56. void set_control_mode(RotorControlMode mode) { _control_mode = mode; }
  57. // reset_rsc_mode_param - resets rsc mode param to current control mode
  58. void reset_rsc_mode_param() { _rsc_mode.set((uint8_t)_control_mode); }
  59. // get_control_mode - gets control mode
  60. uint8_t get_control_mode() const { return _control_mode; }
  61. // set_critical_speed
  62. void set_critical_speed(float critical_speed) { _critical_speed = critical_speed; }
  63. // set_idle_output
  64. void set_idle_output(float idle_output) { _idle_output = idle_output; }
  65. // set rotor speed governor parameters
  66. void set_governor_output(float governor_output) {_governor_output = governor_output; }
  67. // get_desired_speed
  68. float get_desired_speed() const { return _desired_speed; }
  69. // set_desired_speed - this requires input to be 0-1
  70. void set_desired_speed(float desired_speed) { _desired_speed = desired_speed; }
  71. // get_control_speed
  72. float get_control_output() const { return _control_output; }
  73. // get_rotor_speed - estimated rotor speed when no governor or rpm sensor is used
  74. float get_rotor_speed() const;
  75. // set_rotor_rpm - when speed sensor is available for governor
  76. void set_rotor_rpm(float rotor_rpm) {_rotor_rpm = (float)rotor_rpm; }
  77. // get_governor_output
  78. float get_governor_output() const { return _governor_output; }
  79. // is_runup_complete
  80. bool is_runup_complete() const { return _runup_complete; }
  81. // set_ramp_time
  82. void set_ramp_time(int8_t ramp_time) { _ramp_time = ramp_time; }
  83. // set_runup_time
  84. void set_runup_time(int8_t runup_time) { _runup_time = runup_time; }
  85. // set_throttle_curve
  86. void set_throttle_curve();
  87. // set_collective. collective for throttle curve calculation
  88. void set_collective(float collective) { _collective_in = collective; }
  89. // output - update value to send to ESC/Servo
  90. void output(RotorControlState state);
  91. // var_info for holding Parameter information
  92. static const struct AP_Param::GroupInfo var_info[];
  93. // parameters
  94. AP_Int16 _rsc_setpoint; // rotor speed when RSC mode is set to is enabled
  95. AP_Int8 _rsc_mode; // Which main rotor ESC control mode is active
  96. AP_Int8 _ramp_time; // Time in seconds for the output to the main rotor's ESC to reach setpoint
  97. AP_Int8 _runup_time; // Time in seconds for the main rotor to reach full speed. Must be longer than _rsc_ramp_time
  98. AP_Int16 _critical_speed; // Rotor speed below which flight is not possible
  99. AP_Int16 _idle_output; // Rotor control output while at idle
  100. private:
  101. uint64_t _last_update_us;
  102. // channel setup for aux function
  103. SRV_Channel::Aux_servo_function_t _aux_fn;
  104. uint8_t _default_channel;
  105. // internal variables
  106. RotorControlMode _control_mode = ROTOR_CONTROL_MODE_DISABLED; // motor control mode, Passthrough or Setpoint
  107. float _desired_speed; // latest desired rotor speed from pilot
  108. float _control_output; // latest logic controlled output
  109. float _rotor_ramp_output; // scalar used to ramp rotor speed between _rsc_idle_output and full speed (0.0-1.0f)
  110. float _rotor_runup_output; // scalar used to store status of rotor run-up time (0.0-1.0f)
  111. bool _runup_complete; // flag for determining if runup is complete
  112. float _thrcrv_poly[4][4]; // spline polynomials for throttle curve interpolation
  113. float _collective_in; // collective in for throttle curve calculation, range 0-1.0f
  114. float _rotor_rpm; // rotor rpm from speed sensor for governor
  115. float _governor_output; // governor output for rotor speed control
  116. bool _governor_engage; // RSC governor status flag for soft-start
  117. // update_rotor_ramp - slews rotor output scalar between 0 and 1, outputs float scalar to _rotor_ramp_output
  118. void update_rotor_ramp(float rotor_ramp_input, float dt);
  119. // update_rotor_runup - function to slew rotor runup scalar, outputs float scalar to _rotor_runup_ouptut
  120. void update_rotor_runup(float dt);
  121. // write_rsc - outputs pwm onto output rsc channel. servo_out parameter is of the range 0 ~ 1
  122. void write_rsc(float servo_out);
  123. // calculate_desired_throttle - uses throttle curve and collective input to determine throttle setting
  124. float calculate_desired_throttle(float collective_in);
  125. // parameters
  126. AP_Int16 _power_slewrate; // throttle slew rate (percentage per second)
  127. AP_Int16 _thrcrv[5]; // throttle value sent to throttle servo at 0, 25, 50, 75 and 100 percent collective
  128. AP_Int16 _governor_reference; // sets rotor speed for governor
  129. AP_Float _governor_range; // RPM range +/- governor rpm reference setting where governor is operational
  130. AP_Float _governor_disengage; // sets the throttle percent where the governor disengages for return to flight idle
  131. AP_Float _governor_droop_response; // governor response to droop under load
  132. AP_Float _governor_tcgain; // governor throttle curve weighting, range 50-100%
  133. // parameter accessors to allow conversions
  134. float get_critical_speed() const { return _critical_speed * 0.01; }
  135. float get_idle_output() { return _idle_output * 0.01; }
  136. float get_governor_disengage() { return _governor_disengage * 0.01; }
  137. float get_governor_droop_response() { return _governor_droop_response * 0.01; }
  138. float get_governor_tcgain() { return _governor_tcgain * 0.01; }
  139. };