AP_LeakDetector.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "AP_LeakDetector.h"
  2. #include "AP_LeakDetector_Analog.h"
  3. #include "AP_LeakDetector_Digital.h"
  4. const AP_Param::GroupInfo AP_LeakDetector::var_info[] = {
  5. // @Param: 1_PIN
  6. // @DisplayName: Pin that leak detector is connected to
  7. // @Description: Pin that the leak detector is connected to
  8. // @Values: -1:Disabled,50:Pixhawk Aux1,51:Pixhawk Aux2,52:Pixhawk Aux3,53:Pixhawk Aux4,54:Pixhawk Aux5,55:Pixhawk Aux6,13:Pixhawk 3.3ADC1,14:Pixhawk 3.3ADC2,15:Pixhawk 6.6ADC
  9. // @User: Standard
  10. // @RebootRequired: True
  11. AP_GROUPINFO("1_PIN", 1, AP_LeakDetector, _pin[0], -1),
  12. // @Param: 1_LOGIC
  13. // @DisplayName: Default reading of leak detector when dry
  14. // @Description: Default reading of leak detector when dry
  15. // @Values: 0:Low,1:High
  16. // @User: Standard
  17. AP_GROUPINFO("1_LOGIC", 2, AP_LeakDetector, _default_reading[0], 0),
  18. #if LEAKDETECTOR_MAX_INSTANCES > 1
  19. // @Param: 2_PIN
  20. // @DisplayName: Pin that leak detector is connected to
  21. // @Description: Pin that the leak detector is connected to
  22. // @Values: -1:Disabled,50:Pixhawk Aux1,51:Pixhawk Aux2,52:Pixhawk Aux3,53:Pixhawk Aux4,54:Pixhawk Aux5,55:Pixhawk Aux6,13:Pixhawk 3.3ADC1,14:Pixhawk 3.3ADC2,15:Pixhawk 6.6ADC
  23. // @User: Standard
  24. // @RebootRequired: True
  25. AP_GROUPINFO("2_PIN", 3, AP_LeakDetector, _pin[1], -1),
  26. // @Param: 2_LOGIC
  27. // @DisplayName: Default reading of leak detector when dry
  28. // @Description: Default reading of leak detector when dry
  29. // @Values: 0:Low,1:High
  30. // @User: Standard
  31. AP_GROUPINFO("2_LOGIC", 4, AP_LeakDetector, _default_reading[1], 0),
  32. #endif
  33. #if LEAKDETECTOR_MAX_INSTANCES > 2
  34. // @Param: 3_PIN
  35. // @DisplayName: Pin that leak detector is connected to
  36. // @Description: Pin that the leak detector is connected to
  37. // @Values: -1:Disabled,50:Pixhawk Aux1,51:Pixhawk Aux2,52:Pixhawk Aux3,53:Pixhawk Aux4,54:Pixhawk Aux5,55:Pixhawk Aux6,13:Pixhawk 3.3ADC1,14:Pixhawk 3.3ADC2,15:Pixhawk 6.6ADC
  38. // @User: Standard
  39. // @RebootRequired: True
  40. AP_GROUPINFO("3_PIN", 5, AP_LeakDetector, _pin[2], -1),
  41. // @Param: 3_LOGIC
  42. // @DisplayName: Default reading of leak detector when dry
  43. // @Description: Default reading of leak detector when dry
  44. // @Values: 0:Low,1:High
  45. // @User: Standard
  46. AP_GROUPINFO("3_LOGIC", 6, AP_LeakDetector, _default_reading[2], 0),
  47. #endif
  48. AP_GROUPEND
  49. };
  50. AP_LeakDetector::AP_LeakDetector() :
  51. _status(false),
  52. _last_detect_ms(0)
  53. {
  54. AP_Param::setup_object_defaults(this, var_info);
  55. memset(_state,0,sizeof(_state));
  56. memset(_drivers,0,sizeof(_drivers));
  57. };
  58. void AP_LeakDetector::init()
  59. {
  60. for (int i = 0; i < LEAKDETECTOR_MAX_INSTANCES; i++) {
  61. switch (_pin[i]) {
  62. case 50 ... 55:
  63. _state[i].instance = i;
  64. _drivers[i] = new AP_LeakDetector_Digital(*this, _state[i]);
  65. break;
  66. case 13 ... 15:
  67. _state[i].instance = i;
  68. _drivers[i] = new AP_LeakDetector_Analog(*this, _state[i]);
  69. break;
  70. default:
  71. _drivers[i] = NULL;
  72. break;
  73. }
  74. }
  75. }
  76. bool AP_LeakDetector::update()
  77. {
  78. uint32_t tnow = AP_HAL::millis();
  79. for (int i = 0; i < LEAKDETECTOR_MAX_INSTANCES; i++) {
  80. if (_drivers[i] != NULL) {
  81. _drivers[i]->read();
  82. if (_state[i].status) {
  83. _last_detect_ms = tnow;
  84. }
  85. }
  86. }
  87. _status = tnow < _last_detect_ms + LEAKDETECTOR_COOLDOWN_MS;
  88. return _status;
  89. }
  90. void AP_LeakDetector::set_detect()
  91. {
  92. _last_detect_ms = AP_HAL::millis();
  93. }