File: trig.c

package info (click to toggle)
gpivtools 0.6.0-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,472 kB
  • ctags: 528
  • sloc: ansic: 6,804; sh: 5,105; perl: 1,035; makefile: 299; python: 207
file content (351 lines) | stat: -rw-r--r-- 9,722 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4 c-style: "K&R" -*- */

/*-----------------------------------------------------------------------------
   gpiv_trig - triggers a (double Nd_YAGG) laser on a (CCD) camera

   Copyright (C) 2005 Gerber van der Graaf
                      Julio Soria

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  

-----------------------------------------------------------------------------*/

#include <stdio.h>
#include <sys/types.h>
#include <getopt.h>
#include "system.h"
#ifdef ENABLE_TRIG
#include <rtai.h>
#endif
#include <sys/ioctl.h>
#include <sys/time.h>

#include <gpiv.h>


#ifdef ENABLE_TRIG

#define EXIT_FAILURE 1

/* char *xmalloc (); */
/* char *xrealloc (); */
/* char *xstrdup (); */


static void usage (int status);
/*
 * The name the program was run with, stripped of any leading path.
 */
char *program_name;

/* getopt_long return codes */
enum {DUMMY_CODE=129
};

/* Option flags and variables */

gboolean interactive;		/* --interactive */
gboolean verbose;		/* -- verbose output during running process */

static struct option const long_options[] =
{
    {"interactive", no_argument, 0, 'i'},
    {"verbose", no_argument, 0, 'V'},
    {"help", no_argument, 0, 'h'},
    {"version", no_argument, 0, 'v'},
    {NULL, 0, NULL, 0}
};

static int 
command_args (int argc, 
              char **argv);

static int 
interactive_func(void);

static int 
initialise (GpivTrigPar *trig_par
/* GpivTrigTime *ttime */
	    );

/*
 * Set all the option flags according to the switches specified.
 * Return the index of the first non-option argument.
 */

static int
command_args (int argc, 
              char *argv[]
              )
/*-----------------------------------------------------------------------------
 */
{
    int c;


    while ((c = getopt_long (argc, argv, 
                             "i"	/* interactive */
                             "V"	/* verbose */
                             "h"	/* help */
                             "v",	/* version */
                             long_options, (int *) 0)) != EOF) {
        switch (c) {
        case 'i':		/* --interactive */
            interactive = TRUE;
            break;
	case 'V':		/* --verbose */
            verbose = TRUE;
            break;
	case 'v':
            printf ("gpiv_trig %s\n", VERSION);
            exit (0);
	case 'h':
            usage (0);
	default:
            usage (EXIT_FAILURE);
	}
    }

    return 0;
}



static void
usage (int status
       )
/*-----------------------------------------------------------------------------
 */
{
    printf (_("%s - \
triggers a (double Nd_YAGG) laser on a CCD camera\n"), program_name);
    printf (_("Usage: %s [OPTION]... [FILE]...\n"), program_name);
    printf (_("\
Options:\n\
  -i, --interactive          prompt for confirmation\n\
  -V, --verbose              print more information\n\
  -h, --help                 display this help and exit\n\
  -v, --version              output version information and exit\n\
"));
    exit (status);
}



static int 
initialise (GpivTrigPar *trig_par
/*             GpivTrigTime *ttime */
	    ) 
/*-----------------------------------------------------------------------------
 */
{
    int ok = 0;
    float dtime, acq_time, increment;
    int mode, cycles;

/*
 * private user input variables
 * hardwire some of the timings ....
 */

    if (verbose) printf("\nEntering initialise\n");
    trig_par->ttime.laser_trig_pw = (RTIME)(0.02*1000*1000);  /* laser trigger pulse width 0.02ms */
    trig_par->ttime.time2laser    = (RTIME)(0.19*1000*1000);  /* time from laser trigger->Hi
					      until laser pulse: 0.19ms  */


    printf("\n\tmode:\n");
    printf("\t\t1 - indefinite periodic\n");
    printf("\t\t2 - duration\n");
    printf("\t\t3 - interrupt one shot\n");
    printf("\t\t4 - interrupt periodic\n");
    printf("\t\t5 - incremented dt\n");
    printf("\t\t6 - double exposure\n");
    printf("\t\t9 - exit\n");
    printf("\t(1/2/3/4/5/6/9) > ");
    scanf("%d", &mode);
    trig_par->ttime.mode = mode;
    
    if (trig_par->ttime.mode == 9) {
        return 1;
    }
    
/*
 * ask for other timings ....
 */
    printf("\n\tAcquisition parameters: \n");

    printf("\ttime between 2 laser exposures in ms > ");
    scanf("%f", &dtime);
    trig_par->ttime.dt = (RTIME)1000 * 1000 * dtime;
    
/* gpiv_trig_test_parameter(GpivTrigPar * trig_par */
/*                         ); */
    if ((trig_par->ttime.mode == GPIV_TIMER_MODE__PERIODIC) 
        || (trig_par->ttime.mode == GPIV_TIMER_MODE__DURATION) 
        || (trig_par->ttime.mode == GPIV_TIMER_MODE__TRIGGER_IRQ) 
        || (trig_par->ttime.mode == GPIV_TIMER_MODE__DOUBLE)) {
        printf("\tdouble frame acquisition period in ms (typ. 250 ms) > ");
        scanf("%f", &acq_time);
        trig_par->ttime.cam_acq_period = (RTIME)1000 * 1000 * acq_time;
    } else {
        trig_par->ttime.cam_acq_period  = (trig_par->ttime.dt) * 2;
    }
    
    if ((trig_par->ttime.mode == GPIV_TIMER_MODE__DURATION) 
        || (trig_par->ttime.mode == GPIV_TIMER_MODE__ONE_SHOT_IRQ) 
        || (trig_par->ttime.mode == GPIV_TIMER_MODE__TRIGGER_IRQ) 
        || (trig_par->ttime.mode == GPIV_TIMER_MODE__INCREMENT)) {
        printf("\tnumber of cycles > ");
        scanf("%d", &cycles);
        trig_par->ttime.cycles = cycles;
    } else {
        trig_par->ttime.cycles = 1;
    }
    
    if (trig_par->ttime.mode == GPIV_TIMER_MODE__INCREMENT) {
        printf("\tincrement in ms > ");
        scanf("%f", &increment);
        trig_par->ttime.increment = (RTIME)1000 * 1000 * increment;
    } else {
        trig_par->ttime.increment = 1;
    }

    ok = 1;
    if (verbose) printf("\nLeaving initialise\n");
    return ok;
}



static int
interactive_func (void
		  )
/*-----------------------------------------------------------------------------
 */
{
    GpivTrigPar trig_per;
/*     GpivTrigTime ttime = trig_per.ttime; */
    int    init, start, stop, error;    /* file pointer int */
    int    on = 1, off = 0, param_ok;
    
    if (verbose)   printf("\nEntering interactive_func\n");
    if (!gpiv_trig_openrtfs(&init, &start, &stop, &error)) {
        printf("\n\nFAIL IN FIFO OPEN, BYE ..... \n\n");
    }

    printf("\n\n\tProgram to control camera with external triggering & laser\n");

    while (1) {
        if(!initialise(&trig_per /* timing */))  {
            printf("\n\nPROBLEM INITIALISING PROGRAM, BYE ..... \n\n");
            exit(0);
        }

        if (trig_per.ttime.mode == 9) {
            printf("\n\tEx(c)iting program\n\n");
            break;
        }

        printf("\n\tWriting details to RT module\n\n");
/*
 * write the timing details to /dev/rtf/1
 */

        if((write(init, &trig_per.ttime, sizeof(GpivTrigTime))) < 0)  {
            printf("\n\nfail in  setting Camera and Laser timing ...\n\n");
            exit(0);
        }

        if((read(error, &param_ok, sizeof(int))) < 0) {
            printf("\n\nfail in receipt of confirmation ...\n\n");
            exit(0);
        }
        
        if (param_ok != 1) {
            printf("\n\tInvalid parameters entered \n");
        
        } else {
            printf("\n\tParameters :\n");
            printf("\t\tcam_acq_period: %lld\n", trig_per.ttime.cam_acq_period);
            printf("\t\tlaser_trig_pw:  %lld\n", trig_per.ttime.laser_trig_pw);
            printf("\t\ttime2laser:     %lld\n", trig_per.ttime.time2laser);
            printf("\t\tdt:             %lld\n", trig_per.ttime.dt);
            printf("\t\tmode:           %d\n", trig_per.ttime.mode);
            printf("\t\tcycles:         %d\n", trig_per.ttime.cycles);
            printf("\t\tincrement:      %d\n", (int) trig_per.ttime.increment);
            
            printf("\n\n\tStart on <enter>  >");
            getchar();
            getchar();

            if((write(start, &on, sizeof(int))) < 0) {
                printf("\n\nfail in starting camera and laser timing ...\n\n");
                exit(0);
            }

            printf("\n\tStop on <enter>   >");
            getchar();
            
            if((write(stop, &off, sizeof(int))) < 0)  {
                printf("\nfail in stopping camera and laser timing ...\n\n");
                exit(0);
            }
        }
        
    }
    
    if (verbose)   printf("\nLeaving interactive_func\n");
    return 0;
}



int
main (int argc, 
      char *argv[]
      )
/*-----------------------------------------------------------------------------
 * Main program to send trigger pulses 
 */
{
    int i, return_val = 0;
    
    program_name = argv[0];
    
    i = command_args (argc, argv);
    if (interactive) {
        return_val = interactive_func();
    }

    exit (return_val);
}



#else /* ENABLE_TRIG */

int 
main(int argc, 
     char *argv[]
     )
/*-----------------------------------------------------------------------------
 */ 
{
    g_error("%s: this is dead code as ENABLE_TRIG has not been set during building the program", argv[0]);
    return -1;
}

#endif /* ENABLE_TRIG */