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