source: flair-src/trunk/lib/FlairSensorActuator/src/V4LCamera.cpp@ 319

Last change on this file since 319 was 307, checked in by Sanahuja Guillaume, 6 years ago

resove bug in times

File size: 6.3 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2014/07/17
6// filename: V4LCamera.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: base class for V4l camera
14//
15//
16/*********************************************************************/
17
18#include "V4LCamera.h"
19#include <GroupBox.h>
20#include <DoubleSpinBox.h>
21#include <CheckBox.h>
22#include <Label.h>
23#include <cvimage.h>
24#include <FrameworkManager.h>
25#include <linux/videodev2.h>
26
27using std::string;
28using namespace flair::core;
29using namespace flair::gui;
30
31namespace flair {
32namespace sensor {
33
34V4LCamera::V4LCamera(string name,
35 uint8_t camera_index, uint16_t width, uint16_t height,
36 cvimage::Type::Format format, uint8_t priority)
37 : Thread(getFrameworkManager(), name, priority),
38 Camera(name, width, height, format) {
39 capture = cvCaptureFromCAM(camera_index);
40 if (capture < 0)
41 Thread::Err("cvCaptureFromCAM error\n");
42
43 if (cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width)<0)
44 Thread::Err("cvSetCaptureProperty error\n");
45 if (cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height)<0)
46 Thread::Err("cvSetCaptureProperty error\n");
47
48 if (format == cvimage::Type::Format::UYVY) {
49 if (cvSetCaptureProperty(capture, CV_CAP_PROP_FORMAT, V4L2_PIX_FMT_UYVY)<0)
50 Thread::Err("cvSetCaptureProperty error\n");
51 } else if (format == cvimage::Type::Format::YUYV) {
52 if (cvSetCaptureProperty(capture, CV_CAP_PROP_FORMAT, V4L2_PIX_FMT_YUYV) <
53 0)
54 Thread::Err("cvSetCaptureProperty error\n");
55 } else {
56 Thread::Err("format not supported\n");
57 }
58
59 // station sol
60 gain = new DoubleSpinBox(GetGroupBox()->NewRow(), "gain:", 0, 1, 0.1);
61 exposure = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "exposure:", 0,
62 1, 0.1);
63 bright =
64 new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "bright:", 0, 1, 0.1);
65 contrast = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "contrast:", 0,
66 1, 0.1);
67 hue = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "hue:", 0, 1, 0.1);
68 sharpness = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "sharpness:",
69 0, 1, 0.1);
70 sat = new DoubleSpinBox(GetGroupBox()->LastRowLastCol(), "saturation:", 0, 1,
71 0.1);
72 autogain = new CheckBox(GetGroupBox()->NewRow(), "autogain:");
73 autoexposure = new CheckBox(GetGroupBox()->LastRowLastCol(), "autoexposure:");
74 awb = new CheckBox(GetGroupBox()->LastRowLastCol(), "awb:");
75 fps = new Label(GetGroupBox()->NewRow(), "fps");
76
77 hasProblems=false;
78}
79
80V4LCamera::~V4LCamera() {
81 SafeStop();
82 Join();
83}
84
85void V4LCamera::Run(void) {
86 Time cam_time, new_time, fpsNow, fpsPrev;
87 IplImage *img; // raw image
88 int fpsCounter = 0;
89
90 // init image old
91 if (!cvGrabFrame(capture)) {
92 Printf("Could not grab a frame\n");
93 }
94 cam_time = GetTime();
95 fpsPrev = cam_time;
96
97 while (!ToBeStopped()) {
98 //check for ps3eye deconnection in hds uav
99 if(cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH)<0) {
100 Thread::Warn("camera disconnected\n");
101 hasProblems=true;
102 }
103
104 // fps counter
105 fpsCounter++;
106 if (GetTime() > (fpsPrev + 5 * (Time)1000000000)) {
107 // toute les 5 secondes
108 fpsNow = GetTime();
109 fps->SetText("fps: %.1f",
110 fpsCounter / ((float)(fpsNow - fpsPrev) / 1000000000.));
111 fpsCounter = 0;
112 fpsPrev = fpsNow;
113 }
114
115 // cam properties
116 if (gain->ValueChanged() == true && autogain->Value() == false)
117 SetGain(gain->Value());
118 if (exposure->ValueChanged() == true && autoexposure->Value() == false)
119 SetExposure(exposure->Value());
120 if (bright->ValueChanged() == true)
121 SetBrightness(bright->Value());
122 if (sat->ValueChanged() == true)
123 SetSaturation(sat->Value());
124 if (contrast->ValueChanged() == true)
125 SetContrast(contrast->Value());
126 if (hue->ValueChanged() == true)
127 SetHue(hue->Value());
128 if (sharpness->ValueChanged() == true)
129 cvSetCaptureProperty(capture, CV_CAP_PROP_SHARPNESS, sharpness->Value());
130 if (autogain->ValueChanged() == true) {
131 if (autogain->Value() == true) {
132 gain->setEnabled(false);
133 } else {
134 gain->setEnabled(true);
135 SetGain(gain->Value());
136 }
137 SetAutoGain(autogain->Value());
138 }
139 if (autoexposure->ValueChanged() == true) {
140 if (autoexposure->Value() == true) {
141 exposure->setEnabled(false);
142 } else {
143 exposure->setEnabled(true);
144 SetExposure(exposure->Value());
145 }
146 SetAutoExposure(autoexposure->Value());
147 }
148 if (awb->ValueChanged() == true)
149 cvSetCaptureProperty(capture, CV_CAP_PROP_AWB, awb->Value());
150
151 // cam pictures
152 img = cvRetrieveRawFrame(capture);
153 if (!cvGrabFrame(capture)) {
154 Printf("Could not grab a frame\n");
155 }
156 new_time = GetTime();
157
158 //check for ps3eye deconnection in hds uav
159 if(new_time-cam_time>100*1000*1000) {
160 Thread::Warn("delta trop grand\n");
161 hasProblems=true;
162 }
163
164 output->GetMutex();
165 output->img->imageData = img->imageData;
166 output->ReleaseMutex();
167
168 output->SetDataTime(cam_time);
169 ProcessUpdate(output);
170
171 cam_time = new_time;
172 }
173
174 cvReleaseCapture(&capture);
175}
176
177bool V4LCamera::HasProblems(void) {
178 return hasProblems;
179}
180
181void V4LCamera::SetAutoGain(bool value) {
182 cvSetCaptureProperty(capture, CV_CAP_PROP_AUTOGAIN, value);
183}
184
185void V4LCamera::SetAutoExposure(bool value) {
186 Thread::Warn("not implemented in opencv\n");
187}
188
189void V4LCamera::SetGain(float value) {
190 cvSetCaptureProperty(capture, CV_CAP_PROP_GAIN, value);
191}
192
193void V4LCamera::SetExposure(float value) {
194 cvSetCaptureProperty(capture, CV_CAP_PROP_EXPOSURE, value);
195}
196
197void V4LCamera::SetBrightness(float value) {
198 cvSetCaptureProperty(capture, CV_CAP_PROP_BRIGHTNESS, value);
199}
200
201void V4LCamera::SetSaturation(float value) {
202 cvSetCaptureProperty(capture, CV_CAP_PROP_SATURATION, value);
203}
204
205void V4LCamera::SetHue(float value) {
206 cvSetCaptureProperty(capture, CV_CAP_PROP_HUE, value);
207}
208
209void V4LCamera::SetContrast(float value) {
210 cvSetCaptureProperty(capture, CV_CAP_PROP_CONTRAST, value);
211}
212
213} // end namespace sensor
214} // end namespace flair
Note: See TracBrowser for help on using the repository browser.