ViSP  3.0.0
ringLight.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Example of ring light control.
32  *
33  * Author:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
46 #include <visp3/core/vpConfig.h>
47 #include <visp3/core/vpDebug.h>
48 #include <cmath> // std::fabs
49 #include <limits> // numeric_limits
50 #if defined VISP_HAVE_PARPORT
51 #include <stdlib.h>
52 #include <stdio.h>
53 #include <iostream>
54 
55 #include <visp3/robot/vpRingLight.h>
56 #include <visp3/io/vpParseArgv.h>
57 #include <visp3/core/vpTime.h>
58 
59 // List of allowed command line options
60 #define GETOPTARGS "d:hn:ot:"
61 
72 void usage(const char *name, const char *badparam, int nsec, double nmsec)
73 {
74  fprintf(stdout, "\n\
75 Send a pulse to activate the ring light or turn on the ring light \n\
76 during %d s.\n\
77 \n\
78 By default, that means without parameters, send a pulse which duration\n\
79 is fixed by the harware. To control the duration of the pulse, use \n\
80 \"-t <pulse width in ms>\" option. To turn on the light permanently, \n\
81 use \"-o -n <on duration in second>]\"\n \
82 \n\
83 SYNOPSIS\n\
84  %s [-o] [-n <on duration in second>] [-t <pulse width in ms>] [-h]\n\
85 ", nsec, name);
86 
87  fprintf(stdout, "\n\
88 OPTIONS: Default\n\
89 \n\
90  -o\n\
91  Turn the ring light on during %d s.\n\
92  If this option is not set, send a short pulse\n\
93  to activate the light.\n\
94 \n\
95  -t %%g : <pulse width in ms> %g\n\
96  Pulse width in milli-second.\n\
97  Send a pulse which duration is fixed by this parameter.\n\
98  Without this option, the pulse width is fixed by the \n\
99  harware.\n\
100 \n\
101  -n %%d : <on duration in second> %d\n\
102  Time in second while the ring light is turned on.\n\
103  This option is to make into realtion with option \"-o\".\n\
104 \n\
105  -h\n\
106  Print the help.\n\n", nsec, nmsec, nsec);
107 
108  if (badparam) {
109  fprintf(stderr, "ERROR: \n" );
110  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
111  }
112 
113 }
114 
128 bool getOptions(int argc, const char **argv, bool &on, int &nsec, double &nmsec)
129 {
130  const char *optarg;
131  int c;
132 
133  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
134 
135  switch (c) {
136  case 'n': nsec = atoi(optarg); break;
137  case 'o': on = true; break;
138  case 't': nmsec = atof(optarg); break;
139  case 'h': usage(argv[0], NULL, nsec, nmsec); return false; break;
140 
141  default:
142  usage(argv[0], optarg, nsec, nmsec); return false; break;
143  }
144  }
145 
146  if ((c == 1) || (c == -1)) {
147  // standalone param or error
148  usage(argv[0], NULL, nsec, nmsec);
149  std::cerr << "ERROR: " << std::endl;
150  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
151  return false;
152  }
153 
154  return true;
155 }
156 
162 int
163 main(int argc, const char **argv)
164 {
165  try {
166  bool on = false;
167  int nsec = 5; // Time while the ring light is turned on
168  double nmsec = 0; // Pulse duration
169 
170  // Read the command line options
171  if (getOptions(argc, argv, on, nsec, nmsec) == false) {
172  exit (-1);
173  }
174 
175  vpRingLight light;
176 
177  //if (nmsec == 0.)
178  if (std::fabs(nmsec) <= std::numeric_limits<double>::epsilon())
179  light.pulse();
180  else
181  light.pulse(nmsec);
182 
183  if (on) {
184  printf("Turn on ring light\n");
185  light.on(); // Turn the ring light on
186  vpTime::wait(nsec * 1000); // Wait 5 s
187  light.off(); // and then turn the ring light off
188  }
189  else {
190  printf("Send a pulse to activate the ring light\n");
191  light.pulse();
192  }
193  }
194  catch (vpParallelPortException e) {
195  switch(e.getCode()) {
197  printf("Can't open the parallel port to access to the ring light device\n");
198  break;
200  printf("Can't close the parallel port\n");
201  break;
202  }
203  }
204  catch(...) {
205  printf("An error occurs...\n");
206  }
207  return 0;
208 }
209 #else
210 int
211 main()
212 {
213  vpTRACE("Sorry, for the moment, vpRingLight class works only on unix...");
214  return 0;
215 }
216 #endif
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
int getCode(void)
send the object code
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
#define vpTRACE
Definition: vpDebug.h:414
void pulse()
Definition: vpRingLight.cpp:98
Ring light management under unix.
Definition: vpRingLight.h:112
Error that can be emited by the vpParallelPort class and its derivates.