AP_Compass_MMC3416.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /*
  16. Driver by Andrew Tridgell, Nov 2016
  17. */
  18. #include "AP_Compass_MMC3416.h"
  19. #include <AP_HAL/AP_HAL.h>
  20. #include <utility>
  21. #include <AP_Math/AP_Math.h>
  22. #include <stdio.h>
  23. #include <AP_Logger/AP_Logger.h>
  24. extern const AP_HAL::HAL &hal;
  25. #define REG_PRODUCT_ID 0x20
  26. #define REG_XOUT_L 0x00
  27. #define REG_STATUS 0x06
  28. #define REG_CONTROL0 0x07
  29. #define REG_CONTROL1 0x08
  30. // bits in REG_CONTROL0
  31. #define REG_CONTROL0_REFILL 0x80
  32. #define REG_CONTROL0_RESET 0x40
  33. #define REG_CONTROL0_SET 0x20
  34. #define REG_CONTROL0_NB 0x10
  35. #define REG_CONTROL0_TM 0x01
  36. // datasheet says 50ms min for refill
  37. #define MIN_DELAY_SET_RESET 50
  38. AP_Compass_Backend *AP_Compass_MMC3416::probe(AP_HAL::OwnPtr<AP_HAL::I2CDevice> dev,
  39. bool force_external,
  40. enum Rotation rotation)
  41. {
  42. if (!dev) {
  43. return nullptr;
  44. }
  45. AP_Compass_MMC3416 *sensor = new AP_Compass_MMC3416(std::move(dev), force_external, rotation);
  46. if (!sensor || !sensor->init()) {
  47. delete sensor;
  48. return nullptr;
  49. }
  50. return sensor;
  51. }
  52. AP_Compass_MMC3416::AP_Compass_MMC3416(AP_HAL::OwnPtr<AP_HAL::Device> _dev,
  53. bool _force_external,
  54. enum Rotation _rotation)
  55. : dev(std::move(_dev))
  56. , force_external(_force_external)
  57. , rotation(_rotation)
  58. {
  59. }
  60. bool AP_Compass_MMC3416::init()
  61. {
  62. if (!dev->get_semaphore()->take(0)) {
  63. return false;
  64. }
  65. dev->set_retries(10);
  66. uint8_t whoami;
  67. if (!dev->read_registers(REG_PRODUCT_ID, &whoami, 1) ||
  68. whoami != 0x06) {
  69. // not a MMC3416
  70. dev->get_semaphore()->give();
  71. return false;
  72. }
  73. // reset sensor
  74. dev->write_register(REG_CONTROL1, 0x80);
  75. hal.scheduler->delay(10);
  76. dev->write_register(REG_CONTROL0, 0x00); // single shot
  77. dev->write_register(REG_CONTROL1, 0x00); // 16 bit, 7.92ms
  78. dev->get_semaphore()->give();
  79. /* register the compass instance in the frontend */
  80. compass_instance = register_compass();
  81. printf("Found a MMC3416 on 0x%x as compass %u\n", dev->get_bus_id(), compass_instance);
  82. set_rotation(compass_instance, rotation);
  83. if (force_external) {
  84. set_external(compass_instance, true);
  85. }
  86. dev->set_device_type(DEVTYPE_MMC3416);
  87. set_dev_id(compass_instance, dev->get_bus_id());
  88. dev->set_retries(1);
  89. // call timer() at 100Hz
  90. dev->register_periodic_callback(10000,
  91. FUNCTOR_BIND_MEMBER(&AP_Compass_MMC3416::timer, void));
  92. // wait 250ms for the compass to make it's initial readings
  93. hal.scheduler->delay(250);
  94. return true;
  95. }
  96. void AP_Compass_MMC3416::timer()
  97. {
  98. const uint16_t measure_count_limit = 50;
  99. const uint16_t zero_offset = 32768; // 16 bit mode
  100. const uint16_t sensitivity = 2048; // counts per Gauss, 16 bit mode
  101. const float counts_to_milliGauss = 1.0e3f / sensitivity;
  102. uint32_t now = AP_HAL::millis();
  103. if (now - last_sample_ms > 500) {
  104. // seems to be stuck or on first sample, reset state machine
  105. state = STATE_REFILL1;
  106. last_sample_ms = now;
  107. }
  108. /*
  109. we use the SET/RESET method to remove bridge offset every
  110. measure_count_limit measurements. This involves a fairly complex
  111. state machine, but means we are much less sensitive to
  112. temperature changes
  113. */
  114. switch (state) {
  115. case STATE_REFILL1:
  116. if (dev->write_register(REG_CONTROL0, REG_CONTROL0_REFILL)) {
  117. state = STATE_REFILL1_WAIT;
  118. refill_start_ms = AP_HAL::millis();
  119. }
  120. break;
  121. case STATE_REFILL1_WAIT: {
  122. uint8_t status;
  123. if (AP_HAL::millis() - refill_start_ms > MIN_DELAY_SET_RESET &&
  124. dev->read_registers(REG_STATUS, &status, 1) &&
  125. (status & 0x02) == 0) {
  126. if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_SET) ||
  127. !dev->write_register(REG_CONTROL0, REG_CONTROL0_TM)) { // Take Measurement
  128. state = STATE_REFILL1;
  129. } else {
  130. state = STATE_MEASURE_WAIT1;
  131. }
  132. }
  133. break;
  134. }
  135. case STATE_MEASURE_WAIT1: {
  136. uint8_t status;
  137. if (dev->read_registers(REG_STATUS, &status, 1) && (status & 1)) {
  138. if (!dev->read_registers(REG_XOUT_L, (uint8_t *)&data0[0], 6)) {
  139. state = STATE_REFILL1;
  140. break;
  141. }
  142. if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_REFILL)) {
  143. state = STATE_REFILL1;
  144. } else {
  145. state = STATE_REFILL2_WAIT;
  146. refill_start_ms = AP_HAL::millis();
  147. }
  148. }
  149. break;
  150. }
  151. case STATE_REFILL2_WAIT: {
  152. uint8_t status;
  153. if (AP_HAL::millis() - refill_start_ms > MIN_DELAY_SET_RESET &&
  154. dev->read_registers(REG_STATUS, &status, 1) &&
  155. (status & 0x02) == 0) {
  156. if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_RESET) ||
  157. !dev->write_register(REG_CONTROL0, REG_CONTROL0_TM)) { // Take Measurement
  158. state = STATE_REFILL1;
  159. } else {
  160. state = STATE_MEASURE_WAIT2;
  161. }
  162. }
  163. break;
  164. }
  165. case STATE_MEASURE_WAIT2: {
  166. uint8_t status;
  167. if (!dev->read_registers(REG_STATUS, &status, 1) || !(status & 1)) {
  168. break;
  169. }
  170. uint16_t data1[3];
  171. if (!dev->read_registers(REG_XOUT_L, (uint8_t *)&data1[0], 6)) {
  172. state = STATE_REFILL1;
  173. break;
  174. }
  175. Vector3f field;
  176. /*
  177. calculate field and offset
  178. */
  179. Vector3f f1(float(data0[0]) - zero_offset,
  180. float(data0[1]) - zero_offset,
  181. float(data0[2]) - zero_offset);
  182. Vector3f f2(float(data1[0]) - zero_offset,
  183. float(data1[1]) - zero_offset,
  184. float(data1[2]) - zero_offset);
  185. field = (f1 - f2) * (counts_to_milliGauss / 2);
  186. Vector3f new_offset = (f1 + f2) * (counts_to_milliGauss / 2);
  187. if (!have_initial_offset) {
  188. offset = new_offset;
  189. have_initial_offset = true;
  190. } else {
  191. // low pass changes to the offset
  192. offset = offset * 0.95f + new_offset * 0.05f;
  193. }
  194. #if 0
  195. AP::logger().Write("MMO", "TimeUS,Nx,Ny,Nz,Ox,Oy,Oz", "Qffffff",
  196. AP_HAL::micros64(),
  197. (double)new_offset.x,
  198. (double)new_offset.y,
  199. (double)new_offset.z,
  200. (double)offset.x,
  201. (double)offset.y,
  202. (double)offset.z);
  203. printf("F(%.1f %.1f %.1f) O(%.1f %.1f %.1f)\n",
  204. field.x, field.y, field.z,
  205. offset.x, offset.y, offset.z);
  206. #endif
  207. last_sample_ms = AP_HAL::millis();
  208. accumulate_sample(field, compass_instance);
  209. if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_TM)) {
  210. state = STATE_REFILL1;
  211. } else {
  212. state = STATE_MEASURE_WAIT3;
  213. }
  214. break;
  215. }
  216. case STATE_MEASURE_WAIT3: {
  217. uint8_t status;
  218. if (!dev->read_registers(REG_STATUS, &status, 1) || !(status & 1)) {
  219. break;
  220. }
  221. uint16_t data1[3];
  222. if (!dev->read_registers(REG_XOUT_L, (uint8_t *)&data1[0], 6)) {
  223. state = STATE_REFILL1;
  224. break;
  225. }
  226. Vector3f field(float(data1[0]) - zero_offset,
  227. float(data1[1]) - zero_offset,
  228. float(data1[2]) - zero_offset);
  229. field *= -counts_to_milliGauss;
  230. field += offset;
  231. last_sample_ms = AP_HAL::millis();
  232. accumulate_sample(field, compass_instance);
  233. // we stay in STATE_MEASURE_WAIT3 for measure_count_limit cycles
  234. if (measure_count++ >= measure_count_limit) {
  235. measure_count = 0;
  236. state = STATE_REFILL1;
  237. } else {
  238. if (!dev->write_register(REG_CONTROL0, REG_CONTROL0_TM)) { // Take Measurement
  239. state = STATE_REFILL1;
  240. }
  241. }
  242. break;
  243. }
  244. }
  245. }
  246. void AP_Compass_MMC3416::read()
  247. {
  248. drain_accumulated_samples(compass_instance);
  249. }