Paparazzi UAS  v5.8.2_stable-0-g6260b7c
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 
40 /* Default sonar/agl to use in opticflow visual_estimator */
41 #ifndef OPTICFLOW_AGL_ID
42 #define OPTICFLOW_AGL_ID ABI_BROADCAST
43 #endif
44 PRINT_CONFIG_VAR(OPTICFLOW_AGL_ID)
45 
46 #ifndef OPTICFLOW_SENDER_ID
47 #define OPTICFLOW_SENDER_ID 1
48 #endif
49 
50 /* The video device */
51 #ifndef OPTICFLOW_DEVICE
52 #define OPTICFLOW_DEVICE /dev/video2
53 #endif
54 PRINT_CONFIG_VAR(OPTICFLOW_DEVICE)
55 
56 /* The video device size (width, height) */
57 #ifndef OPTICFLOW_DEVICE_SIZE
58 #define OPTICFLOW_DEVICE_SIZE 320,240
59 #endif
60 #define __SIZE_HELPER(x, y) #x", "#y
61 #define _SIZE_HELPER(x) __SIZE_HELPER(x)
62 PRINT_CONFIG_MSG("OPTICFLOW_DEVICE_SIZE = " _SIZE_HELPER(OPTICFLOW_DEVICE_SIZE))
63 
64 /* The video device buffers (the amount of V4L2 buffers) */
65 #ifndef OPTICFLOW_DEVICE_BUFFERS
66 #define OPTICFLOW_DEVICE_BUFFERS 15
67 #endif
68 PRINT_CONFIG_VAR(OPTICFLOW_DEVICE_BUFFERS)
69 
70 /* The main opticflow variables */
74 static struct v4l2_device *opticflow_dev;
76 static pthread_t opticflow_calc_thread;
77 static bool_t opticflow_got_result;
78 static pthread_mutex_t opticflow_mutex;
79 
80 /* Static functions */
81 static void *opticflow_module_calc(void *data);
82 static void opticflow_agl_cb(uint8_t sender_id, float distance);
83 
84 #if PERIODIC_TELEMETRY
91 static void opticflow_telem_send(struct transport_tx *trans, struct link_device *dev)
92 {
93  pthread_mutex_lock(&opticflow_mutex);
94  pprz_msg_send_OPTIC_FLOW_EST(trans, dev, AC_ID,
95  &opticflow_result.fps, &opticflow_result.corner_cnt,
96  &opticflow_result.tracked_cnt, &opticflow_result.flow_x,
97  &opticflow_result.flow_y, &opticflow_result.flow_der_x,
98  &opticflow_result.flow_der_y, &opticflow_result.vel_x,
99  &opticflow_result.vel_y, &opticflow_result.div_size,
100  &opticflow_result.surface_roughness, &opticflow_result.divergence);
101  pthread_mutex_unlock(&opticflow_mutex);
102 }
103 #endif
104 
109 {
110  // Subscribe to the altitude above ground level ABI messages
111  AbiBindMsgAGL(OPTICFLOW_AGL_ID, &opticflow_agl_ev, opticflow_agl_cb);
112 
113  // Set the opticflow state to 0
114  opticflow_state.phi = 0;
115  opticflow_state.theta = 0;
116  opticflow_state.agl = 0;
117 
118  // Initialize the opticflow calculation
119  opticflow_calc_init(&opticflow, 320, 240);
120  opticflow_got_result = FALSE;
121 
122 #ifdef OPTICFLOW_SUBDEV
123  PRINT_CONFIG_MSG("[opticflow_module] Configuring a subdevice!")
124  PRINT_CONFIG_VAR(OPTICFLOW_SUBDEV)
125 
126  /* Initialize the V4L2 subdevice (TODO: fix hardcoded path, which and code) */
127  if (!v4l2_init_subdev(STRINGIFY(OPTICFLOW_SUBDEV), 0, 1, V4L2_MBUS_FMT_UYVY8_2X8, OPTICFLOW_DEVICE_SIZE)) {
128  printf("[opticflow_module] Could not initialize the %s subdevice.\n", STRINGIFY(OPTICFLOW_SUBDEV));
129  return;
130  }
131 #endif
132 
133  /* Try to initialize the video device */
135  V4L2_PIX_FMT_UYVY);
136  if (opticflow_dev == NULL) {
137  printf("[opticflow_module] Could not initialize the video device\n");
138  }
139 
140 #if PERIODIC_TELEMETRY
141  register_periodic_telemetry(DefaultPeriodic, PPRZ_MSG_ID_OPTIC_FLOW_EST, opticflow_telem_send);
142 #endif
143 }
144 
150 {
151  pthread_mutex_lock(&opticflow_mutex);
152  // Send Updated data to thread
153  opticflow_state.phi = stateGetNedToBodyEulers_f()->phi;
154  opticflow_state.theta = stateGetNedToBodyEulers_f()->theta;
155 
156  // Update the stabilization loops on the current calculation
157  if (opticflow_got_result) {
158  uint32_t now_ts = get_sys_time_usec();
159  uint8_t quality = opticflow_result.divergence; // FIXME, scale to some quality measure 0-255
160  AbiSendMsgOPTICAL_FLOW(OPTICFLOW_SENDER_ID, now_ts,
161  opticflow_result.flow_x,
162  opticflow_result.flow_y,
163  opticflow_result.flow_der_x,
164  opticflow_result.flow_der_x,
165  quality,
166  opticflow_state.agl);
167  //TODO Find an appropiate quality measure for the noise model in the state filter, for now it is tracked_cnt
168  if (opticflow_result.tracked_cnt > 0) {
169  AbiSendMsgVELOCITY_ESTIMATE(OPTICFLOW_SENDER_ID, now_ts,
170  opticflow_result.vel_x,
171  opticflow_result.vel_y,
172  0.0f,
173  opticflow_result.noise_measurement
174  );
175  }
176  opticflow_got_result = FALSE;
177  }
178  pthread_mutex_unlock(&opticflow_mutex);
179 }
180 
185 {
186  // Check if we are not already running
187  if (opticflow_calc_thread != 0) {
188  printf("[opticflow_module] Opticflow already started!\n");
189  return;
190  }
191 
192  // Create the opticalflow calculation thread
193  int rc = pthread_create(&opticflow_calc_thread, NULL, opticflow_module_calc, NULL);
194  if (rc) {
195  printf("[opticflow_module] Could not initialize opticflow thread (return code: %d)\n", rc);
196  }
197 }
198 
203 {
204  // Stop the capturing
205  v4l2_stop_capture(opticflow_dev);
206 
207  // TODO: fix thread stop
208 }
209 
215 #include "errno.h"
216 static void *opticflow_module_calc(void *data __attribute__((unused)))
217 {
218  // Start the streaming on the V4L2 device
219  if (!v4l2_start_capture(opticflow_dev)) {
220  printf("[opticflow_module] Could not start capture of the camera\n");
221  return 0;
222  }
223 
224 #if OPTICFLOW_DEBUG
225  // Create a new JPEG image
226  struct image_t img_jpeg;
227  image_create(&img_jpeg, opticflow_dev->w, opticflow_dev->h, IMAGE_JPEG);
228 #endif
229 
230  /* Main loop of the optical flow calculation */
231  while (TRUE) {
232  // Try to fetch an image
233  struct image_t img;
234  v4l2_image_get(opticflow_dev, &img);
235 
236  // Copy the state
237  pthread_mutex_lock(&opticflow_mutex);
238  struct opticflow_state_t temp_state;
239  memcpy(&temp_state, &opticflow_state, sizeof(struct opticflow_state_t));
240  pthread_mutex_unlock(&opticflow_mutex);
241 
242  // Do the optical flow calculation
243  struct opticflow_result_t temp_result;
244  opticflow_calc_frame(&opticflow, &temp_state, &img, &temp_result);
245 
246  // Copy the result if finished
247  pthread_mutex_lock(&opticflow_mutex);
248  memcpy(&opticflow_result, &temp_result, sizeof(struct opticflow_result_t));
249  opticflow_got_result = TRUE;
250  pthread_mutex_unlock(&opticflow_mutex);
251 
252 #if OPTICFLOW_DEBUG
253  jpeg_encode_image(&img, &img_jpeg, 70, FALSE);
255  &VIEWVIDEO_DEV, // UDP device
256  &img_jpeg,
257  0, // Format 422
258  70, // Jpeg-Quality
259  0, // DRI Header
260  0 // 90kHz time increment
261  );
262 #endif
263 
264  // Free the image
265  v4l2_image_free(opticflow_dev, &img);
266  }
267 
268 #if OPTICFLOW_DEBUG
269  image_free(&img_jpeg);
270 #endif
271 }
272 
278 static void opticflow_agl_cb(uint8_t sender_id __attribute__((unused)), float distance)
279 {
280  // Update the distance if we got a valid measurement
281  if (distance > 0) {
282  opticflow_state.agl = distance;
283  }
284 }
#define _SIZE_HELPER(x)
Event structure to store callbacks in a linked list.
Definition: abi_common.h:65
bool_t v4l2_start_capture(struct v4l2_device *dev)
Start capturing images in streaming mode (Thread safe)
Definition: v4l2.c:387
float phi
in radians
void opticflow_module_stop(void)
Stop the optical flow calculation.
Generic transmission transport header.
Definition: transport.h:89
struct opticflow_t opticflow
Opticflow calculations.
static struct FloatEulers * stateGetNedToBodyEulers_f(void)
Get vehicle body attitude euler angles (float).
Definition: state.h:1114
Periodic telemetry system header (includes downlink utility and generated code).
static struct v4l2_device * opticflow_dev
The opticflow camera V4L2 device.
void v4l2_image_free(struct v4l2_device *dev, struct image_t *img)
Free the image and enqueue the buffer (Thread safe) This must be done after processing the image...
Definition: v4l2.c:366
void v4l2_image_get(struct v4l2_device *dev, struct image_t *img)
Get the latest image buffer and lock it (Thread safe, BLOCKING) This functions blocks until image acc...
Definition: v4l2.c:294
static struct opticflow_state_t opticflow_state
State of the drone to communicate with the opticflow.
void opticflow_calc_init(struct opticflow_t *opticflow, uint16_t w, uint16_t h)
Initialize the opticflow calculator.
optical-flow calculation for Parrot Drones
static abi_event opticflow_agl_ev
The altitude ABI event.
void image_free(struct image_t *img)
Free the image.
Definition: image.c:63
Capture images from a V4L2 device (Video for Linux 2)
Main include for ABI (AirBorneInterface).
void image_create(struct image_t *img, uint16_t width, uint16_t height, enum image_type type)
Create a new image.
Definition: image.c:38
Definition: image.h:42
bool_t v4l2_stop_capture(struct v4l2_device *dev)
Stop capturing of the image stream (Thread safe) This function is blocking until capturing thread is ...
Definition: v4l2.c:446
void rtp_frame_send(struct UdpSocket *udp, struct image_t *img, uint8_t format_code, uint8_t quality_code, uint8_t has_dri_header, uint32_t delta_t)
Send an RTP frame.
Definition: rtp.c:97
#define FALSE
Definition: std.h:5
void opticflow_module_init(void)
Initialize the optical flow module for the bottom camera.
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:39
static pthread_mutex_t opticflow_mutex
Mutex lock fo thread safety.
float theta
in radians
void jpeg_encode_image(struct image_t *in, struct image_t *out, uint32_t quality_factor, bool_t add_dri_header)
Encode an YUV422 image.
Definition: jpeg.c:425
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.
#define TRUE
Definition: std.h:4
static pthread_t opticflow_calc_thread
The optical flow calculation thread.
uint16_t w
Image width.
Definition: image.h:44
unsigned long uint32_t
Definition: types.h:18
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
#define DefaultPeriodic
Set default periodic telemetry.
Definition: telemetry.h:66
static struct opticflow_result_t opticflow_result
The opticflow result.
#define OPTICFLOW_SENDER_ID
bool_t v4l2_init_subdev(char *subdev_name, uint8_t pad, uint8_t which, uint16_t code, uint16_t width, uint16_t height)
Initialize a V4L2 subdevice.
Definition: v4l2.c:130
Encode images with the use of the JPEG encoding.
static const struct usb_device_descriptor dev
Definition: usb_ser_hw.c:69
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
#define OPTICFLOW_DEVICE
The video device.
#define OPTICFLOW_DEVICE_BUFFERS
The video device buffers (the amount of V4L2 buffers)
void opticflow_module_run(void)
Update the optical flow state for the calculation thread and update the stabilization loops with the ...
void opticflow_module_start(void)
Start the optical flow calculation.
#define OPTICFLOW_DEVICE_SIZE
The video device size (width, height)
Encodes a vide stream with RTP (JPEG)
static bool_t opticflow_got_result
When we have an optical flow calculation.
#define OPTICFLOW_AGL_ID
Default sonar/agl to use in opticflow visual_estimator.
static void * opticflow_module_calc(void *data)
The main optical flow calculation thread.
struct v4l2_device * v4l2_init(char *device_name, uint16_t width, uint16_t height, uint8_t buffers_cnt, uint32_t _pixelformat)
Initialize a V4L2(Video for Linux 2) device.
Definition: v4l2.c:177
static void opticflow_agl_cb(uint8_t sender_id, float distance)
Callback function of the ground altitude.
int8_t register_periodic_telemetry(struct periodic_telemetry *_pt, uint8_t _id, telemetry_cb _cb)
Register a telemetry callback function.
Definition: telemetry.c:46
An JPEG encoded image (not per pixel encoded)
Definition: image.h:37