VideoCapture.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include "hrpsys/util/VectorConvert.h"
11 #include "VideoCapture.h"
12 
13 // Module specification
14 // <rtc-template block="module_spec">
15 static const char* videocapture_spec[] =
16  {
17  "implementation_id", "VideoCapture",
18  "type_name", "VideoCapture",
19  "description", "video capture component",
20  "version", HRPSYS_PACKAGE_VERSION,
21  "vendor", "JSK",
22  "category", "example",
23  "activity_type", "DataFlowComponent",
24  "max_instance", "10",
25  "language", "C++",
26  "lang_type", "compile",
27  // Configuration variables
28  "conf.default.initialMode", "continuous",
29  "conf.default.devIds", "0",
30  "conf.default.width", "640",
31  "conf.default.height", "480",
32  "conf.default.frameRate", "1",
33 
34  ""
35  };
36 // </rtc-template>
37 
39  : RTC::DataFlowComponentBase(manager),
40  // <rtc-template block="initializer">
41  m_MultiCameraImagesOut ("MultiCameraImages", m_MultiCameraImages),
42  m_CameraImageOut ("CameraImage", m_CameraImage),
43  m_CameraCaptureServicePort("CameraCaptureService"),
44  m_CameraCaptureService(this),
45  // </rtc-template>
46  m_mode(CONTINUOUS),
47  m_needToReactivate(false)
48 {
49 }
50 
52 {
53 }
54 
55 
56 
57 RTC::ReturnCode_t VideoCapture::onInitialize()
58 {
59  std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
60 
61  // <rtc-template block="bind_config">
62  // Bind variables and configuration variable
63  bindParameter("initialMode", m_initialMode, "continuous");
64  bindParameter("devIds", m_devIds, "0");
65  bindParameter("width", m_width, "640");
66  bindParameter("height", m_height, "480");
67  bindParameter("frameRate", m_frameRate, "1");
68 
69  // </rtc-template>
70 
71  // Registration: InPort/OutPort/Service
72  // <rtc-template block="registration">
73  // Set InPort buffers
74 
75  // Set OutPort buffer
76  if (m_devIds.size() == 1){
77  addOutPort ("CameraImage", m_CameraImageOut);
78  }else{
79  addOutPort ("MultiCameraImages", m_MultiCameraImagesOut);
80  }
81 
82  // Set service provider to Ports
83  m_CameraCaptureServicePort.registerProvider("service0", "CameraCaptureService", m_CameraCaptureService);
84 
85  // Set service consumers to Ports
86 
87  // Set CORBA Service Ports
89 
90  // </rtc-template>
91 
92  return RTC::RTC_OK;
93 }
94 
95 
96 
97 /*
98 RTC::ReturnCode_t VideoCapture::onFinalize()
99 {
100  return RTC::RTC_OK;
101 }
102 */
103 
104 RTC::ReturnCode_t VideoCapture::onStartup(RTC::UniqueId ec_id)
105 {
106  return RTC::RTC_OK;
107 }
108 
109 /*
110 RTC::ReturnCode_t VideoCapture::onShutdown(RTC::UniqueId ec_id)
111 {
112  return RTC::RTC_OK;
113 }
114 */
115 
116 RTC::ReturnCode_t VideoCapture::onActivated(RTC::UniqueId ec_id)
117 {
118  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
119  m_tOld = (double)(coil::gettimeofday());
120  if (m_initialMode == "continuous"){
121  m_mode = CONTINUOUS;
122  }else{
123  m_mode = SLEEP;
124  }
125 
126  if (m_devIds.size() == 1){
127  std::cout << "** devId:" << m_devIds[0] << std::endl;
128  v4l_capture *cam = new v4l_capture ();
129  if (cam->init(m_width, m_height, m_devIds[0]) != 0) return RTC::RTC_ERROR;
130  m_cameras.push_back (cam);
131  m_CameraImage.data.image.format = Img::CF_RGB;
132  m_CameraImage.data.image.width = cam->getWidth ();
133  m_CameraImage.data.image.height = cam->getHeight ();
134  m_CameraImage.data.image.raw_data.length (cam->getWidth () * cam->getHeight () * 3);
135  }else{
136  m_MultiCameraImages.data.image_seq.length (m_devIds.size ());
137  m_MultiCameraImages.data.camera_set_id = 0;
138  for (unsigned int i = 0; i < m_devIds.size (); i++)
139  {
140  std::cout << "** devId:" << m_devIds[i] << std::endl;
141  v4l_capture *cam = new v4l_capture ();
142  cam->init(m_width, m_height, m_devIds[i]);
143  m_cameras.push_back (cam);
144  m_MultiCameraImages.data.image_seq[i].image.format = Img::CF_RGB;
145  m_MultiCameraImages.data.image_seq[i].image.width = cam->getWidth ();
146  m_MultiCameraImages.data.image_seq[i].image.height = cam->getHeight ();
147  m_MultiCameraImages.data.image_seq[i].image.raw_data.length (cam->getWidth () * cam->getHeight () * 3);
148  }
149  }
150 
151  return RTC::RTC_OK;
152 }
153 
155 {
156  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
157  for (unsigned int i=0; i< m_cameras.size(); i++){
158  delete m_cameras[i];
159  }
160  m_cameras.clear();
161  return RTC::RTC_OK;
162 }
163 
164 RTC::ReturnCode_t VideoCapture::onExecute(RTC::UniqueId ec_id)
165 {
166  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
167  if (m_needToReactivate){
168  if (onActivated(ec_id) == RTC::RTC_OK){
169  m_needToReactivate = false;
170  }
171  }
172  if (!capture()){
173  std::cerr << m_profile.instance_name << ": failed to capture." << std::endl;
174  onDeactivated(ec_id);
175  m_needToReactivate = true;
176  return RTC::RTC_OK;
177  }
178 
179  double tNew = (double)(coil::gettimeofday());
180  double dt = (double)(tNew - m_tOld);
181  if (dt > 1.0/m_frameRate){
182  m_tOld = tNew;
183  }else{
184  return RTC::RTC_OK;
185  }
186 
187  if (m_mode == SLEEP) return RTC::RTC_OK;
188 
189  if (m_cameras.size() == 1){
191  }else{
193  }
194 
195  if (m_mode == ONESHOT) m_mode = SLEEP;
196 
197  return RTC::RTC_OK;
198 }
199 
201 {
202  if (m_cameras.size() == 1){
203  m_CameraImage.error_code = 0;
204  uchar *imgFrom = m_cameras[0]->capture();
205  if (!imgFrom) return false;
206  memcpy(m_CameraImage.data.image.raw_data.get_buffer(), imgFrom,
207  m_CameraImage.data.image.raw_data.length() * sizeof(uchar));
208  }else{
209  m_MultiCameraImages.error_code = 0;
210  for (unsigned int i = 0; i < m_cameras.size (); i++)
211  {
212  uchar *imgFrom = m_cameras[i]->capture();
213  if (!imgFrom) return false;
214  memcpy (m_MultiCameraImages.data.image_seq[i].image.raw_data.get_buffer(), imgFrom,
215  m_MultiCameraImages.data.image_seq[i].image.raw_data.length() * sizeof (uchar));
216  }
217  }
218  return true;
219 }
220 
221 /*
222 RTC::ReturnCode_t VideoCapture::onAborting(RTC::UniqueId ec_id)
223 {
224  return RTC::RTC_OK;
225 }
226 */
227 
228 /*
229 RTC::ReturnCode_t VideoCapture::onError(RTC::UniqueId ec_id)
230 {
231  return RTC::RTC_OK;
232 }
233 */
234 
235 /*
236 RTC::ReturnCode_t VideoCapture::onReset(RTC::UniqueId ec_id)
237 {
238  return RTC::RTC_OK;
239 }
240 */
241 
242 /*
243 RTC::ReturnCode_t VideoCapture::onStateUpdate(RTC::UniqueId ec_id)
244 {
245  return RTC::RTC_OK;
246 }
247 */
248 
249 /*
250 RTC::ReturnCode_t VideoCapture::onRateChanged(RTC::UniqueId ec_id)
251 {
252  return RTC::RTC_OK;
253 }
254 */
255 
257 {
258  m_mode = ONESHOT;
259 }
260 
262 {
263  m_mode = CONTINUOUS;
264 }
265 
267 {
268  m_mode = SLEEP;
269 }
270 
271 
272 extern "C"
273 {
274 
276  {
278  manager->registerFactory(profile,
279  RTC::Create<VideoCapture>,
280  RTC::Delete<VideoCapture>);
281  }
282 
283 };
284 
285 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
virtual ~VideoCapture()
Destructor.
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
OutPort< Img::TimedCameraImage > m_CameraImageOut
Definition: VideoCapture.h:122
int getHeight()
Definition: camera.cpp:45
video capture component
void stop_continuous()
int getWidth()
Definition: camera.cpp:40
png_uint_32 i
bool addOutPort(const char *name, OutPortBase &outport)
Img::TimedMultiCameraImage m_MultiCameraImages
Definition: VideoCapture.h:119
static const char * videocapture_spec[]
void VideoCaptureInit(RTC::Manager *manager)
void start_continuous()
int gettimeofday(struct timeval *tv, struct timezone *tz)
int init(size_t _width, size_t _height, unsigned int devId)
Definition: camera.cpp:23
virtual RTC::ReturnCode_t onStartup(RTC::UniqueId ec_id)
bool m_needToReactivate
Definition: VideoCapture.h:151
ExecutionContextHandle_t UniqueId
OutPort< Img::TimedMultiCameraImage > m_MultiCameraImagesOut
Definition: VideoCapture.h:120
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
void take_one_frame()
virtual RTC::ReturnCode_t onInitialize()
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
bool addPort(PortBase &port)
virtual bool write(DataType &value)
Img::TimedCameraImage m_CameraImage
Definition: VideoCapture.h:121
std::vector< int > m_devIds
Definition: VideoCapture.h:147
std::vector< v4l_capture *> m_cameras
Definition: VideoCapture.h:148
VideoCapture(RTC::Manager *manager)
Constructor.
RTC::CorbaPort m_CameraCaptureServicePort
Definition: VideoCapture.h:133
CameraCaptureService_impl m_CameraCaptureService
Definition: VideoCapture.h:139
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
std::string m_initialMode
Definition: VideoCapture.h:146
bool registerProvider(const char *instance_name, const char *type_name, PortableServer::RefCountServantBase &provider)


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Sat Dec 17 2022 03:52:21