Projektarbeit Line Following Robot bei Prof. Chowanetz im WS22/23
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

raspicam_test.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**********************************************************
  2. Software developed by AVA ( Ava Group of the University of Cordoba, ava at uco dot es)
  3. Main author Rafael Munoz Salinas (rmsalinas at uco dot es)
  4. This software is released under BSD license as expressed below
  5. -------------------------------------------------------------------
  6. Copyright (c) 2013, AVA ( Ava Group University of Cordoba, ava at uco dot es)
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without
  9. modification, are permitted provided that the following conditions
  10. are met:
  11. 1. Redistributions of source code must retain the above copyright
  12. notice, this list of conditions and the following disclaimer.
  13. 2. Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. 3. All advertising materials mentioning features or use of this software
  17. must display the following acknowledgement:
  18. This product includes software developed by the Ava group of the University of Cordoba.
  19. 4. Neither the name of the University nor the names of its contributors
  20. may be used to endorse or promote products derived from this software
  21. without specific prior written permission.
  22. THIS SOFTWARE IS PROVIDED BY AVA ''AS IS'' AND ANY
  23. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. DISCLAIMED. IN NO EVENT SHALL AVA BE LIABLE FOR ANY
  26. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ****************************************************************/
  33. #include <iostream>
  34. #include <ctime>
  35. #include <cstdlib>
  36. #include <fstream>
  37. #include <sstream>
  38. #include <sys/timeb.h>
  39. #include "raspicam.h"
  40. using namespace std;
  41. bool doTestSpeedOnly=false;
  42. bool useUserCallback=false;
  43. size_t nFramesCaptured=100;
  44. //parse command line
  45. //returns the index of a command line param in argv. If not found, return -1
  46. int findParam ( string param,int argc,char **argv ) {
  47. int idx=-1;
  48. for ( int i=0; i<argc && idx==-1; i++ )
  49. if ( string ( argv[i] ) ==param ) idx=i;
  50. return idx;
  51. }
  52. //parse command line
  53. //returns the value of a command line param. If not found, defvalue is returned
  54. float getParamVal ( string param,int argc,char **argv,float defvalue=-1 ) {
  55. int idx=-1;
  56. for ( int i=0; i<argc && idx==-1; i++ )
  57. if ( string ( argv[i] ) ==param ) idx=i;
  58. if ( idx==-1 ) return defvalue;
  59. else return atof ( argv[ idx+1] );
  60. }
  61. raspicam::RASPICAM_EXPOSURE getExposureFromString ( string str ) {
  62. if ( str=="OFF" ) return raspicam::RASPICAM_EXPOSURE_OFF;
  63. if ( str=="AUTO" ) return raspicam::RASPICAM_EXPOSURE_AUTO;
  64. if ( str=="NIGHT" ) return raspicam::RASPICAM_EXPOSURE_NIGHT;
  65. if ( str=="NIGHTPREVIEW" ) return raspicam::RASPICAM_EXPOSURE_NIGHTPREVIEW;
  66. if ( str=="BACKLIGHT" ) return raspicam::RASPICAM_EXPOSURE_BACKLIGHT;
  67. if ( str=="SPOTLIGHT" ) return raspicam::RASPICAM_EXPOSURE_SPOTLIGHT;
  68. if ( str=="SPORTS" ) return raspicam::RASPICAM_EXPOSURE_SPORTS;
  69. if ( str=="SNOW" ) return raspicam::RASPICAM_EXPOSURE_SNOW;
  70. if ( str=="BEACH" ) return raspicam::RASPICAM_EXPOSURE_BEACH;
  71. if ( str=="VERYLONG" ) return raspicam::RASPICAM_EXPOSURE_VERYLONG;
  72. if ( str=="FIXEDFPS" ) return raspicam::RASPICAM_EXPOSURE_FIXEDFPS;
  73. if ( str=="ANTISHAKE" ) return raspicam::RASPICAM_EXPOSURE_ANTISHAKE;
  74. if ( str=="FIREWORKS" ) return raspicam::RASPICAM_EXPOSURE_FIREWORKS;
  75. return raspicam::RASPICAM_EXPOSURE_AUTO;
  76. }
  77. raspicam::RASPICAM_AWB getAwbFromString ( string str ) {
  78. if ( str=="OFF" ) return raspicam::RASPICAM_AWB_OFF;
  79. if ( str=="AUTO" ) return raspicam::RASPICAM_AWB_AUTO;
  80. if ( str=="SUNLIGHT" ) return raspicam::RASPICAM_AWB_SUNLIGHT;
  81. if ( str=="CLOUDY" ) return raspicam::RASPICAM_AWB_CLOUDY;
  82. if ( str=="SHADE" ) return raspicam::RASPICAM_AWB_SHADE;
  83. if ( str=="TUNGSTEN" ) return raspicam::RASPICAM_AWB_TUNGSTEN;
  84. if ( str=="FLUORESCENT" ) return raspicam::RASPICAM_AWB_FLUORESCENT;
  85. if ( str=="INCANDESCENT" ) return raspicam::RASPICAM_AWB_INCANDESCENT;
  86. if ( str=="FLASH" ) return raspicam::RASPICAM_AWB_FLASH;
  87. if ( str=="HORIZON" ) return raspicam::RASPICAM_AWB_HORIZON;
  88. return raspicam::RASPICAM_AWB_AUTO;
  89. }
  90. void processCommandLine ( int argc,char **argv,raspicam::RaspiCam &Camera ) {
  91. Camera.setWidth ( getParamVal ( "-w",argc,argv,1280 ) );
  92. Camera.setHeight ( getParamVal ( "-h",argc,argv,960 ) );
  93. Camera.setBrightness ( getParamVal ( "-br",argc,argv,50 ) );
  94. Camera.setSharpness ( getParamVal ( "-sh",argc,argv,0 ) );
  95. Camera.setContrast ( getParamVal ( "-co",argc,argv,0 ) );
  96. Camera.setSaturation ( getParamVal ( "-sa",argc,argv,0 ) );
  97. Camera.setShutterSpeed( getParamVal ( "-ss",argc,argv,0 ) );
  98. Camera.setISO ( getParamVal ( "-iso",argc,argv ,400 ) );
  99. if ( findParam ( "-vs",argc,argv ) !=-1 )
  100. Camera.setVideoStabilization ( true );
  101. Camera.setExposureCompensation ( getParamVal ( "-ec",argc,argv ,0 ) );
  102. if ( findParam ( "-gr",argc,argv ) !=-1 )
  103. Camera.setFormat(raspicam::RASPICAM_FORMAT_GRAY);
  104. if ( findParam ( "-yuv",argc,argv ) !=-1 )
  105. Camera.setFormat(raspicam::RASPICAM_FORMAT_YUV420);
  106. if ( findParam ( "-test_speed",argc,argv ) !=-1 )
  107. doTestSpeedOnly=true;
  108. if ( findParam ( "-usr_cb",argc,argv ) !=-1 )
  109. useUserCallback=true;
  110. int idx;
  111. if ( ( idx=findParam ( "-ex",argc,argv ) ) !=-1 )
  112. Camera.setExposure ( getExposureFromString ( argv[idx+1] ) );
  113. if ( ( idx=findParam ( "-awb",argc,argv ) ) !=-1 )
  114. Camera.setAWB( getAwbFromString ( argv[idx+1] ) );
  115. nFramesCaptured=getParamVal("-nframes",argc,argv,100);
  116. Camera.setAWB_RB(getParamVal("-awb_b",argc,argv ,1), getParamVal("-awb_g",argc,argv ,1));
  117. Camera.setFrameRate(getParamVal("-fps",argc,argv, 0));
  118. }
  119. void showUsage() {
  120. cout<<"Usage: "<<endl;
  121. cout<<"[-help shows this help]\n"<<endl;
  122. cout<<"[-gr sets gray color mode]\n"<<endl;
  123. cout<<"[-test_speed use for test speed and no images will be saved]\n";
  124. cout<<"[-usr_cb use for test the user-defined callback mechanism]\n";
  125. cout<<"[-yuv sets yuv420 color mode]\n"<<endl;
  126. cout<<"[-w width] [-h height] \n[-br brightness_val(0,100)]\n[-sh sharpness_val (-100 to 100)]\n";
  127. cout<<"[-co contrast_val (-100 to 100)]\n[-sa saturation_val (-100 to 100)]\n";
  128. cout<<"[-iso ISO_val (100 to 800)]\n[-vs turns on video stabilisation]\n[-ec exposure_compensation_value(-10,10)]\n";
  129. cout<<"[-ss shutter_speed (value in microsecs (max 330000)]\n[-ec exposure_compensation_value(-10,10)]\n";
  130. cout<<"[-exp mode (OFF,AUTO,NIGHT,NIGHTPREVIEW,BACKLIGHT,SPOTLIGHT,SPORTS,SNOW,BEACH,VERYLONG,FIXEDFPS,ANTISHAKE,FIREWORKS)]"<<endl;
  131. cout<<"[-awb (OFF,AUTO,SUNLIGHT,CLOUDY,TUNGSTEN,FLUORESCENT,INCANDESCENT,FLASH,HORIZON)]"<<endl;
  132. cout<<"[-nframes val: number of frames captured (100 default). 0 == Infinite lopp]\n";
  133. cout<<"[-awb_r val:(0,8):set the value for the red component of white balance]"<<endl;
  134. cout<<"[-awb_g val:(0,8):set the value for the green component of white balance]"<<endl;
  135. cout<<"[-fps frame_rate (0 to 120) 0 auto]\n";
  136. cout<<endl;
  137. }
  138. //timer functions
  139. #include <sys/time.h>
  140. #include <unistd.h>
  141. class Timer{
  142. private:
  143. struct timeval _start, _end;
  144. public:
  145. Timer(){}
  146. void start(){
  147. gettimeofday(&_start, NULL);
  148. }
  149. void end(){
  150. gettimeofday(&_end, NULL);
  151. }
  152. double getSecs(){
  153. return double(((_end.tv_sec - _start.tv_sec) * 1000 + (_end.tv_usec - _start.tv_usec)/1000.0) + 0.5)/1000.;
  154. }
  155. };
  156. void saveImage ( string filepath,unsigned char *data,raspicam::RaspiCam &Camera ) {
  157. std::ofstream outFile ( filepath.c_str(),std::ios::binary );
  158. if ( Camera.getFormat()==raspicam::RASPICAM_FORMAT_BGR || Camera.getFormat()==raspicam::RASPICAM_FORMAT_RGB ) {
  159. outFile<<"P6\n";
  160. } else if ( Camera.getFormat()==raspicam::RASPICAM_FORMAT_GRAY ) {
  161. outFile<<"P5\n";
  162. } else if ( Camera.getFormat()==raspicam::RASPICAM_FORMAT_YUV420 ) { //made up format
  163. // Openable as a .yuv format
  164. outFile.write ( ( char* ) data,Camera.getImageBufferSize() );
  165. return;
  166. }
  167. outFile<<Camera.getWidth() <<" "<<Camera.getHeight() <<" 255\n";
  168. outFile.write ( ( char* ) data,Camera.getImageBufferSize() );
  169. }
  170. string getExtension ( raspicam::RaspiCam &Camera ) {
  171. if ( Camera.getFormat()==raspicam::RASPICAM_FORMAT_YUV420 ) {
  172. return ".yuv";
  173. }
  174. else {
  175. return ".ppm";
  176. }
  177. }
  178. struct CallBackData_t{
  179. raspicam::RaspiCam* Camera;
  180. unsigned char *data;
  181. size_t i=0;
  182. volatile bool isCapturing = false;
  183. };
  184. void vidInCallback(void* data){
  185. // new image captured
  186. if (0 == data) return;
  187. struct CallBackData_t* camData = (struct CallBackData_t*)data;
  188. if (!camData->isCapturing) return;
  189. camData->Camera->retrieve ( camData->data );
  190. if ( !doTestSpeedOnly ) {
  191. if ( camData->i%5==0 ) cout<<"\r capturing ..."<<camData->i<<"/"<<nFramesCaptured<<std::flush;
  192. if ( camData->i%30==0 && camData->i!=0 && nFramesCaptured>0 ) { //save image if not in inifite loop
  193. std::stringstream fn;
  194. fn<<"image";
  195. if (camData->i<10) fn<<"0";
  196. fn<<camData->i<<getExtension(*camData->Camera);
  197. saveImage ( fn.str(),camData->data,*(camData->Camera) );
  198. cerr<<"Saving "<<fn.str()<<endl;
  199. }
  200. }
  201. if (++(camData->i) >= nFramesCaptured){
  202. //stops when nFrames captured or at infinity lpif nFramesCaptured<0
  203. camData->Camera->setUserCallback(0);
  204. camData->isCapturing = false;
  205. }
  206. }
  207. int main ( int argc,char **argv ) {
  208. if ( argc==1 ) {
  209. cerr<<"Usage (-help for help)"<<endl;
  210. }
  211. if ( findParam ( "-help",argc,argv ) !=-1) {
  212. showUsage();
  213. return -1;
  214. }
  215. raspicam::RaspiCam Camera;
  216. processCommandLine ( argc,argv,Camera );
  217. cout<<"Connecting to camera"<<endl;
  218. if ( !Camera.open() ) {
  219. cerr<<"Error opening camera"<<endl;
  220. return -1;
  221. }
  222. cout<<"Connected to camera ="<<Camera.getId() <<" bufs="<<Camera.getImageBufferSize( )<<endl;
  223. unsigned char *data=new unsigned char[ Camera.getImageBufferSize( )];
  224. Timer timer;
  225. // prepare callback data
  226. struct CallBackData_t camData;
  227. if (useUserCallback) {
  228. camData.Camera = &Camera;
  229. camData.data = data;
  230. camData.i = 0;
  231. // set the user callback
  232. Camera.setUserCallback(&vidInCallback, &camData);
  233. cout<<"user callback set"<<endl;
  234. }
  235. cout<<"Capturing...."<<endl;
  236. size_t i=0;
  237. timer.start();
  238. if (useUserCallback) {
  239. // start the test
  240. camData.isCapturing = true;
  241. while( camData.isCapturing) {usleep(10);};
  242. } else {
  243. // not using user callback
  244. do{
  245. Camera.grab();
  246. Camera.retrieve ( data );
  247. if ( !doTestSpeedOnly ) {
  248. if ( i%5==0 ) cout<<"\r capturing ..."<<i<<"/"<<nFramesCaptured<<std::flush;
  249. if ( i%30==0 && i!=0 && nFramesCaptured>0 ) { //save image if not in inifite loop
  250. std::stringstream fn;
  251. fn<<"image";
  252. if (i<10) fn<<"0";
  253. fn<<i<<getExtension(Camera);
  254. saveImage ( fn.str(),data,Camera );
  255. cerr<<"Saving "<<fn.str()<<endl;
  256. }
  257. }
  258. }while(++i<nFramesCaptured || nFramesCaptured==0);//stops when nFrames captured or at infinity lpif nFramesCaptured<0
  259. }
  260. timer.end();
  261. if ( !doTestSpeedOnly ) cout<<endl<<"Images saved in imagexx"<<getExtension(Camera)<<endl;
  262. cerr<< timer.getSecs()<< " seconds for "<< nFramesCaptured<< " frames : FPS " << ( ( float ) ( nFramesCaptured ) / timer.getSecs() ) <<endl;
  263. Camera.release();
  264. }