19 #define SELECTOR_PING_CH 0
20 #define SHUTTER_AUTO_CH 1
28 frameselector::frameselector(
int argc,
char **argv)
throw (AOException) :
VisAOApp_standalone(argc, argv)
33 frameselector::frameselector(std::string name,
const std::string &conffile)
throw (AOException) : VisAOApp_standalone(name, conffile)
38 int frameselector::Create()
42 std::string visao_root = getenv(
"VISAO_ROOT");
45 signal(SIGIO, SIG_IGN);
50 pathtmp = (std::string)(ConfigDictionary())[
"ping_fifo_path"];
52 catch(Config_File_Exception)
56 ping_fifo_path = visao_root +
"/" + pathtmp +
"/";
57 _logger->log(Logger::LOG_LEV_INFO,
"Set ping_fifo_path: %s", ping_fifo_path.c_str());
59 std::string shutter_fifo_path = visao_root +
"/fifos/shuttercontrol_com_auto";
61 _logger->log(Logger::LOG_LEV_INFO,
"Set shutter_fifo_path: %s", shutter_fifo_path.c_str());
63 std::string sf_in = shutter_fifo_path +
"_in";
64 std::string sf_out = shutter_fifo_path +
"_out";
70 set_fifo_list_channel(&
fl, SELECTOR_PING_CH,
RWBUFF_SZ, (
char *)std::string(ping_fifo_path + MyName()+
"_ping_in").c_str(), (
char *)std::string(ping_fifo_path + MyName()+
"_ping_out").c_str(), 0, (
void *)
this);
76 shmem_key = (
int)(ConfigDictionary())[
"shmem_key"];
78 catch(Config_File_Exception)
80 _logger->log(Logger::LOG_LEV_FATAL,
"shmem_key is a required config parameter.");
83 attached_to_shmem =
false;
93 _logger->log(Logger::LOG_LEV_ERROR,
"Could not create status board.");
98 strncpy(bsb->appname, MyFullName().c_str(), 25);
103 frame_select =
false;
105 pthread_mutex_init(&select_mutex, NULL);
106 pthread_cond_init(&strehl_ready_cond, NULL);
107 pthread_mutex_init(&strehl_ready_mutex, NULL);
112 int frameselector::connect_shmem()
117 ERROR_REPORT(
"Error attaching to shared memory.");
118 attached_to_shmem =
false;
122 attached_to_shmem =
true;
131 signal(SIGIO, SIG_IGN);
132 signal(RTSIGIO, SIG_IGN);
137 ERROR_REPORT(
"Error installing main thread catcher.");
144 ERROR_REPORT(
"Error starting signal catching thread.");
153 ERROR_REPORT(
"Error blocking SIGIO in main thread.");
159 global_selector =
this;
161 fcntl(
fl.
fifo_ch[SELECTOR_PING_CH].fd_in, F_SETOWN, getpid());
163 int rv = fcntl(
fl.
fifo_ch[SELECTOR_PING_CH].fd_in, F_SETSIG, RTSIGPING);
166 std::cerr <<
"Error changing signal.\n";
167 perror(
"frameselector");
170 struct sigaction act;
173 act.sa_sigaction = &strehl_ready;
174 act.sa_flags = SA_SIGINFO;
180 if(sigaction(RTSIGPING, &act, 0) < 0)
183 logss <<
"Error setting signal handler for RTSIGPING. Errno says: " << strerror(errno) <<
".";
188 LOG_INFO(
"starting up . . .");
205 int frameselector::selector()
207 static int last_image = -1;
210 if(!frame_select)
return 0;
212 if(pthread_mutex_trylock(&select_mutex) != 0 )
219 if(curr_image == last_image || curr_image < -1)
return 0;
222 last_image = curr_image;
224 if(
sim.imdata[7] >= thresh)
235 pthread_mutex_unlock(&select_mutex);
243 _logger->log(Logger::LOG_LEV_TRACE,
"Received remote command: %s.", com.c_str());
245 if(resp ==
"") resp = (std::string(
"UNKOWN COMMAND: ") + com +
"\n");
246 _logger->log(Logger::LOG_LEV_TRACE,
"Response to remote command: %s.", resp.c_str());
253 _logger->log(Logger::LOG_LEV_TRACE,
"Received local command: %s.", com.c_str());
255 if(resp ==
"") resp = (std::string(
"UNKOWN COMMAND: ") + com +
"\n");
256 _logger->log(Logger::LOG_LEV_TRACE,
"Response to local command: %s.", resp.c_str());
263 _logger->log(Logger::LOG_LEV_TRACE,
"Received script command: %s.", com.c_str());
265 if(resp ==
"") resp = (std::string(
"UNKOWN COMMAND: ") + com +
"\n");
266 _logger->log(Logger::LOG_LEV_TRACE,
"Response to script command: %s.", resp.c_str());
273 _logger->log(Logger::LOG_LEV_TRACE,
"Received auto command: %s.", com.c_str());
277 _logger->log(Logger::LOG_LEV_TRACE,
"Response to auto command: %s.", resp.c_str());
290 snprintf(str, 256,
"%0.4f\n", thresh);
296 return get_state_str();
299 if(com ==
"frame_select?")
301 snprintf(str, 256,
"%i\n", frame_select);
308 logss <<
"start selecting - thresh: " << thresh;
310 _logger->log(Logger::LOG_LEV_INFO,
logss.str().c_str());
322 frame_select =
false;
327 logss <<
"stop selecting";
329 _logger->log(Logger::LOG_LEV_INFO,
logss.str().c_str());
336 if(com.substr(0,6) ==
"thresh" && cmode ==
control_mode)
338 double newthresh = strtod(com.substr(6, com.length()-6).c_str(), 0);
339 if(newthresh >= 0. && newthresh < 1.0)
353 std::string frameselector::get_state_str()
381 snprintf(schr, 50,
"%c %i %f", sstr[0], frame_select, thresh);
389 pthread_cond_broadcast(&strehl_ready_cond);
401 fssb->frame_select = frame_select;
402 fssb->thresh = thresh;
410 void strehl_ready(
int signum, siginfo_t *siginf,
void *ucont)
412 if(siginf->si_code == POLL_IN)
414 global_selector->selector();
std::string common_command(std::string com, int cmode)
The common command processor for commands received by fifo.
virtual int Run()
The application main loop, to be re-implemented in derived classes.
virtual int start_signal_catcher(bool inherit_sched=true)
Starts the signal catching loop.
frameselector * global_selector
For the auto command handler, which live in the main thread.
virtual std::string script_command(std::string com)
Overridden from VisAOApp_base::script_command, here just calls common_command.
int read_fifo_channel(fifo_channel *fc)
Read data from the input fifo channel.
#define STATUS_frameselector
Shared memory key for the frame selector status board.
virtual int update_statusboard()
Update the status board.
The standalone VisAO application, does not interface with the AO Supervisor.
std::ostringstream logss
Conveninence string stream for building log messages.
void * statusboard_shmemptr
The pointer to the shared memory block for the statusboard.
virtual void error_report(int LogLevel, std::string emsg)
Report an error. Also calls log_msg.
virtual int update_statusboard()
Update the status board.
virtual int setup_baseApp(bool usethreads=false)
Install fifo channels.
fifo_channel * fifo_ch
An array of fifo_channels.
int setup_fifo_list(int nfifos)
Allocate the fifo_list.
virtual std::string local_command(std::string com)
Overridden from VisAOApp_base::local_command, here just calls common_command.
pthread_t signal_thread
Identifier for the separate signal handling thread.
key_t statusboard_shmemkey
The key used to lookup the shared memory.
virtual int block_sigio()
Sets the signal mask to block SIGIO and RTSIGIO.
int TimeToDie
Global set by SIGTERM.
int attach_shm(key_t mkey)
Attachess the shared memory. A Reader process should start here.
fifo_list fl
The list of named-pipe fifos used for inter-process comms.
double pause_time
Time to pause during application main loop.
sharedim< IMDATA_TYPE > get_image(int imno)
Gets an image, as a sharedim structure, with the imdata pointer properly set for the calling process...
virtual std::string remote_command(std::string com)
Overridden from VisAOApp_base::remote_command, here just calls common_command.
int create_statusboard(size_t sz)
Creates and attaches to the statusboard shared memory.
static int control_mode
The current control mode.
virtual int install_sig_mainthread_catcher()
Install the SIG_MAINTHREAD signal catcher.
std::string auto_command(std::string com, char *seqmsg)
Overridden from VisAOApp_base::auto_command, here just calls common_command.
virtual int kill_me()
Handle a timetodie condition upon exiting the signal catcher thread (e.g. tell main thread it is abou...
sharedim_stack< float > * sis
Manages a VisAO shared memory image stack.
sharedim< float > sim
The sharedim structure retreived from the stack.
The namespace of VisAO software.
virtual std::string post_auto_command(std::string com, char *seqmsg=0)
Processing for auto commands - not normally overriden.
int get_last_image()
Returns the value of last_image currently in the header.
Declarations for the real time frame selector.
#define RWBUFF_SZ
The size of the i/o buffer.
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 write_fifo_channel(int ch, const char *com, int comlen, std::string *resp)
Write data to a fifo_channel, and get a response if desired.