/* main.c --------- Co. E.Benninghaus Pfaffenhoffentstr. 1 78166 Donaueschingen created: 08.09.2000 last Work: 08.09.2000 */ #ifndef __main_cxx #define __main_cxx #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include int run = 0; pthread_t ethread = NULL; #include "elctrcam.h" #include "filter.c" #include "compare.c" #include "cam.c" #ifdef __cplusplus extern "C" { #endif #include "../elctr/elctr.c" #ifdef __cplusplus } #endif #include "config.c" #include "special.c" #ifdef VIEW #include "win.cxx" #include #include #include "callback.cxx" #endif typedef void ( *sighandler_t)(int); sighandler_t sighandler_old = SIG_ERR; void sigterminate(int sig) { if(sig != SIGTERM)return; if(sighandler_old != SIG_ERR){ signal(SIGTERM,sighandler_old); sighandler_old = SIG_ERR; } endall(); exit(0); } void sigrestore(void) { if(sighandler_old == SIG_ERR)return; signal(SIGTERM,sighandler_old); sighandler_old = SIG_ERR; } int sigset(void) { sigrestore(); sighandler_old = signal(SIGTERM,sigterminate); if(sighandler_old == SIG_ERR)return(-1); return(0); } int guideinit = 0; int correctactive = 0; int threaded_grab_busy = 0; void endall( void ) { run = 0; while( threaded_grab_busy ){ sleep(1); } close_video(); freeBuffers(); if( cfg.elctr && (! cfg.test) ){ ctrendall(); } sigrestore(); #ifdef VIEW if(cfgwin != NULL){ cfgwin->~Fl_Window(); } if(mainwin != NULL){ mainwin->~Fl_Window(); } #else exit(0); #endif } int __prevShowCol = 0; void ShowFlag( int col ) { if(col == __prevShowCol){ return; } __prevShowCol = col; #ifdef VIEW mainwin->make_current(); #endif switch(col){ #ifdef VIEW case 0: fl_rectf(5, 150, 160, 15, 0,0,0); break; case 1: fl_rectf(5, 150, 160, 15, 0,255,0); break; case 2: fl_rectf(5, 150, 160, 15, 0,0,255); break; case 3: fl_rectf(5, 150, 160, 15, 255,0,0); break; #else case 0: break; case 1: break; case 2: break; case 3: break; #endif default: break; } } /* This is called by elctr-loop, whenever no event is present */ void elctr_thread_noevent( void ) { if( ( cfg.control ) && ( elctr.motorrun == MNORUN ) ) { if(elctr.fruse){ fr_work(); } if( cfg.guide && guideinit ) { guide_work(); } } ctrif_waitmillisecs( 50 ); } /* This is called by elctr-loop, whenever key follow is pressed */ void elctr_kp_follow( void ) { #ifdef VIEW cb_btn_control(NULL,NULL); #endif } /* This is called by elctr-loop, whenever key high+follow is pressed */ void elctr_kp_high_follow( void ) { if( ( cfg.control ) && ( elctr.motorrun == MNORUN ) ) { } } int correctresult = 0; int steerflag = 0; void do_work( void ) { int ct,x,y; if( prepare_image() ){ /*-- start of calculations -----*/ if( count <= cfg.calcstart ){ /* prepare also the first picture, which can then be compared with the following*/ resetPrevCorrectionDir(); filter( v_firstbuf, &fv1 ); #ifdef VIEW txt_mincol->value( fv1.mincol ); txt_maxcol->value( fv1.maxcol ); #endif if( ! find( v_firstbuf, &fv1, &cv1 ) ){ ShowFlag(3); } else { if( ( cfg.guide ) && ( count == cfg.calcstart ) && ( ! guideinit ) && ( elctr.motorrun == MNORUN ) ) { guideinit = guide_initialisation_work(); } } } else { filter( v_imagebuf, &fv2 ); if( ( correctresult = compare( v_imagebuf ) ) > 0 ) { /* and here we do the stuff of calculations results*/ if(correctresult == 1 ){ /* all is ok, and picture on correct position */ if(! correctactive){ correctactive = 1; } ShowFlag(1); if( steerflag && cfg.control ){ doCorrect( 1 ); steerflag = 0; } if( cfg.snap && run ){ /*get greater picture, and save it on correct position*/ snap_image(); } } else { /* picture is no more in correct position */ ShowFlag(2); if(cfg.control){ if( (cfg.elctr) && (correctactive) ){ if( steerflag && cv1.changed ){ steerflag = 0; } if(! steerflag){ doCorrect( 0 ); steerflag = 1; } } } } if(cfg.showcross && cfg.view){ if(cfg.savebuf){ drawCross( v_savebuf, cv1.spot[0].x, cv1.spot[0].y, 10 , 180 ); } else { drawCross( v_imagebuf, cv1.spot[0].x, cv1.spot[0].y, 10 , 180 ); } } /* ----------------------------------------------- */ } else { ShowFlag(3); } } /*--- end of calculations ------*/ #ifdef VIEW if(( mainwin != NULL) && (cfg.view) ){ mainwin->make_current(); if(cfg.savebuf){ fl_draw_image((const uchar *)v_savebuf,5,25, WIDTH ,HEIGHT,1,0); } else { fl_draw_image((const uchar *)v_imagebuf,5,25,WIDTH ,HEIGHT,1,0); } } #endif } } void threaded_grabbing(char * data) { ShowFlag(0); if(! createBuffers()){ return; } correctactive = 0; guideinit = 0; run = 1; count = 0; threaded_grab_busy = 1; steerflag = 0; while(run){ if( get_video_image() ){ do_work(); } } correctactive = 0; freeBuffers(); run = 0; threaded_grab_busy = 0; return; } int main(int argc, char * argv[]) { int arg; char *p; if(! readconfigfile( "elctrcamrc" )){ fprintf(stderr,"Invalid settings, or configfile, - steering not valid\n"); if(! cfg.test){ cfg.elctr = 0; } } if(! parsecommandline( argc, argv )){ exit(0); } if( sigset() < 0){ fprintf(stderr,"Cannot install signalhandler\n"); exit(-1); } if(! open_video()){ fprintf(stderr,"Cannot open video\n"); sigrestore(); exit(0); } if(cfg.elctr && (! cfg.test )){ ctrSetCB_noevent( &elctr_thread_noevent ); if(! ctrstartall( 1 ) ){ cfg.elctr = 0; fprintf(stderr,"Cannot start elctr, - steering not valid\n"); } else { ctrSetCB_kp_follow( &elctr_kp_follow ); ctrSetCB_kp_high_follow( &elctr_kp_high_follow ); } } #ifdef VIEW make_window(); if(cfg.savebuf){ btn_realview->value(1); } else { btn_realview->value(0); } if( cfg.snap ){ btn_snap->value(1); } else { btn_snap->value(0); } if( cfg.view ){ btn_view->value(1); } else { btn_view->value(0); } if( cfg.control && cfg.elctr ){ btn_control->value(1); } else { btn_control->value(0); } ch_snapsize->add("160x120 QSIF"); ch_snapsize->add("176x144 QCIF"); ch_snapsize->add("320x240 SIF"); ch_snapsize->add("352x288 CIF"); ch_snapsize->add("640x480 VGA"); ch_snapsize->value(cfg.snapsize); mainwin->end(); Fl::visual(FL_RGB); mainwin->show(); return Fl::run(); #endif endall(); } #endif /*__main_cxx*/