AP_OpticalFlow_Pixart.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. /*
  14. driver for Pixart PMW3900DH optical flow sensor
  15. NOTE: This sensor does not use traditional SPI register access. The
  16. timing for register reads and writes is critical
  17. */
  18. #include <AP_HAL/AP_HAL.h>
  19. #include <AP_Math/crc.h>
  20. #include <AP_AHRS/AP_AHRS.h>
  21. #include <utility>
  22. #include "OpticalFlow.h"
  23. #include "AP_OpticalFlow_Pixart.h"
  24. #include "AP_OpticalFlow_Pixart_SROM.h"
  25. #include <stdio.h>
  26. #define debug(fmt, args ...) do {printf(fmt, ## args); } while(0)
  27. extern const AP_HAL::HAL& hal;
  28. #define PIXART_REG_PRODUCT_ID 0x00
  29. #define PIXART_REG_REVISION_ID 0x01
  30. #define PIXART_REG_MOTION 0x02
  31. #define PIXART_REG_DELTA_X_L 0x03
  32. #define PIXART_REG_DELTA_X_H 0x04
  33. #define PIXART_REG_DELTA_Y_L 0x05
  34. #define PIXART_REG_DELTA_Y_H 0x06
  35. #define PIXART_REG_SQUAL 0x07
  36. #define PIXART_REG_RAWDATA_SUM 0x08
  37. #define PIXART_REG_RAWDATA_MAX 0x09
  38. #define PIXART_REG_RAWDATA_MIN 0x0A
  39. #define PIXART_REG_SHUTTER_LOW 0x0B
  40. #define PIXART_REG_SHUTTER_HI 0x0C
  41. #define PIXART_REG_CONFIG1 0x0F
  42. #define PIXART_REG_CONFIG2 0x10
  43. #define PIXART_REG_FRAME_CAP 0x12
  44. #define PIXART_REG_SROM_EN 0x13
  45. #define PIXART_REG_RUN_DS 0x14
  46. #define PIXART_REG_REST1_RATE 0x15
  47. #define PIXART_REG_REST1_DS 0x16
  48. #define PIXART_REG_REST2_RATE 0x17
  49. #define PIXART_REG_REST2_DS 0x18
  50. #define PIXART_REG_REST3_RATE 0x19
  51. #define PIXART_REG_OBS 0x24
  52. #define PIXART_REG_DOUT_L 0x25
  53. #define PIXART_REG_DOUT_H 0x26
  54. #define PIXART_REG_RAW_GRAB 0x29
  55. #define PIXART_REG_SROM_ID 0x2A
  56. #define PIXART_REG_POWER_RST 0x3A
  57. #define PIXART_REG_SHUTDOWN 0x3B
  58. #define PIXART_REG_INV_PROD_ID 0x3F
  59. #define PIXART_REG_INV_PROD_ID2 0x5F // for 3901
  60. #define PIXART_REG_MOT_BURST 0x50
  61. #define PIXART_REG_MOT_BURST2 0x16
  62. #define PIXART_REG_SROM_BURST 0x62
  63. #define PIXART_REG_RAW_BURST 0x64
  64. // writing to registers needs this flag
  65. #define PIXART_WRITE_FLAG 0x80
  66. // timings in microseconds
  67. #define PIXART_Tsrad 300
  68. // correct result for SROM CRC
  69. #define PIXART_SROM_CRC_RESULT 0xBEEF
  70. // constructor
  71. AP_OpticalFlow_Pixart::AP_OpticalFlow_Pixart(const char *devname, OpticalFlow &_frontend) :
  72. OpticalFlow_backend(_frontend)
  73. {
  74. _dev = std::move(hal.spi->get_device(devname));
  75. }
  76. // detect the device
  77. AP_OpticalFlow_Pixart *AP_OpticalFlow_Pixart::detect(const char *devname, OpticalFlow &_frontend)
  78. {
  79. AP_OpticalFlow_Pixart *sensor = new AP_OpticalFlow_Pixart(devname, _frontend);
  80. if (!sensor) {
  81. return nullptr;
  82. }
  83. if (!sensor->setup_sensor()) {
  84. delete sensor;
  85. return nullptr;
  86. }
  87. return sensor;
  88. }
  89. // setup the device
  90. bool AP_OpticalFlow_Pixart::setup_sensor(void)
  91. {
  92. if (!_dev) {
  93. return false;
  94. }
  95. WITH_SEMAPHORE(_dev->get_semaphore());
  96. uint8_t id;
  97. uint16_t crc;
  98. // power-up sequence
  99. reg_write(PIXART_REG_POWER_RST, 0x5A);
  100. hal.scheduler->delay(50);
  101. // check product ID
  102. uint8_t id1 = reg_read(PIXART_REG_PRODUCT_ID);
  103. uint8_t id2;
  104. if (id1 == 0x3f) {
  105. id2 = reg_read(PIXART_REG_INV_PROD_ID);
  106. } else {
  107. id2 = reg_read(PIXART_REG_INV_PROD_ID2);
  108. }
  109. debug("id1=0x%02x id2=0x%02x ~id1=0x%02x\n", id1, id2, uint8_t(~id1));
  110. if (id1 == 0x3F && id2 == uint8_t(~id1)) {
  111. model = PIXART_3900;
  112. } else if (id1 == 0x49 && id2 == uint8_t(~id1)) {
  113. model = PIXART_3901;
  114. } else {
  115. debug("Not a recognised device\n");
  116. return false;
  117. }
  118. if (model == PIXART_3900) {
  119. srom_download();
  120. id = reg_read(PIXART_REG_SROM_ID);
  121. if (id != srom_id) {
  122. debug("Pixart: bad SROM ID: 0x%02x\n", id);
  123. return false;
  124. }
  125. reg_write(PIXART_REG_SROM_EN, 0x15);
  126. hal.scheduler->delay(10);
  127. crc = reg_read16u(PIXART_REG_DOUT_L);
  128. if (crc != 0xBEEF) {
  129. debug("Pixart: bad SROM CRC: 0x%04x\n", crc);
  130. return false;
  131. }
  132. }
  133. if (model == PIXART_3900) {
  134. load_configuration(init_data_3900, ARRAY_SIZE(init_data_3900));
  135. } else {
  136. load_configuration(init_data_3901_1, ARRAY_SIZE(init_data_3901_1));
  137. hal.scheduler->delay(100);
  138. load_configuration(init_data_3901_2, ARRAY_SIZE(init_data_3901_2));
  139. }
  140. hal.scheduler->delay(50);
  141. debug("Pixart %s ready\n", model==PIXART_3900?"3900":"3901");
  142. integral.last_frame_us = AP_HAL::micros();
  143. _dev->register_periodic_callback(2000, FUNCTOR_BIND_MEMBER(&AP_OpticalFlow_Pixart::timer, void));
  144. return true;
  145. }
  146. // write an 8 bit register
  147. void AP_OpticalFlow_Pixart::reg_write(uint8_t reg, uint8_t value)
  148. {
  149. _dev->set_chip_select(true);
  150. reg |= PIXART_WRITE_FLAG;
  151. _dev->transfer(&reg, 1, nullptr, 0);
  152. hal.scheduler->delay_microseconds(PIXART_Tsrad);
  153. _dev->transfer(&value, 1, nullptr, 0);
  154. _dev->set_chip_select(false);
  155. hal.scheduler->delay_microseconds(120);
  156. }
  157. // read from an 8 bit register
  158. uint8_t AP_OpticalFlow_Pixart::reg_read(uint8_t reg)
  159. {
  160. uint8_t v = 0;
  161. _dev->set_chip_select(true);
  162. _dev->transfer(&reg, 1, nullptr, 0);
  163. hal.scheduler->delay_microseconds(35);
  164. _dev->transfer(nullptr, 0, &v, 1);
  165. _dev->set_chip_select(false);
  166. hal.scheduler->delay_microseconds(200);
  167. return v;
  168. }
  169. // read from a 16 bit unsigned register
  170. uint16_t AP_OpticalFlow_Pixart::reg_read16u(uint8_t reg)
  171. {
  172. uint16_t low = reg_read(reg);
  173. uint16_t high = reg_read(reg+1);
  174. return low | (high<<8);
  175. }
  176. // read from a 16 bit signed register
  177. int16_t AP_OpticalFlow_Pixart::reg_read16s(uint8_t reg)
  178. {
  179. return (int16_t)reg_read16u(reg);
  180. }
  181. void AP_OpticalFlow_Pixart::srom_download(void)
  182. {
  183. reg_write(0x39, 0x02);
  184. hal.scheduler->delay(1);
  185. reg_write(PIXART_REG_SROM_EN, 0x1D);
  186. hal.scheduler->delay(10);
  187. reg_write(PIXART_REG_SROM_EN, 0x18);
  188. if (!_dev->set_chip_select(true)) {
  189. debug("Failed to force CS\n");
  190. }
  191. hal.scheduler->delay_microseconds(1);
  192. uint8_t reg = PIXART_REG_SROM_BURST | PIXART_WRITE_FLAG;
  193. _dev->transfer(&reg, 1, nullptr, 0);
  194. for (uint16_t i = 0; i < ARRAY_SIZE(srom_data); i++) {
  195. hal.scheduler->delay_microseconds(15);
  196. _dev->transfer(&srom_data[i], 1, nullptr, 0);
  197. }
  198. hal.scheduler->delay_microseconds(125);
  199. if (!_dev->set_chip_select(false)) {
  200. debug("Failed to force CS off\n");
  201. }
  202. hal.scheduler->delay_microseconds(160);
  203. }
  204. void AP_OpticalFlow_Pixart::load_configuration(const RegData *init_data, uint16_t n)
  205. {
  206. for (uint16_t i = 0; i < n; i++) {
  207. // writing a config register can fail - retry up to 5 times
  208. for (uint8_t tries=0; tries<5; tries++) {
  209. reg_write(init_data[i].reg, init_data[i].value);
  210. uint8_t v = reg_read(init_data[i].reg);
  211. if (v == init_data[i].value) {
  212. break;
  213. }
  214. //debug("reg[%u:%02x] 0x%02x 0x%02x\n", (unsigned)i, (unsigned)init_data[i].reg, (unsigned)init_data[i].value, (unsigned)v);
  215. }
  216. }
  217. }
  218. void AP_OpticalFlow_Pixart::motion_burst(void)
  219. {
  220. uint8_t *b = (uint8_t *)&burst;
  221. burst.delta_x = 0;
  222. burst.delta_y = 0;
  223. _dev->set_chip_select(true);
  224. uint8_t reg = model==PIXART_3900?PIXART_REG_MOT_BURST:PIXART_REG_MOT_BURST2;
  225. _dev->transfer(&reg, 1, nullptr, 0);
  226. hal.scheduler->delay_microseconds(150);
  227. for (uint8_t i=0; i<sizeof(burst); i++) {
  228. _dev->transfer(nullptr, 0, &b[i], 1);
  229. if (i == 0 && (burst.motion & 0x80) == 0) {
  230. // no motion, save some bus bandwidth
  231. _dev->set_chip_select(false);
  232. return;
  233. }
  234. }
  235. _dev->set_chip_select(false);
  236. }
  237. void AP_OpticalFlow_Pixart::timer(void)
  238. {
  239. if (AP_HAL::micros() - last_burst_us < 500) {
  240. return;
  241. }
  242. motion_burst();
  243. last_burst_us = AP_HAL::micros();
  244. uint32_t dt_us = last_burst_us - integral.last_frame_us;
  245. float dt = dt_us * 1.0e-6;
  246. const Vector3f &gyro = AP::ahrs_navekf().get_gyro();
  247. {
  248. WITH_SEMAPHORE(_sem);
  249. integral.sum.x += burst.delta_x;
  250. integral.sum.y += burst.delta_y;
  251. integral.sum_us += dt_us;
  252. integral.last_frame_us = last_burst_us;
  253. integral.gyro += Vector2f(gyro.x, gyro.y) * dt;
  254. }
  255. #if 0
  256. static uint32_t last_print_ms;
  257. static int fd = -1;
  258. if (fd == -1) {
  259. fd = open("/dev/ttyACM0", O_WRONLY);
  260. }
  261. // used for debugging
  262. static int32_t sum_x;
  263. static int32_t sum_y;
  264. sum_x += burst.delta_x;
  265. sum_y += burst.delta_y;
  266. uint32_t now = AP_HAL::millis();
  267. if (now - last_print_ms >= 100 && (sum_x != 0 || sum_y != 0)) {
  268. last_print_ms = now;
  269. dprintf(fd, "Motion: %d %d obs:0x%02x squal:%u rds:%u maxr:%u minr:%u sup:%u slow:%u\n",
  270. (int)sum_x, (int)sum_y, (unsigned)burst.squal, (unsigned)burst.rawdata_sum, (unsigned)burst.max_raw,
  271. (unsigned)burst.max_raw, (unsigned)burst.min_raw, (unsigned)burst.shutter_upper, (unsigned)burst.shutter_lower);
  272. sum_x = sum_y = 0;
  273. }
  274. #endif
  275. }
  276. // update - read latest values from sensor and fill in x,y and totals.
  277. void AP_OpticalFlow_Pixart::update(void)
  278. {
  279. uint32_t now = AP_HAL::millis();
  280. if (now - last_update_ms < 100) {
  281. return;
  282. }
  283. last_update_ms = now;
  284. struct OpticalFlow::OpticalFlow_state state;
  285. state.surface_quality = burst.squal;
  286. if (integral.sum_us > 0) {
  287. WITH_SEMAPHORE(_sem);
  288. const Vector2f flowScaler = _flowScaler();
  289. float flowScaleFactorX = 1.0f + 0.001f * flowScaler.x;
  290. float flowScaleFactorY = 1.0f + 0.001f * flowScaler.y;
  291. float dt = integral.sum_us * 1.0e-6;
  292. state.flowRate = Vector2f(integral.sum.x * flowScaleFactorX,
  293. integral.sum.y * flowScaleFactorY);
  294. state.flowRate *= flow_pixel_scaling / dt;
  295. // we only apply yaw to flowRate as body rate comes from AHRS
  296. _applyYaw(state.flowRate);
  297. state.bodyRate = integral.gyro / dt;
  298. integral.sum.zero();
  299. integral.sum_us = 0;
  300. integral.gyro.zero();
  301. } else {
  302. state.flowRate.zero();
  303. state.bodyRate.zero();
  304. }
  305. // copy results to front end
  306. _update_frontend(state);
  307. }