24 reconstructor * global_reconstructor;
26 reconstructor::reconstructor(
int argc,
char **argv)
throw (AOException) : VisAOApp_standalone(argc, argv)
31 reconstructor::reconstructor(std::string name,
const std::string &conffile)
throw (AOException) : VisAOApp_standalone(name, conffile)
42 std::string visao_root = getenv(
"VISAO_ROOT");
47 pathtmp = (std::string)(ConfigDictionary())[
"ping_fifo_path"];
49 catch(Config_File_Exception)
53 ping_fifo_path = visao_root +
"/" + pathtmp +
"/";
54 _logger->log(Logger::LOG_LEV_INFO,
"Set ping_fifo_path: %s", ping_fifo_path.c_str());
59 std::string pingin = ping_fifo_path +
"reconstructor39_ping_in";
60 std::string pingout = ping_fifo_path +
"reconstructor39_ping_out";
63 pingin = ping_fifo_path +
"frameselector_ping_in";
64 pingout = ping_fifo_path +
"frameselector_ping_out";
71 catch(Config_File_Exception)
75 _logger->log(Logger::LOG_LEV_INFO,
"Set the shared memory key for slopes (bcu39_shmem_key): %i",
slopes_shmem_key);
81 catch(Config_File_Exception)
85 _logger->log(Logger::LOG_LEV_INFO,
"Set the shared memory key for strehls (strehls_shmem_key): %i",
strehls_shmem_key);
89 num_strehls = (
int)(ConfigDictionary())[
"num_strehls"];
91 catch(Config_File_Exception)
95 _logger->log(Logger::LOG_LEV_INFO,
"Set the number of strehls in circular buffer to (num_strehls): %i", num_strehls);
97 pthread_mutex_init(&reconMutex, NULL);
105 _logger->log(Logger::LOG_LEV_ERROR,
"Could not create status board.");
110 strncpy(bsb->appname, MyFullName().c_str(), 25);
118 rmat.
rec_tech = (
int)(ConfigDictionary())[
"rec_tech"];
120 catch(Config_File_Exception)
124 _logger->log(Logger::LOG_LEV_INFO,
"Set the reconstruction technique to (rec_tech): %i", rmat.
rec_tech);
129 rmat.tel_diam = (double)(ConfigDictionary())[
"tel_diam"];
130 rmat.median_r0 = (double)(ConfigDictionary())[
"median_r0"];
131 rmat.median_r0_lam = (double)(ConfigDictionary())[
"median_r0_lam"];
132 rmat.fitting_A = (double)(ConfigDictionary())[
"fitting_A"];
133 rmat.fitting_B = (double)(ConfigDictionary())[
"fitting_B"];
134 rmat.
reflection_gain = (double)(ConfigDictionary())[
"reflection_gain"];
136 catch(Config_File_Exception)
139 rmat.median_r0 = 18.;
140 rmat.median_r0_lam = .55;
141 rmat.fitting_A = 0.232555;
142 rmat.fitting_B = -0.840466;
160 rmsAccum.resize(5,330.);
164 int reconstructor::set_matint(std::string fname)
166 std::string dir = getenv(
"VISAO_ROOT");
168 if(fname ==
"none" || fname ==
"")
return -1;
172 dir +=
"/calib/visao/reconstructor/RecMats/";
175 pthread_mutex_lock(&reconMutex);
177 if(rmat.load_recmat_LBT(dir) == 0)
182 logss <<
"set recmat: " << dir;
183 _logger->log(Logger::LOG_LEV_INFO,
logss.str().c_str());
187 if(n_modes <= 200) ho_middle = 150;
188 if(n_modes <= 100) ho_middle = 66;
190 std::cout <<
"n_modes = " << n_modes <<
"\n";
191 std::cout <<
"ho_middle = " << ho_middle <<
"\n";
192 pthread_mutex_unlock(&reconMutex);
197 std::cout <<
"setting invalid recmat\n";
201 logss <<
"failed to set recmat: " << dir;
202 _logger->log(Logger::LOG_LEV_ERROR,
logss.str().c_str());
207 std::cout << n_modes <<
"\n";
208 std::cout << ho_middle <<
"\n";
210 pthread_mutex_unlock(&reconMutex);
216 int reconstructor::set_lambda(
float lam)
220 lamgain = -pow(2.*3.14159/lam,2);
223 logss <<
"set lambda: " << lambda;
224 _logger->log(Logger::LOG_LEV_INFO,
logss.str().c_str());
229 int reconstructor::set_filter(std::string fname)
233 fir.read_coef_file(fname);
236 if(strehldata == 0)
delete[] strehldata;
237 strehldata =
new float[fir.get_order()];
239 if(wfedata == 0)
delete[] wfedata;
240 wfedata =
new float[fir.get_order()];
242 for(
int i=0; i< fir.get_order(); i++)
249 logss <<
"set filter: " << fname;
250 _logger->log(Logger::LOG_LEV_INFO,
logss.str().c_str());
255 int reconstructor::set_cal_a(
float a)
260 logss <<
"set cal_a: " << cal_a;
261 _logger->log(Logger::LOG_LEV_INFO,
logss.str().c_str());
266 int reconstructor::set_cal_b(
float b)
271 logss <<
"set cal_b: " << cal_b;
272 _logger->log(Logger::LOG_LEV_INFO,
logss.str().c_str());
277 int reconstructor::reconstruct()
280 static int last_image = -1;
283 if(pthread_mutex_trylock(&reconMutex) != 0)
290 pthread_mutex_unlock(&reconMutex);
296 if(curr_image == last_image || curr_image < -1)
298 pthread_mutex_unlock(&reconMutex);
303 last_image = curr_image;
313 ho_middle = aosb->homiddle;
323 std::vector<float> forRms(5);
324 forRms[0] = sqrt(sumvar);
327 forRms[3] = sqrt(ho1_wfe);
328 forRms[4] = sqrt(ho2_wfe);
329 rmsAccum.addValues(forRms);
332 rawstrehl = exp(lamgain * sumvar);
333 float filtstrehl, filtwfe;
335 if(fir.get_order() > 0)
337 for(
int i=0;i<fir.get_order()-1;i++)
339 wfedata[i] = wfedata[i+1];
340 strehldata[i] = strehldata[i+1];
342 wfedata[fir.get_order()-1] = sqrt(sumvar);
343 strehldata[fir.get_order()-1] = rawstrehl;
345 filtstrehl = fir.
apply_filter(strehldata, fir.get_order());
349 else filtstrehl = rawstrehl;
362 strehl_sim->imdata[5] = rawstrehl*cal_a + cal_b;
364 strehl_sim->imdata[7] = filtstrehl*cal_a + cal_b;
385 pthread_mutex_unlock(&reconMutex);
395 set_matint(
"Rec_20130329_210902.fits");
399 ERROR_REPORT(
"Error attaching to shared memory for slopes. Waiting and trying again.");
409 ERROR_REPORT(
"Error attaching to shared memory for sharedim_stack.");
418 ERROR_REPORT(
"Error installing main thread catcher.");
422 signal(SIGIO, SIG_IGN);
427 ERROR_REPORT(
"Error starting signal catching thread.");
434 ERROR_REPORT(
"Error starting signal catching thread.");
438 LOG_INFO(
"starting up . . .");
442 global_reconstructor =
this;
444 fcntl(
fl.
fifo_ch[PING_IN_FIFO].fd_in, F_SETOWN, getpid());
446 int rv = fcntl(
fl.
fifo_ch[PING_IN_FIFO].fd_in, F_SETSIG, RTSIGPING);
449 std::cerr <<
"Error changing signal.\n";
450 perror(
"reconstructor");
453 struct sigaction act;
456 act.sa_sigaction = &frame_ready;
457 act.sa_flags = SA_SIGINFO;
463 if(sigaction(RTSIGPING, &act, 0) < 0)
466 logss <<
"Error setting signal handler for RTSIGPING. Errno says: " << strerror(errno) <<
".";
482 int last_loop_on = -1;
490 if(recFileName != aosb->reconstructor)
492 set_matint(aosb->reconstructor);
505 _logger->log(Logger::LOG_LEV_TRACE,
"Received local command: %s.", com.c_str());
506 resp = common_command(com, CMODE_LOCAL);
507 _logger->log(Logger::LOG_LEV_TRACE,
"Response to local command: %s.", resp.c_str());
514 _logger->log(Logger::LOG_LEV_TRACE,
"Received local command: %s.", com.c_str());
515 resp = common_command(com, CMODE_LOCAL);
516 _logger->log(Logger::LOG_LEV_TRACE,
"Response to local command: %s.", resp.c_str());
520 std::string reconstructor::common_command(std::string com,
int cmode)
527 if(rmat.get_filePath() ==
"")
return " ";
533 snprintf(resp, 50,
"%f\n", cal_a);
538 snprintf(resp, 50,
"%f\n", cal_b);
544 snprintf(resp, 50,
"%f\n", lambda);
550 if(filter_name ==
"")
return " ";
556 if(com.substr(0, 5) ==
"cal_a")
560 std::string a = com.substr(5, com.length()-4);
561 if(set_cal_a(strtod(a.c_str(),0)) == 0)
return "0\n";
569 if(com.substr(0, 5) ==
"cal_b")
573 std::string b = com.substr(5, com.length()-4);
574 if(set_cal_b(strtod(b.c_str(),0)) == 0)
return "0\n";
587 if(com.substr(0, 6)==
"recmat")
591 std::string newmat = com.substr(6, com.length()-5);
592 if(set_matint(newmat) == 0)
return "0\n";
601 if(com.substr(0, 6)==
"lambda")
605 std::string newlam = com.substr(6, com.length()-5);
606 if(set_lambda(strtod(newlam.c_str(),0)) == 0)
return "0\n";
615 if(com.substr(0, 6)==
"filter")
619 std::string newfil = com.substr(7, com.length()-6);
620 if(set_filter(newfil) == 0)
return "0\n";
629 if(com.substr(0, 6) ==
"select")
633 int s = atoi(com.substr(6, com.length()-6).c_str());
635 if(s) FSPing_enabled = 1;
636 else FSPing_enabled = 0;
645 return "UNKOWN COMMAND?";
656 if(startn < 1)
return 0;
665 double avgwfe=0, avgwfe2 = 0;
668 while(fabs(t0 - t1) < 1.0 )
672 if(tmpwfe2 < 1e8 && !isnan(tmpwfe2))
675 avgwfe += sqrt(tmpwfe2);
693 if(avgwfe2 >= avgwfe*avgwfe) rsb->
stdwfe_1_sec = sqrt(avgwfe2 - avgwfe*avgwfe);
696 std::cout <<
"homiddle: " << ho_middle <<
"\n";
704 void frame_ready(
int signum __attribute__((unused)), siginfo_t *siginf,
void *ucont __attribute__((unused)))
706 if(siginf->si_code == POLL_IN && !
TimeToDie)
708 global_reconstructor->reconstruct();
int enable_next_image()
Increments last_image.
double inst_wfe
Instantaneous WFE.
#define DATA_strehls
Shared memory key for reconstructed strehl data.
virtual int start_signal_catcher(bool inherit_sched=true)
Starts the signal catching loop.
int strehls_shmem_key
Pointer to the slopes vector.
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.
int rec_tech
Which reconstructor technique to use, either REC_ATLAS or REC_CPU.
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.
sharedim_stack_header * header
Convenient pointer to the stack header, has same value as shmemptr.
virtual int Run()
The application main loop, to be re-implemented in derived classes.
virtual int update_statusboard()
Update the status board.
virtual int update_statusboard()
Update the reconstructor Status Board shared memory.
virtual int setup_baseApp(bool usethreads=false)
Install fifo channels.
float unit_conversion
Factor to convert variances to nanometers.
fifo_channel * fifo_ch
An array of fifo_channels.
int setup_fifo_list(int nfifos)
Allocate the fifo_list.
float * amp
The reconstructed amplitudes, a vector of length n_modes.
sharedim< IMDATA_TYPE > * set_next_image(int nx, int ny)
Sets the next image, and returns a pointer to it, but does not increment last_image.
double stdwfe_1_sec
1 second std dev of WFE
sharedim< float > * strehl_sim
Shared memory image, from ring buffer, for strehl data.
void Create()
Processes the config file.
sharedim_stack< float > strehl_sis
Shared memory ring buffer for strehl data.
pthread_t signal_thread
Identifier for the separate signal handling thread.
key_t statusboard_shmemkey
The key used to lookup the shared memory.
int calc_sumvar(float *sumvar, int m0=2, int m1=-1, bool fiterr=true)
Calculate the sum of the variances, e.g. the dot product of the amplitude vector. ...
int n_modes
The number of modes, and the rows in the reconstructor matrix.
virtual int block_sigio()
Sets the signal mask to block SIGIO and RTSIGIO.
int TimeToDie
Global set by SIGTERM.
#define REC_TELEM_N
The size of each telemetry vector.
sharedim< unsigned char > slopes_sim
Shared memory image of slopes (a vector actually)
int attach_shm(key_t mkey)
Attachess the shared memory. A Reader process should start here.
std::string control_mode_response()
Convenience function to return the control type response string, e.g. "A\n".
void * attach_shm(size_t *sz, key_t mkey, int shmemid)
Attach to a shared memory buffer and get its size.
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...
#define DATA_framegrabber39
Shared memory key for BCU39 data.
int create_statusboard(size_t sz)
Creates and attaches to the statusboard shared memory.
sharedim_stack< unsigned char > slopes_sis
The shared memory ring buffer for slopes storage.
std::string local_command(std::string com)
Called by __local_command after control state logic.
static int control_mode
The current control mode.
Declarations for a class to perform real-time reconstruction of wavefronts.
virtual int install_sig_mainthread_catcher()
Install the SIG_MAINTHREAD signal catcher.
int create_shm(key_t mkey, size_t sz)
For the writer process, creates then attaches the shared memory.
float apply_filter(float *data, int len)
Apply the filter to the data fector of length len.
std::string script_command(std::string com)
Called by __script_command after control state logic.
int reconstruct(float *slopes)
Given a new slopes vector, reconstruct the amplitudes.
The namespace of VisAO software.
float reflection_gain
Factor to apply to reconstructed amplitudes to account for mirror reflection.
int get_n_images()
Returns the value of n_images currently in the header.
int get_last_image()
Returns the value of last_image currently in the header.
double avgwfe_1_sec
1 second average WFE
int slopes_shmem_key
The key for the shared memory for slopes.
double tv_to_curr_time(struct timeval *tvp)
Convert a timeval structure to double seconds.
#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.
sharedim< float > strehl_simavg
Shared memory image, from ring buffer, for strehl data.