AP_Logger_test.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Example of AP_Logger library.
  3. * originally based on code by Jordi MuÒoz and Jose Julio
  4. */
  5. #include <AP_HAL/AP_HAL.h>
  6. #include <AP_Logger/AP_Logger.h>
  7. #include <GCS_MAVLink/GCS_Dummy.h>
  8. #include <stdio.h>
  9. const AP_HAL::HAL& hal = AP_HAL::get_HAL();
  10. #define LOG_TEST_MSG 1
  11. struct PACKED log_Test {
  12. LOG_PACKET_HEADER;
  13. uint16_t v1, v2, v3, v4;
  14. int32_t l1, l2;
  15. };
  16. static const struct LogStructure log_structure[] = {
  17. LOG_COMMON_STRUCTURES,
  18. { LOG_TEST_MSG, sizeof(log_Test),
  19. "TEST",
  20. "HHHHii",
  21. "V1,V2,V3,V4,L1,L2",
  22. "------",
  23. "------"
  24. }
  25. };
  26. #define NUM_PACKETS 500
  27. static uint16_t log_num;
  28. class AP_LoggerTest {
  29. public:
  30. void setup();
  31. void loop();
  32. private:
  33. AP_Int32 log_bitmask;
  34. AP_Logger logger{log_bitmask};
  35. };
  36. static AP_LoggerTest loggertest;
  37. void AP_LoggerTest::setup(void)
  38. {
  39. hal.console->printf("Logger Log Test 1.0\n");
  40. log_bitmask = (uint32_t)-1;
  41. logger.Init(log_structure, ARRAY_SIZE(log_structure));
  42. logger.set_vehicle_armed(true);
  43. logger.Write_Message("AP_Logger Test");
  44. // Test
  45. hal.scheduler->delay(20);
  46. // We start to write some info (sequentialy) starting from page 1
  47. // This is similar to what we will do...
  48. log_num = logger.find_last_log();
  49. hal.console->printf("Using log number %u\n", log_num);
  50. hal.console->printf("Writing to flash... wait...\n");
  51. uint32_t total_micros = 0;
  52. uint16_t i;
  53. for (i = 0; i < NUM_PACKETS; i++) {
  54. uint32_t start = AP_HAL::micros();
  55. // note that we use g++ style initialisers to make larger
  56. // structures easier to follow
  57. struct log_Test pkt = {
  58. LOG_PACKET_HEADER_INIT(LOG_TEST_MSG),
  59. v1 : (uint16_t)(2000 + i),
  60. v2 : (uint16_t)(2001 + i),
  61. v3 : (uint16_t)(2002 + i),
  62. v4 : (uint16_t)(2003 + i),
  63. l1 : (int32_t)(i * 5000),
  64. l2 : (int32_t)(i * 16268)
  65. };
  66. logger.WriteBlock(&pkt, sizeof(pkt));
  67. total_micros += AP_HAL::micros() - start;
  68. hal.scheduler->delay(20);
  69. }
  70. hal.console->printf("Average write time %.1f usec/byte\n",
  71. (double)total_micros/((double)i*sizeof(struct log_Test)));
  72. uint64_t now = AP_HAL::micros64();
  73. hal.console->printf("Testing Write\n");
  74. logger.Write("MARY",
  75. "TimeUS,GoodValue",
  76. "sm",
  77. "F0",
  78. "Qf",
  79. now,
  80. -1.5673);
  81. hal.console->printf("Testing WriteCritical\n");
  82. logger.WriteCritical("BOB",
  83. "TimeUS,GreatValue",
  84. "sm",
  85. "F0",
  86. "Qf",
  87. now,
  88. 17.3);
  89. #if CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX
  90. logger.flush();
  91. #endif
  92. logger.set_vehicle_armed(false);
  93. }
  94. void AP_LoggerTest::loop(void)
  95. {
  96. hal.console->printf("\nTest complete.\n");
  97. hal.scheduler->delay(20000);
  98. }
  99. /*
  100. compatibility with old pde style build
  101. */
  102. void setup(void);
  103. void loop(void);
  104. void setup()
  105. {
  106. loggertest.setup();
  107. }
  108. void loop()
  109. {
  110. loggertest.loop();
  111. }
  112. const struct AP_Param::GroupInfo GCS_MAVLINK_Parameters::var_info[] = {
  113. AP_GROUPEND
  114. };
  115. GCS_Dummy _gcs;
  116. AP_HAL_MAIN();