Changeset 351 in flair-src for trunk/lib/FlairSensorActuator/src


Ignore:
Timestamp:
Feb 19, 2020, 3:10:11 PM (5 years ago)
Author:
Sanahuja Guillaume
Message:

update v4l

Location:
trunk/lib/FlairSensorActuator/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/FlairSensorActuator/src/V4LCamera.cpp

    r350 r351  
    2424#include <FrameworkManager.h>
    2525#include <fcntl.h>
    26 #include <linux/videodev2.h>
     26//#include <linux/videodev2.h>
    2727#include <sys/ioctl.h>
    2828#include <unistd.h>
     
    4040V4LCamera::V4LCamera(string name,uint8_t camera_index, uint16_t width, uint16_t height,
    4141                     Image::Type::Format format, uint8_t priority)
    42     : Thread(getFrameworkManager(), name, priority,1024*1024),
     42    : Thread(getFrameworkManager(), name, priority),
    4343      Camera(name, width, height, format) {
    4444 
     
    6363  }
    6464 
    65   dQueuedBuffer.index=-1;//mark current buffer not valid for initialisation
     65  //todo: better handling of detection, only neeeded for omap/dm
     66  //also in run, copy from v4l to cmem is not necessary if not using cmem...
     67#ifdef ARMV7A
     68  useMemoryUsrPtr=true;
     69#else
    6670  useMemoryUsrPtr=false;
     71#endif
    6772  if(useMemoryUsrPtr) {
    6873    AllocUserBuffers();
     
    153158}
    154159
    155 /*
    156 void V4LCamera::Run(void) {
    157   Time cam_time, new_time, fpsNow, fpsPrev;
    158   int fpsCounter = 0;
    159 
    160   // init image old
    161   GrabFrame();
    162   cam_time = GetTime();
    163   fpsPrev = cam_time;
    164 
    165   while (!ToBeStopped()) {
    166     //check for ps3eye deconnection in hds uav
    167     if(hasProblems==false) {
    168       struct v4l2_format form;
    169       form.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    170       xioctl(device, VIDIOC_G_FMT,&form);
    171       if(xioctl (device, VIDIOC_G_FMT,&form)<0) {
    172         Thread::Warn("camera disconnected\n");
    173         hasProblems=true;
    174       }
    175     }
    176    
    177     // fps counter
    178     fpsCounter++;
    179     if (GetTime() > (fpsPrev + 5 * (Time)1000000000)) {
    180       // every 5 secondes
    181       fpsNow = GetTime();
    182       fps->SetText("fps: %.1f",
    183       fpsCounter / ((float)(fpsNow - fpsPrev) / 1000000000.));
    184       fpsCounter = 0;
    185       fpsPrev = fpsNow;
    186     }
    187    
    188     // cam properties
    189     if (gain->ValueChanged() == true && autogain->Value() == false)
    190       SetGain(gain->Value());
    191     if (exposure->ValueChanged() == true && autoexposure->Value() == false)
    192       SetExposure(exposure->Value());
    193     if (bright->ValueChanged() == true)
    194       SetBrightness(bright->Value());
    195     if (sat->ValueChanged() == true)
    196       SetSaturation(sat->Value());
    197     if (contrast->ValueChanged() == true)
    198       SetContrast(contrast->Value());
    199     if (hue->ValueChanged() == true)
    200       SetHue(hue->Value());
    201     if (sharpness->ValueChanged() == true)
    202       SetProperty(V4L2_CID_SHARPNESS, sharpness->Value());
    203     if (autogain->ValueChanged() == true) {
    204       if (autogain->Value() == true) {
    205         gain->setEnabled(false);
    206       } else {
    207         gain->setEnabled(true);
    208         SetGain(gain->Value());
    209       }
    210       SetAutoGain(autogain->Value());
    211     }
    212     if (autoexposure->ValueChanged() == true) {
    213       if (autoexposure->Value() == true) {
    214         exposure->setEnabled(false);
    215       } else {
    216         exposure->setEnabled(true);
    217         SetExposure(exposure->Value());
    218       }
    219       SetAutoExposure(autoexposure->Value());
    220     }
    221     if (awb->ValueChanged() == true)
    222       SetProperty(V4L2_CID_AUTO_WHITE_BALANCE, awb->Value());
    223 
    224     // get picture
    225     GrabFrame();
    226     new_time = GetTime();
    227    
    228     //check for ps3eye deconnection in hds uav
    229     if(new_time-cam_time>100*1000*1000) {
    230       Thread::Warn("delta trop grand\n");
    231       hasProblems=true;
    232     }
    233 
    234     output->GetMutex();
    235     output->buffer=(char*)buffers[bufferIndex].start;
    236     output->ReleaseMutex();
    237 
    238     output->SetDataTime(cam_time);
    239     ProcessUpdate(output);
    240 
    241     cam_time = new_time;
    242   }
    243 
    244   close(device);
    245 }
    246 */
    247160int V4LCamera::AllocV4LBuffers() {
    248161  struct v4l2_requestbuffers req;
     
    304217  //allocate output data
    305218  imageData = AllocFunction(output->GetDataType().GetSize());
    306   Printf("cmem allocated %i at %x\n",output->GetDataType().GetSize(),imageData);
     219  //Printf("cmem allocated %i at %x\n",output->GetDataType().GetSize(),imageData);
    307220
    308221  return 1;
     
    333246  return 1;
    334247};
    335 /*
     248
    336249int V4LCamera::GrabFrame(void) {
    337     //queue previous buffer
    338     if(QueueBuffer(bufferIndex)<0) return -1;
    339    
    340     fd_set fds;
    341     struct timeval tv;
    342     FD_ZERO (&fds);
    343     FD_SET (device, &fds);
    344 
    345     tv.tv_sec = 0;
    346     tv.tv_usec = 100000;
    347 
    348     int r = select (device+1, &fds, NULL, NULL, &tv);
    349 
    350     if (-1 == r) {
    351         char errorMsg[256];
    352         Thread::Err("select (%s)\n", strerror_r(-r, errorMsg, sizeof(errorMsg)));
    353         return -1;
    354     }
    355 
    356     if (0 == r) {
    357         Thread::Err("select timeout\n");
    358         return -1;
    359     }
    360 
    361     struct v4l2_buffer buf;
    362     memset(&buf, 0, sizeof (v4l2_buffer));
    363     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
    364     buf.memory = V4L2_MEMORY_USERPTR;//V4L2_MEMORY_MMAP;
    365 
    366     //get last captured image
    367     int prevDQbuf=-1;
    368     for(int i=0;i<nbBuffers;i++) {
    369         if (xioctl (device, VIDIOC_DQBUF, &buf)==-1) {
    370             if (errno==EAGAIN) {
    371                 break;
    372             } else {
    373                 Thread::Err("VIDIOC_DQBUF xioctl\n");
    374                 return -1;
    375             }
    376        } else {
    377            if(prevDQbuf!=-1) {
    378                QueueBuffer(prevDQbuf);
    379            }
    380            for (int i=0; i<nbBuffers; i++) {
    381                if((void*)(buf.m.userptr)==buffers[i]) {
    382                    prevDQbuf=i;
    383                    bufferIndex=i;
    384                    break;
    385                }
    386            }
    387        }
    388     }
    389    
    390    return 1;
    391 }
    392 */
    393 int V4LCamera::cvGrabFrame(void) {
    394   if(QueueBuffer(&dQueuedBuffer)<0) return -1;
    395    
    396250  fd_set fds;
    397251  struct timeval tv;
     
    424278  }
    425279
    426    //get last captured image
     280   //get last captured buffer
    427281  dQueuedBuffer.index=-1;
    428282  for(int i=0;i<nbBuffers;i++) {
     
    505359   
    506360    // cam pictures
    507     if (!cvGrabFrame()) {
     361    if (!GrabFrame()) {
    508362      Printf("Could not grab a frame\n");
    509     }
    510    
    511     //copy to CMEM allocated buffer if necessary
    512     if(!useMemoryUsrPtr) {
    513       memcpy(imageData,(char *)buffers[dQueuedBuffer.index],output->GetDataType().GetSize());
    514363    }
    515364       
     
    530379    }*/
    531380
     381    //select buffer
     382    if(useMemoryUsrPtr) {//buffers are already in CMEM
     383      imageData=(char*)buffers[dQueuedBuffer.index];
     384      //dequeue it latter (buffer used by ProcessUpdate)
     385    } else {//copy to CMEM allocated buffer
     386      memcpy(imageData,(char *)buffers[dQueuedBuffer.index],output->GetDataType().GetSize());
     387      QueueBuffer(&dQueuedBuffer);//we can do it right now
     388    }
     389   
    532390    output->GetMutex();
    533391    output->buffer=imageData;
     
    537395    ProcessUpdate(output);
    538396    cam_time = new_time;
     397   
     398    if(useMemoryUsrPtr) {
     399      QueueBuffer(&dQueuedBuffer);//now it is possible to dequeue
     400    }
    539401  }
    540402}
  • trunk/lib/FlairSensorActuator/src/V4LCamera.h

    r350 r351  
    157157  void SetProperty(int property,float value);
    158158  float GetProperty(int property);
    159   //int GrabFrame(void);
     159  int GrabFrame(void);
    160160  int AllocV4LBuffers(void);
    161161  int AllocUserBuffers(void);
     
    163163  int QueueBuffer(struct v4l2_buffer *buf);
    164164  struct v4l2_buffer dQueuedBuffer;
    165  
    166 
    167 int cvGrabFrame(void);
    168 
    169 
    170 
    171165};
    172166} // end namespace sensor
Note: See TracChangeset for help on using the changeset viewer.