The VisAO Camera
WollastonStatus.cpp
Go to the documentation of this file.
1 /************************************************************
2 * WollastonStatus.cpp
3 *
4 * Author: Jared R. Males (jrmales@email.arizona.edu)
5 *
6 * Definitions for the VisAO wollaston lift status maintainer.
7 *
8 * Developed as part of the Magellan Adaptive Optics system.
9 ************************************************************/
10 
11 /** \file WollastonStatus.cpp
12  * \author Jared R. Males
13  * \brief Definitions for the VisAO wollaston lift status maintainer.
14  *
15 */
16 
17 #include "WollastonStatus.h"
18 
19 namespace VisAO
20 {
21 
22 WollastonStatus::WollastonStatus( std::string name, const std::string& conffile) throw (AOException) : VisAOApp_standalone(name, conffile)
23 {
24  initApp();
25 }
26 
27 WollastonStatus::WollastonStatus( int argc, char**argv) throw (AOException) : VisAOApp_standalone(argc, argv)
28 {
29  initApp();
30 }
31 
33 {
34 
35  std::string pathtmp;
36  std::string visao_root = getenv("VISAO_ROOT");
37 
38  setup_fifo_list(2);
39  setup_baseApp(1, 1, 0, 0, false);
40 
41  //Init the status board
42  statusboard_shmemkey = 11000;
44  {
46  _logger->log(Logger::LOG_LEV_ERROR, "Could not create status board.");
47  }
48  else
49  {
51  strncpy(bsb->appname, MyFullName().c_str(), 25);
52  bsb->max_update_interval = pause_time;
53  }
54 
55  size_t sz;
56  fw3sb = (VisAO::filterwheel_status_board*) attach_shm(&sz, 9500, 0);
57 
58 
59 }//void WollastonStatus::initapp()
60 
61 
62 int WollastonStatus::WollastonStatus::set_cur_pos(int cp)
63 {
64  //Avoid prompting until logic in main loop.
65  prompt = 0;
66 
67  if(cp > 0) cur_pos = 1;
68  if(cp == 0) cur_pos = 0;
69  if(cp < 0) cur_pos = -1;
70 
71  return 0;
72 }
73 
74 
75 
77 {
78  double t0, dt;
79 
80  //Install the main thread handler
82  {
83  ERROR_REPORT("Error installing main thread catcher.");
84  return -1;
85  }
86 
87  request_control(CMODE_LOCAL);
88 
89  signal(SIGIO, SIG_IGN);
90  if(connect_fifo_list() == 0)
91  {
92  LOG_INFO("fifo_list connected.");
93  }
94  else
95  {
96  ERROR_REPORT("Error connecting the fifo list.");
97  TimeToDie = 1;
98  return -1;
99  }
100 
101  global_fifo_list = &fl;
102 
103  setup_RTsigio();
104 
105  LOG_INFO("starting up . . .");
106 
107  while(!TimeToDie)
108  {
109  t0 = get_curr_time();
110 
111  while((dt = get_curr_time() - t0) < pause_time && !TimeToDie)
112  {
114 
115  if(pause_time - dt >= 1) sleep((int)(pause_time - dt));
116  else usleep((int)((pause_time-dt)*1e6 * .99));
117  }
118 
119  if(!fw3sb)
120  {
121  size_t sz;
122  fw3sb = (VisAO::filterwheel_status_board*) attach_shm(&sz, 9500, 0);
123  }
124 
125  prompt = 0;
126 
127  if(fw3sb)
128  {
129  if(fw3sb->state != STATE_OPERATING && fw3sb->state != STATE_HOMING && fw3sb->type != last_type)
130  {
131  ignored = 0;
132  }
133 
134  if(fw3sb->state != STATE_OPERATING && fw3sb->state != STATE_HOMING && fw3sb->type == FTYPE_SDI && cur_pos != 1 && !ignored)
135  {
136  prompt = PROMPT_UP;
137  }
138 
139  if(fw3sb->state != STATE_OPERATING && fw3sb->state != STATE_HOMING && fw3sb->type != FTYPE_SDI && cur_pos != -1 && !ignored)
140  {
141  prompt = PROMPT_DOWN;
142  }
143 
144  last_type = fw3sb->type;
145  }
146 
147  if(cur_pos == 0 && !ignored) prompt = PROMPT_INT;
148 
151  }
152 
153  return 0;
154 }
155 
156 std::string WollastonStatus::remote_command(std::string com)
157 {
158  std::string resp;
159  _logger->log(Logger::LOG_LEV_TRACE, "Received remote command: %s.", com.c_str());
160  resp = common_command(com, CMODE_REMOTE);
161  _logger->log(Logger::LOG_LEV_TRACE, "Response to remote command: %s.", resp.c_str());
162  return resp;
163 }
164 
165 std::string WollastonStatus::local_command(std::string com)
166 {
167  std::string resp;
168  _logger->log(Logger::LOG_LEV_TRACE, "Received local command: %s.", com.c_str());
169  resp = common_command(com, CMODE_LOCAL);
170  _logger->log(Logger::LOG_LEV_TRACE, "Response to local command: %s.", resp.c_str());
171  return resp;
172 }
173 
174 std::string WollastonStatus::common_command(std::string com, int cmode)
175 {
176  char rstr[50];
177 
178  if(com == "pos?")
179  {
180  snprintf(rstr, 50, "%i\n", get_cur_pos());
181  return rstr;
182  }
183 
184  if(com == "state?")
185  {
186  return get_state_str();
187  }
188 
189  if(com == "up")
190  {
191  if(control_mode == cmode)
192  {
193  set_cur_pos(1);
194  return "0\n";
195  }
196  else return control_mode_response();
197  }
198 
199  if(com == "down")
200  {
201  if(control_mode == cmode)
202  {
203  set_cur_pos(-1);
204  return "0\n";
205  }
206  else return control_mode_response();
207  }
208 
209  if(com == "ignore")
210  {
211  if(control_mode == cmode)
212  {
213  ignored = 1;
214  return "0\n";
215  }
216  else return control_mode_response();
217  }
218 
219  return (std::string("UNKOWN COMMAND: ") + com + "\n");
220 }
221 
223 {
224  char statestr[50];
225  std::string str = control_mode_response();
226 
227  snprintf(statestr, 50, "%c %2i %i\n", str[0], cur_pos, prompt);
228 
229  return statestr;
230 }
231 
233 {
235  {
237 
239 
240  wsb->cur_pos = cur_pos;
241 
242  }
243  return 0;
244 }
245 
246 } //namespace VisAO
247 
int get_cur_pos()
Get the current positon of the motor.
virtual std::string remote_command(std::string com)
Overridden from VisAOApp_base::remote_command, here just calls common_command.
fifo_list * global_fifo_list
The global fifo_list, for signal handling.
Definition: dioserver.cpp:19
double get_curr_time(void)
Gets the current CLOCK_REALTIME time, returns it in seconds to double precision.
Definition: libvisaoc.c:40
Declarations for the VisAO wollaston lift status maintainer.
virtual int update_statusboard()
Update the status board.
The standalone VisAO application, does not interface with the AO Supervisor.
void * statusboard_shmemptr
The pointer to the shared memory block for the statusboard.
virtual int update_statusboard()
Update the status board.
int set_cur_pos(int cp)
Set the value of cur_pos of the motor.
virtual int setup_baseApp(bool usethreads=false)
Install fifo channels.
int check_fifo_list_RTpending()
Check for pending reads for the fifo list while using the RT signals.
int setup_fifo_list(int nfifos)
Allocate the fifo_list.
WollastonStatus(std::string name, const std::string &conffile)
Standard constructor with a config file.
key_t statusboard_shmemkey
The key used to lookup the shared memory.
int TimeToDie
Global set by SIGTERM.
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.
Definition: libvisaoc.c:90
fifo_list fl
The list of named-pipe fifos used for inter-process comms.
virtual std::string local_command(std::string com)
Overridden from VisAOApp_base::local_command, here just calls common_command.
int cur_pos
The current position of the lift, -1 is down, 0 is intermediate, 1 is up.
std::string get_state_str()
Get the state string.
double pause_time
Time to pause during application main loop.
Definition: VisAOApp_base.h:84
virtual int request_control(int cmode)
Calls request_control(cmode, 0).
virtual int Run()
The main loop.
int create_statusboard(size_t sz)
Creates and attaches to the statusboard shared memory.
std::string common_command(std::string com, int cmode)
The common command processor for commands received by fifo.
int connect_fifo_list()
Connect the fifo_list with exclusive locking.
static int control_mode
The current control mode.
int setup_RTsigio()
Setup SIGIO signal handling using realtime signals.
virtual int install_sig_mainthread_catcher()
Install the SIG_MAINTHREAD signal catcher.
The namespace of VisAO software.
void initApp()
Initialization common to both constructors.