Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
ringLight.cpp
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 https://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 */
34
40
41#include <cmath> // std::fabs
42#include <limits> // numeric_limits
43#include <visp3/core/vpConfig.h>
44#include <visp3/core/vpDebug.h>
45#if defined VISP_HAVE_PARPORT
46#include <iostream>
47#include <stdio.h>
48#include <stdlib.h>
49
50#include <visp3/core/vpTime.h>
51#include <visp3/io/vpParseArgv.h>
52#include <visp3/robot/vpRingLight.h>
53
54// List of allowed command line options
55#define GETOPTARGS "d:hn:ot:"
56
57#ifdef ENABLE_VISP_NAMESPACE
58using namespace VISP_NAMESPACE_NAME;
59#endif
60
71void usage(const char *name, const char *badparam, int nsec, double nmsec)
72{
73 fprintf(stdout, "\n\
74Send a pulse to activate the ring light or turn on the ring light \n\
75during %d s.\n\
76\n\
77By default, that means without parameters, send a pulse which duration\n\
78is 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\
80use \"-o -n <on duration in second>]\"\n\
81\n\
82SYNOPSIS\n\
83 %s [-o] [-n <on duration in second>] [-t <pulse width in ms>] [-h]\n\
84",
85nsec, name);
86
87 fprintf(stdout, "\n\
88OPTIONS: 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",
107 nsec, nmsec, nsec);
108
109 if (badparam) {
110 fprintf(stderr, "ERROR: \n");
111 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
112 }
113}
114
128bool 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':
137 nsec = atoi(optarg);
138 break;
139 case 'o':
140 on = true;
141 break;
142 case 't':
143 nmsec = atof(optarg);
144 break;
145 case 'h':
146 usage(argv[0], nullptr, nsec, nmsec);
147 return false;
148
149 default:
150 usage(argv[0], optarg, nsec, nmsec);
151 return false;
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
171int 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
219int 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
Error that can be emitted by the vpParallelPort class and its derivates.
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Ring light management under unix.
VISP_EXPORT int wait(double t0, double t)