AP_Stats.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #include "AP_Stats.h"
  2. #include <AP_Math/AP_Math.h>
  3. #include <AP_RTC/AP_RTC.h>
  4. const extern AP_HAL::HAL& hal;
  5. // table of user settable parameters
  6. const AP_Param::GroupInfo AP_Stats::var_info[] = {
  7. // @Param: _BOOTCNT
  8. // @DisplayName: Boot Count
  9. // @Description: Number of times board has been booted
  10. // @ReadOnly: True
  11. // @User: Standard
  12. AP_GROUPINFO("_BOOTCNT", 0, AP_Stats, params.bootcount, 0),
  13. // @Param: _FLTTIME
  14. // @DisplayName: Total FlightTime
  15. // @Description: Total FlightTime (seconds)
  16. // @Units: s
  17. // @ReadOnly: True
  18. // @User: Standard
  19. AP_GROUPINFO("_FLTTIME", 1, AP_Stats, params.flttime, 0),
  20. // @Param: _RUNTIME
  21. // @DisplayName: Total RunTime
  22. // @Description: Total time autopilot has run
  23. // @Units: s
  24. // @ReadOnly: True
  25. // @User: Standard
  26. AP_GROUPINFO("_RUNTIME", 2, AP_Stats, params.runtime, 0),
  27. // @Param: _RESET
  28. // @DisplayName: Statistics Reset Time
  29. // @Description: Seconds since January 1st 2016 (Unix epoch+1451606400) since statistics reset (set to 0 to reset statistics)
  30. // @Units: s
  31. // @ReadOnly: True
  32. // @User: Standard
  33. AP_GROUPINFO("_RESET", 3, AP_Stats, params.reset, 1),
  34. AP_GROUPEND
  35. };
  36. AP_Stats *AP_Stats::_singleton;
  37. // constructor
  38. AP_Stats::AP_Stats(void)
  39. {
  40. _singleton = this;
  41. }
  42. void AP_Stats::copy_variables_from_parameters()
  43. {
  44. flttime = params.flttime;
  45. runtime = params.runtime;
  46. reset = params.reset;
  47. flttime_boot = flttime;
  48. }
  49. void AP_Stats::init()
  50. {
  51. params.bootcount.set_and_save(params.bootcount+1);
  52. // initialise our variables from parameters:
  53. copy_variables_from_parameters();
  54. }
  55. void AP_Stats::flush()
  56. {
  57. params.flttime.set_and_save_ifchanged(flttime);
  58. params.runtime.set_and_save_ifchanged(runtime);
  59. }
  60. void AP_Stats::update_flighttime()
  61. {
  62. if (_flying_ms) {
  63. const uint32_t now = AP_HAL::millis();
  64. const uint32_t delta = (now - _flying_ms)/1000;
  65. flttime += delta;
  66. _flying_ms += delta*1000;
  67. }
  68. }
  69. void AP_Stats::update_runtime()
  70. {
  71. const uint32_t now = AP_HAL::millis();
  72. const uint32_t delta = (now - _last_runtime_ms)/1000;
  73. runtime += delta;
  74. _last_runtime_ms += delta*1000;
  75. }
  76. void AP_Stats::update()
  77. {
  78. const uint32_t now_ms = AP_HAL::millis();
  79. if (now_ms - last_flush_ms > flush_interval_ms) {
  80. update_flighttime();
  81. update_runtime();
  82. flush();
  83. last_flush_ms = now_ms;
  84. }
  85. const uint32_t params_reset = params.reset;
  86. if (params_reset != reset || params_reset == 0) {
  87. params.bootcount.set_and_save_ifchanged(params_reset == 0 ? 1 : 0);
  88. params.flttime.set_and_save_ifchanged(0);
  89. params.runtime.set_and_save_ifchanged(0);
  90. uint32_t system_clock = 0; // in seconds
  91. uint64_t rtc_clock_us;
  92. if (AP::rtc().get_utc_usec(rtc_clock_us)) {
  93. system_clock = rtc_clock_us / 1000000;
  94. // can't store Unix seconds in a 32-bit float. Change the
  95. // time base to Jan 1st 2016:
  96. system_clock -= 1451606400;
  97. }
  98. params.reset.set_and_save_ifchanged(system_clock);
  99. copy_variables_from_parameters();
  100. }
  101. }
  102. void AP_Stats::set_flying(const bool is_flying)
  103. {
  104. if (is_flying) {
  105. if (!_flying_ms) {
  106. _flying_ms = AP_HAL::millis();
  107. }
  108. } else {
  109. update_flighttime();
  110. _flying_ms = 0;
  111. }
  112. }
  113. /*
  114. get time in flight since boot
  115. */
  116. uint32_t AP_Stats::get_flight_time_s(void)
  117. {
  118. update_flighttime();
  119. return flttime - flttime_boot;
  120. }
  121. AP_Stats *AP::stats(void)
  122. {
  123. return AP_Stats::get_singleton();
  124. }