18 #ifndef __framegrabber_h__
19 #define __framegrabber_h__
23 #include "../lib/sharedim_stack.h"
82 framegrabber(std::string,
const std::string &conffile)
throw (AOException);
166 std::string
auto_command(std::string com,
char *seqmsg);
167 std::string common_command(std::string com,
int cmode);
173 int init_save_interval;
185 template <
class dataT> framegrabber<dataT>::framegrabber(std::string name,
const std::string &conffile)
throw (AOException) : VisAOApp_standalone(name, conffile)
190 template <
class dataT> framegrabber<dataT>::~framegrabber()
200 num_sharedim = (
int)(ConfigDictionary())[
"num_sharedim"];
202 catch(Config_File_Exception)
206 _logger->log(Logger::LOG_LEV_INFO,
"Set number of images in the shared image buffer (num_sharedim): %i", num_sharedim);
210 max_imsize = (
int)(ConfigDictionary())[
"max_imsize"];
212 catch(Config_File_Exception)
214 max_imsize = 1024*1024;
216 _logger->log(Logger::LOG_LEV_INFO,
"Set the maximum image size to (max_imsize): %i", max_imsize);
220 shmem_key = (
int)(ConfigDictionary())[
"shmem_key"];
222 catch(Config_File_Exception)
226 _logger->log(Logger::LOG_LEV_INFO,
"Set the shared memory key (shmem_key): %i", shmem_key);
230 dark_shmem_key = (
int)(ConfigDictionary())[
"dark_shmem_key"];
232 catch(Config_File_Exception)
236 _logger->log(Logger::LOG_LEV_INFO,
"Set the shared memory key (dark_shmem_key): %i", shmem_key);
239 std::string wPingPath, wPingName;
243 wPingPath = (std::string)(ConfigDictionary())[
"writer_ping_path"];
245 catch(Config_File_Exception)
252 wPingName = (std::string)(ConfigDictionary())[
"writer_ping_name"];
255 catch(Config_File_Exception)
260 std::string sPingPath, sPingName;
264 sPingPath = (std::string)(ConfigDictionary())[
"server_ping_path"];
267 catch(Config_File_Exception)
274 sPingName = (std::string)(ConfigDictionary())[
"server_ping_name"];
277 catch(Config_File_Exception)
284 setup_baseApp(0, 1, 1, 1,
false);
287 std::string visao_root = getenv(
"VISAO_ROOT");
292 tmppath = visao_root +
"/" + wPingPath +
"/" + wPingName;
294 writerPingChan = nPings;
298 _logger->log(Logger::LOG_LEV_INFO,
"Set writer ping: %s", tmppath.c_str());
302 serverPingEnabled = 0;
305 tmppath = visao_root +
"/" + sPingPath +
"/" + sPingName;
307 serverPingChan = nPings;
311 _logger->log(Logger::LOG_LEV_INFO,
"Set server ping", tmppath.c_str());
314 serverPingEnabled = 1;
319 init_save_interval = (
int)(ConfigDictionary())[
"init_save_interval"];
321 catch(Config_File_Exception)
323 init_save_interval = 100;
325 _logger->log(Logger::LOG_LEV_INFO,
"Set interval to save init info to disk (init_save_interval): %i", init_save_interval);
332 frameNo = (*init_vars)[
"frameNo"];
334 catch(Config_File_Exception)
345 if(dark_sis.create_shm(dark_shmem_key, num_sharedim,
sizeof(
sharedim_stack_header) + num_darks*
sizeof(intptr_t) + (num_darks)*(
sizeof(
sharedim<dataT>) + max_imsize*
sizeof(dataT))) != 0)
347 ERROR_REPORT(
"Error attaching to shared memory for dark frame.");
351 dark_sis.header->save_sequence = 0;
352 dark_sim = dark_sis.set_next_image(1024, 1024);
353 dark_temp.resize(1024*1024);
354 dark_sis.enable_next_image();
356 for(sh_i=0; sh_i<1024; sh_i++)
358 for(sh_j=0; sh_j<1024; sh_j++)
360 dark_sim->imdata[sh_i*1024+sh_j] = 0.;
365 writerPingEnabled = 0;
369 STOP_FRAMEGRABBER = 1;
379 if(sis.create_shm(shmem_key, num_sharedim,
sizeof(
sharedim_stack_header) + num_sharedim*
sizeof(intptr_t) + (num_sharedim)*(
sizeof(
sharedim<dataT>) + max_imsize*
sizeof(dataT))) != 0)
381 ERROR_REPORT(
"Error attaching to shared memory.");
385 sis.header->save_sequence = 0;
388 if(install_sig_mainthread_catcher() != 0)
390 ERROR_REPORT(
"Error installing main thread catcher.");
394 signal(SIGIO, SIG_IGN);
397 if(start_signal_catcher() != 0)
399 ERROR_REPORT(
"Error starting signal catching thread.");
404 if(block_sigio() != 0)
406 ERROR_REPORT(
"Error starting signal catching thread.");
410 LOG_INFO(
"starting up . . .");
415 pthread_mutex_lock(&signal_mutex);
416 pthread_cond_wait(&signal_cond, &signal_mutex);
417 pthread_mutex_unlock(&signal_mutex);
419 if(!STOP_FRAMEGRABBER) start_framegrabber();
422 pthread_join(signal_thread, 0);
442 if((frameNo % init_save_interval) == 0) save_init();
450 if(writerPingEnabled)
452 if(skips_remaining <= 0)
454 skips_remaining = num_skip;
456 if(saves_remaining > 0) saves_remaining--;
457 if(saves_remaining == 0)
459 writerPingEnabled = 0;
460 std::cout <<
'\a' << std::endl;
465 if(skips_remaining == 1)
467 sis.header->save_sequence++;
474 if(serverPingEnabled)
484 sim = sis.set_next_image(width, height);
486 sim->frameNo = frameNo;
491 for(sh_i=0; sh_i<width; sh_i++)
493 for(sh_j=0; sh_j<height; sh_j++)
495 sim->imdata[sh_i*width+sh_j] = (
short) ((
short *)im)[sh_i*width+sh_j];
498 sim->frame_time = tv;
499 sim->frame_time_dma = tv_dma;
500 sis.enable_next_image();
508 sim = sis.set_next_image(width, height);
510 sim->frameNo = frameNo;
515 for(sh_i=0; sh_i<width; sh_i++)
517 for(sh_j=0; sh_j<height; sh_j++)
519 sim->imdata[sh_i*width+sh_j] = (
short) ((
short *)im)[sh_j*width+sh_i];
522 sim->frame_time = tv;
523 sim->frame_time_dma = tv_dma;
524 sis.enable_next_image();
532 sim = sis.set_next_image(height, width);
534 sim->frameNo = frameNo;
541 for(sh_i=0; sh_i<(height-bias_height); sh_i+=2, sh_k++)
547 for(sh_j=(width-bias_width)-1; sh_j>=0; sh_j-=1)
549 sim->imdata[sh_k*(height-bias_height)+sh_j] = (
short) ((
short *)im)[sh_j*(height-bias_height)+sh_i];
553 sh_k = (height-bias_height)-1;
554 for(sh_i=1; sh_i<(height-bias_height); sh_i+=2, sh_k--)
560 for(sh_j=(width-bias_width)-1; sh_j>= 0; sh_j-=1)
562 sim->imdata[sh_k*(height-bias_height)+sh_j] = (
short) ((
short *)im)[sh_j*(height-bias_height)+sh_i];
565 sim->frame_time = tv;
566 sim->frame_time_dma = tv_dma;
567 sis.enable_next_image();
574 if(!dark_shmem_key)
return 0;
576 if(no_darks_added == 0)
578 dark_sim->
depth = depth;
579 dark_sim->nx = width;
580 dark_sim->ny = height;
581 for(sh_i=0; sh_i<width; sh_i++)
583 for(sh_j=0; sh_j<height; sh_j++)
585 dark_temp[sh_i*width+sh_j] =0;
588 std::cout << dark_sim->imdata << std::endl;
591 if(no_darks_added < calc_dark)
595 for(sh_i=0; sh_i<width; sh_i++)
597 for(sh_j=0; sh_j<height; sh_j++)
599 dark_temp[sh_i*width+sh_j] += sim->imdata[sh_i*width+sh_j];
607 for(sh_i=0; sh_i<width; sh_i++)
609 for(sh_j=0; sh_j<height; sh_j++)
611 dark_sim->imdata[sh_i*width+sh_j] = (
int) ((
double)dark_temp[sh_i*width+sh_j])/((
double) no_darks_added);
624 _logger->log(Logger::LOG_LEV_TRACE,
"Received local command: %s.", com.c_str());
625 resp = common_command(com, CMODE_LOCAL);
626 if(resp ==
"") resp =
"UNKOWN COMMAND\n";
627 _logger->log(Logger::LOG_LEV_TRACE,
"Response to local command: %s.", resp.c_str());
634 std::cout <<
"In script command" << std::endl;
635 _logger->log(Logger::LOG_LEV_TRACE,
"Received script command: %s.", com.c_str());
636 resp = common_command(com, CMODE_SCRIPT);
637 if(resp ==
"") resp =
"UNKOWN COMMAND\n";
638 _logger->log(Logger::LOG_LEV_TRACE,
"Response to script command: %s.", resp.c_str());
645 _logger->log(Logger::LOG_LEV_TRACE,
"Received auto command: %s.", com.c_str());
646 resp = common_command(com, CMODE_AUTO);
648 if(resp ==
"") resp = post_auto_command(com);
649 _logger->log(Logger::LOG_LEV_TRACE,
"Response to auto command: %s.", resp.c_str());
661 if(cmode == control_mode)
663 STOP_FRAMEGRABBER = 0;
667 else return control_mode_response();
672 if(cmode == control_mode)
674 STOP_FRAMEGRABBER = 1;
675 pthread_kill(main_thread, SIG_MAINTHREAD);
676 writerPingEnabled = 0;
679 else return control_mode_response();
684 if(com ==
"running?")
686 if(RUNNING)
return "1\n";
692 if(writerPingEnabled )
return "1\n";
698 if(serverPingEnabled )
return "1\n";
702 if(com ==
"remaining?")
704 snprintf(rstr, 50,
"%i\n", saves_remaining);
710 snprintf(rstr, 50,
"%i\n", num_skip);
714 if(com.substr(0,4) ==
"skip")
716 if(cmode == control_mode)
718 int nskip = atoi(com.substr(5, com.length()-5).c_str());
720 if(nskip > 0) num_skip = nskip;
724 else return control_mode_response();
727 if(com.substr(0,8) ==
"savedark")
729 if(cmode == control_mode)
731 int nsav = atoi(com.substr(9, com.length()-9).c_str());
732 std::cout << nsav <<
"\n";
733 if(nsav != 0 && !writerPingEnabled)
735 writerPingEnabled = 1;
736 sis.header->save_sequence++;
737 saves_remaining = nsav;
748 writerPingEnabled = 0;
754 else return control_mode_response();
757 if(com.substr(0,4) ==
"save")
759 if(cmode == control_mode)
761 int nsav = atoi(com.substr(5, com.length()-5).c_str());
763 if(nsav != 0 && !writerPingEnabled)
765 writerPingEnabled = 1;
766 sis.header->save_sequence++;
767 if(nsav > 0) saves_remaining = nsav;
768 else saves_remaining = -1;
774 writerPingEnabled = 0;
780 else return control_mode_response();
785 if(com.substr(0,4) ==
"dark")
787 if(cmode == control_mode)
789 int ndrk = atoi(com.substr(5, com.length()-5).c_str());
799 else return control_mode_response();
802 if(com.substr(0,5) ==
"serve")
804 if(cmode == control_mode)
806 int nsav = atoi(com.substr(5, com.length()-5).c_str());
810 serverPingEnabled = 1;
811 if(writerPingEnabled == 0) sis.header->save_sequence++;
816 serverPingEnabled = 0;
821 else return control_mode_response();
826 snprintf(rstr, 50,
"%c,%i,%i,%i,%i,%i\n", control_mode_response()[0], RUNNING, writerPingEnabled, num_skip, saves_remaining-1, serverPingEnabled);
838 of.open((std::string(getenv(
"VISAO_ROOT")) +
"/" + init_file).c_str());
841 of <<
"frameNo int " << frameNo <<
"\n";
850 remove((std::string(getenv(
"VISAO_ROOT")) +
"/" + init_file).c_str());
859 #endif //__framegrabber_h__
sharedim_stack< dataT > dark_sis
The shared memory ring buffer for image storage.
int depth
Image bit depth.
int copyto_sharedim_short_deintlv_rotated(unsigned char *im)
Copies the image to data to shared memory, as shorts, rotated by 90 degrees, with DALSA 2 channel de-...
int write_fifo_channel(fifo_channel *fc, const char *com, int comlen)
Write data to the output channel.
int postGetImage()
Call this immediately after getting each frame. updates frameNo and saves the init file...
int setup_fifo_list(fifo_list *fl, int nch)
Setup a fifo_list.
virtual int stop_framegrabber()
Override this: framegrabber clean up.
sharedim< dataT > * dark_sim
Pointer to a shared memory image.
timeval tv_dma
Convenience variable for filling in the dma timestamp.
sharedim_stack< dataT > sis
The shared memory ring buffer for image storage.
int writerPingChan
The fifo channel for the writer pings.
int copyto_sharedim_short_rotated(unsigned char *im)
Copies the image data to shared memory, as shorts, rotated by 90 degrees.
The standalone VisAO application, does not interface with the AO Supervisor.
int dark_shmem_key
The key for the darks shared memory.
int serverPingChan
The fifo channel for the writer pings.
int delete_init()
Delete the initialization file.
int saves_remaining
Number pings left to issue.
int frameNo
For use as a running tally of images captured.
std::vector< dataT > dark_temp
Temporary data storage for dark calculations.
int bias_height
Height of bias stripe (if any)
std::string local_command(std::string com)
Called by __local_command after control state logic.
Declarations for the standalone VisAO application.
int sh_j
Convenience variable for loops.
int save_init()
Save the initialization data to disk.
sharedim< dataT > * sim
Pointer to a shared memory image.
int shmem_key
The key for the shared memory.
int num_skip
The number of frames to skip between pings.
int hasServerPing
If true, open fifo for pinging the frame server.
int skips_remaining
Skips remaining.
int writerPingEnabled
Send pings on frame ready when true.
int hasWriterPing
If true, open fifo for pinging the framewriter.
int TimeToDie
Global set by SIGTERM.
int num_sharedim
The number of shared images available in the buffer.
virtual int Run()
Main loop, normally won't need to be overridden.
int serverPingEnabled
Send pings on frame ready when true.
std::string script_command(std::string com)
Called by __script_command after control state logic.
int STOP_FRAMEGRABBER
If true, framegrabber stops at top of next loop.
void Create()
Processes the config file.
#define DATA_framegrabber47
Shared memory key for ccd47 data.
int send_ping()
Sends a ping to the waiting process (usuallly a framewriter).
The basic VisAO framegrabber.
int sh_i
Convenience variable for loops.
u_char * image_p
Pointer to the current image data.
std::string auto_command(std::string com, char *seqmsg)
Processing for auto commands - to be overriden.
virtual int start_framegrabber()
Override this: it is where your framegrabber should do all its work. Check for !STOP_FRAMEGRABBER and...
The namespace of VisAO software.
int bias_width
Width of bias stripe (if any)
timeval tv
Convenience variable for filling in the frame time in the image header.
size_t max_imsize
The maximum size (x*y) of images that will be put in this buffer.
Declarations for various image utility functions.
#define RWBUFF_SZ
The size of the i/o buffer.
int copyto_sharedim_short(unsigned char *im)
Copies the image data to shared memory, as shorts.
int set_fifo_list_channel(fifo_list *fl, int nch, int buffsz, const char *fin, const char *fout, int(*inp_hand)(fifo_channel *), void *adata)
Set the details of one channel in the list.
int RUNNING
Status of framegrabber.