Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
ringLight.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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  *
35 *****************************************************************************/
36 
43 #include <cmath> // std::fabs
44 #include <limits> // numeric_limits
45 #include <visp3/core/vpConfig.h>
46 #include <visp3/core/vpDebug.h>
47 #if defined VISP_HAVE_PARPORT
48 #include <iostream>
49 #include <stdio.h>
50 #include <stdlib.h>
51 
52 #include <visp3/core/vpTime.h>
53 #include <visp3/io/vpParseArgv.h>
54 #include <visp3/robot/vpRingLight.h>
55 
56 // List of allowed command line options
57 #define GETOPTARGS "d:hn:ot:"
58 
59 #ifdef ENABLE_VISP_NAMESPACE
60 using namespace VISP_NAMESPACE_NAME;
61 #endif
62 
73 void usage(const char *name, const char *badparam, int nsec, double nmsec)
74 {
75  fprintf(stdout, "\n\
76 Send a pulse to activate the ring light or turn on the ring light \n\
77 during %d s.\n\
78 \n\
79 By default, that means without parameters, send a pulse which duration\n\
80 is fixed by the harware. To control the duration of the pulse, use \n\
81 \"-t <pulse width in ms>\" option. To turn on the light permanently, \n\
82 use \"-o -n <on duration in second>]\"\n\
83 \n\
84 SYNOPSIS\n\
85  %s [-o] [-n <on duration in second>] [-t <pulse width in ms>] [-h]\n\
86 ",
87 nsec, name);
88 
89  fprintf(stdout, "\n\
90 OPTIONS: Default\n\
91 \n\
92  -o\n\
93  Turn the ring light on during %d s.\n\
94  If this option is not set, send a short pulse\n\
95  to activate the light.\n\
96 \n\
97  -t %%g : <pulse width in ms> %g\n\
98  Pulse width in milli-second.\n\
99  Send a pulse which duration is fixed by this parameter.\n\
100  Without this option, the pulse width is fixed by the \n\
101  harware.\n\
102 \n\
103  -n %%d : <on duration in second> %d\n\
104  Time in second while the ring light is turned on.\n\
105  This option is to make into realtion with option \"-o\".\n\
106 \n\
107  -h\n\
108  Print the help.\n\n",
109  nsec, nmsec, nsec);
110 
111  if (badparam) {
112  fprintf(stderr, "ERROR: \n");
113  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
114  }
115 }
116 
130 bool getOptions(int argc, const char **argv, bool &on, int &nsec, double &nmsec)
131 {
132  const char *optarg;
133  int c;
134 
135  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
136 
137  switch (c) {
138  case 'n':
139  nsec = atoi(optarg);
140  break;
141  case 'o':
142  on = true;
143  break;
144  case 't':
145  nmsec = atof(optarg);
146  break;
147  case 'h':
148  usage(argv[0], nullptr, nsec, nmsec);
149  return false;
150  break;
151 
152  default:
153  usage(argv[0], optarg, nsec, nmsec);
154  return false;
155  break;
156  }
157  }
158 
159  if ((c == 1) || (c == -1)) {
160  // standalone param or error
161  usage(argv[0], nullptr, nsec, nmsec);
162  std::cerr << "ERROR: " << std::endl;
163  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
164  return false;
165  }
166 
167  return true;
168 }
169 
175 int main(int argc, const char **argv)
176 {
177  try {
178  bool on = false;
179  int nsec = 5; // Time while the ring light is turned on
180  double nmsec = 0; // Pulse duration
181 
182  // Read the command line options
183  if (getOptions(argc, argv, on, nsec, nmsec) == false) {
184  return EXIT_FAILURE;
185  }
186 
187  vpRingLight light;
188 
189  // if (nmsec == 0.)
190  if (std::fabs(nmsec) <= std::numeric_limits<double>::epsilon())
191  light.pulse();
192  else
193  light.pulse(nmsec);
194 
195  if (on) {
196  printf("Turn on ring light\n");
197  light.on(); // Turn the ring light on
198  vpTime::wait(nsec * 1000); // Wait 5 s
199  light.off(); // and then turn the ring light off
200  }
201  else {
202  printf("Send a pulse to activate the ring light\n");
203  light.pulse();
204  }
205  }
206  catch (vpParallelPortException &e) {
207  switch (e.getCode()) {
209  printf("Can't open the parallel port to access to the ring light "
210  "device\n");
211  break;
213  printf("Can't close the parallel port\n");
214  break;
215  }
216  }
217  catch (...) {
218  printf("An error occurs...\n");
219  }
220  return EXIT_SUCCESS;
221 }
222 #else
223 int main()
224 {
225  std::cout << "vpRingLight class works only on unix on a the Inria Afma6 platform..." << std::endl;
226  return EXIT_SUCCESS;
227 }
228 #endif
int getCode() const
Definition: vpException.cpp:69
Error that can be emitted by the vpParallelPort class and its derivates.
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:70
VISP_EXPORT int wait(double t0, double t)