AP_MotorsHeli.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /// @file AP_MotorsHeli.h
  2. /// @brief Motor control class for Traditional Heli
  3. #pragma once
  4. #include <inttypes.h>
  5. #include <AP_Common/AP_Common.h>
  6. #include <AP_Math/AP_Math.h> // ArduPilot Mega Vector/Matrix math Library
  7. #include <RC_Channel/RC_Channel.h>
  8. #include <SRV_Channel/SRV_Channel.h>
  9. #include "AP_Motors_Class.h"
  10. #include "AP_MotorsHeli_RSC.h"
  11. // servo output rates
  12. #define AP_MOTORS_HELI_SPEED_DEFAULT 125 // default servo update rate for helicopters
  13. // default swash min and max angles and positions
  14. #define AP_MOTORS_HELI_SWASH_CYCLIC_MAX 2500
  15. #define AP_MOTORS_HELI_COLLECTIVE_MIN 1250
  16. #define AP_MOTORS_HELI_COLLECTIVE_MAX 1750
  17. #define AP_MOTORS_HELI_COLLECTIVE_MID 1500
  18. // flybar types
  19. #define AP_MOTORS_HELI_NOFLYBAR 0
  20. // rsc function output channels.
  21. #define AP_MOTORS_HELI_RSC CH_8
  22. class AP_HeliControls;
  23. /// @class AP_MotorsHeli
  24. class AP_MotorsHeli : public AP_Motors {
  25. public:
  26. /// Constructor
  27. AP_MotorsHeli( uint16_t loop_rate,
  28. uint16_t speed_hz = AP_MOTORS_HELI_SPEED_DEFAULT) :
  29. AP_Motors(loop_rate, speed_hz),
  30. _main_rotor(SRV_Channel::k_heli_rsc, AP_MOTORS_HELI_RSC)
  31. {
  32. AP_Param::setup_object_defaults(this, var_info);
  33. };
  34. // init
  35. void init(motor_frame_class frame_class, motor_frame_type frame_type) override;
  36. // set frame class (i.e. quad, hexa, heli) and type (i.e. x, plus)
  37. void set_frame_class_and_type(motor_frame_class frame_class, motor_frame_type frame_type) override;
  38. // set update rate to motors - a value in hertz
  39. virtual void set_update_rate( uint16_t speed_hz ) override = 0;
  40. // output_min - sets servos to neutral point with motors stopped
  41. void output_min() override;
  42. // output_test_seq - spin a motor at the pwm value specified
  43. // motor_seq is the motor's sequence number from 1 to the number of motors on the frame
  44. // pwm value is an actual pwm value that will be output, normally in the range of 1000 ~ 2000
  45. virtual void output_test_seq(uint8_t motor_seq, int16_t pwm) override = 0;
  46. //
  47. // heli specific methods
  48. //
  49. // parameter_check - returns true if helicopter specific parameters are sensible, used for pre-arm check
  50. virtual bool parameter_check(bool display_msg) const;
  51. // has_flybar - returns true if we have a mechical flybar
  52. virtual bool has_flybar() const { return AP_MOTORS_HELI_NOFLYBAR; }
  53. // set_collective_for_landing - limits collective from going too low if we know we are landed
  54. void set_collective_for_landing(bool landing) { _heliflags.landing_collective = landing; }
  55. // set_inverted_flight - enables/disables inverted flight
  56. void set_inverted_flight(bool inverted) { _heliflags.inverted_flight = inverted; }
  57. // get_rsc_mode - gets the current rotor speed control method
  58. uint8_t get_rsc_mode() const { return _main_rotor.get_control_mode(); }
  59. // get_rsc_setpoint - gets contents of _rsc_setpoint parameter (0~1)
  60. float get_rsc_setpoint() const { return _main_rotor._rsc_setpoint.get() * 0.01f; }
  61. // set_rpm - for rotor speed governor
  62. virtual void set_rpm(float rotor_rpm) = 0;
  63. // set_desired_rotor_speed - sets target rotor speed as a number from 0 ~ 1
  64. virtual void set_desired_rotor_speed(float desired_speed) = 0;
  65. // get_desired_rotor_speed - gets target rotor speed as a number from 0 ~ 1
  66. virtual float get_desired_rotor_speed() const = 0;
  67. // get_main_rotor_speed - estimated rotor speed when no governor or speed sensor used
  68. virtual float get_main_rotor_speed() const = 0;
  69. // return true if the main rotor is up to speed
  70. bool rotor_runup_complete() const { return _heliflags.rotor_runup_complete; }
  71. // rotor_speed_above_critical - return true if rotor speed is above that critical for flight
  72. virtual bool rotor_speed_above_critical() const = 0;
  73. //get rotor governor output
  74. virtual float get_governor_output() const = 0;
  75. //get engine throttle output
  76. virtual float get_control_output() const = 0;
  77. // get_motor_mask - returns a bitmask of which outputs are being used for motors or servos (1 means being used)
  78. // this can be used to ensure other pwm outputs (i.e. for servos) do not conflict
  79. virtual uint16_t get_motor_mask() override = 0;
  80. virtual void set_acro_tail(bool set) {}
  81. // ext_gyro_gain - set external gyro gain in range 0 ~ 1
  82. virtual void ext_gyro_gain(float gain) {}
  83. // output - sends commands to the motors
  84. void output() override;
  85. // supports_yaw_passthrough
  86. virtual bool supports_yaw_passthrough() const { return false; }
  87. float get_throttle_hover() const override { return 0.5f; }
  88. // support passing init_targets_on_arming flag to greater code
  89. bool init_targets_on_arming() const { return _heliflags.init_targets_on_arming; }
  90. // var_info for holding Parameter information
  91. static const struct AP_Param::GroupInfo var_info[];
  92. protected:
  93. // manual servo modes (used for setup)
  94. enum ServoControlModes {
  95. SERVO_CONTROL_MODE_AUTOMATED = 0,
  96. SERVO_CONTROL_MODE_MANUAL_PASSTHROUGH,
  97. SERVO_CONTROL_MODE_MANUAL_MAX,
  98. SERVO_CONTROL_MODE_MANUAL_CENTER,
  99. SERVO_CONTROL_MODE_MANUAL_MIN,
  100. SERVO_CONTROL_MODE_MANUAL_OSCILLATE,
  101. };
  102. // output - sends commands to the motors
  103. void output_armed_stabilizing() override;
  104. void output_armed_zero_throttle();
  105. void output_disarmed();
  106. // external objects we depend upon
  107. AP_MotorsHeli_RSC _main_rotor; // main rotor
  108. // update_motor_controls - sends commands to motor controllers
  109. virtual void update_motor_control(RotorControlState state) = 0;
  110. // run spool logic
  111. void output_logic();
  112. // output_to_motors - sends commands to the motors
  113. virtual void output_to_motors() = 0;
  114. // reset_flight_controls - resets all controls and scalars to flight status
  115. void reset_flight_controls();
  116. // update the throttle input filter
  117. void update_throttle_filter() override;
  118. // move_actuators - moves swash plate and tail rotor
  119. virtual void move_actuators(float roll_out, float pitch_out, float coll_in, float yaw_out) = 0;
  120. // reset_swash_servo - free up swash servo for maximum movement
  121. void reset_swash_servo(SRV_Channel::Aux_servo_function_t function);
  122. // init_outputs - initialise Servo/PWM ranges and endpoints
  123. virtual bool init_outputs() = 0;
  124. // calculate_armed_scalars - must be implemented by child classes
  125. virtual void calculate_armed_scalars() = 0;
  126. // calculate_scalars - must be implemented by child classes
  127. virtual void calculate_scalars() = 0;
  128. // servo_test - move servos through full range of movement
  129. // to be overloaded by child classes, different vehicle types would have different movement patterns
  130. virtual void servo_test() = 0;
  131. // write to a swash servo. output value is pwm
  132. void rc_write_swash(uint8_t chan, float swash_in);
  133. // flags bitmask
  134. struct heliflags_type {
  135. uint8_t landing_collective : 1; // true if collective is setup for landing which has much higher minimum
  136. uint8_t rotor_runup_complete : 1; // true if the rotors have had enough time to wind up
  137. uint8_t inverted_flight : 1; // true for inverted flight
  138. uint8_t init_targets_on_arming : 1; // 0 if targets were initialized, 1 if targets were not initialized after arming
  139. uint8_t save_rsc_mode : 1; // used to determine the rsc mode needs to be saved while disarmed
  140. } _heliflags;
  141. // parameters
  142. AP_Int16 _cyclic_max; // Maximum cyclic angle of the swash plate in centi-degrees
  143. AP_Int16 _collective_min; // Lowest possible servo position for the swashplate
  144. AP_Int16 _collective_max; // Highest possible servo position for the swashplate
  145. AP_Int16 _collective_mid; // Swash servo position corresponding to zero collective pitch (or zero lift for Asymmetrical blades)
  146. AP_Int8 _servo_mode; // Pass radio inputs directly to servos during set-up through mission planner
  147. AP_Int8 _servo_test; // sets number of cycles to test servo movement on bootup
  148. // internal variables
  149. float _collective_mid_pct = 0.0f; // collective mid parameter value converted to 0 ~ 1 range
  150. uint8_t _servo_test_cycle_counter = 0; // number of test cycles left to run after bootup
  151. motor_frame_type _frame_type;
  152. motor_frame_class _frame_class;
  153. };