Paparazzi UAS  v5.15_devel-230-gc96ce27
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
opticflow_pmw3901.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) Tom van Dijk
3  *
4  * This file is part of paparazzi
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
27 
28 #include "subsystems/abi.h"
30 #include "generated/modules.h"
31 
32 #include "state.h"
33 
34 #include "std.h"
35 
36 #include <math.h>
37 
38 
39 #ifndef OPTICFLOW_PMW3901_SENSOR_ANGLE
40 #define OPTICFLOW_PMW3901_SENSOR_ANGLE 270 // [deg] Sensor rotation around body z axis (down). 0 rad = sensor x forward, y right.
41 #endif
42 
43 #ifndef OPTICFLOW_PMW3901_SUBPIXEL_FACTOR
44 #define OPTICFLOW_PMW3901_SUBPIXEL_FACTOR 100
45 #endif
46 
47 #ifndef OPTICFLOW_PMW3901_STD_PX
48 #define OPTICFLOW_PMW3901_STD_PX 50 // [px] standard deviation of flow measurement
49 #endif
50 
51 #ifndef OPTICFLOW_PMW3901_AGL_ID
52 #define OPTICFLOW_PMW3901_AGL_ID ABI_BROADCAST
53 #endif
54 PRINT_CONFIG_VAR(OPTICFLOW_PMW3901_AGL_ID)
55 
56 #ifndef OPTICFLOW_PMW3901_AGL_TIMEOUT_US
57 #define OPTICFLOW_PMW3901_AGL_TIMEOUT_US 500000
58 #endif
59 
60 
62 
64 static float agl_dist;
66 
67 
68 static void agl_cb(uint8_t sender_id, uint32_t stamp, float distance) {
69  (void) sender_id;
70  agl_dist = distance;
71  agl_ts = stamp;
72 }
73 
74 static bool agl_valid(uint32_t at_ts) {
75  return \
76  ((at_ts - agl_ts) < of_pmw.agl_timeout) &&
77  (agl_dist > 0.080);
78 }
79 
80 
81 static void opticflow_pmw3901_publish(int16_t delta_x, int16_t delta_y, uint32_t ts_usec) {
82  /* Prepare message variables */
83  // Time
84  static uint32_t prev_ts_usec = 0;
85  float dt = (ts_usec - prev_ts_usec) / 1.0e6;
86  if (prev_ts_usec == 0) {
87  dt = OPTICFLOW_PMW3901_PERIODIC_PERIOD;
88  }
89  prev_ts_usec = ts_usec;
90  // Sensor orientation
91  float c = cosf(RadOfDeg(of_pmw.sensor_angle));
92  float s = sinf(RadOfDeg(of_pmw.sensor_angle));
93  // Flow [px/s] (body-frame)
94  float flow_x = (c * delta_x - s * delta_y) / dt;
95  float flow_y = (s * delta_x + c * delta_y) / dt;
96  int16_t flow_x_subpix = (int16_t)(of_pmw.subpixel_factor * flow_x);
97  int16_t flow_y_subpix = (int16_t)(of_pmw.subpixel_factor * flow_y);
98  // Derotated flow [px/s] (body-frame)
99  struct FloatRates *rates = stateGetBodyRates_f();
100  float flow_dy_p = rates->p / of_pmw.pmw.rad_per_px;
101  float flow_dx_q = -rates->q / of_pmw.pmw.rad_per_px;
102  float flow_der_x = flow_x - flow_dx_q;
103  float flow_der_y = flow_y - flow_dy_p;
104  int16_t flow_der_x_subpix = (int16_t)(of_pmw.subpixel_factor * flow_der_x);
105  int16_t flow_der_y_subpix = (int16_t)(of_pmw.subpixel_factor * flow_der_y);
106  // Velocity
107  static float vel_x = 0; // static: keep last measurement for telemetry if agl not valid
108  static float vel_y = 0;
109  static float noise = 0;
110  if (agl_valid(ts_usec)) {
111  vel_x = -flow_der_x * of_pmw.pmw.rad_per_px * agl_dist;
112  vel_y = -flow_der_y * of_pmw.pmw.rad_per_px * agl_dist;
114  }
115 
116  /* Send ABI messages */
117  // Note: INS only subscribes to VELOCITY_ESTIMATE. OPTICAL_FLOW is only used
118  // for niche applications(?) and therefore only uses (sub)pixels without any
119  // camera intrinsics?? On the bright side, the sensor datasheet does not
120  // provide any intrinsics either.....
121  AbiSendMsgOPTICAL_FLOW(FLOW_OPTICFLOW_PMW3901_ID,
122  ts_usec, /* stamp [us] */
123  flow_x_subpix, /* flow_x [subpixels] */
124  flow_y_subpix, /* flow_y [subpixels] */
125  flow_der_x_subpix, /* flow_der_x [subpixels] */
126  flow_der_y_subpix, /* flow_der_y [subpixels] */
127  0.f, /* quality [???] */
128  0.f /* size_divergence [1/s] */
129  );
130  if (agl_valid(ts_usec)) {
131  AbiSendMsgVELOCITY_ESTIMATE(VEL_OPTICFLOW_PMW3901_ID,
132  ts_usec, /* stamp [us] */
133  vel_x, /* x [m/s] */
134  vel_y, /* y [m/s] */
135  0.f, /* z [m/s] */
136  noise, /* noise_x [m/s] */
137  noise, /* noise_y [m/s] */
138  -1.f /* noise_z [disabled] */
139  );
140  }
141 
142  /* Send telemetry */
143 #if SENSOR_SYNC_SEND_OPTICFLOW_PMW3901
144  float dummy_f = 0.f;
145  uint16_t dummy_u16 = 0;
146  float fps = 1.f / dt;
147  DOWNLINK_SEND_OPTIC_FLOW_EST(DefaultChannel, DefaultDevice,
148  &fps, /* fps */
149  &dummy_u16, /* corner_cnt */
150  &dummy_u16, /* tracked_cnt */
151  &flow_x_subpix, /* flow_x */
152  &flow_y_subpix, /* flow_y */
153  &flow_der_x_subpix, /* flow_der_x */
154  &flow_der_y_subpix, /* flow_der_y */
155  &vel_x, /* vel_x */
156  &vel_y, /* vel_y */
157  &dummy_f, /* vel_z */
158  &dummy_f, /* div_size */
159  &dummy_f, /* surface_roughness */
160  &dummy_f /* divergence */
161  );
162 #endif
163 }
164 
165 
167  pmw3901_init(&of_pmw.pmw, &OPTICFLOW_PMW3901_SPI_DEV, OPTICFLOW_PMW3901_SPI_SLAVE_IDX);
168  AbiBindMsgAGL(OPTICFLOW_PMW3901_AGL_ID, &agl_ev, agl_cb);
173 #ifdef OPTICFLOW_PMW3901_RAD_PER_PX
174  of_pmw.pmw.rad_per_px = OPTICFLOW_PMW3901_RAD_PER_PX;
175 #endif
176 }
177 
179  if (pmw3901_is_idle(&of_pmw.pmw)) {
181  }
182 }
183 
187  int16_t delta_x, delta_y;
188  pmw3901_get_data(&of_pmw.pmw, &delta_x, &delta_y);
189  opticflow_pmw3901_publish(delta_x, delta_y, get_sys_time_usec());
190  }
191 }
192 
193 
Event structure to store callbacks in a linked list.
Definition: abi_common.h:65
unsigned short uint16_t
Definition: types.h:16
static uint32_t agl_ts
static uint32_t s
static bool agl_valid(uint32_t at_ts)
bool pmw3901_get_data(struct pmw3901_t *pmw, int16_t *delta_x, int16_t *delta_y)
Definition: pmw3901.c:326
Main include for ABI (AirBorneInterface).
bool pmw3901_data_available(struct pmw3901_t *pmw)
Definition: pmw3901.c:322
float q
in rad/s
float p
in rad/s
#define VEL_OPTICFLOW_PMW3901_ID
static void agl_cb(uint8_t sender_id, uint32_t stamp, float distance)
void opticflow_pmw3901_init(void)
void pmw3901_init(struct pmw3901_t *pmw, struct spi_periph *periph, uint8_t slave_idx)
Definition: pmw3901.c:249
#define OPTICFLOW_PMW3901_AGL_ID
unsigned long uint32_t
Definition: types.h:18
void opticflow_pmw3901_event(void)
signed short int16_t
Definition: types.h:17
void pmw3901_event(struct pmw3901_t *pmw)
Definition: pmw3901.c:274
struct opticflow_pmw3901_t of_pmw
void pmw3901_start_read(struct pmw3901_t *pmw)
Definition: pmw3901.c:316
bool pmw3901_is_idle(struct pmw3901_t *pmw)
Definition: pmw3901.c:312
static void opticflow_pmw3901_publish(int16_t delta_x, int16_t delta_y, uint32_t ts_usec)
#define OPTICFLOW_PMW3901_STD_PX
static struct FloatRates * stateGetBodyRates_f(void)
Get vehicle body angular rate (float).
Definition: state.h:1200
static float agl_dist
float rad_per_px
Definition: pmw3901.h:71
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
void opticflow_pmw3901_periodic(void)
struct pmw3901_t pmw
#define OPTICFLOW_PMW3901_SUBPIXEL_FACTOR
#define OPTICFLOW_PMW3901_AGL_TIMEOUT_US
abi_event agl_ev
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
angular rates
#define FLOW_OPTICFLOW_PMW3901_ID
#define OPTICFLOW_PMW3901_SENSOR_ANGLE