18 #ifndef __framewriter_h__
19 #define __framewriter_h__
61 framewriter(
int argc,
char **argv)
throw (AOException);
62 framewriter(std::string name,
const std::string &conffile)
throw (AOException);
67 std::string ping_fifo_path;
69 std::string save_path;
71 std::string name_base;
73 std::string path_name;
77 int change_subdir(std::string sd);
85 bool attached_to_shmem;
94 bool write_aosys_head;
95 bool write_visao_head;
97 pthread_mutex_t frame_ready_mutex;
99 pthread_cond_t frame_ready_cond;
102 int set_ping_fifo_path(std::string &);
103 std::string get_ping_fifo_path(){
return ping_fifo_path;}
105 int set_save_path(std::string &);
106 std::string get_save_path(){
return save_path;}
108 int set_name_base(std::string &);
109 std::string get_name_base(){
return name_base;}
114 int set_shmem_key(
int sk);
115 int get_shmem_key(){
return shmem_key;}
130 int attach_status_boards();
139 std::string
auto_command(std::string com,
char *seqmsg);
151 template <
class dataT>
int frame_ready(fifo_channel *fc);
154 template <
class dataT> framewriter<dataT>::framewriter(
int argc,
char **argv)
throw (AOException) : VisAOApp_standalone(argc, argv)
159 template <
class dataT> framewriter<dataT>::framewriter(std::string name,
const std::string &conffile)
throw (AOException) : VisAOApp_standalone(name, conffile)
164 template <
class dataT>
int framewriter<dataT>::Create()
168 std::string visao_root = getenv(
"VISAO_ROOT");
173 pathtmp = (std::string)(ConfigDictionary())[
"ping_fifo_path"];
175 catch(Config_File_Exception)
179 ping_fifo_path = visao_root +
"/" + pathtmp +
"/";
180 _logger->log(Logger::LOG_LEV_INFO,
"Set ping_fifo_path: %s", ping_fifo_path.c_str());
185 pathtmp = (std::string)(ConfigDictionary())[
"save_path"];
187 catch(Config_File_Exception)
191 save_path = visao_root +
"/" + pathtmp +
"/";
192 set_save_path(save_path);
194 _logger->log(Logger::LOG_LEV_INFO,
"Set save_path: %s", save_path.c_str());
199 pathtmp = (std::string)(ConfigDictionary())[
"name_base"];
201 catch(Config_File_Exception)
203 _logger->log(Logger::LOG_LEV_FATAL,
"name_base is a required config parameter.");
208 _logger->log(Logger::LOG_LEV_INFO,
"Set name_base: %s", name_base.c_str());
210 path_name = save_path+name_base;
211 _logger->log(Logger::LOG_LEV_INFO,
"Generated path_name: %s", path_name.c_str());
214 setup_baseApp(0, 1, 1, 1,
false);
217 set_fifo_list_channel(&fl, 0,
RWBUFF_SZ, (
char *)std::string(ping_fifo_path + MyName()+
"_ping_in").c_str(), (
char *)std::string(ping_fifo_path + MyName()+
"_ping_out").c_str(), &frame_ready<dataT>, (
void *)
this);
222 imhead.origin = (std::string)(ConfigDictionary())[
"origin"];
224 catch(Config_File_Exception)
226 imhead.origin =
"Magellan AO System";
232 imhead.telescop = (std::string)(ConfigDictionary())[
"telescope"];
234 catch(Config_File_Exception)
236 imhead.telescop =
"Magellan Clay";
242 imhead.instrume = (std::string)(ConfigDictionary())[
"instrument"];
244 catch(Config_File_Exception)
246 imhead.instrume =
"Magellan VisAO";
252 write_aosys_head = (
int)(ConfigDictionary())[
"write_aosys_head"];
254 catch(Config_File_Exception)
256 write_aosys_head = 1;
262 write_visao_head = (
int)(ConfigDictionary())[
"write_visao_head"];
264 catch(Config_File_Exception)
266 write_visao_head = 1;
271 shmem_key = (
int)(ConfigDictionary())[
"shmem_key"];
273 catch(Config_File_Exception)
275 _logger->log(Logger::LOG_LEV_FATAL,
"shmem_key is a required config parameter.");
278 attached_to_shmem =
false;
283 init_visao_imheader(&imhead);
286 statusboard_shmemkey = 0;
287 if(MyFullName() ==
"framewriter47.L")
289 std::cout <<
"Initing for framewriter47.L\n";
292 if(MyFullName() ==
"framewriter39.L")
294 std::cout <<
"Initing for framewriter39.L\n";
295 statusboard_shmemkey = STATUS_framewriter39;
298 if(statusboard_shmemkey)
300 if(create_statusboard(
sizeof(basic_status_board)) != 0)
302 statusboard_shmemptr = 0;
303 _logger->log(Logger::LOG_LEV_ERROR,
"Could not create status board.");
308 strncpy(bsb->appname, MyFullName().c_str(), 25);
309 bsb->max_update_interval = pause_time;
313 pthread_cond_init(&frame_ready_cond, NULL);
314 pthread_mutex_init(&frame_ready_mutex, NULL);
319 template <
class dataT>
int framewriter<dataT>::change_subdir(std::string sd)
322 int idx = sd.find_first_of(
" \t\r\n", 0);
323 if(idx > -1) subdir = sd.substr(0, idx);
326 path_name = save_path+ subdir+
"/" + name_base;
327 _logger->log(Logger::LOG_LEV_INFO,
"Generated path_name: %s", path_name.c_str());
329 if(subdir !=
"." && subdir !=
"")
331 std::string com =
"mkdir -p ";
332 com += save_path+ subdir;
340 template <
class dataT>
int framewriter<dataT>::set_ping_fifo_path(std::string &pfp)
342 ping_fifo_path = pfp;
346 template <
class dataT>
int framewriter<dataT>::set_save_path(std::string &sp)
349 path_name = save_path+name_base;
352 std::string com =
"mkdir -p ";
360 template <
class dataT>
int framewriter<dataT>::set_name_base(std::string &nb)
363 path_name = save_path+name_base;
373 template <
class dataT>
int framewriter<dataT>::set_shmem_key(
int sk)
379 template <
class dataT>
int framewriter<dataT>::connect_shmem()
382 if(sis->attach_shm(shmem_key) != 0)
384 ERROR_REPORT(
"Error attaching to shared memory.");
385 attached_to_shmem =
false;
389 attached_to_shmem =
true;
393 template <
class dataT>
int framewriter<dataT>::set_sim(
sharedim<dataT> s)
404 if(install_sig_mainthread_catcher() != 0)
406 ERROR_REPORT(
"Error installing main thread catcher.");
411 if(start_signal_catcher(
true) != 0)
413 ERROR_REPORT(
"Error starting signal catching thread.");
418 if(block_sigio() != 0)
420 ERROR_REPORT(
"Error blocking SIGIO in main thread.");
448 attach_status_boards();
449 LOG_INFO(
"starting up . . .");
456 pthread_mutex_lock(&frame_ready_mutex);
457 pthread_cond_wait(&frame_ready_cond, &frame_ready_mutex);
458 pthread_mutex_unlock(&frame_ready_mutex);
468 static int last_image_abs = -1, last_image = -1, last_save_sequence = -1;
472 std::stringstream ss;
476 if(attached_to_shmem ==
false)
478 if(connect_shmem() != 0)
return -1;
482 if(sis->get_last_image_abs() < last_image_abs || last_save_sequence != sis->header->save_sequence)
484 last_image_abs = sis->get_last_image_abs()-1;
486 last_image = sis->get_last_image();
487 last_save_sequence = sis->header->save_sequence;
488 attach_status_boards();
490 if(sis->get_last_image_abs() < last_image_abs) LOG_INFO(
"Detected framegrabber restart, resetting");
491 std::cout <<
"Detected framegrabber restart, resetting" <<
"\n";
494 if(last_image_abs < 0)
496 last_image = sis->get_last_image();
497 last_save_sequence = sis->header->save_sequence;
498 last_image_abs = sis->get_last_image_abs()-1;
501 while(last_image_abs < sis->get_last_image_abs() && !
TimeToDie)
503 sim = sis->get_image(last_image);
509 fitsStatus = write_visao_fits<dataT>(path_name.c_str(), &sim, &imhead, write_aosys_head, write_visao_head);
513 fits_get_errstatus(fitsStatus, fitsError);
514 ss <<
"write_visao_fits returned status: [" << fitsStatus <<
"] " << fitsError <<
" - Error writing image";
515 ERROR_REPORT(ss.str().c_str());
519 sis->set_saved(last_image, 1);
526 if(last_image >= sis->get_max_n_images()) last_image = 0;
530 std::cout <<
"last_image " << last_image <<
"\n";
531 std::cout <<
"last_image_abs " << last_image_abs <<
"\n";
532 std::cout <<
"max_n_images " << sis->get_max_n_images() <<
"\n";
533 ERROR_REPORT(
"Error getting image in write_frame().");
536 behind = sis->get_last_image_abs() - last_image_abs;
541 if(behind >= sis->get_max_n_images())
543 skipped = (
int)(behind - .5*sis->get_max_n_images());
544 total_skipped += skipped;
545 _logger->log(Logger::LOG_LEV_ERROR,
"Behind %i frames, skipping %i frames. Total skipped: %i", behind, skipped, total_skipped);
547 last_image_abs += skipped;
548 last_image += skipped;
549 if(last_image >= sis->get_max_n_images()) last_image = last_image - sis->get_max_n_images();
552 update_statusboard();
561 template <
class dataT>
int framewriter<dataT>::attach_status_boards()
604 _logger->log(Logger::LOG_LEV_TRACE,
"Received remote command: %s.", com.c_str());
605 resp = common_command(com, CMODE_REMOTE);
606 if(resp ==
"") resp = (std::string(
"UNKOWN COMMAND: ") + com +
"\n");
607 _logger->log(Logger::LOG_LEV_TRACE,
"Response to remote command: %s.", resp.c_str());
614 _logger->log(Logger::LOG_LEV_TRACE,
"Received local command: %s.", com.c_str());
615 resp = common_command(com, CMODE_LOCAL);
616 if(resp ==
"") resp = (std::string(
"UNKOWN COMMAND: ") + com +
"\n");
617 _logger->log(Logger::LOG_LEV_TRACE,
"Response to local command: %s.", resp.c_str());
624 _logger->log(Logger::LOG_LEV_TRACE,
"Received script command: %s.", com.c_str());
625 resp = common_command(com, CMODE_SCRIPT);
626 if(resp ==
"") resp = (std::string(
"UNKOWN COMMAND: ") + com +
"\n");
627 _logger->log(Logger::LOG_LEV_TRACE,
"Response to script command: %s.", resp.c_str());
634 _logger->log(Logger::LOG_LEV_TRACE,
"Received auto command: %s.", com.c_str());
635 resp = common_command(com, CMODE_AUTO);
637 if(resp ==
"") resp = post_auto_command(com);
638 _logger->log(Logger::LOG_LEV_TRACE,
"Response to auto command: %s.", resp.c_str());
646 return subdir +
"\n";
649 if(com.substr(0,6) ==
"subdir")
651 if(change_subdir(com.substr(7, com.length()-7)) == 0)
return "0\n";
660 pthread_cond_broadcast(&frame_ready_cond);
664 template <
class dataT>
int frame_ready(fifo_channel *fc)
673 pthread_cond_broadcast(&fw->frame_ready_cond);
684 #endif //__framewriter_h__
int setup_fifo_list(fifo_list *fl, int nch)
Setup a fifo_list.
A class for writing image frames from shared memory to disk.
int read_fifo_channel(fifo_channel *fc)
Read data from the input fifo channel.
#define STATUS_reconstructor
Shared memory key for the reconstructor status board.
The standalone VisAO application, does not interface with the AO Supervisor.
std::string auto_command(std::string com, char *seqmsg)
Overridden from VisAOApp_base::auto_command, here just calls common_command.
Declarations for the standalone VisAO application.
#define STATUS_framewriter47
Shared memory key for the ccd47 framewriter status board.
virtual int kill_me()
Handle a timetodie condition upon exiting the signal catcher thread (e.g. tell main thread it is abou...
virtual std::string remote_command(std::string com)
Overridden from VisAOApp_base::remote_command, here just calls common_command.
Declarations for the VisAO status information structures.
#define STATUS_shuttertester
Shared memory key for shutter tester status board.
int TimeToDie
Global set by SIGTERM.
void * attach_shm(size_t *sz, key_t mkey, int shmemid)
Attach to a shared memory buffer and get its size.
#define STATUS_gimbal
Shared memory key for the Gimbal mirror status board.
int total_skipped
The total number of frames skipped.
virtual std::string script_command(std::string com)
Overridden from VisAOApp_base::script_command, here just calls common_command.
sharedim_stack< dataT > * sis
Manages a VisAO shared memory image stack.
#define STATUS_focusstage
Shared memory key for the focus motor controller status board.
virtual int Run()
The application main loop, to be re-implemented in derived classes.
int behind
The number of frames currently behind.
virtual std::string local_command(std::string com)
Overridden from VisAOApp_base::local_command, here just calls common_command.
The namespace of VisAO software.
Declarations for various image utility functions.
sharedim< dataT > sim
The sharedim structure retreived from the stack.
std::string common_command(std::string com, int cmode)
The common command processor for commands received by fifo.
#define RWBUFF_SZ
The size of the i/o buffer.
#define STATUS_ccd47
Shared memory key for the ccd47 controller status board.
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.