Library containing Crazyflie 2.0 sensors drivers: - LPS25H (barometer) - MPU9250 (IMU) - PMW3901 (optical flow) - VL53L0X (range)
Dependents: Drones-Controlador controladoatitude_cteste Drone_Controlador_Atitude optical_test
PMW3901/PMW3901.h@6:c7bc001826ba, 2018-05-08 (annotated)
- Committer:
- fbob
- Date:
- Tue May 08 13:35:42 2018 +0000
- Revision:
- 6:c7bc001826ba
- Parent:
- 5:1ef8b91a0318
- Child:
- 7:56608cce81ee
Updated optical flow library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fbob | 5:1ef8b91a0318 | 1 | #ifndef PMW3901_h |
fbob | 5:1ef8b91a0318 | 2 | #define PMW3901_h |
fbob | 5:1ef8b91a0318 | 3 | |
fbob | 5:1ef8b91a0318 | 4 | #include "mbed.h" |
fbob | 5:1ef8b91a0318 | 5 | |
fbob | 6:c7bc001826ba | 6 | // |
fbob | 6:c7bc001826ba | 7 | #define PRODUCT_ID 0x00 |
fbob | 6:c7bc001826ba | 8 | // |
fbob | 5:1ef8b91a0318 | 9 | #define MOTION 0x02 |
fbob | 5:1ef8b91a0318 | 10 | // Accelerometer output register addresses |
fbob | 5:1ef8b91a0318 | 11 | #define DELTA_X_L 0x03 |
fbob | 5:1ef8b91a0318 | 12 | #define DELTA_X_H 0x04 |
fbob | 5:1ef8b91a0318 | 13 | #define DELTA_Y_L 0x05 |
fbob | 5:1ef8b91a0318 | 14 | #define DELTA_Y_H 0x06 |
fbob | 5:1ef8b91a0318 | 15 | |
fbob | 5:1ef8b91a0318 | 16 | /** PMW3901 (optical flow sensor) class |
fbob | 5:1ef8b91a0318 | 17 | * |
fbob | 5:1ef8b91a0318 | 18 | * Example code (print optical flow data on serial port every 1 second): |
fbob | 5:1ef8b91a0318 | 19 | * @code |
fbob | 5:1ef8b91a0318 | 20 | * #include "mbed.h" |
fbob | 5:1ef8b91a0318 | 21 | * #include "USBSerial.h" |
fbob | 5:1ef8b91a0318 | 22 | * #include "PMW3901.h" |
fbob | 5:1ef8b91a0318 | 23 | * |
fbob | 6:c7bc001826ba | 24 | * // Declare serial object |
fbob | 5:1ef8b91a0318 | 25 | * USBSerial pc; |
fbob | 6:c7bc001826ba | 26 | * // Declare sensor object with correpondent SPI pins (miso, mosi, sclk, csel) |
fbob | 5:1ef8b91a0318 | 27 | * PMW3901 flow(PA_7,PA_6,PA_5,PB_4); |
fbob | 5:1ef8b91a0318 | 28 | * |
fbob | 6:c7bc001826ba | 29 | * // Main program |
fbob | 5:1ef8b91a0318 | 30 | * int main() |
fbob | 5:1ef8b91a0318 | 31 | * { |
fbob | 6:c7bc001826ba | 32 | * // Initialize sensor |
fbob | 5:1ef8b91a0318 | 33 | * flow.init(); |
fbob | 6:c7bc001826ba | 34 | * // Infinite loop |
fbob | 5:1ef8b91a0318 | 35 | * while(1); |
fbob | 5:1ef8b91a0318 | 36 | * { |
fbob | 6:c7bc001826ba | 37 | * // Read data from sensor |
fbob | 5:1ef8b91a0318 | 38 | * flow.read(); |
fbob | 6:c7bc001826ba | 39 | * // Print data from sensor |
fbob | 6:c7bc001826ba | 40 | * pc.printf("Optical flow []: %6.2f %6.2f \n\n", flow.x, flow.y); |
fbob | 6:c7bc001826ba | 41 | * // Wait 0.5 seconds |
fbob | 6:c7bc001826ba | 42 | * wait(0.5); |
fbob | 5:1ef8b91a0318 | 43 | * } |
fbob | 5:1ef8b91a0318 | 44 | * } |
fbob | 5:1ef8b91a0318 | 45 | * @endcode |
fbob | 5:1ef8b91a0318 | 46 | * (Need to target to NUCLEO-F401RE board platform) |
fbob | 5:1ef8b91a0318 | 47 | */ |
fbob | 5:1ef8b91a0318 | 48 | class PMW3901 |
fbob | 5:1ef8b91a0318 | 49 | { |
fbob | 5:1ef8b91a0318 | 50 | public: |
fbob | 5:1ef8b91a0318 | 51 | /** Class constructor */ |
fbob | 5:1ef8b91a0318 | 52 | PMW3901(PinName mosi, PinName miso, PinName sclk, PinName csel); |
fbob | 5:1ef8b91a0318 | 53 | |
fbob | 5:1ef8b91a0318 | 54 | /** Initialize optical flow */ |
fbob | 6:c7bc001826ba | 55 | bool init(); |
fbob | 5:1ef8b91a0318 | 56 | /** **/ |
fbob | 5:1ef8b91a0318 | 57 | void read(); |
fbob | 5:1ef8b91a0318 | 58 | |
fbob | 6:c7bc001826ba | 59 | /** Optical flow data in x-axis (px) **/ |
fbob | 6:c7bc001826ba | 60 | float x; |
fbob | 6:c7bc001826ba | 61 | /** Optical flow data in y-axis (px) **/ |
fbob | 6:c7bc001826ba | 62 | float y; |
fbob | 5:1ef8b91a0318 | 63 | private: |
fbob | 5:1ef8b91a0318 | 64 | /** SPI bus */ |
fbob | 5:1ef8b91a0318 | 65 | SPI spi; |
fbob | 5:1ef8b91a0318 | 66 | /** Chip select */ |
fbob | 5:1ef8b91a0318 | 67 | DigitalOut cs; |
fbob | 5:1ef8b91a0318 | 68 | |
fbob | 6:c7bc001826ba | 69 | /** **/ |
fbob | 6:c7bc001826ba | 70 | void setup_spi(); |
fbob | 6:c7bc001826ba | 71 | /** **/ |
fbob | 6:c7bc001826ba | 72 | bool test_spi(); |
fbob | 5:1ef8b91a0318 | 73 | /** **/ |
fbob | 5:1ef8b91a0318 | 74 | void setup_flow(); |
fbob | 5:1ef8b91a0318 | 75 | /** **/ |
fbob | 5:1ef8b91a0318 | 76 | void read_flow(); |
fbob | 5:1ef8b91a0318 | 77 | }; |
fbob | 5:1ef8b91a0318 | 78 | |
fbob | 5:1ef8b91a0318 | 79 | #endif |