RemoteIDModule.ino 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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_ENABLED
  30. static BLE_TX ble;
  31. #endif
  32. #define OUTPUT_RATE_HZ 5
  33. #define DEBUG_BAUDRATE 57600
  34. #define MAVLINK_BAUDRATE 57600
  35. // OpenDroneID output data structure
  36. static ODID_UAS_Data UAS_data;
  37. static uint32_t last_location_ms;
  38. /*
  39. setup serial ports
  40. */
  41. void setup()
  42. {
  43. // Serial for debug printf
  44. Serial.begin(DEBUG_BAUDRATE);
  45. Serial.printf("ArduRemoteID version %u.%u %08x\n",
  46. FW_VERSION_MAJOR, FW_VERSION_MINOR, GIT_VERSION);
  47. // Serial1 for MAVLink
  48. Serial1.begin(MAVLINK_BAUDRATE, SERIAL_8N1, PIN_UART_RX, PIN_UART_TX);
  49. // set all fields to invalid/initial values
  50. odid_initUasData(&UAS_data);
  51. #if AP_MAVLINK_ENABLED
  52. mavlink1.init();
  53. mavlink2.init();
  54. #endif
  55. #if AP_DRONECAN_ENABLED
  56. dronecan.init();
  57. #endif
  58. #if AP_WIFI_NAN_ENABLED
  59. wifi.init();
  60. #endif
  61. #if AP_BLE_ENABLED
  62. ble.init();
  63. #endif
  64. #if defined(PIN_CAN_EN)
  65. // optional CAN enable pin
  66. pinMode(PIN_CAN_EN, OUTPUT);
  67. digitalWrite(PIN_CAN_EN, HIGH);
  68. #endif
  69. #if defined(PIN_CAN_nSILENT)
  70. // disable silent pin
  71. pinMode(PIN_CAN_nSILENT, OUTPUT);
  72. digitalWrite(PIN_CAN_nSILENT, HIGH);
  73. #endif
  74. #if defined(PIN_CAN_TERM)
  75. // optional CAN termination control
  76. pinMode(PIN_CAN_TERM, OUTPUT);
  77. digitalWrite(PIN_CAN_TERM, HIGH);
  78. #endif
  79. }
  80. #define IMIN(x,y) ((x)<(y)?(x):(y))
  81. #define ODID_COPY_STR(to, from) strncpy(to, (const char*)from, IMIN(sizeof(to), sizeof(from)))
  82. /*
  83. check parsing of UAS_data, this checks ranges of values to ensure we
  84. will produce a valid pack
  85. */
  86. static const char *check_parse(void)
  87. {
  88. {
  89. ODID_Location_encoded encoded {};
  90. if (encodeLocationMessage(&encoded, &UAS_data.Location) != ODID_SUCCESS) {
  91. return "bad LOCATION data";
  92. }
  93. }
  94. {
  95. ODID_System_encoded encoded {};
  96. if (encodeSystemMessage(&encoded, &UAS_data.System) != ODID_SUCCESS) {
  97. return "bad SYSTEM data";
  98. }
  99. }
  100. {
  101. ODID_BasicID_encoded encoded {};
  102. if (encodeBasicIDMessage(&encoded, &UAS_data.BasicID[0]) != ODID_SUCCESS) {
  103. return "bad BASIC_ID data";
  104. }
  105. }
  106. {
  107. ODID_SelfID_encoded encoded {};
  108. if (encodeSelfIDMessage(&encoded, &UAS_data.SelfID) != ODID_SUCCESS) {
  109. return "bad SELF_ID data";
  110. }
  111. }
  112. {
  113. ODID_OperatorID_encoded encoded {};
  114. if (encodeOperatorIDMessage(&encoded, &UAS_data.OperatorID) != ODID_SUCCESS) {
  115. return "bad OPERATOR_ID data";
  116. }
  117. }
  118. return nullptr;
  119. }
  120. static void set_data_mavlink(MAVLinkSerial &m)
  121. {
  122. const auto &operator_id = m.get_operator_id();
  123. const auto &basic_id = m.get_basic_id();
  124. const auto &system = m.get_system();
  125. const auto &self_id = m.get_self_id();
  126. const auto &location = m.get_location();
  127. // BasicID
  128. UAS_data.BasicID[0].UAType = (ODID_uatype_t)basic_id.ua_type;
  129. UAS_data.BasicID[0].IDType = (ODID_idtype_t)basic_id.id_type;
  130. ODID_COPY_STR(UAS_data.BasicID[0].UASID, basic_id.uas_id);
  131. UAS_data.BasicIDValid[0] = 1;
  132. // OperatorID
  133. UAS_data.OperatorID.OperatorIdType = (ODID_operatorIdType_t)operator_id.operator_id_type;
  134. ODID_COPY_STR(UAS_data.OperatorID.OperatorId, operator_id.operator_id);
  135. UAS_data.OperatorIDValid = 1;
  136. // SelfID
  137. UAS_data.SelfID.DescType = (ODID_desctype_t)self_id.description_type;
  138. ODID_COPY_STR(UAS_data.SelfID.Desc, self_id.description);
  139. UAS_data.SelfIDValid = 1;
  140. // System
  141. if (system.timestamp != 0) {
  142. UAS_data.System.OperatorLocationType = (ODID_operator_location_type_t)system.operator_location_type;
  143. UAS_data.System.ClassificationType = (ODID_classification_type_t)system.classification_type;
  144. UAS_data.System.OperatorLatitude = system.operator_latitude * 1.0e-7;
  145. UAS_data.System.OperatorLongitude = system.operator_longitude * 1.0e-7;
  146. UAS_data.System.AreaCount = system.area_count;
  147. UAS_data.System.AreaRadius = system.area_radius;
  148. UAS_data.System.AreaCeiling = system.area_ceiling;
  149. UAS_data.System.AreaFloor = system.area_floor;
  150. UAS_data.System.CategoryEU = (ODID_category_EU_t)system.category_eu;
  151. UAS_data.System.ClassEU = (ODID_class_EU_t)system.class_eu;
  152. UAS_data.System.OperatorAltitudeGeo = system.operator_altitude_geo;
  153. UAS_data.System.Timestamp = system.timestamp;
  154. UAS_data.SystemValid = 1;
  155. }
  156. // Location
  157. if (location.timestamp != 0) {
  158. UAS_data.Location.Status = (ODID_status_t)location.status;
  159. UAS_data.Location.Direction = location.direction*0.01;
  160. UAS_data.Location.SpeedHorizontal = location.speed_horizontal*0.01;
  161. UAS_data.Location.SpeedVertical = location.speed_vertical*0.01;
  162. UAS_data.Location.Latitude = location.latitude*1.0e-7;
  163. UAS_data.Location.Longitude = location.longitude*1.0e-7;
  164. UAS_data.Location.AltitudeBaro = location.altitude_barometric;
  165. UAS_data.Location.AltitudeGeo = location.altitude_geodetic;
  166. UAS_data.Location.HeightType = (ODID_Height_reference_t)location.height_reference;
  167. UAS_data.Location.Height = location.height;
  168. UAS_data.Location.HorizAccuracy = (ODID_Horizontal_accuracy_t)location.horizontal_accuracy;
  169. UAS_data.Location.VertAccuracy = (ODID_Vertical_accuracy_t)location.vertical_accuracy;
  170. UAS_data.Location.BaroAccuracy = (ODID_Vertical_accuracy_t)location.barometer_accuracy;
  171. UAS_data.Location.SpeedAccuracy = (ODID_Speed_accuracy_t)location.speed_accuracy;
  172. UAS_data.Location.TSAccuracy = (ODID_Timestamp_accuracy_t)location.timestamp_accuracy;
  173. UAS_data.Location.TimeStamp = location.timestamp;
  174. UAS_data.LocationValid = 1;
  175. }
  176. m.set_parse_fail(check_parse());
  177. uint32_t now_ms = millis();
  178. uint32_t location_age_ms = now_ms - m.get_last_location_ms();
  179. uint32_t last_location_age_ms = now_ms - last_location_ms;
  180. if (location_age_ms < last_location_age_ms) {
  181. last_location_ms = m.get_last_location_ms();
  182. }
  183. }
  184. #undef ODID_COPY_STR
  185. #define ODID_COPY_STR(to, from) memcpy(to, from.data, IMIN(from.len, sizeof(to)))
  186. #if AP_DRONECAN_ENABLED
  187. static void set_data_dronecan(void)
  188. {
  189. const auto &operator_id = dronecan.get_operator_id();
  190. const auto &basic_id = dronecan.get_basic_id();
  191. const auto &system = dronecan.get_system();
  192. const auto &self_id = dronecan.get_self_id();
  193. const auto &location = dronecan.get_location();
  194. // BasicID
  195. UAS_data.BasicID[0].UAType = (ODID_uatype_t)basic_id.ua_type;
  196. UAS_data.BasicID[0].IDType = (ODID_idtype_t)basic_id.id_type;
  197. ODID_COPY_STR(UAS_data.BasicID[0].UASID, basic_id.uas_id);
  198. UAS_data.BasicIDValid[0] = 1;
  199. // OperatorID
  200. UAS_data.OperatorID.OperatorIdType = (ODID_operatorIdType_t)operator_id.operator_id_type;
  201. ODID_COPY_STR(UAS_data.OperatorID.OperatorId, operator_id.operator_id);
  202. UAS_data.OperatorIDValid = 1;
  203. // SelfID
  204. UAS_data.SelfID.DescType = (ODID_desctype_t)self_id.description_type;
  205. ODID_COPY_STR(UAS_data.SelfID.Desc, self_id.description);
  206. UAS_data.SelfIDValid = 1;
  207. // System
  208. if (system.timestamp != 0) {
  209. UAS_data.System.OperatorLocationType = (ODID_operator_location_type_t)system.operator_location_type;
  210. UAS_data.System.ClassificationType = (ODID_classification_type_t)system.classification_type;
  211. UAS_data.System.OperatorLatitude = system.operator_latitude * 1.0e-7;
  212. UAS_data.System.OperatorLongitude = system.operator_longitude * 1.0e-7;
  213. UAS_data.System.AreaCount = system.area_count;
  214. UAS_data.System.AreaRadius = system.area_radius;
  215. UAS_data.System.AreaCeiling = system.area_ceiling;
  216. UAS_data.System.AreaFloor = system.area_floor;
  217. UAS_data.System.CategoryEU = (ODID_category_EU_t)system.category_eu;
  218. UAS_data.System.ClassEU = (ODID_class_EU_t)system.class_eu;
  219. UAS_data.System.OperatorAltitudeGeo = system.operator_altitude_geo;
  220. UAS_data.System.Timestamp = system.timestamp;
  221. UAS_data.SystemValid = 1;
  222. }
  223. // Location
  224. if (location.timestamp != 0) {
  225. UAS_data.Location.Status = (ODID_status_t)location.status;
  226. UAS_data.Location.Direction = location.direction*0.01;
  227. UAS_data.Location.SpeedHorizontal = location.speed_horizontal*0.01;
  228. UAS_data.Location.SpeedVertical = location.speed_vertical*0.01;
  229. UAS_data.Location.Latitude = location.latitude*1.0e-7;
  230. UAS_data.Location.Longitude = location.longitude*1.0e-7;
  231. UAS_data.Location.AltitudeBaro = location.altitude_barometric;
  232. UAS_data.Location.AltitudeGeo = location.altitude_geodetic;
  233. UAS_data.Location.HeightType = (ODID_Height_reference_t)location.height_reference;
  234. UAS_data.Location.Height = location.height;
  235. UAS_data.Location.HorizAccuracy = (ODID_Horizontal_accuracy_t)location.horizontal_accuracy;
  236. UAS_data.Location.VertAccuracy = (ODID_Vertical_accuracy_t)location.vertical_accuracy;
  237. UAS_data.Location.BaroAccuracy = (ODID_Vertical_accuracy_t)location.barometer_accuracy;
  238. UAS_data.Location.SpeedAccuracy = (ODID_Speed_accuracy_t)location.speed_accuracy;
  239. UAS_data.Location.TSAccuracy = (ODID_Timestamp_accuracy_t)location.timestamp_accuracy;
  240. UAS_data.Location.TimeStamp = location.timestamp;
  241. UAS_data.LocationValid = 1;
  242. }
  243. dronecan.set_parse_fail(check_parse());
  244. uint32_t now_ms = millis();
  245. uint32_t location_age_ms = now_ms - dronecan.get_last_location_ms();
  246. uint32_t last_location_age_ms = now_ms - last_location_ms;
  247. if (location_age_ms < last_location_age_ms) {
  248. last_location_ms = dronecan.get_last_location_ms();
  249. }
  250. }
  251. #endif // AP_DRONECAN_ENABLED
  252. void loop()
  253. {
  254. static uint32_t last_update;
  255. mavlink1.update();
  256. mavlink2.update();
  257. #if AP_DRONECAN_ENABLED
  258. dronecan.update();
  259. #endif
  260. const uint32_t now_ms = millis();
  261. if (now_ms - last_update < 1000/OUTPUT_RATE_HZ) {
  262. // not ready for a new frame yet
  263. return;
  264. }
  265. bool have_location = false;
  266. #if AP_MAVLINK_ENABLED
  267. const bool mavlink1_ok = mavlink1.get_last_location_ms() != 0;
  268. const bool mavlink2_ok = mavlink2.get_last_location_ms() != 0;
  269. have_location |= mavlink1_ok || mavlink2_ok;
  270. #endif
  271. #if AP_DRONECAN_ENABLED
  272. const bool dronecan_ok = dronecan.get_last_location_ms() != 0;
  273. have_location |= dronecan_ok;
  274. #endif
  275. #if AP_BROADCAST_ON_POWER_UP
  276. // if we are broadcasting on powerup we always mark location valid
  277. // so the location with default data is sent
  278. if (!UAS_data.LocationValid) {
  279. UAS_data.Location.Status = ODID_STATUS_REMOTE_ID_SYSTEM_FAILURE;
  280. UAS_data.LocationValid = 1;
  281. }
  282. #else
  283. // only broadcast if we have received a location at least once
  284. if (!have_location) {
  285. return;
  286. }
  287. #endif
  288. if (now_ms - last_location_ms > 5000) {
  289. UAS_data.Location.Status = ODID_STATUS_REMOTE_ID_SYSTEM_FAILURE;
  290. }
  291. #if AP_MAVLINK_ENABLED
  292. if (mavlink1_ok) {
  293. set_data_mavlink(mavlink1);
  294. }
  295. if (mavlink2_ok) {
  296. set_data_mavlink(mavlink2);
  297. }
  298. #endif
  299. #if AP_DRONECAN_ENABLED
  300. if (dronecan_ok) {
  301. set_data_dronecan();
  302. }
  303. #endif
  304. last_update = now_ms;
  305. #if AP_WIFI_NAN_ENABLED
  306. wifi.transmit(UAS_data);
  307. #endif
  308. #if AP_BLE_ENABLED
  309. ble.transmit(UAS_data);
  310. #endif
  311. }