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
video_usb_logger.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Christophe De Wagter
3  * Copyright (C) 2016 Roland Meertens
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  */
23 
27 #include "video_usb_logger.h"
28 
29 #include <stdio.h>
30 #include "state.h"
31 #include "viewvideo.h"
32 #include "cv.h"
33 #include <unistd.h>
34 
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <unistd.h>
40 
42 #ifndef VIDEO_USB_LOGGER_PATH
43 #define VIDEO_USB_LOGGER_PATH /data/video/usb
44 #endif
45 
47 static FILE *video_usb_logger = NULL;
50 char foldername[512];
51 int shotNumber = 0;
52 
53 static void save_shot_on_disk(struct image_t *img, struct image_t *img_jpeg)
54 {
55 
56  // Search for a file where we can write to
57  char save_name[128];
58 
59  snprintf(save_name, sizeof(save_name), "%s/img_%05d.jpg", foldername, shotNumber);
60 
61  shotNumber++;
62  // Check if file exists or not
63  if (access(save_name, F_OK) == -1) {
64 
65  // Create a high quality image (99% JPEG encoded)
66  jpeg_encode_image(img, img_jpeg, 99, TRUE);
67 
68 #if VIDEO_USB_LOGGER_JPEG_WITH_EXIF_HEADER
69  write_exif_jpeg(save_name, img_jpeg->buf, img_jpeg->buf_size, img_jpeg->w, img_jpeg->h);
70 #else
71  FILE *fp = fopen(save_name, "w");
72  if (fp == NULL) {
73  printf("[video_thread-thread] Could not write shot %s.\n", save_name);
74  } else {
75  // Save it to the file and close it
76  fwrite(img_jpeg->buf, sizeof(uint8_t), img_jpeg->buf_size, fp);
77  fclose(fp);
78  printf("Wrote image\n");
79  }
80 #endif
81 
82 
83 
85  if (video_usb_logger == NULL) {
86  return;
87  }
88 
89  static uint32_t counter = 0;
90  struct pose_t pose = get_rotation_at_timestamp(img->pprz_ts);
91  struct NedCoor_i *ned = stateGetPositionNed_i();
92  struct NedCoor_i *accel = stateGetAccelNed_i();
93  static uint32_t sonar = 0;
94 
95 
96  // Save current information to a file
97  fprintf(video_usb_logger, "%d,%d,%f,%f,%f,%d,%d,%d,%d,%d,%d,%f,%f,%f,%d\n", counter,
98  shotNumber,
99  pose.eulers.phi, pose.eulers.theta, pose.eulers.psi,
100  ned->x, ned->y, ned->z,
101  accel->x, accel->y, accel->z,
102  pose.rates.p, pose.rates.q, pose.rates.r,
103  sonar);
104  counter++;
105  }
106 
107 }
108 
109 static struct image_t *log_image(struct image_t *img)
110 {
111  if (!created_jpeg) {
112 
113  // Create the jpeg image used later
114  image_create(&img_jpeg_global, img->w, img->h, IMAGE_JPEG);
115  created_jpeg = TRUE;
116  }
118  return img;
119 }
120 
123 {
124 
125  uint32_t counter = 0;
126  char filename[512];
127  struct stat st = {0};
128 
129  // Search and create a new folder
130  do {
131  snprintf(foldername, sizeof(foldername), "%s/pprzvideo%05d", STRINGIFY(VIDEO_USB_LOGGER_PATH), counter);
132  counter++;
133  } while (stat(foldername, &st) >= 0);
134 
135  mkdir(foldername, 0700);
136 
137 // In this folder create a textlog
138  snprintf(filename, sizeof(filename), "%s/log.csv", foldername);
139  video_usb_logger = fopen(filename, "w");
140 
141  if (video_usb_logger != NULL) {
142  fprintf(video_usb_logger, "counter,image,roll,pitch,yaw,x,y,z,accelx,accely,accelz,ratep,rateq,rater,sonar\n");
143  }
144 
145  // Subscribe to a camera
146  cv_add_to_device(&VIDEO_USB_LOGGER_CAMERA, log_image);
147 }
148 
151 {
152  if (video_usb_logger != NULL) {
153  fclose(video_usb_logger);
154  video_usb_logger = NULL;
155  }
156 }
157 
159 {
160 
161 }
uint32_t buf_size
The buffer size.
Definition: image.h:50
float phi
in radians
struct FloatRates rates
Definition: pose_history.h:34
struct video_listener * cv_add_to_device(struct video_config_t *device, cv_function func)
Definition: cv.c:45
float r
in rad/s
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
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
float psi
in radians
int shotNumber
void video_usb_logger_periodic(void)
float q
in rad/s
uint32_t pprz_ts
The timestamp in us since system startup.
Definition: image.h:47
float p
in rad/s
int32_t z
Down.
#define FALSE
Definition: std.h:5
bool created_jpeg
int32_t y
East.
float theta
in radians
#define TRUE
Definition: std.h:4
Camera image logger for Linux based autopilots.
int write_exif_jpeg(char *filename, const unsigned char *image_jpg, const unsigned int image_jpg_len, const unsigned int image_jpg_x, const unsigned int image_jpg_y)
Definition: exif_module.c:146
struct FloatEulers eulers
Definition: pose_history.h:33
#define VIDEO_USB_LOGGER_PATH
Set the default File logger path to the USB drive.
uint16_t w
Image width.
Definition: image.h:44
int32_t x
North.
unsigned long uint32_t
Definition: types.h:18
Computer vision framework for onboard processing.
uint16_t h
Image height.
Definition: image.h:45
int32_t counter
void * buf
Image buffer (depending on the image_type)
Definition: image.h:51
struct image_t img_jpeg_global
Encode images with the use of the JPEG encoding.
void video_usb_logger_stop(void)
Stop the logger an nicely close the file.
static struct image_t * log_image(struct image_t *img)
Get live images from a RTP/UDP stream and save pictures on internal memory.
unsigned char uint8_t
Definition: types.h:14
API to get/set the generic vehicle states.
vector in North East Down coordinates
void video_usb_logger_start(void)
Start the file logger and open a new file.
void jpeg_encode_image(struct image_t *in, struct image_t *out, uint32_t quality_factor, bool add_dri_header)
Encode an YUV422 image.
Definition: jpeg.c:408
static void save_shot_on_disk(struct image_t *img, struct image_t *img_jpeg)
static FILE * video_usb_logger
The file pointer.
static struct NedCoor_i * stateGetPositionNed_i(void)
Get position in local NED coordinates (int).
Definition: state.h:659
char foldername[512]
static struct NedCoor_i * stateGetAccelNed_i(void)
Get acceleration in NED coordinates (int).
Definition: state.h:1002
An JPEG encoded image (not per pixel encoded)
Definition: image.h:37