altb_pmic / Mbed 2 deprecated Test_optical_flow_PX4

Dependencies:   mbed

Committer:
pmic
Date:
Sun Apr 05 08:47:25 2020 +0000
Revision:
12:19fe4f6a8b6b
Parent:
11:d367224f2194
Child:
13:89b65bfe6dda
Commit before implementing driver update (reduce ammount of data transmitted). i2c driver has to be updated on both software sides (F446RE and px4flow)!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
altb2 0:4b02060af95b 1 #ifndef _PX4FLOW_MBED_H
altb2 0:4b02060af95b 2 #define _PX4FLOW_MBED_H
altb2 0:4b02060af95b 3
altb2 0:4b02060af95b 4 # include <stdint.h>
altb2 0:4b02060af95b 5 # include "mbed.h"
altb2 0:4b02060af95b 6
altb2 0:4b02060af95b 7 // 7 Bit I2C Address of the Flow Module: Default 0x42 (user selectable bits 0,1,2)
altb2 0:4b02060af95b 8 #define PX4FLOW_ADDRESS 0x42<<1
altb2 0:4b02060af95b 9
altb2 0:4b02060af95b 10 // Commands
altb2 0:4b02060af95b 11 #define FRAME 0x00
altb2 0:4b02060af95b 12 #define INTEGRAL_FRAME 0x16
altb2 0:4b02060af95b 13
altb2 0:4b02060af95b 14 // define buffer indicees
altb2 0:4b02060af95b 15 // simple frame
altb2 0:4b02060af95b 16 #define FRAME_COUNT 0
altb2 0:4b02060af95b 17 #define PIXEL_FLOW_X_SUM 2
altb2 0:4b02060af95b 18 #define PIXEL_FLOW_Y_SUM 4
altb2 0:4b02060af95b 19 #define FLOW_COMP_M_X 6
altb2 0:4b02060af95b 20 #define FLOW_COMP_M_Y 8
altb2 0:4b02060af95b 21 #define QUAL 10
altb2 0:4b02060af95b 22 #define GYRO_X_RATE 12
altb2 0:4b02060af95b 23 #define GYRO_Y_RATE 14
altb2 0:4b02060af95b 24 #define GYRO_Z_RATE 16
altb2 0:4b02060af95b 25 #define GYRO_RANGE 18
altb2 0:4b02060af95b 26 #define SONAR_TIMESTAMP 19
altb2 0:4b02060af95b 27 #define GROUND_DISTANCE 20
altb2 0:4b02060af95b 28
altb2 0:4b02060af95b 29 // integral frame
altb2 0:4b02060af95b 30 #define FRAME_COUNT_SINCE_LAST_READOUT 0
altb2 0:4b02060af95b 31 #define PIXEL_FLOW_X_INTEGRAL 2
altb2 0:4b02060af95b 32 #define PIXEL_FLOW_Y_INTEGRAL 4
altb2 0:4b02060af95b 33 #define GYRO_X_RATE_INTEGRAL 6
altb2 0:4b02060af95b 34 #define GYRO_Y_RATE_INTEGRAL 8
altb2 0:4b02060af95b 35 #define GYRO_Z_RATE_INTEGRAL 10
altb2 0:4b02060af95b 36 #define INTEGRATION_TIMESPAN 12
altb2 0:4b02060af95b 37 #define SONAR_TIMESTAMP_INTEGRAL 16
altb2 0:4b02060af95b 38 #define GROUND_DISTANCE_INTEGRAL 20
altb2 0:4b02060af95b 39 #define GYRO_TEMPERATURE 22
altb2 0:4b02060af95b 40 #define QUALITY_INTEGRAL 24
altb2 0:4b02060af95b 41
pmic 9:be40afb750d3 42 // valid with original and modified firmware, but different scaling with modified firmware, see Git-branch IndNav of the Firmware itselt, pmic 12.09.2019
altb2 1:562a583eb77c 43 typedef struct i2c_frame {
altb2 0:4b02060af95b 44 uint16_t frame_count; // counts created I2C frames 0
altb2 0:4b02060af95b 45 int16_t pixel_flow_x_sum; // accumulated x flow in pixels*10 since last I2C frame 2
altb2 0:4b02060af95b 46 int16_t pixel_flow_y_sum; // accumulated y flow in pixels*10 since last I2C frame 4
altb2 0:4b02060af95b 47 int16_t flow_comp_m_x; // x velocity*1000 in meters / timestep 6
altb2 0:4b02060af95b 48 int16_t flow_comp_m_y; // y velocity*1000 in meters / timestep 8
altb2 0:4b02060af95b 49 int16_t qual; // Optical flow quality / confidence 0: bad, 255: maximum quality 10
pmic 3:e03714326b83 50 int16_t gyro_x_rate; // gyro x rate 12
pmic 3:e03714326b83 51 int16_t gyro_y_rate; // gyro y rate 14
pmic 3:e03714326b83 52 int16_t gyro_z_rate; // gyro z rate 16
altb2 0:4b02060af95b 53 uint8_t gyro_range; // gyro range 18
altb2 0:4b02060af95b 54 uint8_t sonar_timestamp; // timestep in milliseconds between I2C frames 19
altb2 0:4b02060af95b 55 int16_t ground_distance; // Ground distance in meters*1000. Positive value: distance known. Negative value: Unknown distance 20
altb2 0:4b02060af95b 56 } i2c_frame;
altb2 0:4b02060af95b 57
pmic 9:be40afb750d3 58 // only valid with original firmware, pmic 12.09.2019
altb2 0:4b02060af95b 59 typedef struct i2c_integral_frame {
pmic 3:e03714326b83 60 uint16_t frame_count_since_last_readout; // number of flow measurements since last I2C readout [#frames] 22
pmic 3:e03714326b83 61 int16_t pixel_flow_x_integral; // accumulated flow in radians*10000 around x axis since last I2C readout [rad*10000] 24
pmic 3:e03714326b83 62 int16_t pixel_flow_y_integral; // accumulated flow in radians*10000 around y axis since last I2C readout [rad*10000] 26
pmic 3:e03714326b83 63 int16_t gyro_x_rate_integral; // accumulated gyro x rates in radians*10000 since last I2C readout [rad*10000] 28
pmic 3:e03714326b83 64 int16_t gyro_y_rate_integral; // accumulated gyro y rates in radians*10000 since last I2C readout [rad*10000] 30
pmic 3:e03714326b83 65 int16_t gyro_z_rate_integral; // accumulated gyro z rates in radians*10000 since last I2C readout [rad*10000] 32
pmic 3:e03714326b83 66 uint32_t integration_timespan; // accumulation timespan in microseconds since last I2C readout [microseconds] 34
pmic 3:e03714326b83 67 uint32_t sonar_timestamp; // time since last sonar update [microseconds] 38
pmic 3:e03714326b83 68 int16_t ground_distance; // Ground distance in meters*1000 [meters*1000] 42
pmic 3:e03714326b83 69 int16_t gyro_temperature; // Temperature * 100 in centi-degrees Celsius [degcelsius*100] 44
pmic 3:e03714326b83 70 uint8_t quality; // averaged quality of accumulated flow values [0:bad quality;255: max quality] 46
altb2 0:4b02060af95b 71 } i2c_integral_frame;
altb2 0:4b02060af95b 72
altb2 0:4b02060af95b 73
altb2 0:4b02060af95b 74 class PX4Flow
altb2 0:4b02060af95b 75 {
altb2 0:4b02060af95b 76 public:
altb2 0:4b02060af95b 77 // Constructor
pmic 11:d367224f2194 78 PX4Flow(I2C& i2c);
pmic 3:e03714326b83 79
altb2 0:4b02060af95b 80 // Deconstructor
altb2 0:4b02060af95b 81 virtual ~PX4Flow();
altb2 0:4b02060af95b 82
altb2 0:4b02060af95b 83 // Methods
altb2 0:4b02060af95b 84 bool update();
altb2 0:4b02060af95b 85 bool update_integral();
altb2 0:4b02060af95b 86
pmic 9:be40afb750d3 87 // Simple frame, valid with original and modified firmware, but different scaling with modified firmware, see Git-branch IndNav of the Firmware itselt, pmic 12.09.2019
altb2 0:4b02060af95b 88 uint16_t frame_count();
altb2 0:4b02060af95b 89 int16_t pixel_flow_x_sum();
altb2 0:4b02060af95b 90 int16_t pixel_flow_y_sum();
altb2 0:4b02060af95b 91 int16_t flow_comp_m_x();
altb2 0:4b02060af95b 92 int16_t flow_comp_m_y();
altb2 0:4b02060af95b 93 int16_t qual();
altb2 0:4b02060af95b 94 int16_t gyro_x_rate();
altb2 0:4b02060af95b 95 int16_t gyro_y_rate();
altb2 0:4b02060af95b 96 int16_t gyro_z_rate();
altb2 0:4b02060af95b 97 uint8_t gyro_range();
altb2 0:4b02060af95b 98 uint8_t sonar_timestamp();
altb2 0:4b02060af95b 99 int16_t ground_distance();
altb2 0:4b02060af95b 100
pmic 9:be40afb750d3 101 // Integral frame, only valid with original firmware, pmic 12.09.2019
altb2 0:4b02060af95b 102 uint16_t frame_count_since_last_readout();
altb2 0:4b02060af95b 103 int16_t pixel_flow_x_integral();
altb2 0:4b02060af95b 104 int16_t pixel_flow_y_integral();
altb2 0:4b02060af95b 105 int16_t gyro_x_rate_integral();
altb2 0:4b02060af95b 106 int16_t gyro_y_rate_integral();
altb2 0:4b02060af95b 107 int16_t gyro_z_rate_integral();
altb2 0:4b02060af95b 108 uint32_t integration_timespan();
altb2 0:4b02060af95b 109 uint32_t sonar_timestamp_integral();
altb2 0:4b02060af95b 110 int16_t ground_distance_integral();
altb2 0:4b02060af95b 111 int16_t gyro_temperature();
altb2 0:4b02060af95b 112 uint8_t quality_integral();
altb2 0:4b02060af95b 113
pmic 9:be40afb750d3 114 /* IndNav Git-branch modifications, start here */
pmic 9:be40afb750d3 115 // Integral frame, only valid with modified firmware, see Git-branch IndNav of the Firmware itselt, pmic 12.09.2019
pmic 9:be40afb750d3 116 float avg_flowx(); // avg flow x in mm/s
pmic 9:be40afb750d3 117 float avg_flowy(); // avg flow y in mm/s
pmic 9:be40afb750d3 118 uint8_t valid_frame_count(); // nr. of valid frames (qual > 0) between i2c readings
pmic 9:be40afb750d3 119 uint8_t all_frame_count(); // nr. of frames between i2c readings
pmic 9:be40afb750d3 120 float update_fs(); // i2c averaging update rate in Hz
pmic 9:be40afb750d3 121 float readout_fs(); // i2c readout update rate in Hz
pmic 9:be40afb750d3 122 uint8_t avg_quality(); // avg 0-255 linear quality measurement 0=bad, 255=best
pmic 9:be40afb750d3 123 float int_timespan(); // integration timespan in ms, now you can integrate flow values, if valid...
pmic 9:be40afb750d3 124 float avg_gyro_x(); // avg gyro x in rad/s
pmic 9:be40afb750d3 125 float avg_gyro_y(); // avg gyro y in rad/s
pmic 9:be40afb750d3 126 float avg_gyro_z(); // avg gyro z in rad/s
pmic 9:be40afb750d3 127 uint8_t avg_scaled_quality(); // avg 0-255 linear quality measurement 0=bad, 255=best, scaled with N_valid/N_frames
pmic 9:be40afb750d3 128 /* IndNav Git-branch modifications, end here */
pmic 12:19fe4f6a8b6b 129 private:
pmic 12:19fe4f6a8b6b 130 DigitalOut dout1;
altb2 0:4b02060af95b 131
altb2 0:4b02060af95b 132 protected:
altb2 0:4b02060af95b 133 I2C i2c;
altb2 0:4b02060af95b 134
altb2 0:4b02060af95b 135 // Buffer to read in the sensordata
altb2 0:4b02060af95b 136 char bufferS[22];
altb2 0:4b02060af95b 137 char bufferI[26];
altb2 0:4b02060af95b 138
altb2 0:4b02060af95b 139 char i2c_commands[2];
altb2 0:4b02060af95b 140
altb2 0:4b02060af95b 141 struct i2c_frame frame;
altb2 0:4b02060af95b 142 struct i2c_integral_frame iframe;
altb2 0:4b02060af95b 143
altb2 0:4b02060af95b 144 uint8_t read8(char *buffer, const unsigned int& idx = 0);
altb2 0:4b02060af95b 145 uint16_t read16(char *buffer, const unsigned int& idx = 0);
altb2 0:4b02060af95b 146 uint32_t read32(char *buffer, const unsigned int& idx = 0);
altb2 0:4b02060af95b 147 };
altb2 0:4b02060af95b 148
altb2 0:4b02060af95b 149 #endif