-1

I'm experiencing a strange behaviour of my Camera Module v2 when using the raspicam API in my code. Here is what I've done:

  1. I've downloaded the last version (0.1.3) of the raspicam API from https://sourceforge.net/projects/raspicam/files/

  2. I've added the missing .h file at line 5 in src/CMakeLists.txt:

    SET(public_hdrs_base raspicamtypes.h raspicam.h raspicam_still.h)

  3. I've successfully compiled and installed the API

  4. I've modified an example code as follows:

    #include <fstream>
    #include <cstdlib>
    #include <iostream>
    #include "raspicam/raspicam_still.h"
    
    using namespace std;
    
    //Returns the value of a param. If not present, returns the defvalue
    float getParamVal ( string id,int argc,char **argv,float defvalue ) {
          for ( int i=0; i<argc; i++ )
              if ( id== argv[i] )
                  return atof ( argv[i+1] );
          return defvalue;
      }
    
    //prints program command line usage
    void usage() {
        cout<<"-w val : sets image width (648 default)"<<endl;
        cout<<"-h val : sets image height (486 default)"<<endl;
        cout<<"-iso val: set iso [100,800] (400 default)"<<endl;
    }
    
    
    int main ( int argc, char *argv[] ) {
        usage();
    
        raspicam::RaspiCam_Still *Camera = new raspicam::RaspiCam_Still();
    
    //  int width = getParamVal ( "-w",argc,argv,2592 );
    //  int height =getParamVal ( "-h",argc,argv,1944 );
    int width = getParamVal ( "-w",argc,argv,648 );
    int height =getParamVal ( "-h",argc,argv,486 );
    int iso=getParamVal ( "-iso",argc,argv,400);
    
    
    cout << "Initializing ..."<<width<<"x"<<height<<endl;
    Camera->setWidth ( width );
    Camera->setHeight ( height );
    Camera->setISO(iso);
    Camera->setEncoding ( raspicam::RASPICAM_ENCODING_BMP );
    Camera->open();
    cout<<"capture"<<endl;
    unsigned int length = Camera->getImageBufferSize(); // Header + Image Data + Padding
    unsigned char * data = new unsigned char[length];
    if ( !Camera->grab_retrieve(data, length) ) {
        cerr<<"Error in grab"<<endl;
        return -1;
    }
    
    cout<<"saving picture.bmp"<<endl;
    ofstream file ( "picture.bmp",ios::binary );
    file.write ( ( char* ) data,   length );
    
    delete Camera;
    
    return 0;
    }
    
  5. I've compiled it using:

    g++ simpletest_raspicam.cpp -o simpletest_raspicam -I/usr/local/include -L/opt/vc/lib -lraspicam -lmmal -lmmal_core -lmmal_util

  6. I can run the executable and I get picture.bmp as output

  7. If I remove the picture.bmp and I try to get a picture again, my app can't reach the end and gets stucked as if some resources were not freed after the first run. To get another picture the only option is to reboot my RPi.

    root : ~/tmp> ./simpletest_raspicam -w val : sets image width (648 default) -h val : sets image height (486 default) -iso val: set iso [100,800] (400 default) Initializing ...648x486 capture saving picture.bmp root : ~/tmp> rm picture.bmp root : ~/tmp> ./simpletest_raspicam -w val : sets image width (648 default) -h val : sets image height (486 default) -iso val: set iso [100,800] (400 default) Initializing ...648x486

  8. This issue is not reproducible using raspistill; I can run it all the times I want:

    root : ~/tmp> raspistill -w 648 -h 486 -e bmp -o test1.bmp root : ~/tmp> raspistill -w 648 -h 486 -e bmp -o test2.bmp root : ~/tmp> raspistill -w 648 -h 486 -e bmp -o test3.bmp root : ~/tmp> ll -rwxr-xr-x 1 root 45K Jan 8 22:20 simpletest_raspicam -rw-r--r-- 1 root 1.6K Jan 8 22:20 simpletest_raspicam.cpp -rw-r--r-- 1 root 923K Jan 8 22:35 test1.bmp -rw-r--r-- 1 root 923K Jan 8 22:35 test2.bmp -rw-r--r-- 1 root 923K Jan 8 22:35 test3.bmp

Any idea on what I am missing?

  • Same issue with version 0.1.6! – TShirt Mar 12 '17 at 9:29
0

I had the same issue with RaspiCam_Cv. It turned out an object is automatically created when you declare it, so instead of writing:

raspicam::RaspiCam_Still *Camera = raspicam::RaspiCam_Still();

which creates two objects, try simply:

raspicam::RaspiCam_Still Camera;

It believe it has something to do with instantiating two mmal clients, but I don't know what the details are.

  • Hi, thanks for your answer, unfortunately nothing changed. The second run still gets stuck. It looks like some system resource is not released on exit and the second time I run I can not access it. – TShirt Apr 8 '17 at 21:32
0

The Raspicam Still implementation doesn't have a working release(). So the code hangs when you try to re-open the camera.

My C++ isn't that great yet, so I'm having a hard time working out how to implement the release() function myself. It seems to be a bug that no one knows how to resolve, for it's been in this API for over 3 years already ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.