Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
opticflow_module.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Hann Woei Ho
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  */
20 
29 #include "opticflow_module.h"
30 
31 #include <stdio.h>
32 #include <pthread.h>
33 #include "state.h"
34 #include "subsystems/abi.h"
35 
36 #include "lib/v4l/v4l2.h"
37 #include "lib/encoding/jpeg.h"
38 #include "lib/encoding/rtp.h"
39 #include "errno.h"
40 
41 #include "cv.h"
42 
43 /* ABI messages sender ID */
44 #ifndef OPTICFLOW_AGL_ID
45 #define OPTICFLOW_AGL_ID ABI_BROADCAST
46 #endif
47 PRINT_CONFIG_VAR(OPTICFLOW_AGL_ID)
48 
49 #ifndef OPTICFLOW_IMU_ID
50 #define OPTICFLOW_IMU_ID ABI_BROADCAST
51 #endif
52 PRINT_CONFIG_VAR(OPTICFLOW_IMU_ID)
53 
54 #ifndef OPTICFLOW_BODY_TO_IMU_ID
55 #define OPTICFLOW_BODY_TO_IMU_ID ABI_BROADCAST
56 #endif
57 PRINT_CONFIG_VAR(OPTICFLOW_BODY_TO_IMU_ID)
58 
59 #ifndef OPTICFLOW_SEND_ABI_ID
60 #define OPTICFLOW_SEND_ABI_ID 1
61 #endif
62 PRINT_CONFIG_VAR(OPTICFLOW_SEND_ABI_ID)
63 
64 #ifndef OPTICFLOW_FPS
65 #define OPTICFLOW_FPS 0
66 #endif
67 PRINT_CONFIG_VAR(OPTICFLOW_FPS)
68 
69 /* The main opticflow variables */
76 
77 static bool opticflow_got_result;
78 static pthread_mutex_t opticflow_mutex;
79 
80 /* Static functions */
81 struct image_t *opticflow_module_calc(struct image_t *img);
82 static void opticflow_agl_cb(uint8_t sender_id, float distance);
83 static void opticflow_imu_accel_cb(uint8_t sender_id, uint32_t stamp,
84  struct Int32Vect3 *accel);
85 static void opticflow_body_to_imu_cb(uint8_t sender_id,
86  struct FloatQuat *q_b2i_f);
87 
88 #if PERIODIC_TELEMETRY
95 static void opticflow_telem_send(struct transport_tx *trans, struct link_device *dev)
96 {
97  pthread_mutex_lock(&opticflow_mutex);
98  if (opticflow_result.noise_measurement < 0.8) {
99  pprz_msg_send_OPTIC_FLOW_EST(trans, dev, AC_ID,
100  &opticflow_result.fps, &opticflow_result.corner_cnt,
101  &opticflow_result.tracked_cnt, &opticflow_result.flow_x,
102  &opticflow_result.flow_y, &opticflow_result.flow_der_x,
103  &opticflow_result.flow_der_y, &opticflow_result.vel_x,
104  &opticflow_result.vel_y, &opticflow_result.div_size,
105  &opticflow_result.surface_roughness, &opticflow_result.divergence); // TODO: no noise measurement here...
106  }
107  pthread_mutex_unlock(&opticflow_mutex);
108 }
109 #endif
110 
115 {
116  // Subscribe ABI messages
117  AbiBindMsgAGL(OPTICFLOW_AGL_ID, &opticflow_agl_ev, opticflow_agl_cb); // ABI to the altitude above ground level
118  AbiBindMsgIMU_ACCEL_INT32(OPTICFLOW_IMU_ID, &opticflow_imu_accel_ev,
119  &opticflow_imu_accel_cb); // ABI to the IMU accelerometer measurements
120  AbiBindMsgBODY_TO_IMU_QUAT(OPTICFLOW_BODY_TO_IMU_ID, &opticflow_body_to_imu_ev,
121  &opticflow_body_to_imu_cb); // ABI to the quaternion of body to imu
122 
123  // Set the opticflow state to 0
124  FLOAT_RATES_ZERO(opticflow_state.rates);
125  float_quat_identity(&opticflow_state.imu_to_body_quat);
126  INT_VECT3_ZERO(opticflow_state.accel_imu_meas);
127  opticflow_state.agl = 0;
128 
129  // Initialize the opticflow calculation
130  opticflow_got_result = false;
131  opticflow_calc_init(&opticflow);
132 
134 
135 #if PERIODIC_TELEMETRY
137 #endif
138 
139 }
140 
146 {
147  pthread_mutex_lock(&opticflow_mutex);
148  // Update the stabilization loops on the current calculation
149  if (opticflow_got_result) {
150  uint32_t now_ts = get_sys_time_usec();
151  AbiSendMsgOPTICAL_FLOW(OPTICFLOW_SEND_ABI_ID, now_ts,
152  opticflow_result.flow_x,
153  opticflow_result.flow_y,
154  opticflow_result.flow_der_x,
155  opticflow_result.flow_der_y,
156  opticflow_result.noise_measurement,
157  opticflow_result.div_size,
158  opticflow_state.agl);
159  //TODO Find an appropiate quality measure for the noise model in the state filter, for now it is tracked_cnt
160  if (opticflow_result.noise_measurement < 0.8) {
161  AbiSendMsgVELOCITY_ESTIMATE(OPTICFLOW_SEND_ABI_ID, now_ts,
162  opticflow_result.vel_body_x,
163  opticflow_result.vel_body_y,
164  0.0f,
165  opticflow_result.noise_measurement
166  );
167  }
168  opticflow_got_result = false;
169  }
170  pthread_mutex_unlock(&opticflow_mutex);
171 }
172 
181 {
182  // Copy the state
183  // TODO : put accelerometer values at pose of img timestamp
184  struct opticflow_state_t temp_state;
185  struct pose_t pose = get_rotation_at_timestamp(img->pprz_ts);
186  temp_state = opticflow_state;
187  temp_state.rates = pose.rates;
188 
189  // Do the optical flow calculation
190  static struct opticflow_result_t temp_result = {}; // static so that the number of corners is kept between frames
191  opticflow_calc_frame(&opticflow, &temp_state, img, &temp_result);
192 
193  // Copy the result if finished
194  pthread_mutex_lock(&opticflow_mutex);
195  opticflow_result = temp_result;
196  opticflow_got_result = true;
197 
198 
199  // release the mutex as we are done with editing the opticflow result
200  pthread_mutex_unlock(&opticflow_mutex);
201  return img;
202 }
203 
209 static void opticflow_agl_cb(uint8_t sender_id __attribute__((unused)), float distance)
210 {
211  // Update the distance if we got a valid measurement
212  if (distance > 0) {
213  opticflow_state.agl = distance;
214  }
215 }
216 
223 static void opticflow_imu_accel_cb(uint8_t sender_id __attribute__((unused)), uint32_t stamp, struct Int32Vect3 *accel)
224 {
225  opticflow_state.accel_imu_meas = *accel;
226 }
227 
233 static void opticflow_body_to_imu_cb(uint8_t sender_id __attribute__((unused)),
234  struct FloatQuat *q_b2i_f)
235 {
236  struct FloatQuat imu_to_body_quat_temp;
237  float_quat_invert(&imu_to_body_quat_temp, q_b2i_f); // invert quaternion for body-to-imu to imu-to-body
238  opticflow_state.imu_to_body_quat = imu_to_body_quat_temp;
239 }
Event structure to store callbacks in a linked list.
Definition: abi_common.h:65
#define OPTICFLOW_BODY_TO_IMU_ID
Default body to IMU to use in opticflow visual_estimator.
struct FloatRates rates
Definition: pose_history.h:34
struct opticflow_t opticflow
Opticflow calculations.
Periodic telemetry system header (includes downlink utility and generated code).
struct FloatRates rates
Body rates.
static bool opticflow_got_result
When we have an optical flow calculation.
static struct opticflow_state_t opticflow_state
State of the drone to communicate with the opticflow.
optical-flow calculation for Parrot Drones
static abi_event opticflow_imu_accel_ev
The altitude ABI event.
static abi_event opticflow_agl_ev
The accelerometers ABI event.
static void float_quat_identity(struct FloatQuat *q)
initialises a quaternion to identity
Capture images from a V4L2 device (Video for Linux 2)
Main include for ABI (AirBorneInterface).
Definition: image.h:43
struct pose_t get_rotation_at_timestamp(uint32_t timestamp)
Given a pprz timestamp in used (obtained with get_sys_time_usec) we return the pose in FloatEulers cl...
Definition: pose_history.c:53
#define FLOAT_RATES_ZERO(_r)
uint32_t pprz_ts
The timestamp in us since system startup.
Definition: image.h:49
static void opticflow_body_to_imu_cb(uint8_t sender_id, struct FloatQuat *q_b2i_f)
Callback function of imu to body.
struct video_listener * cv_add_to_device(struct video_config_t *device, cv_function func, uint16_t fps)
Definition: cv.c:46
void opticflow_module_init(void)
Initialize the optical flow module for the bottom camera.
struct image_t * opticflow_module_calc(struct image_t *img)
The main optical flow calculation thread.
Roation quaternion.
static pthread_mutex_t opticflow_mutex
Mutex lock fo thread safety.
void opticflow_calc_init(struct opticflow_t *opticflow)
Initialize the opticflow calculator.
#define OPTICFLOW_IMU_ID
Default IMU (accelerometers) to use in opticflow visual_estimator.
static void opticflow_imu_accel_cb(uint8_t sender_id, uint32_t stamp, struct Int32Vect3 *accel)
Callback function of the IMU's accelerometers.
unsigned long uint32_t
Definition: types.h:18
Computer vision framework for onboard processing.
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
static struct opticflow_result_t opticflow_result
The opticflow result.
static abi_event opticflow_body_to_imu_ev
The body-to-imu ABI event.
static void float_quat_invert(struct FloatQuat *qo, struct FloatQuat *qi)
Encode images with the use of the JPEG encoding.
#define OPTICFLOW_FPS
Default FPS (zero means run at camera fps)
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
#define INT_VECT3_ZERO(_v)
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
void opticflow_module_run(void)
Update the optical flow state for the calculation thread and update the stabilization loops with the ...
void opticflow_calc_frame(struct opticflow_t *opticflow, struct opticflow_state_t *cam_state, struct image_t *img, struct opticflow_result_t *result)
Run the optical flow on a new image frame.
Encodes a vide stream with RTP (JPEG)
#define OPTICFLOW_AGL_ID
Default sonar/agl to use in opticflow visual_estimator.
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
#define OPTICFLOW_SEND_ABI_ID
Default ID to send abi messages.
static void opticflow_agl_cb(uint8_t sender_id, float distance)
Callback function of the ground altitude.
static void opticflow_telem_send(struct transport_tx *trans, struct link_device *dev)
Send optical flow telemetry information.
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46