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

Last change on this file since 253 was 169, checked in by Sanahuja Guillaume, 8 years ago

cosmetic

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 (fpsCounter == 100) {
107 fpsNow = GetTime();
108 fps->SetText("fps: %.1f",
109 fpsCounter / ((float)(fpsNow - fpsPrev) / 1000000000.));
110 fpsCounter = 0;
111 fpsPrev = fpsNow;
112 }
113
114 // cam properties
115 if (gain->ValueChanged() == true && autogain->Value() == false)
116 SetGain(gain->Value());
117 if (exposure->ValueChanged() == true && autoexposure->Value() == false)
118 SetExposure(exposure->Value());
119 if (bright->ValueChanged() == true)
120 SetBrightness(bright->Value());
121 if (sat->ValueChanged() == true)
122 SetSaturation(sat->Value());
123 if (contrast->ValueChanged() == true)
124 SetContrast(contrast->Value());
125 if (hue->ValueChanged() == true)
126 SetHue(hue->Value());
127 if (sharpness->ValueChanged() == true)
128 cvSetCaptureProperty(capture, CV_CAP_PROP_SHARPNESS, sharpness->Value());
129 if (autogain->ValueChanged() == true) {
130 if (autogain->Value() == true) {
131 gain->setEnabled(false);
132 } else {
133 gain->setEnabled(true);
134 SetGain(gain->Value());
135 }
136 SetAutoGain(autogain->Value());
137 }
138 if (autoexposure->ValueChanged() == true) {
139 if (autoexposure->Value() == true) {
140 exposure->setEnabled(false);
141 } else {
142 exposure->setEnabled(true);
143 SetExposure(exposure->Value());
144 }
145 SetAutoExposure(autoexposure->Value());
146 }
147 if (awb->ValueChanged() == true)
148 cvSetCaptureProperty(capture, CV_CAP_PROP_AWB, awb->Value());
149
150 // cam pictures
151 img = cvRetrieveRawFrame(capture);
152 if (!cvGrabFrame(capture)) {
153 Printf("Could not grab a frame\n");
154 }
155 new_time = GetTime();
156
157 //check for ps3eye deconnection in hds uav
158 if(new_time-cam_time>100*1000*1000) {
159 Thread::Warn("delta trop grand\n");
160 hasProblems=true;
161 }
162
163 output->GetMutex();
164 output->img->imageData = img->imageData;
165 output->ReleaseMutex();
166
167 output->SetDataTime(cam_time);
168 ProcessUpdate(output);
169
170 cam_time = new_time;
171 }
172
173 cvReleaseCapture(&capture);
174}
175
176bool V4LCamera::HasProblems(void) {
177 return hasProblems;
178}
179
180void V4LCamera::SetAutoGain(bool value) {
181 cvSetCaptureProperty(capture, CV_CAP_PROP_AUTOGAIN, value);
182}
183
184void V4LCamera::SetAutoExposure(bool value) {
185 Thread::Warn("not implemented in opencv\n");
186}
187
188void V4LCamera::SetGain(float value) {
189 cvSetCaptureProperty(capture, CV_CAP_PROP_GAIN, value);
190}
191
192void V4LCamera::SetExposure(float value) {
193 cvSetCaptureProperty(capture, CV_CAP_PROP_EXPOSURE, value);
194}
195
196void V4LCamera::SetBrightness(float value) {
197 cvSetCaptureProperty(capture, CV_CAP_PROP_BRIGHTNESS, value);
198}
199
200void V4LCamera::SetSaturation(float value) {
201 cvSetCaptureProperty(capture, CV_CAP_PROP_SATURATION, value);
202}
203
204void V4LCamera::SetHue(float value) {
205 cvSetCaptureProperty(capture, CV_CAP_PROP_HUE, value);
206}
207
208void V4LCamera::SetContrast(float value) {
209 cvSetCaptureProperty(capture, CV_CAP_PROP_CONTRAST, value);
210}
211
212} // end namespace sensor
213} // end namespace flair
Note: See TracBrowser for help on using the repository browser.