|
| 1 | +/* |
| 2 | + common EKF buffer classes. These handles the storage buffers for EKF |
| 3 | + data to bring it onto the fusion time horizon |
| 4 | +*/ |
| 5 | + |
| 6 | +#include <stdint.h> |
| 7 | +#include <type_traits> |
| 8 | + |
| 9 | +typedef struct { |
| 10 | + // measurement timestamp (msec) |
| 11 | + uint32_t time_ms; |
| 12 | +} EKF_obs_element_t; |
| 13 | + |
| 14 | +// this class is to be used for observation buffers, the data is |
| 15 | +// pushed into buffer like any standard ring buffer return is based on |
| 16 | +// the sample time provided |
| 17 | +class ekf_ring_buffer |
| 18 | +{ |
| 19 | +public: |
| 20 | + ekf_ring_buffer(uint8_t elsize); |
| 21 | + |
| 22 | + // initialise buffer, returns false when allocation has failed |
| 23 | + bool init(uint8_t size); |
| 24 | + |
| 25 | + /* |
| 26 | + * Searches through a ring buffer and return the newest data that is older than the |
| 27 | + * time specified by sample_time_ms |
| 28 | + * Zeros old data so it cannot not be used again |
| 29 | + * Returns false if no data can be found that is less than 100msec old |
| 30 | + */ |
| 31 | + bool recall(void *element, uint32_t sample_time); |
| 32 | + |
| 33 | + /* |
| 34 | + * Writes data and timestamp to a Ring buffer and advances indices that |
| 35 | + * define the location of the newest and oldest data |
| 36 | + */ |
| 37 | + void push(const void *element); |
| 38 | + |
| 39 | + // zeroes all data in the ring buffer |
| 40 | + void reset(); |
| 41 | + |
| 42 | +private: |
| 43 | + const uint8_t elsize; |
| 44 | + void *buffer; |
| 45 | + uint8_t _size, _head, _tail, _new_data; |
| 46 | + |
| 47 | + uint32_t &time_ms(uint8_t idx); |
| 48 | + void *get_offset(uint8_t idx) const; |
| 49 | +}; |
| 50 | + |
| 51 | +/* |
| 52 | + template class for more convenient type handling |
| 53 | + */ |
| 54 | +template <typename element_type> |
| 55 | +class EKF_obs_buffer_t : ekf_ring_buffer |
| 56 | +{ |
| 57 | + static_assert( |
| 58 | + std::is_base_of<EKF_obs_element_t, element_type>::value, |
| 59 | + "must be a descendant of EKF_obs_element_t" |
| 60 | + ); |
| 61 | +public: |
| 62 | + EKF_obs_buffer_t() : |
| 63 | + ekf_ring_buffer(sizeof(element_type)) |
| 64 | + {} |
| 65 | + |
| 66 | + bool init(uint8_t size) { |
| 67 | + return ekf_ring_buffer::init(size); |
| 68 | + } |
| 69 | + |
| 70 | + bool recall(element_type &element,uint32_t sample_time) { |
| 71 | + return ekf_ring_buffer::recall(&element, sample_time); |
| 72 | + } |
| 73 | + |
| 74 | + void push(element_type element) { |
| 75 | + return ekf_ring_buffer::push(&element); |
| 76 | + } |
| 77 | + |
| 78 | + void reset() { |
| 79 | + return ekf_ring_buffer::reset(); |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | + |
| 84 | +/* |
| 85 | + ring buffer for IMU data, |
| 86 | +*/ |
| 87 | +class ekf_imu_buffer |
| 88 | +{ |
| 89 | +public: |
| 90 | + ekf_imu_buffer(uint8_t elsize); |
| 91 | + |
| 92 | + // initialise buffer, returns false when allocation has failed |
| 93 | + bool init(uint32_t size); |
| 94 | + |
| 95 | + /* |
| 96 | + Writes data to a Ring buffer and advances indices that |
| 97 | + define the location of the newest and oldest data |
| 98 | + */ |
| 99 | + void push_youngest_element(const void *element); |
| 100 | + |
| 101 | + // return true if the buffer has been filled at least once |
| 102 | + bool is_filled(void) const { |
| 103 | + return _filled; |
| 104 | + } |
| 105 | + |
| 106 | + // retrieve the oldest data from the ring buffer tail |
| 107 | + void get_oldest_element(void *element); |
| 108 | + |
| 109 | + // writes the same data to all elements in the ring buffer |
| 110 | + void reset_history(const void *element); |
| 111 | + |
| 112 | + // zeroes all data in the ring buffer |
| 113 | + void reset(); |
| 114 | + |
| 115 | + // retrieves data from the ring buffer at a specified index |
| 116 | + void *get(uint8_t index) const; |
| 117 | + |
| 118 | + // returns the index for the ring buffer oldest data |
| 119 | + uint8_t get_oldest_index() const { |
| 120 | + return _oldest; |
| 121 | + } |
| 122 | + |
| 123 | + // returns the index for the ring buffer youngest data |
| 124 | + uint8_t get_youngest_index() const { |
| 125 | + return _youngest; |
| 126 | + } |
| 127 | + |
| 128 | +protected: |
| 129 | + const uint8_t elsize; |
| 130 | + void *buffer; |
| 131 | + uint8_t _size,_oldest,_youngest; |
| 132 | + bool _filled; |
| 133 | + |
| 134 | + void *get_offset(uint8_t idx) const; |
| 135 | +}; |
| 136 | + |
| 137 | +/* |
| 138 | + template class for more convenient type handling |
| 139 | + */ |
| 140 | +template <typename element_type> |
| 141 | +class EKF_IMU_buffer_t : ekf_imu_buffer |
| 142 | +{ |
| 143 | +public: |
| 144 | + EKF_IMU_buffer_t() : |
| 145 | + ekf_imu_buffer(sizeof(element_type)) |
| 146 | + {} |
| 147 | + |
| 148 | + bool init(uint8_t size) { |
| 149 | + return ekf_imu_buffer::init(size); |
| 150 | + } |
| 151 | + |
| 152 | + /* |
| 153 | + Writes data to a Ring buffer and advances indices that |
| 154 | + define the location of the newest and oldest data |
| 155 | + */ |
| 156 | + void push_youngest_element(element_type element) { |
| 157 | + return ekf_imu_buffer::push_youngest_element(&element); |
| 158 | + } |
| 159 | + |
| 160 | + // return true if the buffer has been filled at least once |
| 161 | + bool is_filled(void) const { |
| 162 | + return ekf_imu_buffer::is_filled(); |
| 163 | + } |
| 164 | + |
| 165 | + // retrieve the oldest data from the ring buffer tail |
| 166 | + element_type get_oldest_element() { |
| 167 | + element_type ret; |
| 168 | + ekf_imu_buffer::get_oldest_element(&ret); |
| 169 | + return ret; |
| 170 | + } |
| 171 | + |
| 172 | + // writes the same data to all elements in the ring buffer |
| 173 | + void reset_history(element_type element) { |
| 174 | + ekf_imu_buffer::reset_history(&element); |
| 175 | + } |
| 176 | + |
| 177 | + // zeroes all data in the ring buffer |
| 178 | + void reset() { |
| 179 | + ekf_imu_buffer::reset(); |
| 180 | + } |
| 181 | + |
| 182 | + // retrieves data from the ring buffer at a specified index |
| 183 | + element_type& operator[](uint32_t index) { |
| 184 | + element_type *ret = (element_type *)ekf_imu_buffer::get(index); |
| 185 | + return *ret; |
| 186 | + } |
| 187 | + |
| 188 | + // returns the index for the ring buffer oldest data |
| 189 | + uint8_t get_oldest_index() { |
| 190 | + return ekf_imu_buffer::get_oldest_index(); |
| 191 | + } |
| 192 | + |
| 193 | + // returns the index for the ring buffer youngest data |
| 194 | + inline uint8_t get_youngest_index() { |
| 195 | + return ekf_imu_buffer::get_youngest_index(); |
| 196 | + } |
| 197 | +}; |
0 commit comments