AP_Relay.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * AP_Relay.cpp
  3. *
  4. * Created on: Oct 2, 2011
  5. * Author: Amilcar Lucas
  6. */
  7. #include <AP_HAL/AP_HAL.h>
  8. #include "AP_Relay.h"
  9. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  10. #define RELAY1_PIN_DEFAULT 13
  11. #elif CONFIG_HAL_BOARD == HAL_BOARD_LINUX
  12. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE
  13. #define RELAY1_PIN_DEFAULT 57
  14. #define RELAY2_PIN_DEFAULT 49
  15. #define RELAY3_PIN_DEFAULT 116
  16. #define RELAY4_PIN_DEFAULT 113
  17. #endif
  18. #endif
  19. #ifndef RELAY1_PIN_DEFAULT
  20. #define RELAY1_PIN_DEFAULT -1
  21. #endif
  22. #ifndef RELAY2_PIN_DEFAULT
  23. #define RELAY2_PIN_DEFAULT -1
  24. #endif
  25. #ifndef RELAY3_PIN_DEFAULT
  26. #define RELAY3_PIN_DEFAULT -1
  27. #endif
  28. #ifndef RELAY4_PIN_DEFAULT
  29. #define RELAY4_PIN_DEFAULT -1
  30. #endif
  31. #ifndef RELAY5_PIN_DEFAULT
  32. #define RELAY5_PIN_DEFAULT -1
  33. #endif
  34. #ifndef RELAY6_PIN_DEFAULT
  35. #define RELAY6_PIN_DEFAULT -1
  36. #endif
  37. const AP_Param::GroupInfo AP_Relay::var_info[] = {
  38. // @Param: PIN
  39. // @DisplayName: First Relay Pin
  40. // @Description: Digital pin number for first relay control. This is the pin used for camera control.
  41. // @User: Standard
  42. // @Values: -1:Disabled,49:BB Blue GP0 pin 4,50:AUXOUT1,51:AUXOUT2,52:AUXOUT3,53:AUXOUT4,54:AUXOUT5,55:AUXOUT6,57:BB Blue GP0 pin 3,113:BB Blue GP0 pin 6,116:BB Blue GP0 pin 5
  43. AP_GROUPINFO("PIN", 0, AP_Relay, _pin[0], RELAY1_PIN_DEFAULT),
  44. // @Param: PIN2
  45. // @DisplayName: Second Relay Pin
  46. // @Description: Digital pin number for 2nd relay control.
  47. // @User: Standard
  48. // @Values: -1:Disabled,49:BB Blue GP0 pin 4,50:AUXOUT1,51:AUXOUT2,52:AUXOUT3,53:AUXOUT4,54:AUXOUT5,55:AUXOUT6,57:BB Blue GP0 pin 3,113:BB Blue GP0 pin 6,116:BB Blue GP0 pin 5
  49. AP_GROUPINFO("PIN2", 1, AP_Relay, _pin[1], RELAY2_PIN_DEFAULT),
  50. // @Param: PIN3
  51. // @DisplayName: Third Relay Pin
  52. // @Description: Digital pin number for 3rd relay control.
  53. // @User: Standard
  54. // @Values: -1:Disabled,49:BB Blue GP0 pin 4,50:AUXOUT1,51:AUXOUT2,52:AUXOUT3,53:AUXOUT4,54:AUXOUT5,55:AUXOUT6,57:BB Blue GP0 pin 3,113:BB Blue GP0 pin 6,116:BB Blue GP0 pin 5
  55. AP_GROUPINFO("PIN3", 2, AP_Relay, _pin[2], RELAY3_PIN_DEFAULT),
  56. // @Param: PIN4
  57. // @DisplayName: Fourth Relay Pin
  58. // @Description: Digital pin number for 4th relay control.
  59. // @User: Standard
  60. // @Values: -1:Disabled,49:BB Blue GP0 pin 4,50:AUXOUT1,51:AUXOUT2,52:AUXOUT3,53:AUXOUT4,54:AUXOUT5,55:AUXOUT6,57:BB Blue GP0 pin 3,113:BB Blue GP0 pin 6,116:BB Blue GP0 pin 5
  61. AP_GROUPINFO("PIN4", 3, AP_Relay, _pin[3], RELAY4_PIN_DEFAULT),
  62. // @Param: DEFAULT
  63. // @DisplayName: Default relay state
  64. // @Description: The state of the relay on boot.
  65. // @User: Standard
  66. // @Values: 0:Off,1:On,2:NoChange
  67. AP_GROUPINFO("DEFAULT", 4, AP_Relay, _default, 0),
  68. // @Param: PIN5
  69. // @DisplayName: Fifth Relay Pin
  70. // @Description: Digital pin number for 5th relay control.
  71. // @User: Standard
  72. // @Values: -1:Disabled,49:BB Blue GP0 pin 4,50:AUXOUT1,51:AUXOUT2,52:AUXOUT3,53:AUXOUT4,54:AUXOUT5,55:AUXOUT6,57:BB Blue GP0 pin 3,113:BB Blue GP0 pin 6,116:BB Blue GP0 pin 5
  73. AP_GROUPINFO("PIN5", 5, AP_Relay, _pin[4], RELAY5_PIN_DEFAULT),
  74. // @Param: PIN6
  75. // @DisplayName: Sixth Relay Pin
  76. // @Description: Digital pin number for 6th relay control.
  77. // @User: Standard
  78. // @Values: -1:Disabled,49:BB Blue GP0 pin 4,50:AUXOUT1,51:AUXOUT2,52:AUXOUT3,53:AUXOUT4,54:AUXOUT5,55:AUXOUT6,57:BB Blue GP0 pin 3,113:BB Blue GP0 pin 6,116:BB Blue GP0 pin 5
  79. AP_GROUPINFO("PIN6", 6, AP_Relay, _pin[5], RELAY6_PIN_DEFAULT),
  80. AP_GROUPEND
  81. };
  82. AP_Relay *AP_Relay::singleton;
  83. extern const AP_HAL::HAL& hal;
  84. AP_Relay::AP_Relay(void)
  85. {
  86. AP_Param::setup_object_defaults(this, var_info);
  87. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL
  88. if (singleton != nullptr) {
  89. AP_HAL::panic("AP_Relay must be singleton");
  90. }
  91. #endif
  92. singleton = this;
  93. }
  94. void AP_Relay::init()
  95. {
  96. for (uint8_t i=0; i<AP_RELAY_NUM_RELAYS; i++) {
  97. if (_pin[i].get() != -1) {
  98. switch (_default) {
  99. case 0:
  100. off(i);
  101. break;
  102. case 1:
  103. on(i);
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109. }
  110. }
  111. void AP_Relay::on(uint8_t relay)
  112. {
  113. if (relay < AP_RELAY_NUM_RELAYS && _pin[relay] != -1) {
  114. hal.gpio->pinMode(_pin[relay], HAL_GPIO_OUTPUT);
  115. hal.gpio->write(_pin[relay], 1);
  116. }
  117. }
  118. void AP_Relay::off(uint8_t relay)
  119. {
  120. if (relay < AP_RELAY_NUM_RELAYS && _pin[relay] != -1) {
  121. hal.gpio->pinMode(_pin[relay], HAL_GPIO_OUTPUT);
  122. hal.gpio->write(_pin[relay], 0);
  123. }
  124. }
  125. void AP_Relay::toggle(uint8_t relay)
  126. {
  127. if (relay < AP_RELAY_NUM_RELAYS && _pin[relay] != -1) {
  128. bool ison = hal.gpio->read(_pin[relay]);
  129. if (ison)
  130. off(relay);
  131. else
  132. on(relay);
  133. }
  134. }
  135. namespace AP {
  136. AP_Relay *relay()
  137. {
  138. return AP_Relay::get_singleton();
  139. }
  140. }