The VisAO Camera
CCD47Ctrl.h
Go to the documentation of this file.
1 /************************************************************
2 * CCD47Ctrl.h
3 *
4 * Author: Jared R. Males (jrmales@email.arizona.edu)
5 *
6 * Declarations for the VisAO CCD47 controller.
7 *
8 * Developed as part of the Magellan Adaptive Optics system.
9 ************************************************************/
10 
11 /** \file CCD47Ctrl.h
12  * \author Jared R. Males
13  * \brief Declarations for the VisAO CCD47 controller.
14  *
15  * VisAO specific CCD47 controller. Instead of trying to calculate the correct program (as the adopt version does)
16  * we expect this to be explicitly set by the user, along with gain and repititions. We'll eventually have to adapt this
17  * for arbitrary integrations (using the TTL input on the Little Joe).
18  *
19  * Is a standalone VisAOApp, doesn't depend on MsgD.
20 */
21 
22 
23 #ifndef __CCD47Ctrl_h__
24 #define __CCD47Ctrl_h__
25 
26 #include <vector>
27 #include <string>
28 
29 #include "VisAOApp_standalone.h"
30 #include "AOStates.h"
31 
32 extern "C"
33 {
34 
35 #include <stdio.h>
36 #include <string.h> // strncmp()
37 #include <stdlib.h> // atoi()
38 #include <stdarg.h>
39 #include <time.h> // time(), localtime()
40 //#include <unistd.h> // sleep()
41 #include <pthread.h>
42 
43 // Joe related libs
44 #include "hwlib/netseriallib.h"
45 #include "hwlib/joelib.h"
46 
47 // General Supervisor libs
48 #include "base/thrdlib.h"
49 }//extern "C"
50 
51 #include "EDTutils.h"
52 
53 //#define _debug
54 
55 namespace VisAO
56 {
57 
58 ///Holds the details of one little joe program
59 /** A few differences from the Arcetri version.
60  */
61 typedef struct
62 {
63  std::string name; ///< Program name
64  int readout_speed; ///< Program readout speed, pixel rate in khz
65  int binx; ///< x binning
66  int biny; ///< y binning
67  int windowx; ///< x window size
68  int windowy; ///< y window size
69  float32 delay_base;
70  float32 delay_inc;
71  /// Array of default black levels.
72  /** This should be long as the highest no. of black level possible.
73  */
74  int black_levels[4];
75 
76  std::string EDT_cfg_fname; ///<Name of the EDT config file for this program
77 
79 
80 ///Holds the details of a little joe program set (i.e. a pattern/con file pair)
81 /** A few differences from the Arcetri version.
82  */
83 typedef struct
84 {
85  std::string name; ///< Name of the program set
86  std::string control_filename; ///< Name of the control file
87  std::string pattern_filename; ///< Name of the pattern file
88  std::vector<littlejoe_program> programs; ///< Details of the individual programs
89  double load_time; ///< Typical time for this programs set to upload to the little joe.
91 
92 
93 ///The controller for the VisAO CCD47 via its Little Joe
94 /** Mostly based on the Arcetri JoeCtrl class.
95  * This is the VisAO specific CCD47 controller. Instead of trying to calculate the correct program (as the adopt version does)
96  * we expect this to be explicitly set by the user, along with gain and repititions. We'll eventually have to adapt this
97  * for arbitrary integrations.
98  *
99  * See \ref VisAOApp_standalone for command line arguments. There are no additional command line arguments for CCD47Ctrl.
100  *
101  * This is a standalone VisAOApp, so it doesn't depend on MsgD. In addition to the optional standard config options inherited from \ref VisAOApp_standalone this class REQUIRES:
102  * - <b>ccd47_name</b> <tt>string</tt> - name of the ccd, usually ccd47, used to find the adopt config file, e.g. ccd47.conf
103  * - <b>EDT_cfgdir</b> <tt>string</tt> - directory where the EDT configuration files are stored.
104  *
105  * The config file referenced by ccd47_name must have the following
106  * values in it:
107  * - <b>ccdName</b> <tt>string</tt> - Name of the ccd, in this case always ccd47, used for conf file lookup.
108  * - <b>ccdNetAddr</b> <tt>string</tt> - IP address, or host name, of the port server
109  * - <b>ccdNetPort</b> <tt>int</tt> - Port number of the ccd on the port server
110  * - <b>ccdXdim</b> <tt>int</tt>
111  * - <b>ccdYdim</b> <tt>int</tt>
112  * - <b>ccdDefaultXbin</b> <tt>int</tt>
113  * - <b>ccdDefaultYbin</b> <tt>int</tt>
114  * - <b>ccdDefaultSpeed</b> <tt>int</tt>
115  * - <b>ccdDefaultBlack</b> <tt>int</tt>
116  * - <b>ccdBlacksNum</b> <tt>int</tt>
117  * - <b>minRep</b> <tt>int</tt>
118  * - <b>maxRep</b> <tt>int</tt>
119  * - <b>maxNumSpeeds</b> <tt>int</tt>
120  * - <b>maxNumBins</b> <tt>int</tt>
121  * - <b>startProgramSet</b> <tt>int</tt>
122  * - <b>startProgram</b> <tt>int</tt>
123  * - <b>startGain</b> <tt>int</tt>
124  * - <b>startReps</b> <tt>int</tt>
125  *
126  */
127 
129 {
130  public:
131  /// Name and config file constructor.
132  CCD47Ctrl( std::string name, const std::string &conffile) throw (AOException);
133 
134  /// Command line constructor.
135  CCD47Ctrl( int argc, char **argv) throw (AOException);
136 
137  protected:
138 
139  /// Common creation tasks, called by constructors.
140  void Create(void) throw (AOException);
141 
142  /// Initialize the details of the VisAOApp.
143  void init_VisAOApp();
144 
145 
146  /// Name of the adopt config file (this is specified in the main config file)
147  std::string adopt_cfg_file;
148  /// The dictionary of config details from the adopt config file.
149  Config_File* adopt_cfg;
150 
151  /// CCD name
152  std::string _ccdName;
153 
154  /// CCD network address
155  std::string _ccdNetAddr;
156 
157  ///The TCP/IP port of the CCD at its network address.
159 
160  int _ccdDx; ///< CCD x dimension
161  int _ccdDy; ///< CCD y dimension
162  unsigned int _ccdNumSpeeds; ///< Number of speeds
163  int _maxNumSpeeds; ///< Maximum number of speeds
164  int _maxNumBins; ///< Maximum number of bins
165 
166  int _ccdBlacksNum; ///< Number of blacks
167  int _minRep; ///< Minimum repetitions
168  int _maxRep; ///< Maximum repetitions.
169 
170  ///EDT configuration files directory
171  std::string EDT_cfgdir; ///<EDT configuration file directory
172 
173  std::string FrameGrabberName; ///<Process name of the relevant framegrabber
174  std::string FrameWriterName; ///<Process name of the relevant framewriter
175 
176  int _ccdDefaultXbin; ///< Default x binning
177  int _ccdDefaultYbin; ///< Default y binning
178  int _ccdDefaultSpeed; ///< Default speed
179  int _ccdDefaultBlack; ///< Default black levels
180 
181  //Current values:
182  int cur_State; ///< Current state
183 
184  /// Whether or not the framegrabber is running.
186 
187  /// Whether or not the framegrabber is saving, and how many to save.
188  /** If -1 then saving indefinately.
189  */
190  int FGsaving;
191 
192  /// How many saves are remaining.
194 
195  /// Whether or not the framegrabber is skipping, and how many frames to skip.
197 
198  /// The current subdirectory of the framewriter
199  std::string FWsubdir;
200 
201  /// Image type, 0=science (default), 1=acquisition, 2=dark, 3=sky
202  int imtype;
203 
204  ///The Framewriter47 status board is used to check if framewriter is running before saving.
206 
207  int cur_ProgramSet; ///< The currently loaded program set
208  int cur_Program; ///< The currently running program
209  int cur_Gain; ///< The currently loaded gain
210  int cur_Reps; ///< The currently loaded repetitions.
211  int cur_xbin; ///< Current x binning
212  int cur_ybin; ///< Current y binning
213  int cur_windowx; ///< Current x window size
214  int cur_windowy; ///< Current y window size
215  int cur_speed; ///< Current speed
216 
217  double cur_framerate; ///< Current frame rate
218  double temps[3]; ///< Current temperatures
219 
220  int blackLevel[2]; ///< Current blacklevels
221 
222  /// Struct array representing files that can be uploaded to LittleJoe.
223  std::vector<littlejoe_programset> ondisk;
224  int _startProgramSet;
225  int _startProgram;
226  int _startGain;
227  int _startReps;
228 
229  /// Load the configuration files specifying the different program sets that can be uploaded to LittleJoe
230  int LoadJoeDiskFiles(void);
231 
232  /// Read a configuration file with the parameters of a little Joe Program SET
233  littlejoe_programset ReadProgramSet( Config_File &cfg);
234 
235  /// Read a configuration file with the parameters of a single LittleJoe program
236  littlejoe_program ReadProgram( Config_File &cfg);
237 
238  ///Start sequencing and start the framegrabber
239  int Start(void);
240 
241  ///Stop sequencing and stop the framegrabber
242  int Stop(void);
243 
244  ///Sets the LittleJoe controller in a known state
245  /** \retval NO_ERROR on success
246  * \todo Make this do something usefule.
247  */
248  int FirstJoeConfig(void);
249 
250  ///Check the LittleJoe CCD camera status
251  /** Gets the CCD status from the serial interface and checks
252  * the status of the frame grabber process.
253  * \retval NO_ERROR on success
254  */
255  int ReadJoeStatus();
256 
257  ///Read the LittleJoe CCD camera temperatures
258  /** Reads the CCD temperature from the serial interface and logs
259  * them to a file.
260  * \retval NO_ERROR on success
261  */
262  int ReadJoeTemps(void);
263 
264  ///Checks whether the framegrabber process is running, and what its current status is.
265  int GetFramegrabberStatus();
266 
267  ///Setup network connection with LittleJoe
268  /** This function sets up the network connection with the LittleJoe CCD Camera.
269  * After the network is up, the communication is tested with the TestJoe() function.
270  * \retval 0 on success
271  * \retval <0 otherwise (errordb.h code)
272  */
273  int SetupNetwork(void);
274 
275  ///Reprogram the little joe, including file uploads as necessary
276  /** Actually launches a thread, which does the work using __ReprogramJoe
277  * which is necessary to prevent uploads from being interrupted by signals.
278  */
279  int ReprogramJoe( int program_set, int program, int gain, int rep, int force=0);
280 
281  ///Only change the settings in this software, do not reprogram the little joe
282  /** This is useful if a software restart is necessary, but the Little Joe is still running.
283  */
284  int swonly_reprogram( int program_set, int program, int gain, int rep);
285 
286  pthread_t load_thread; ///<Identifier for the __ReprogramJoe thread
287 
288  ///Mutex for the __ReprogramJoe thread
289  /** Prevents multiple simultaneous reprograms.
290  */
291  pthread_mutex_t reprogMutex;
292 
293  ///Tells the main thread that the __ReprogramJoe thread is working
294  /** is true until the main thread can try_join on load_thread.
295  */
297 
298 
299  public:
300  ///Return the network address of the CCD
301  std::string get_ccdNetAddr(){return _ccdNetAddr;}
302 
303  ///Returns the TCP/IP port of the CCD at its network address
305 
306  ///This function does the actual reprogramming, based on the adopt version.
307  int __ReprogramJoe( int program_set, int program, int gain, int rep, int force=0);
308 
309  ///Set the image type
310  int set_imtype(int it);
311 
312  protected:
313  /// Computes the current framerate from the current state of LittleJoe control variables.
314  /** \retval NO_ERROR on success
315  */
316  int ComputeFramerate();
317 
318  /// Computes the reps required to match a requested framerate
319  int ComputeRepsFrameRate(double fr);
320 
321  /// Computes the reps required to match a requested exptime
322  int ComputeRepsExpTime(double et);
323 
324  /// Get the complete path of a program/pattern file
325  std::string getCompletePath( std::string filename);
326 
327  /// Performs controller functions and manages states
328  /** Switches the controller from one state to another. States can be changed asynchronously
329  * from this thread or the listening thread, and this function will properly react to a new state.
330  * Some states include a sleep() function to slow down the thread when immediate action is not necessary
331  */
332  int DoFSM(void);
333 
334  ///Temperature logger
335  Logger *_tempsLogger;
336 
337  ///Loads the EDT configuration file.
338  /** \param fname is the path to the config file
339  * \retval 0 on success
340  */
341  int load_EDT_config(std::string);
342 
343  public:
344  ///Overriden virtual function, called by Exec().
345  int Run();
346 
347  /// Called by \ref __remote_command after control state logic.
348  /** This should be overriden by derived classes.
349  * \param com is the command string to be processed.
350  * \returns the application response.
351  */
352  virtual std::string remote_command(std::string com);
353 
354  /// Called by \ref __local_command after control state logic.
355  /** This should be overriden by derived classes.
356  * \param com is the command string to be processed.
357  * \returns the application response.
358  */
359  virtual std::string local_command(std::string com);
360 
361  /// Called by \ref __script_command after control state logic.
362  /** This should be overriden by derived classes.
363  * \param com is the command string to be processed.
364  * \returns the application response.
365  */
366  virtual std::string script_command(std::string com);
367 
368  ///Called by all the command handlers, to perform common command processing.
369  std::string common_command(std::string com, int cmode);
370 
371  public:
372  ///Update the CCD47 Status Board shared memory.
373  virtual int update_statusboard();
374 };
375 
376 ///Set the recall "RCL" parameter on the little joe (pixel readout, gain & related settings)
377 /** \param rcl program number 0-31 inclusive
378  * \retval 0 on success
379  * \retval <0 otherwise (errordb code)
380  */
381 int SetJoeRCL( int rcl);
382 
383 ///This is the thread start function for reprogramming the little joe
384 /** First blocks SIGIO so uploads aren't interrupted
385  * Then calls __ReprogramJoe
386  */
387 void * ReprogramJoeThreadWorker(void * adata);
388 
389 } //namespace VisAO
390 
391 #endif //__CCD47Ctrl_h__
std::string EDT_cfg_fname
Name of the EDT config file for this program.
Definition: CCD47Ctrl.h:76
void Create(void)
Common creation tasks, called by constructors.
Definition: CCD47Ctrl.cpp:42
littlejoe_program ReadProgram(Config_File &cfg)
Read a configuration file with the parameters of a single LittleJoe program.
Definition: CCD47Ctrl.cpp:252
Config_File * adopt_cfg
The dictionary of config details from the adopt config file.
Definition: CCD47Ctrl.h:149
std::string adopt_cfg_file
Name of the adopt config file (this is specified in the main config file)
Definition: CCD47Ctrl.h:147
Holds the details of a little joe program set (i.e. a pattern/con file pair)
Definition: CCD47Ctrl.h:83
unsigned int _ccdNumSpeeds
Number of speeds.
Definition: CCD47Ctrl.h:162
double load_time
Typical time for this programs set to upload to the little joe.
Definition: CCD47Ctrl.h:89
int load_EDT_config(std::string)
Loads the EDT configuration file.
Definition: CCD47Ctrl.cpp:1101
int FGrunning
Whether or not the framegrabber is running.
Definition: CCD47Ctrl.h:185
pthread_t load_thread
Identifier for the __ReprogramJoe thread.
Definition: CCD47Ctrl.h:286
int readout_speed
Program readout speed, pixel rate in khz.
Definition: CCD47Ctrl.h:64
int cur_xbin
Current x binning.
Definition: CCD47Ctrl.h:211
int joe_loading
Tells the main thread that the __ReprogramJoe thread is working.
Definition: CCD47Ctrl.h:296
int cur_State
Current state.
Definition: CCD47Ctrl.h:182
int _minRep
Minimum repetitions.
Definition: CCD47Ctrl.h:167
int _ccdDx
CCD x dimension.
Definition: CCD47Ctrl.h:160
The standalone VisAO application, does not interface with the AO Supervisor.
std::string control_filename
Name of the control file.
Definition: CCD47Ctrl.h:86
std::string FrameWriterName
Process name of the relevant framewriter.
Definition: CCD47Ctrl.h:174
int Stop(void)
Stop sequencing and stop the framegrabber.
Definition: CCD47Ctrl.cpp:287
int SetupNetwork(void)
Setup network connection with LittleJoe.
Definition: CCD47Ctrl.cpp:763
The controller for the VisAO CCD47 via its Little Joe.
Definition: CCD47Ctrl.h:128
basic_status_board * fw47sb
The Framewriter47 status board is used to check if framewriter is running before saving.
Definition: CCD47Ctrl.h:205
int cur_Program
The currently running program.
Definition: CCD47Ctrl.h:208
Declarations for the standalone VisAO application.
Logger * _tempsLogger
Temperature logger.
Definition: CCD47Ctrl.h:335
double cur_framerate
Current frame rate.
Definition: CCD47Ctrl.h:217
int FGremaining
How many saves are remaining.
Definition: CCD47Ctrl.h:193
virtual int update_statusboard()
Update the CCD47 Status Board shared memory.
Definition: CCD47Ctrl.cpp:1653
virtual std::string remote_command(std::string com)
Called by __remote_command after control state logic.
Definition: CCD47Ctrl.cpp:1128
int FGskipping
Whether or not the framegrabber is skipping, and how many frames to skip.
Definition: CCD47Ctrl.h:196
std::vector< littlejoe_program > programs
Details of the individual programs.
Definition: CCD47Ctrl.h:88
virtual std::string local_command(std::string com)
Called by __local_command after control state logic.
Definition: CCD47Ctrl.cpp:1133
int cur_ProgramSet
The currently loaded program set.
Definition: CCD47Ctrl.h:207
int cur_windowx
Current x window size.
Definition: CCD47Ctrl.h:213
int cur_ybin
Current y binning.
Definition: CCD47Ctrl.h:212
std::string get_ccdNetAddr()
Return the network address of the CCD.
Definition: CCD47Ctrl.h:301
int _ccdDefaultYbin
Default y binning.
Definition: CCD47Ctrl.h:177
pthread_mutex_t reprogMutex
Mutex for the __ReprogramJoe thread.
Definition: CCD47Ctrl.h:291
std::string name
Program name.
Definition: CCD47Ctrl.h:63
int DoFSM(void)
Performs controller functions and manages states.
Definition: CCD47Ctrl.cpp:931
int windowx
x window size
Definition: CCD47Ctrl.h:67
double temps[3]
Current temperatures.
Definition: CCD47Ctrl.h:218
int Start(void)
Start sequencing and start the framegrabber.
Definition: CCD47Ctrl.cpp:346
int SetJoeRCL(int rcl)
Set the recall "RCL" parameter on the little joe (pixel readout, gain & related settings) ...
Definition: CCD47Ctrl.cpp:1688
int ComputeRepsFrameRate(double fr)
Computes the reps required to match a requested framerate.
Definition: CCD47Ctrl.cpp:474
CCD47Ctrl(std::string name, const std::string &conffile)
Name and config file constructor.
Definition: CCD47Ctrl.cpp:32
int FirstJoeConfig(void)
Sets the LittleJoe controller in a known state.
Definition: CCD47Ctrl.cpp:384
virtual std::string script_command(std::string com)
Called by __script_command after control state logic.
Definition: CCD47Ctrl.cpp:1138
int _maxRep
Maximum repetitions.
Definition: CCD47Ctrl.h:168
Declarations for some utility functions pertaining to the EDT framegrabber PCI card.
int ReprogramJoe(int program_set, int program, int gain, int rep, int force=0)
Reprogram the little joe, including file uploads as necessary.
Definition: CCD47Ctrl.cpp:537
int LoadJoeDiskFiles(void)
Load the configuration files specifying the different program sets that can be uploaded to LittleJoe...
Definition: CCD47Ctrl.cpp:193
int _ccdDefaultXbin
Default x binning.
Definition: CCD47Ctrl.h:176
std::string common_command(std::string com, int cmode)
Called by all the command handlers, to perform common command processing.
Definition: CCD47Ctrl.cpp:1143
int windowy
y window size
Definition: CCD47Ctrl.h:68
int ComputeFramerate()
Computes the current framerate from the current state of LittleJoe control variables.
Definition: CCD47Ctrl.cpp:446
int _ccdDefaultBlack
Default black levels.
Definition: CCD47Ctrl.h:179
int set_imtype(int it)
Set the image type.
Definition: CCD47Ctrl.cpp:429
int cur_speed
Current speed.
Definition: CCD47Ctrl.h:215
std::string _ccdNetAddr
CCD network address.
Definition: CCD47Ctrl.h:155
std::vector< littlejoe_programset > ondisk
Struct array representing files that can be uploaded to LittleJoe.
Definition: CCD47Ctrl.h:223
int ReadJoeStatus()
Check the LittleJoe CCD camera status.
Definition: CCD47Ctrl.cpp:794
Holds the details of one little joe program.
Definition: CCD47Ctrl.h:61
int ReadJoeTemps(void)
Read the LittleJoe CCD camera temperatures.
Definition: CCD47Ctrl.cpp:878
std::string EDT_cfgdir
EDT configuration files directory.
Definition: CCD47Ctrl.h:171
int _maxNumBins
Maximum number of bins.
Definition: CCD47Ctrl.h:164
std::string getCompletePath(std::string filename)
Get the complete path of a program/pattern file.
Definition: CCD47Ctrl.cpp:918
int biny
y binning
Definition: CCD47Ctrl.h:66
int binx
x binning
Definition: CCD47Ctrl.h:65
std::string FWsubdir
The current subdirectory of the framewriter.
Definition: CCD47Ctrl.h:199
int _ccdNetPort
The TCP/IP port of the CCD at its network address.
Definition: CCD47Ctrl.h:158
std::string pattern_filename
Name of the pattern file.
Definition: CCD47Ctrl.h:87
int _maxNumSpeeds
Maximum number of speeds.
Definition: CCD47Ctrl.h:163
int cur_windowy
Current y window size.
Definition: CCD47Ctrl.h:214
int blackLevel[2]
Current blacklevels.
Definition: CCD47Ctrl.h:220
int FGsaving
Whether or not the framegrabber is saving, and how many to save.
Definition: CCD47Ctrl.h:190
int swonly_reprogram(int program_set, int program, int gain, int rep)
Only change the settings in this software, do not reprogram the little joe.
Definition: CCD47Ctrl.cpp:718
std::string FrameGrabberName
Process name of the relevant framegrabber.
Definition: CCD47Ctrl.h:173
int get_ccdNetPort()
Returns the TCP/IP port of the CCD at its network address.
Definition: CCD47Ctrl.h:304
int GetFramegrabberStatus()
Checks whether the framegrabber process is running, and what its current status is.
Definition: CCD47Ctrl.cpp:313
int cur_Gain
The currently loaded gain.
Definition: CCD47Ctrl.h:209
The namespace of VisAO software.
int imtype
Image type, 0=science (default), 1=acquisition, 2=dark, 3=sky.
Definition: CCD47Ctrl.h:202
int _ccdDy
CCD y dimension.
Definition: CCD47Ctrl.h:161
int __ReprogramJoe(int program_set, int program, int gain, int rep, int force=0)
This function does the actual reprogramming, based on the adopt version.
Definition: CCD47Ctrl.cpp:561
int cur_Reps
The currently loaded repetitions.
Definition: CCD47Ctrl.h:210
int _ccdBlacksNum
Number of blacks.
Definition: CCD47Ctrl.h:166
void init_VisAOApp()
Initialize the details of the VisAOApp.
Definition: CCD47Ctrl.cpp:140
littlejoe_programset ReadProgramSet(Config_File &cfg)
Read a configuration file with the parameters of a little Joe Program SET.
Definition: CCD47Ctrl.cpp:217
int _ccdDefaultSpeed
Default speed.
Definition: CCD47Ctrl.h:178
int ComputeRepsExpTime(double et)
Computes the reps required to match a requested exptime.
Definition: CCD47Ctrl.cpp:495
std::string name
Name of the program set.
Definition: CCD47Ctrl.h:85
std::string _ccdName
CCD name.
Definition: CCD47Ctrl.h:152
void * ReprogramJoeThreadWorker(void *adata)
This is the thread start function for reprogramming the little joe.
Definition: CCD47Ctrl.cpp:519
int Run()
Overriden virtual function, called by Exec().
Definition: CCD47Ctrl.cpp:1030