Visual Servoing Platform  version 3.6.1 under development (2024-05-18)
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 
69 void usage(const char *name, const char *badparam, int nsec, double nmsec)
70 {
71  fprintf(stdout, "\n\
72 Send a pulse to activate the ring light or turn on the ring light \n\
73 during %d s.\n\
74 \n\
75 By default, that means without parameters, send a pulse which duration\n\
76 is fixed by the harware. To control the duration of the pulse, use \n\
77 \"-t <pulse width in ms>\" option. To turn on the light permanently, \n\
78 use \"-o -n <on duration in second>]\"\n\
79 \n\
80 SYNOPSIS\n\
81  %s [-o] [-n <on duration in second>] [-t <pulse width in ms>] [-h]\n\
82 ",
83 nsec, name);
84 
85  fprintf(stdout, "\n\
86 OPTIONS: Default\n\
87 \n\
88  -o\n\
89  Turn the ring light on during %d s.\n\
90  If this option is not set, send a short pulse\n\
91  to activate the light.\n\
92 \n\
93  -t %%g : <pulse width in ms> %g\n\
94  Pulse width in milli-second.\n\
95  Send a pulse which duration is fixed by this parameter.\n\
96  Without this option, the pulse width is fixed by the \n\
97  harware.\n\
98 \n\
99  -n %%d : <on duration in second> %d\n\
100  Time in second while the ring light is turned on.\n\
101  This option is to make into realtion with option \"-o\".\n\
102 \n\
103  -h\n\
104  Print the help.\n\n",
105  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], nullptr, 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], nullptr, 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  return EXIT_FAILURE;
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  }
197  else {
198  printf("Send a pulse to activate the ring light\n");
199  light.pulse();
200  }
201  }
202  catch (vpParallelPortException &e) {
203  switch (e.getCode()) {
205  printf("Can't open the parallel port to access to the ring light "
206  "device\n");
207  break;
209  printf("Can't close the parallel port\n");
210  break;
211  }
212  }
213  catch (...) {
214  printf("An error occurs...\n");
215  }
216  return EXIT_SUCCESS;
217 }
218 #else
219 int main()
220 {
221  std::cout << "vpRingLight class works only on unix on a the Inria Afma6 platform..." << std::endl;
222  return EXIT_SUCCESS;
223 }
224 #endif
int getCode() const
Definition: vpException.cpp:68
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:69
Ring light management under unix.
Definition: vpRingLight.h:106
void pulse()
Definition: vpRingLight.cpp:89
VISP_EXPORT int wait(double t0, double t)