Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
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 /* Default sonar/agl to use in opticflow visual_estimator */
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_SENDER_ID
50 #define OPTICFLOW_SENDER_ID 1
51 #endif
52 
53 /* The main opticflow variables */
58 static bool opticflow_got_result;
59 static pthread_mutex_t opticflow_mutex;
60 
61 /* Static functions */
62 struct image_t *opticflow_module_calc(struct image_t *img);
63 static void opticflow_agl_cb(uint8_t sender_id, float distance);
64 
65 #if PERIODIC_TELEMETRY
72 static void opticflow_telem_send(struct transport_tx *trans, struct link_device *dev)
73 {
74  pthread_mutex_lock(&opticflow_mutex);
76  pprz_msg_send_OPTIC_FLOW_EST(trans, dev, AC_ID,
82  &opticflow_result.surface_roughness, &opticflow_result.divergence); // TODO: no noise measurement here...
83  }
84  pthread_mutex_unlock(&opticflow_mutex);
85 }
86 #endif
87 
92 {
93  // Subscribe to the altitude above ground level ABI messages
94  AbiBindMsgAGL(OPTICFLOW_AGL_ID, &opticflow_agl_ev, opticflow_agl_cb);
95 
96  // Set the opticflow state to 0
98  opticflow_state.agl = 0;
99 
100  // Initialize the opticflow calculation
101  opticflow_got_result = false;
102 
103  cv_add_to_device(&OPTICFLOW_CAMERA, opticflow_module_calc);
104 
105 #if PERIODIC_TELEMETRY
107 #endif
108 
109 }
110 
116 {
117  pthread_mutex_lock(&opticflow_mutex);
118  // Update the stabilization loops on the current calculation
119  if (opticflow_got_result) {
120  uint32_t now_ts = get_sys_time_usec();
121  AbiSendMsgOPTICAL_FLOW(OPTICFLOW_SENDER_ID, now_ts,
126  opticflow_result.noise_measurement,// FIXME, scale to some quality measure 0-255
129  //TODO Find an appropiate quality measure for the noise model in the state filter, for now it is tracked_cnt
131  AbiSendMsgVELOCITY_ESTIMATE(OPTICFLOW_SENDER_ID, now_ts,
134  0.0f,
136  );
137  }
138  opticflow_got_result = false;
139  }
140  pthread_mutex_unlock(&opticflow_mutex);
141 }
142 
151 {
152  // Copy the state
153  struct pose_t pose = get_rotation_at_timestamp(img->pprz_ts);
154  struct opticflow_state_t temp_state;
155  temp_state.agl = opticflow_state.agl;
156  temp_state.rates = pose.rates;
157 
158  // Do the optical flow calculation
159  struct opticflow_result_t temp_result = {}; // new initialization
160  opticflow_calc_frame(&opticflow, &temp_state, img, &temp_result);
161 
162  // Copy the result if finished
163  pthread_mutex_lock(&opticflow_mutex);
164  memcpy(&opticflow_result, &temp_result, sizeof(struct opticflow_result_t));
165  opticflow_got_result = true;
166 
167  /* Rotate velocities from camera frame coordinates to body coordinates for control
168  * IMPORTANT!!! This frame to body orientation should be the case for the Parrot
169  * ARdrone and Bebop, however this can be different for other quadcopters
170  * ALWAYS double check!
171  */
174 
175  // release the mutex as we are done with editing the opticflow result
176  pthread_mutex_unlock(&opticflow_mutex);
177  return img;
178 }
179 
185 static void opticflow_agl_cb(uint8_t sender_id __attribute__((unused)), float distance)
186 {
187  // Update the distance if we got a valid measurement
188  if (distance > 0) {
189  opticflow_state.agl = distance;
190  }
191 }
Event structure to store callbacks in a linked list.
Definition: abi_common.h:65
float vel_body_x
The velocity in the x direction (body fixed coordinates)
int16_t flow_y
Flow in y direction from the camera (in subpixels)
int16_t flow_der_y
The derotated flow calculation in the y direction (in subpixels)
float div_size
Divergence as determined with the size_divergence script.
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_agl_ev
The altitude ABI event.
Capture images from a V4L2 device (Video for Linux 2)
struct video_listener * cv_add_to_device(struct video_config_t *device, cv_function func)
Definition: cv.c:45
Main include for ABI (AirBorneInterface).
Definition: image.h:42
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:47
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.
float agl
height above ground [m]
static pthread_mutex_t opticflow_mutex
Mutex lock fo thread safety.
void opticflow_calc_frame(struct opticflow_t *opticflow, struct opticflow_state_t *state, struct image_t *img, struct opticflow_result_t *result)
Run the optical flow on a new image frame.
uint16_t tracked_cnt
The amount of tracked corners.
float vel_body_y
The velocity in the y direction (body fixed coordinates)
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.
#define OPTICFLOW_SENDER_ID
Encode images with the use of the JPEG encoding.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:73
float vel_x
The velocity in the x direction (image coordinates)
uint16_t corner_cnt
The amount of coners found by FAST9.
float vel_y
The velocity in the y direction (image coordinates)
unsigned char uint8_t
Definition: types.h:14
float fps
Frames per second of the optical flow calculation.
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 ...
Encodes a vide stream with RTP (JPEG)
int16_t flow_x
Flow in x direction from the camera (in subpixels)
#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
float noise_measurement
noise of measurement, for state filter
float divergence
Divergence as determined with a linear flow fit.
static void opticflow_agl_cb(uint8_t sender_id, float distance)
Callback function of the ground altitude.
float surface_roughness
Surface roughness as determined with a linear optical flow fit.
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
int16_t flow_der_x
The derotated flow calculation in the x direction (in subpixels)