RemoteIDModule.ino 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. implement OpenDroneID MAVLink and DroneCAN support
  3. */
  4. /*
  5. released under GNU GPL v3 or later
  6. */
  7. #include <Arduino.h>
  8. #include "version.h"
  9. #include <math.h>
  10. #include <time.h>
  11. #include <sys/time.h>
  12. #include <opendroneid.h>
  13. #include "board_config.h"
  14. #include "options.h"
  15. #include "mavlink.h"
  16. #include "DroneCAN.h"
  17. #include "WiFi_TX.h"
  18. #include "BLE_TX.h"
  19. #if AP_DRONECAN_ENABLED
  20. static DroneCAN dronecan;
  21. #endif
  22. #if AP_MAVLINK_ENABLED
  23. static MAVLinkSerial mavlink1 {Serial1, MAVLINK_COMM_0};
  24. static MAVLinkSerial mavlink2{Serial, MAVLINK_COMM_1};
  25. #endif
  26. #if AP_WIFI_NAN_ENABLED
  27. static WiFi_NAN wifi;
  28. #endif
  29. #if AP_BLE_LEGACY_ENABLED || AP_BLE_LONGRANGE_ENABLED
  30. static BLE_TX ble;
  31. #endif
  32. #define DEBUG_BAUDRATE 57600
  33. #define MAVLINK_BAUDRATE 57600
  34. // OpenDroneID output data structure
  35. static ODID_UAS_Data UAS_data;
  36. static uint32_t last_location_ms;
  37. /*
  38. setup serial ports
  39. */
  40. void setup()
  41. {
  42. // Serial for debug printf
  43. Serial.begin(DEBUG_BAUDRATE);
  44. Serial.printf("ArduRemoteID version %u.%u %08x\n",
  45. FW_VERSION_MAJOR, FW_VERSION_MINOR, GIT_VERSION);
  46. // Serial1 for MAVLink
  47. Serial1.begin(MAVLINK_BAUDRATE, SERIAL_8N1, PIN_UART_RX, PIN_UART_TX);
  48. // set all fields to invalid/initial values
  49. odid_initUasData(&UAS_data);
  50. #if AP_MAVLINK_ENABLED
  51. mavlink1.init();
  52. mavlink2.init();
  53. #endif
  54. #if AP_DRONECAN_ENABLED
  55. dronecan.init();
  56. #endif
  57. #if AP_WIFI_NAN_ENABLED
  58. wifi.init();
  59. #endif
  60. #if AP_BLE_LEGACY_ENABLED || AP_BLE_LONGRANGE_ENABLED
  61. ble.init();
  62. #endif
  63. #if defined(PIN_CAN_EN)
  64. // optional CAN enable pin
  65. pinMode(PIN_CAN_EN, OUTPUT);
  66. digitalWrite(PIN_CAN_EN, HIGH);
  67. #endif
  68. #if defined(PIN_CAN_nSILENT)
  69. // disable silent pin
  70. pinMode(PIN_CAN_nSILENT, OUTPUT);
  71. digitalWrite(PIN_CAN_nSILENT, HIGH);
  72. #endif
  73. #if defined(PIN_CAN_TERM)
  74. // optional CAN termination control
  75. pinMode(PIN_CAN_TERM, OUTPUT);
  76. digitalWrite(PIN_CAN_TERM, HIGH);
  77. #endif
  78. }
  79. #define IMIN(x,y) ((x)<(y)?(x):(y))
  80. #define ODID_COPY_STR(to, from) strncpy(to, (const char*)from, IMIN(sizeof(to), sizeof(from)))
  81. /*
  82. check parsing of UAS_data, this checks ranges of values to ensure we
  83. will produce a valid pack
  84. */
  85. static const char *check_parse(void)
  86. {
  87. {
  88. ODID_Location_encoded encoded {};
  89. if (encodeLocationMessage(&encoded, &UAS_data.Location) != ODID_SUCCESS) {
  90. return "bad LOCATION data";
  91. }
  92. }
  93. {
  94. ODID_System_encoded encoded {};
  95. if (encodeSystemMessage(&encoded, &UAS_data.System) != ODID_SUCCESS) {
  96. return "bad SYSTEM data";
  97. }
  98. }
  99. {
  100. ODID_BasicID_encoded encoded {};
  101. if (encodeBasicIDMessage(&encoded, &UAS_data.BasicID[0]) != ODID_SUCCESS) {
  102. return "bad BASIC_ID data";
  103. }
  104. }
  105. {
  106. ODID_SelfID_encoded encoded {};
  107. if (encodeSelfIDMessage(&encoded, &UAS_data.SelfID) != ODID_SUCCESS) {
  108. return "bad SELF_ID data";
  109. }
  110. }
  111. {
  112. ODID_OperatorID_encoded encoded {};
  113. if (encodeOperatorIDMessage(&encoded, &UAS_data.OperatorID) != ODID_SUCCESS) {
  114. return "bad OPERATOR_ID data";
  115. }
  116. }
  117. return nullptr;
  118. }
  119. /*
  120. fill in UAS_data from MAVLink packets
  121. */
  122. static void set_data(Transport &t)
  123. {
  124. const auto &operator_id = t.get_operator_id();
  125. const auto &basic_id = t.get_basic_id();
  126. const auto &system = t.get_system();
  127. const auto &self_id = t.get_self_id();
  128. const auto &location = t.get_location();
  129. // BasicID
  130. UAS_data.BasicID[0].UAType = (ODID_uatype_t)basic_id.ua_type;
  131. UAS_data.BasicID[0].IDType = (ODID_idtype_t)basic_id.id_type;
  132. ODID_COPY_STR(UAS_data.BasicID[0].UASID, basic_id.uas_id);
  133. UAS_data.BasicIDValid[0] = 1;
  134. // OperatorID
  135. UAS_data.OperatorID.OperatorIdType = (ODID_operatorIdType_t)operator_id.operator_id_type;
  136. ODID_COPY_STR(UAS_data.OperatorID.OperatorId, operator_id.operator_id);
  137. UAS_data.OperatorIDValid = 1;
  138. // SelfID
  139. UAS_data.SelfID.DescType = (ODID_desctype_t)self_id.description_type;
  140. ODID_COPY_STR(UAS_data.SelfID.Desc, self_id.description);
  141. UAS_data.SelfIDValid = 1;
  142. // System
  143. if (system.timestamp != 0) {
  144. UAS_data.System.OperatorLocationType = (ODID_operator_location_type_t)system.operator_location_type;
  145. UAS_data.System.ClassificationType = (ODID_classification_type_t)system.classification_type;
  146. UAS_data.System.OperatorLatitude = system.operator_latitude * 1.0e-7;
  147. UAS_data.System.OperatorLongitude = system.operator_longitude * 1.0e-7;
  148. UAS_data.System.AreaCount = system.area_count;
  149. UAS_data.System.AreaRadius = system.area_radius;
  150. UAS_data.System.AreaCeiling = system.area_ceiling;
  151. UAS_data.System.AreaFloor = system.area_floor;
  152. UAS_data.System.CategoryEU = (ODID_category_EU_t)system.category_eu;
  153. UAS_data.System.ClassEU = (ODID_class_EU_t)system.class_eu;
  154. UAS_data.System.OperatorAltitudeGeo = system.operator_altitude_geo;
  155. UAS_data.System.Timestamp = system.timestamp;
  156. UAS_data.SystemValid = 1;
  157. }
  158. // Location
  159. if (location.timestamp != 0) {
  160. UAS_data.Location.Status = (ODID_status_t)location.status;
  161. UAS_data.Location.Direction = location.direction*0.01;
  162. UAS_data.Location.SpeedHorizontal = location.speed_horizontal*0.01;
  163. UAS_data.Location.SpeedVertical = location.speed_vertical*0.01;
  164. UAS_data.Location.Latitude = location.latitude*1.0e-7;
  165. UAS_data.Location.Longitude = location.longitude*1.0e-7;
  166. UAS_data.Location.AltitudeBaro = location.altitude_barometric;
  167. UAS_data.Location.AltitudeGeo = location.altitude_geodetic;
  168. UAS_data.Location.HeightType = (ODID_Height_reference_t)location.height_reference;
  169. UAS_data.Location.Height = location.height;
  170. UAS_data.Location.HorizAccuracy = (ODID_Horizontal_accuracy_t)location.horizontal_accuracy;
  171. UAS_data.Location.VertAccuracy = (ODID_Vertical_accuracy_t)location.vertical_accuracy;
  172. UAS_data.Location.BaroAccuracy = (ODID_Vertical_accuracy_t)location.barometer_accuracy;
  173. UAS_data.Location.SpeedAccuracy = (ODID_Speed_accuracy_t)location.speed_accuracy;
  174. UAS_data.Location.TSAccuracy = (ODID_Timestamp_accuracy_t)location.timestamp_accuracy;
  175. UAS_data.Location.TimeStamp = location.timestamp;
  176. UAS_data.LocationValid = 1;
  177. }
  178. const char *reason = check_parse();
  179. if (reason == nullptr) {
  180. t.arm_status_check(reason);
  181. }
  182. t.set_parse_fail(reason);
  183. #ifdef PIN_STATUS_LED
  184. // LED off if good to arm
  185. pinMode(PIN_STATUS_LED, OUTPUT);
  186. digitalWrite(PIN_STATUS_LED, reason==nullptr?!STATUS_LED_ON:STATUS_LED_ON);
  187. #endif
  188. uint32_t now_ms = millis();
  189. uint32_t location_age_ms = now_ms - t.get_last_location_ms();
  190. uint32_t last_location_age_ms = now_ms - last_location_ms;
  191. if (location_age_ms < last_location_age_ms) {
  192. last_location_ms = t.get_last_location_ms();
  193. }
  194. }
  195. static uint8_t loop_counter = 0;
  196. void loop()
  197. {
  198. static uint32_t last_update;
  199. mavlink1.update();
  200. mavlink2.update();
  201. #if AP_DRONECAN_ENABLED
  202. dronecan.update();
  203. #endif
  204. const uint32_t now_ms = millis();
  205. // we call BT4 send at 5x the desired rate as it has to split the pkts 5 ways
  206. const uint32_t rate_divider = 5;
  207. if (now_ms - last_update < 1000UL/(OUTPUT_RATE_HZ*rate_divider)) {
  208. // not ready for a new frame yet
  209. return;
  210. }
  211. loop_counter++;
  212. loop_counter %= rate_divider;
  213. // the transports have common static data, so we can just use the
  214. // first for status
  215. auto &transport = mavlink1;
  216. bool have_location = false;
  217. const uint32_t last_location_ms = transport.get_last_location_ms();
  218. const uint32_t last_system_ms = transport.get_last_system_ms();
  219. #if AP_BROADCAST_ON_POWER_UP
  220. // if we are broadcasting on powerup we always mark location valid
  221. // so the location with default data is sent
  222. if (!UAS_data.LocationValid) {
  223. UAS_data.Location.Status = ODID_STATUS_REMOTE_ID_SYSTEM_FAILURE;
  224. UAS_data.LocationValid = 1;
  225. }
  226. #else
  227. // only broadcast if we have received a location at least once
  228. if (last_location_ms == 0) {
  229. return;
  230. }
  231. #endif
  232. if (last_location_ms == 0 ||
  233. now_ms - last_location_ms > 5000) {
  234. UAS_data.Location.Status = ODID_STATUS_REMOTE_ID_SYSTEM_FAILURE;
  235. }
  236. if (last_system_ms == 0 ||
  237. now_ms - last_system_ms > 5000) {
  238. UAS_data.Location.Status = ODID_STATUS_REMOTE_ID_SYSTEM_FAILURE;
  239. }
  240. if (transport.get_parse_fail() != nullptr) {
  241. UAS_data.Location.Status = ODID_STATUS_REMOTE_ID_SYSTEM_FAILURE;
  242. }
  243. set_data(transport);
  244. last_update = now_ms;
  245. #if AP_WIFI_NAN_ENABLED
  246. if (loop_counter == 0) { //only run on the original update rate
  247. wifi.transmit(UAS_data);
  248. }
  249. #endif
  250. #if AP_BLE_LONGRANGE_ENABLED
  251. if (loop_counter == 0) { //only run on the original update rate
  252. ble.transmit_longrange(UAS_data);
  253. }
  254. #endif
  255. #if AP_BLE_LEGACY_ENABLED
  256. ble.transmit_legacy(UAS_data);
  257. #endif
  258. #if AP_BLE_LEGACY_ENABLED || AP_BLE_LONGRANGE_ENABLED
  259. ble.transmit_legacy_name(UAS_data);
  260. #endif
  261. }