Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 "modules/core/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
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  uint8_t dummy_u8 = 0;
147  float fps = 1.f / dt;
148  DOWNLINK_SEND_OPTIC_FLOW_EST(DefaultChannel, DefaultDevice,
149  &fps, /* fps */
150  &dummy_u16, /* corner_cnt */
151  &dummy_u16, /* tracked_cnt */
152  &flow_x_subpix, /* flow_x */
153  &flow_y_subpix, /* flow_y */
154  &flow_der_x_subpix, /* flow_der_x */
155  &flow_der_y_subpix, /* flow_der_y */
156  &vel_x, /* vel_x */
157  &vel_y, /* vel_y */
158  &dummy_f, /* vel_z */
159  &dummy_f, /* div_size */
160  &dummy_f, /* surface_roughness */
161  &dummy_f, /* divergence */
162  &dummy_u8 /* camera_id */
163  );
164 #endif
165 }
166 
167 
169  pmw3901_init(&of_pmw.pmw, &OPTICFLOW_PMW3901_SPI_DEV, OPTICFLOW_PMW3901_SPI_SLAVE_IDX);
170  AbiBindMsgAGL(OPTICFLOW_PMW3901_AGL_ID, &agl_ev, agl_cb);
175 #ifdef OPTICFLOW_PMW3901_RAD_PER_PX
176  of_pmw.pmw.rad_per_px = OPTICFLOW_PMW3901_RAD_PER_PX;
177 #endif
178 }
179 
181  if (pmw3901_is_idle(&of_pmw.pmw)) {
183  }
184 }
185 
189  int16_t delta_x, delta_y;
190  pmw3901_get_data(&of_pmw.pmw, &delta_x, &delta_y);
191  opticflow_pmw3901_publish(delta_x, delta_y, get_sys_time_usec());
192  }
193 }
194 
195 
Main include for ABI (AirBorneInterface).
Event structure to store callbacks in a linked list.
Definition: abi_common.h:67
#define FLOW_OPTICFLOW_PMW3901_ID
#define VEL_OPTICFLOW_PMW3901_ID
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
float q
in rad/s
float p
in rad/s
angular rates
static struct FloatRates * stateGetBodyRates_f(void)
Get vehicle body angular rate (float).
Definition: state.h:1367
static uint32_t s
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
#define OPTICFLOW_PMW3901_SENSOR_ANGLE
#define OPTICFLOW_PMW3901_SUBPIXEL_FACTOR
void opticflow_pmw3901_init(void)
struct opticflow_pmw3901_t of_pmw
#define OPTICFLOW_PMW3901_STD_PX
void opticflow_pmw3901_periodic(void)
static void opticflow_pmw3901_publish(int16_t delta_x, int16_t delta_y, uint32_t ts_usec)
static uint32_t agl_ts
static void agl_cb(uint8_t sender_id, uint32_t stamp, float distance)
static float agl_dist
#define OPTICFLOW_PMW3901_AGL_TIMEOUT_US
void opticflow_pmw3901_event(void)
static bool agl_valid(uint32_t at_ts)
#define OPTICFLOW_PMW3901_AGL_ID
abi_event agl_ev
struct pmw3901_t pmw
bool pmw3901_is_idle(struct pmw3901_t *pmw)
Definition: pmw3901.c:312
void pmw3901_start_read(struct pmw3901_t *pmw)
Definition: pmw3901.c:316
void pmw3901_init(struct pmw3901_t *pmw, struct spi_periph *periph, uint8_t slave_idx)
Definition: pmw3901.c:249
void pmw3901_event(struct pmw3901_t *pmw)
Definition: pmw3901.c:274
bool pmw3901_data_available(struct pmw3901_t *pmw)
Definition: pmw3901.c:322
bool pmw3901_get_data(struct pmw3901_t *pmw, int16_t *delta_x, int16_t *delta_y)
Definition: pmw3901.c:326
float rad_per_px
Definition: pmw3901.h:71
API to get/set the generic vehicle states.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
short int16_t
Typedef defining 16 bit short type.
Definition: vl53l1_types.h:93
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98