Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
VideoSource_MDcam.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003, 2004 (see file CONTACTS for details)
5  Multimediale Systeme der Informationsverarbeitung
6  Institut fuer Informatik
7  Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include <sys/io.h>
26 #include <unistd.h>
27 #include <fstream>
28 
29 #include "VideoSource_MDcam.hh"
30 
31 
32 //extern int GetCamIDs(int busid ,int &ncams, int *&camids);
33 
34 using namespace BIAS;
35 using namespace std;
36 
38 {
39  Camera_ = NULL;
40  NumCams_ = 0;
41  ParPort_ = 0x378;
42  ExternalTrigger_ = false;
43  ActiveTrigger_ = false;
44  BytesPerPixel_ = 1;
45 }
46 
47 
48 
50 {
51  if (NumCams_ != 0 )
52  ReleaseCameras();
53 
54 }
55 
56 
58  DebuglevelDcam_ = d;
59  for (int i=0; i<NumCams_; i++)
60  Camera_[i]->SetDebugLevel(d) ;
61 }
62 
63 
64 
65 int VideoSource_MDcam::ScanBus(std::ofstream &ss)
66 {
67 
68  BIASDOUT(D_MDCAM_INIT,"Scanning bus ");
69 
70 
71  VideoSource_DCAM *cam;
72  int ncams = 0;
73  // int *camids = NULL;
74 
75  dc1394_t *d = NULL;
76  dc1394camera_t* cams;
77  dc1394camera_list_t* camsList;
78  dc1394error_t err;
79 
80  err = dc1394_camera_enumerate( d, &camsList);
81  ncams = camsList->num;
82 
83  if(err != DC1394_SUCCESS){
84  BIASERR("Scanning bus failed");
85  }
86 
87  if(ncams > 0){
88  cam = new VideoSource_DCAM();
89  cam->SetDebugLevel(DebuglevelDcam_);
90 
91  for (int i=0; i<ncams; i++) {
92  cams = dc1394_camera_new(d, camsList->ids[i].guid);
93  cam->SetSize(0,0);
94  cam->OpenDevice(cams->guid & 0x00000FFF);
95  if (ss) {
96  ss << "0" <<" "<<(cams->guid & 0x00000FFF)<<" "<<cam->GetWidth()<<" "
97  <<cam->GetHeight()<<" 0"<<" 0" <<endl;
98  }
99  cam->CloseDevice();
100  }
101  delete cam;
102  }else{
103  BIASERR("No cameras found on bus ");
104  return -1;
105  }
106  return ncams;
107 }
108 
109 
110 
111 
112 int VideoSource_MDcam::InitCameras(const string filename)
113 {
114  ifstream ifs;
115  int bus,camid, width,height,xoffset,yoffset;
116  int res;
117  BIASDOUT(D_MDCAM_INIT,"InitCameras() from "<<filename);
118  if (NumCams_ != 0) {
119  BIASERR("VideoSource_MDcam::InitCameras(): Already initialized");
120  return -1;
121  }
122  ifs.open(filename.c_str());
123  if (!ifs) {
124  perror(filename.c_str());
125  return -1;
126  }
127  while (ifs) {
128  char tmpline[128];
129  ifs.getline(tmpline,128);
130  if (tmpline[0]=='0' || tmpline[0]=='1'|| tmpline[0]=='2')
131  NumCams_ ++;
132  }
133 
134  ifs.close();
135  ifs.clear();
136  if (!ifs) {
137  cerr<<"after close"<<endl;
138  perror(filename.c_str());
139  return -1;
140  }
141  ifs.open(filename.c_str());
142  if (!ifs) {
143  perror(filename.c_str());
144  return -1;
145  }
146 
147  Camera_ = new VideoSource_DCAM * [NumCams_];
148  for (int i=0; i< NumCams_; i++){
149  ifs >> bus >> camid >> width >> height >>xoffset >>yoffset;
150  cout <<"Expecting cam "<<i<<" at bus "<<bus<<" with ID "<<camid<<endl;
151  Camera_[i] = new VideoSource_DCAM(bus);
152  Camera_[i]->SetDebugLevel(DebuglevelDcam_) ;
153  // Camera_[i]->ForceFormat7(true);
154  //Camera_[i]->SetSize(width,height);
155  //Camera_[i]->SetBytesPerPixel(BytesPerPixel_);
156  //Camera_[i]->SetLeftTop(xoffset,yoffset);
157  Camera_[i]->SetFPS(Fps_);
158  cout << "Frames Per Second" << Fps_ << endl;
159 
160  res = Camera_[i]->OpenDevice(camid);
161  if (res < 0) {
162  BIASERR("Can not open camera "<<camid<<" on bus "<<bus);
163  return -1;
164  }
165  }
166 
167  ifs.close();
168 
169 
170  return NumCams_;
171 
172 }
173 
174 
176 {
177  if (NumCams_ == 0) {
178  BIASERR("VideoSource_MDcam::Release(): not initialized");
179  return -1;
180  }
181 
182  for (int i=0; i<NumCams_; i++) {
183  Camera_[i]->CloseDevice();
184  delete Camera_[i];
185  }
186  delete[] Camera_;
187  NumCams_ = 0;
188  return 0;
189 }
190 
191 
193 {
194  for (int i=0; i<NumCams_; i++)
195  Camera_[i]->ActivateScorpionTimestamp();
196 }
197 
198 
200 {
201  int res;
202  for (int i=0; i<NumCams_; i++) {
203  res = Camera_[i]->PreGrab();
204  if (res != 0) return res;
205  }
206 
207  return 0;
208 }
209 
210 
211 
212 
214 {
215  int res;
216  for (int i=0; i<NumCams_; i++) {
217  res = Camera_[i]->PostGrab();
218  if (res != 0) return res;
219  }
220 
221  return 0;
222 }
223 
224 
226 {
227  struct timeval now;
228  if (ExternalTrigger_ && ActiveTrigger_ ) {
229  outb(0, ParPort_);
230  usleep(0);
231  outb(255,ParPort_);
232  }
233  if (ExternalTrigger_)
234  gettimeofday(&now,NULL);
235 
236  for (int i=0; i<NumCams_; i++) {
237  Camera_[i]->GrabSingle(Image[i]);
238  }
239 
240  // if external trigger use accurate timestamps
241  if (ExternalTrigger_) {
242  stringstream time_ss;
243  time_ss.str("");
244  time_ss<<now.tv_sec<<":"<<now.tv_usec;
245  // reset trigger
246  if ( ActiveTrigger_ )
247  outb(255,ParPort_);
248  for (int i=0; i<NumCams_; i++)
249  // Image[i].SetTime(now.tv_sec,now.tv_usec);
250  Image[i].SetASCIIString(time_ss.str());
251  }
252  return 0;
253 }
254 
255 #ifdef BUILD_IMAGE_USHORT
257 {
258  struct timeval now;
259  if (ExternalTrigger_ && ActiveTrigger_ ) {
260  outb(0, ParPort_);
261  usleep(0);
262  outb(255,ParPort_);
263  }
264  if (ExternalTrigger_)
265  gettimeofday(&now,NULL);
266 
267  for (int i=0; i<NumCams_; i++) {
268  Camera_[i]->GrabSingle(Image[i]);
269  }
270 
271  // if external trigger use accurate timestamps
272  if (ExternalTrigger_) {
273  stringstream time_ss;
274  time_ss.str("");
275  time_ss<<now.tv_sec<<":"<<now.tv_usec;
276  // reset trigger
277  if ( ActiveTrigger_ )
278  outb(255,ParPort_);
279  for (int i=0; i<NumCams_; i++)
280  // Image[i].SetTime(now.tv_sec,now.tv_usec);
281  Image[i].SetASCIIString(time_ss.str());
282  }
283  return 0;
284 }
285 #endif
286 
287 
288 int VideoSource_MDcam::SetFeature(dc1394feature_t feature, unsigned int value)
289 {
290  int res;
291  for (int i=0; i<NumCams_; i++) {
292  res = Camera_[i]->SetFeature(feature, value);
293  if (res<0) return res;
294  }
295  return 0;
296 }
297 
299 {
300  int res;
301  for (int i=0; i<NumCams_; i++) {
302  res = Camera_[i]->SetGain(g);
303  if (res<0) return res;
304  }
305  return 0;
306 }
307 
309 {
310  int res;
311  for (int i=0; i<NumCams_; i++) {
312  res = Camera_[i]->SetShutter(exptime);
313  if (res<0) return res;
314  }
315  return 0;
316 }
317 
318 
319 // int GetFeature( int feature);
320 int VideoSource_MDcam::AutoMode( dc1394feature_t feature, dc1394feature_mode_t mode)
321 {
322  int res;
323  for (int i=0; i<NumCams_; i++) {
324  res = Camera_[i]->AutoMode(feature,mode);
325  if (res<0) return res;
326  }
327  return 0;
328 }
329 
330 int VideoSource_MDcam::OnePushAuto(dc1394feature_t feature)
331 {
332  int res;
333  for (int i=0; i<NumCams_; i++) {
334  res = Camera_[i]->OnePushAuto(feature);
335  if (res<0) return res;
336  }
337  return 0;
338 }
339 
340 int VideoSource_MDcam::ExternalTrigger(bool on, bool active)
341 {
342  int res;
343  if (on && active) {
344  if (ioperm(ParPort_, 3, 1)) {
345  BIASERR("Can not acces ParPort at "<<ParPort_<<": Permission denied."<<endl
346  <<" External Trigger disabled");
347  ExternalTrigger_ = false;
348  return -1;
349  }
350  // set all bits high
351  else outb(255, ParPort_);
352  }
353 
354  for (int i=0; i<NumCams_; i++) {
355  res = 0;
356  Camera_[i]->SetExternalTrigger(on);
357  if (res<0) return res;
358  }
359  ExternalTrigger_ = on;
360  ActiveTrigger_ = active;
361  return 0;
362 }
363 
364 
365 
366 
367 int VideoSource_MDcam::EqualizeFeature(int master, dc1394feature_t feature)
368 {
369  int value = Camera_[master]->GetFeature(feature);
370  for (int i=0; i<NumCams_; i++) {
371  if (i != master)
372  Camera_[i]->SetFeature(feature, value);
373  }
374 
375  return 0;
376 }
377 
378 
380 {
381  int res=0;
382  for (int i=0; i<NumCams_; i++) {
383  res = Camera_[i]->SetCamera(camparam);
384  if (res <0) return res;
385  }
386 
387  return res;
388 }
Stores the camera parameters such as Shutter, Gain, etc.
virtual void SetSize(int w, int h, int bytesperpixel=1)
Set image size and number of bytes per pixel (e.g.
int EqualizeFeature(int master, dc1394feature_t feature)
Read.
int SetCamera(CameraParameterSet camparam)
int GrabSingle(BIAS::Camera< unsigned char > *Image)
Call GrabSingle() with a prepared array of images to be filled.
int ExternalTrigger(bool on, bool active)
if on==true &amp;&amp; active=true try to open parport at ParPort_, if it fails, return -1 else set cameras...
int ScanBus(std::ofstream &ofs)
This class VideoSource_DCAM implements access to IEEE1394 (Firewire, iLink) cameras following the DCa...
int AutoMode(dc1394feature_t feature, dc1394feature_mode_t mode)
int OpenDevice()
Opens a dc1394 device.
int GetHeight() const
void SetDebugLevel(const long int lv)
Definition: Debug.hh:318
int SetShutter(float exptime)
The image template class for specific storage types.
Definition: Image.hh:78
int CloseDevice()
closes a camera device and cleans up
int SetFeature(dc1394feature_t feature, unsigned int value)
int InitCameras(const std::string filename)
int OnePushAuto(dc1394feature_t feature)