AP_InertialSensor_BMI055.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * This file is free software: you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation, either version 3 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This file is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License along
  13. * with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <utility>
  16. #include <AP_HAL/AP_HAL.h>
  17. #include <AP_Math/AP_Math.h>
  18. #include "AP_InertialSensor_BMI055.h"
  19. /*
  20. device registers, names follow datasheet conventions, with REGA_
  21. prefix for accel, and REGG_ prefix for gyro
  22. */
  23. #define REGA_BGW_CHIPID 0x00
  24. #define REGA_ACCD_X_LSB 0x02
  25. #define REGA_ACCD_TEMP 0x08
  26. #define REGA_INT_STATUS_0 0x09
  27. #define REGA_INT_STATUS_1 0x0A
  28. #define REGA_INT_STATUS_2 0x0B
  29. #define REGA_INT_STATUS_3 0x0C
  30. #define REGA_FIFO_STATUS 0x0E
  31. #define REGA_PMU_RANGE 0x0F
  32. #define REGA_PMU_BW 0x10
  33. #define REGA_PMU_LPW 0x11
  34. #define REGA_ACCD_HBW 0x13
  35. #define REGA_BGW_SOFTRESET 0x14
  36. #define REGA_OUT_CTRL 0x20
  37. #define REGA_EST_LATCH 0x21
  38. #define REGA_FIFO_CONFIG_0 0x30
  39. #define REGA_PMU_SELF_TEST 0x32
  40. #define REGA_FIFO_CONFIG_1 0x3E
  41. #define REGA_FIFO_DATA 0x3F
  42. #define REGG_CHIPID 0x00
  43. #define REGA_RATE_X_LSB 0x02
  44. #define REGG_INT_STATUS_0 0x09
  45. #define REGG_INT_STATUS_1 0x0A
  46. #define REGG_INT_STATUS_2 0x0B
  47. #define REGG_INT_STATUS_3 0x0C
  48. #define REGG_FIFO_STATUS 0x0E
  49. #define REGG_RANGE 0x0F
  50. #define REGG_BW 0x10
  51. #define REGG_LPM1 0x11
  52. #define REGG_RATE_HBW 0x13
  53. #define REGG_BGW_SOFTRESET 0x14
  54. #define REGG_FIFO_CONFIG_1 0x3E
  55. #define REGG_FIFO_DATA 0x3F
  56. extern const AP_HAL::HAL& hal;
  57. #define int16_val(v, idx) ((int16_t)(((uint16_t)v[2*idx] << 8) | v[2*idx+1]))
  58. AP_InertialSensor_BMI055::AP_InertialSensor_BMI055(AP_InertialSensor &imu,
  59. AP_HAL::OwnPtr<AP_HAL::Device> _dev_accel,
  60. AP_HAL::OwnPtr<AP_HAL::Device> _dev_gyro,
  61. enum Rotation _rotation)
  62. : AP_InertialSensor_Backend(imu)
  63. , dev_accel(std::move(_dev_accel))
  64. , dev_gyro(std::move(_dev_gyro))
  65. , rotation(_rotation)
  66. {
  67. }
  68. AP_InertialSensor_Backend *
  69. AP_InertialSensor_BMI055::probe(AP_InertialSensor &imu,
  70. AP_HAL::OwnPtr<AP_HAL::SPIDevice> dev_accel,
  71. AP_HAL::OwnPtr<AP_HAL::SPIDevice> dev_gyro,
  72. enum Rotation rotation)
  73. {
  74. if (!dev_accel || !dev_gyro) {
  75. return nullptr;
  76. }
  77. auto sensor = new AP_InertialSensor_BMI055(imu, std::move(dev_accel), std::move(dev_gyro), rotation);
  78. if (!sensor) {
  79. return nullptr;
  80. }
  81. if (!sensor->init()) {
  82. delete sensor;
  83. return nullptr;
  84. }
  85. return sensor;
  86. }
  87. void AP_InertialSensor_BMI055::start()
  88. {
  89. accel_instance = _imu.register_accel(2000, dev_accel->get_bus_id_devtype(DEVTYPE_INS_BMI055));
  90. gyro_instance = _imu.register_gyro(2000, dev_gyro->get_bus_id_devtype(DEVTYPE_INS_BMI055));
  91. // setup sensor rotations from probe()
  92. set_gyro_orientation(gyro_instance, rotation);
  93. set_accel_orientation(accel_instance, rotation);
  94. // setup callbacks
  95. dev_accel->register_periodic_callback(1000,
  96. FUNCTOR_BIND_MEMBER(&AP_InertialSensor_BMI055::read_fifo_accel, void));
  97. dev_gyro->register_periodic_callback(1000,
  98. FUNCTOR_BIND_MEMBER(&AP_InertialSensor_BMI055::read_fifo_gyro, void));
  99. }
  100. /*
  101. probe and initialise accelerometer
  102. */
  103. bool AP_InertialSensor_BMI055::accel_init()
  104. {
  105. dev_accel->get_semaphore()->take_blocking();
  106. uint8_t v;
  107. if (!dev_accel->read_registers(REGA_BGW_CHIPID, &v, 1) || v != 0xFA) {
  108. goto failed;
  109. }
  110. if (!dev_accel->write_register(REGA_BGW_SOFTRESET, 0xB6)) {
  111. goto failed;
  112. }
  113. hal.scheduler->delay(10);
  114. dev_accel->setup_checked_registers(5, 20);
  115. // setup 16g range
  116. if (!dev_accel->write_register(REGA_PMU_RANGE, 0x0C, true)) {
  117. goto failed;
  118. }
  119. // setup filter bandwidth 1kHz
  120. if (!dev_accel->write_register(REGA_PMU_BW, 0x0F, true)) {
  121. goto failed;
  122. }
  123. // disable low-power mode
  124. if (!dev_accel->write_register(REGA_PMU_LPW, 0, true)) {
  125. goto failed;
  126. }
  127. // setup for unfiltered data
  128. if (!dev_accel->write_register(REGA_ACCD_HBW, 0x80, true)) {
  129. goto failed;
  130. }
  131. // setup FIFO for streaming X,Y,Z
  132. if (!dev_accel->write_register(REGA_FIFO_CONFIG_1, 0x80, true)) {
  133. goto failed;
  134. }
  135. hal.console->printf("BMI055: found accel\n");
  136. dev_accel->get_semaphore()->give();
  137. return true;
  138. failed:
  139. dev_accel->get_semaphore()->give();
  140. return false;
  141. }
  142. /*
  143. probe and initialise gyro
  144. */
  145. bool AP_InertialSensor_BMI055::gyro_init()
  146. {
  147. dev_gyro->get_semaphore()->take_blocking();
  148. uint8_t v;
  149. if (!dev_gyro->read_registers(REGG_CHIPID, &v, 1) || v != 0x0F) {
  150. goto failed;
  151. }
  152. if (!dev_gyro->write_register(REGG_BGW_SOFTRESET, 0xB6)) {
  153. goto failed;
  154. }
  155. hal.scheduler->delay(10);
  156. dev_gyro->setup_checked_registers(5, 20);
  157. // setup 2000dps range
  158. if (!dev_gyro->write_register(REGG_RANGE, 0x00, true)) {
  159. goto failed;
  160. }
  161. // setup filter bandwidth 230Hz, no decimation
  162. if (!dev_gyro->write_register(REGG_BW, 0x81, true)) {
  163. goto failed;
  164. }
  165. // disable low-power mode
  166. if (!dev_gyro->write_register(REGG_LPM1, 0, true)) {
  167. goto failed;
  168. }
  169. // setup for filtered data
  170. if (!dev_gyro->write_register(REGG_RATE_HBW, 0x00, true)) {
  171. goto failed;
  172. }
  173. // setup FIFO for streaming X,Y,Z
  174. if (!dev_gyro->write_register(REGG_FIFO_CONFIG_1, 0x80, true)) {
  175. goto failed;
  176. }
  177. hal.console->printf("BMI055: found gyro\n");
  178. dev_gyro->get_semaphore()->give();
  179. return true;
  180. failed:
  181. dev_gyro->get_semaphore()->give();
  182. return false;
  183. }
  184. bool AP_InertialSensor_BMI055::init()
  185. {
  186. dev_accel->set_read_flag(0x80);
  187. dev_gyro->set_read_flag(0x80);
  188. return accel_init() && gyro_init();
  189. }
  190. /*
  191. read accel fifo
  192. */
  193. void AP_InertialSensor_BMI055::read_fifo_accel(void)
  194. {
  195. uint8_t num_frames;
  196. if (!dev_accel->read_registers(REGA_FIFO_STATUS, &num_frames, 1)) {
  197. _inc_accel_error_count(accel_instance);
  198. return;
  199. }
  200. num_frames &= 0x7F;
  201. // don't read more than 8 frames at a time
  202. if (num_frames > 8) {
  203. num_frames = 8;
  204. }
  205. if (num_frames == 0) {
  206. return;
  207. }
  208. uint8_t data[6*num_frames];
  209. if (!dev_accel->read_registers(REGA_FIFO_DATA, data, num_frames*6)) {
  210. _inc_accel_error_count(accel_instance);
  211. return;
  212. }
  213. // data is 12 bits with 16g range, 7.81mg/LSB
  214. const float scale = 7.81 * 0.001 * GRAVITY_MSS / 16.0f;
  215. for (uint8_t i = 0; i < num_frames; i++) {
  216. const uint8_t *d = &data[i*6];
  217. int16_t xyz[3] {
  218. int16_t(uint16_t((d[0]&0xF0) | (d[1]<<8))),
  219. int16_t(uint16_t((d[2]&0xF0) | (d[3]<<8))),
  220. int16_t(uint16_t((d[4]&0xF0) | (d[5]<<8))) };
  221. Vector3f accel(xyz[0], xyz[1], xyz[2]);
  222. accel *= scale;
  223. _rotate_and_correct_accel(accel_instance, accel);
  224. _notify_new_accel_raw_sample(accel_instance, accel);
  225. }
  226. if (temperature_counter++ == 100) {
  227. temperature_counter = 0;
  228. int8_t t;
  229. if (!dev_accel->read_registers(REGA_ACCD_TEMP, (uint8_t *)&t, 1)) {
  230. _inc_accel_error_count(accel_instance);
  231. } else {
  232. float temp_degc = (0.5f * t) + 23.0f;
  233. _publish_temperature(accel_instance, temp_degc);
  234. }
  235. }
  236. if (!dev_accel->check_next_register()) {
  237. _inc_accel_error_count(accel_instance);
  238. }
  239. }
  240. /*
  241. read gyro fifo
  242. */
  243. void AP_InertialSensor_BMI055::read_fifo_gyro(void)
  244. {
  245. uint8_t num_frames;
  246. if (!dev_gyro->read_registers(REGG_FIFO_STATUS, &num_frames, 1)) {
  247. _inc_gyro_error_count(gyro_instance);
  248. return;
  249. }
  250. num_frames &= 0x7F;
  251. // don't read more than 8 frames at a time
  252. if (num_frames > 8) {
  253. num_frames = 8;
  254. }
  255. if (num_frames == 0) {
  256. return;
  257. }
  258. uint8_t data[6*num_frames];
  259. if (!dev_gyro->read_registers(REGG_FIFO_DATA, data, num_frames*6)) {
  260. _inc_gyro_error_count(gyro_instance);
  261. return;
  262. }
  263. // data is 16 bits with 2000dps range
  264. const float scale = radians(2000.0f) / 32767.0f;
  265. for (uint8_t i = 0; i < num_frames; i++) {
  266. const uint8_t *d = &data[i*6];
  267. int16_t xyz[3] {
  268. int16_t(uint16_t(d[0] | d[1]<<8)),
  269. int16_t(uint16_t(d[2] | d[3]<<8)),
  270. int16_t(uint16_t(d[4] | d[5]<<8)) };
  271. Vector3f gyro(xyz[0], xyz[1], xyz[2]);
  272. gyro *= scale;
  273. _rotate_and_correct_gyro(gyro_instance, gyro);
  274. _notify_new_gyro_raw_sample(gyro_instance, gyro);
  275. }
  276. if (!dev_gyro->check_next_register()) {
  277. _inc_gyro_error_count(gyro_instance);
  278. }
  279. }
  280. bool AP_InertialSensor_BMI055::update()
  281. {
  282. update_accel(accel_instance);
  283. update_gyro(gyro_instance);
  284. return true;
  285. }