AP_RPM.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include "AP_RPM.h"
  14. #include "RPM_Pin.h"
  15. #include "RPM_SITL.h"
  16. extern const AP_HAL::HAL& hal;
  17. // table of user settable parameters
  18. const AP_Param::GroupInfo AP_RPM::var_info[] = {
  19. // @Param: _TYPE
  20. // @DisplayName: RPM type
  21. // @Description: What type of RPM sensor is connected
  22. // @Values: 0:None,1:PX4-PWM,2:AUXPIN
  23. // @User: Standard
  24. AP_GROUPINFO("_TYPE", 0, AP_RPM, _type[0], 0),
  25. // @Param: _SCALING
  26. // @DisplayName: RPM scaling
  27. // @Description: Scaling factor between sensor reading and RPM.
  28. // @Increment: 0.001
  29. // @User: Standard
  30. AP_GROUPINFO("_SCALING", 1, AP_RPM, _scaling[0], 1.0f),
  31. // @Param: _MAX
  32. // @DisplayName: Maximum RPM
  33. // @Description: Maximum RPM to report
  34. // @Increment: 1
  35. // @User: Standard
  36. AP_GROUPINFO("_MAX", 2, AP_RPM, _maximum[0], 100000),
  37. // @Param: _MIN
  38. // @DisplayName: Minimum RPM
  39. // @Description: Minimum RPM to report
  40. // @Increment: 1
  41. // @User: Standard
  42. AP_GROUPINFO("_MIN", 3, AP_RPM, _minimum[0], 10),
  43. // @Param: _MIN_QUAL
  44. // @DisplayName: Minimum Quality
  45. // @Description: Minimum data quality to be used
  46. // @Increment: 0.1
  47. // @User: Advanced
  48. AP_GROUPINFO("_MIN_QUAL", 4, AP_RPM, _quality_min[0], 0.5),
  49. // @Param: _PIN
  50. // @DisplayName: Input pin number
  51. // @Description: Which pin to use
  52. // @Values: -1:Disabled,50:PixhawkAUX1,51:PixhawkAUX2,52:PixhawkAUX3,53:PixhawkAUX4,54:PixhawkAUX5,55:PixhawkAUX6
  53. // @User: Standard
  54. AP_GROUPINFO("_PIN", 5, AP_RPM, _pin[0], 54),
  55. #if RPM_MAX_INSTANCES > 1
  56. // @Param: 2_TYPE
  57. // @DisplayName: Second RPM type
  58. // @Description: What type of RPM sensor is connected
  59. // @Values: 0:None,1:PX4-PWM,2:AUXPIN
  60. // @User: Advanced
  61. AP_GROUPINFO("2_TYPE", 10, AP_RPM, _type[1], 0),
  62. // @Param: 2_SCALING
  63. // @DisplayName: RPM scaling
  64. // @Description: Scaling factor between sensor reading and RPM.
  65. // @Increment: 0.001
  66. // @User: Advanced
  67. AP_GROUPINFO("2_SCALING", 11, AP_RPM, _scaling[1], 1.0f),
  68. #endif
  69. // @Param: 2_PIN
  70. // @DisplayName: RPM2 input pin number
  71. // @Description: Which pin to use
  72. // @Values: -1:Disabled,50:PixhawkAUX1,51:PixhawkAUX2,52:PixhawkAUX3,53:PixhawkAUX4,54:PixhawkAUX5,55:PixhawkAUX6
  73. // @User: Standard
  74. AP_GROUPINFO("2_PIN", 12, AP_RPM, _pin[1], -1),
  75. AP_GROUPEND
  76. };
  77. AP_RPM::AP_RPM(void)
  78. {
  79. AP_Param::setup_object_defaults(this, var_info);
  80. if (_singleton != nullptr) {
  81. AP_HAL::panic("AP_RPM must be singleton");
  82. }
  83. _singleton = this;
  84. }
  85. /*
  86. initialise the AP_RPM class.
  87. */
  88. void AP_RPM::init(void)
  89. {
  90. if (num_instances != 0) {
  91. // init called a 2nd time?
  92. return;
  93. }
  94. for (uint8_t i=0; i<RPM_MAX_INSTANCES; i++) {
  95. uint8_t type = _type[i];
  96. if (type == RPM_TYPE_PX4_PWM) {
  97. // on non-PX4 treat PX4-pin as AUXPIN option, for upgrade
  98. type = RPM_TYPE_PIN;
  99. }
  100. if (type == RPM_TYPE_PIN) {
  101. drivers[i] = new AP_RPM_Pin(*this, i, state[i]);
  102. }
  103. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  104. drivers[i] = new AP_RPM_SITL(*this, i, state[i]);
  105. #endif
  106. if (drivers[i] != nullptr) {
  107. // we loaded a driver for this instance, so it must be
  108. // present (although it may not be healthy)
  109. num_instances = i+1; // num_instances is a high-water-mark
  110. }
  111. }
  112. }
  113. /*
  114. update RPM state for all instances. This should be called by main loop
  115. */
  116. void AP_RPM::update(void)
  117. {
  118. for (uint8_t i=0; i<num_instances; i++) {
  119. if (drivers[i] != nullptr) {
  120. if (_type[i] == RPM_TYPE_NONE) {
  121. // allow user to disable an RPM sensor at runtime and force it to re-learn the quality if re-enabled.
  122. state[i].signal_quality = 0;
  123. continue;
  124. }
  125. drivers[i]->update();
  126. }
  127. }
  128. }
  129. /*
  130. check if an instance is healthy
  131. */
  132. bool AP_RPM::healthy(uint8_t instance) const
  133. {
  134. if (instance >= num_instances || _type[instance] == RPM_TYPE_NONE) {
  135. return false;
  136. }
  137. // check that data quality is above minimum required
  138. if (state[instance].signal_quality < _quality_min[0]) {
  139. return false;
  140. }
  141. return true;
  142. }
  143. /*
  144. check if an instance is activated
  145. */
  146. bool AP_RPM::enabled(uint8_t instance) const
  147. {
  148. if (instance >= num_instances) {
  149. return false;
  150. }
  151. // if no sensor type is selected, the sensor is not activated.
  152. if (_type[instance] == RPM_TYPE_NONE) {
  153. return false;
  154. }
  155. return true;
  156. }
  157. // singleton instance
  158. AP_RPM *AP_RPM::_singleton;
  159. namespace AP {
  160. AP_RPM *rpm()
  161. {
  162. return AP_RPM::get_singleton();
  163. }
  164. }