mavlink.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. mavlink class for handling OpenDroneID messages
  3. */
  4. #include <Arduino.h>
  5. #include "mavlink.h"
  6. #define SERIAL_BAUD 115200
  7. static HardwareSerial *serial_ports[MAVLINK_COMM_NUM_BUFFERS];
  8. #include <generated/mavlink_helpers.h>
  9. mavlink_system_t mavlink_system = {0,MAV_COMP_ID_ODID_TXRX_1};
  10. #define dev_printf(fmt, args ...) do {Serial.printf(fmt, ## args); } while(0)
  11. /*
  12. send a buffer out a MAVLink channel
  13. */
  14. void comm_send_buffer(mavlink_channel_t chan, const uint8_t *buf, uint8_t len)
  15. {
  16. if (chan >= MAVLINK_COMM_NUM_BUFFERS || serial_ports[uint8_t(chan)] == nullptr) {
  17. return;
  18. }
  19. auto &serial = *serial_ports[uint8_t(chan)];
  20. serial.write(buf, len);
  21. }
  22. /*
  23. abstraction for MAVLink on a serial port
  24. */
  25. MAVLinkSerial::MAVLinkSerial(HardwareSerial &_serial, mavlink_channel_t _chan) :
  26. serial(_serial),
  27. chan(_chan)
  28. {
  29. serial_ports[uint8_t(_chan - MAVLINK_COMM_0)] = &serial;
  30. }
  31. void MAVLinkSerial::init(void)
  32. {
  33. }
  34. void MAVLinkSerial::update(void)
  35. {
  36. if (mavlink_system.sysid != 0) {
  37. update_send();
  38. }
  39. update_receive();
  40. }
  41. void MAVLinkSerial::update_send(void)
  42. {
  43. uint32_t now_ms = millis();
  44. if (now_ms - last_hb_ms >= 1000) {
  45. last_hb_ms = now_ms;
  46. mavlink_msg_heartbeat_send(
  47. chan,
  48. MAV_TYPE_ODID,
  49. MAV_AUTOPILOT_ARDUPILOTMEGA,
  50. 0,
  51. 0,
  52. 0);
  53. // send arming status
  54. arm_status_send();
  55. }
  56. }
  57. void MAVLinkSerial::update_receive(void)
  58. {
  59. // receive new packets
  60. mavlink_message_t msg;
  61. mavlink_status_t status;
  62. status.packet_rx_drop_count = 0;
  63. const uint16_t nbytes = serial.available();
  64. for (uint16_t i=0; i<nbytes; i++) {
  65. const uint8_t c = (uint8_t)serial.read();
  66. // Try to get a new message
  67. if (mavlink_parse_char(chan, c, &msg, &status)) {
  68. process_packet(status, msg);
  69. }
  70. }
  71. }
  72. /*
  73. printf via MAVLink STATUSTEXT for debugging
  74. */
  75. void MAVLinkSerial::mav_printf(uint8_t severity, const char *fmt, ...)
  76. {
  77. va_list arg_list;
  78. char text[MAVLINK_MSG_STATUSTEXT_FIELD_TEXT_LEN+1] {};
  79. va_start(arg_list, fmt);
  80. vsnprintf(text, sizeof(text), fmt, arg_list);
  81. va_end(arg_list);
  82. mavlink_msg_statustext_send(chan,
  83. severity,
  84. text,
  85. 0, 0);
  86. }
  87. void MAVLinkSerial::process_packet(mavlink_status_t &status, mavlink_message_t &msg)
  88. {
  89. const uint32_t now_ms = millis();
  90. switch (msg.msgid) {
  91. case MAVLINK_MSG_ID_HEARTBEAT: {
  92. mavlink_heartbeat_t hb;
  93. if (mavlink_system.sysid == 0) {
  94. mavlink_msg_heartbeat_decode(&msg, &hb);
  95. if (msg.sysid > 0 && hb.type != MAV_TYPE_GCS) {
  96. dev_printf("Got HEARTBEAT from %u/%u\n", msg.sysid, msg.compid);
  97. mavlink_system.sysid = msg.sysid;
  98. }
  99. }
  100. break;
  101. }
  102. case MAVLINK_MSG_ID_OPEN_DRONE_ID_LOCATION: {
  103. dev_printf("Got OPEN_DRONE_ID_LOCATION\n");
  104. mavlink_msg_open_drone_id_location_decode(&msg, &location);
  105. last_location_ms = now_ms;
  106. break;
  107. }
  108. case MAVLINK_MSG_ID_OPEN_DRONE_ID_BASIC_ID: {
  109. dev_printf("Got OPEN_DRONE_ID_BASIC_ID\n");
  110. mavlink_msg_open_drone_id_basic_id_decode(&msg, &basic_id);
  111. last_basic_id_ms = now_ms;
  112. break;
  113. }
  114. case MAVLINK_MSG_ID_OPEN_DRONE_ID_AUTHENTICATION: {
  115. dev_printf("Got OPEN_DRONE_ID_AUTHENTICATION\n");
  116. mavlink_msg_open_drone_id_authentication_decode(&msg, &authentication);
  117. break;
  118. }
  119. case MAVLINK_MSG_ID_OPEN_DRONE_ID_SELF_ID: {
  120. dev_printf("Got OPEN_DRONE_ID_SELF_ID\n");
  121. mavlink_msg_open_drone_id_self_id_decode(&msg, &self_id);
  122. last_self_id_ms = now_ms;
  123. break;
  124. }
  125. case MAVLINK_MSG_ID_OPEN_DRONE_ID_SYSTEM: {
  126. dev_printf("Got OPEN_DRONE_ID_SYSTEM\n");
  127. mavlink_msg_open_drone_id_system_decode(&msg, &system);
  128. last_system_ms = now_ms;
  129. break;
  130. }
  131. case MAVLINK_MSG_ID_OPEN_DRONE_ID_SYSTEM_UPDATE: {
  132. dev_printf("Got OPEN_DRONE_ID_SYSTEM_UPDATE\n");
  133. mavlink_open_drone_id_system_update_t pkt_system_update;
  134. mavlink_msg_open_drone_id_system_update_decode(&msg, &pkt_system_update);
  135. system.operator_latitude = pkt_system_update.operator_latitude;
  136. system.operator_longitude = pkt_system_update.operator_longitude;
  137. system.operator_altitude_geo = pkt_system_update.operator_altitude_geo;
  138. system.timestamp = pkt_system_update.timestamp;
  139. if (last_system_ms != 0) {
  140. // we can only mark system as updated if we have the other
  141. // information already
  142. last_system_ms = now_ms;
  143. }
  144. break;
  145. }
  146. case MAVLINK_MSG_ID_OPEN_DRONE_ID_OPERATOR_ID: {
  147. dev_printf("Got OPEN_DRONE_ID_OPERATOR_ID\n");
  148. mavlink_msg_open_drone_id_operator_id_decode(&msg, &operator_id);
  149. last_operator_id_ms = now_ms;
  150. break;
  151. }
  152. default:
  153. // we don't care about other packets
  154. break;
  155. }
  156. }
  157. void MAVLinkSerial::arm_status_send(void)
  158. {
  159. const uint32_t max_age_location_ms = 3000;
  160. const uint32_t max_age_other_ms = 22000;
  161. const uint32_t now_ms = millis();
  162. const char *reason = "";
  163. uint8_t status = MAV_ODID_PRE_ARM_FAIL_GENERIC;
  164. if (last_location_ms == 0 || now_ms - last_location_ms > max_age_location_ms) {
  165. reason = "missing location message";
  166. } else if (last_basic_id_ms == 0 || now_ms - last_basic_id_ms > max_age_other_ms) {
  167. reason = "missing basic_id message";
  168. } else if (last_self_id_ms == 0 || now_ms - last_self_id_ms > max_age_other_ms) {
  169. reason = "missing self_id message";
  170. } else if (last_operator_id_ms == 0 || now_ms - last_operator_id_ms > max_age_other_ms) {
  171. reason = "missing operator_id message";
  172. } else if (last_system_ms == 0 || now_ms - last_system_ms > max_age_other_ms) {
  173. reason = "missing system message";
  174. } else if (location.latitude == 0 && location.longitude == 0) {
  175. reason = "Bad location";
  176. } else if (system.operator_latitude == 0 && system.operator_longitude == 0) {
  177. reason = "Bad operator location";
  178. } else if (parse_fail != nullptr) {
  179. reason = parse_fail;
  180. } else {
  181. status = MAV_ODID_GOOD_TO_ARM;
  182. }
  183. mavlink_msg_open_drone_id_arm_status_send(
  184. chan,
  185. status,
  186. reason);
  187. }