Visual Servoing Platform  version 3.6.1 under development (2024-05-19)
testPololuPosition.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 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  * Common test for Pololu position control of one servo connected to a given channel.
32  */
33 
38 #include <iostream>
39 
40 #include <visp3/core/vpConfig.h>
41 
42 #if defined(VISP_HAVE_POLOLU) && defined(VISP_HAVE_THREADS)
43 
44 #include <chrono>
45 #include <iostream>
46 #include <string>
47 #include <thread>
48 
49 #include <visp3/core/vpMath.h>
50 #include <visp3/robot/vpPololu.h>
51 
52 void usage(const char **argv, int error, const std::string &device, int baudrate, int channel,
53  unsigned short pwm_min, unsigned short pwm_max, float angle_min, float angle_max)
54 {
55  std::cout << "Synopsis" << std::endl
56  << " " << argv[0] << " [--device <name>] [--baud <rate>] [--channel <number>] [--calibrate]"
57  << " [--range-pwm <min max> ] [--verbose, -v] [--help, -h]" << std::endl
58  << std::endl;
59  std::cout << "Description" << std::endl
60  << " --device <name> Device name." << std::endl
61  << " Default: " << device << std::endl
62  << std::endl
63  << " --baud <rate> Serial link baud rate." << std::endl
64  << " Default: " << baudrate << std::endl
65  << std::endl
66  << " --channel <number> Channel to dial with." << std::endl
67  << " Default: " << channel << std::endl
68  << std::endl
69  << " --range-pwm <min max> Set PWM min and max values." << std::endl
70  << " You can use \"--calibrate\" to retrieve min and max pwm values."
71  << " Default: " << pwm_min << " " << pwm_max << std::endl
72  << std::endl
73  << " --range-angles <min max> Set angle min and max values (deg)." << std::endl
74  << " Default: " << vpMath::deg(angle_min) << " " << vpMath::deg(angle_max) << std::endl
75  << std::endl
76  << " --verbose, -v Enable verbosity." << std::endl
77  << std::endl
78  << " --calibrate Start pwm calibration determining min and max admissible values." << std::endl
79  << " Once calibration done you can use \"--range-pwm <min max>\" option to set" << std::endl
80  << " the corresponding values" << std::endl
81  << std::endl
82  << " --help, -h Print this helper message." << std::endl
83  << std::endl;
84  if (error) {
85  std::cout << "Error" << std::endl
86  << " "
87  << "Unsupported parameter " << argv[error] << std::endl;
88  }
89 }
90 
91 int main(int argc, const char **argv)
92 {
93 #ifdef _WIN32
94  std::string opt_device = "COM4";
95 #else
96  std::string opt_device = "/dev/ttyACM0";
97  // Example for Mac OS, the Maestro creates two devices, use the one with the lowest number (the command port)
98  //std::string opt_device = "/dev/cu.usbmodem00031501";
99 #endif
100  int opt_channel = 0;
101  int opt_baudrate = 38400;
102  bool opt_verbose = false;
103  bool opt_calibrate = false;
104  unsigned short opt_pwm_min = 4000;
105  unsigned short opt_pwm_max = 8000;
106  float opt_angle_min = static_cast<float>(vpMath::rad(-45));
107  float opt_angle_max = static_cast<float>(vpMath::rad(45));
108  float opt_positioning_velocity = static_cast<float>(vpMath::rad(10));
109  float last_angle = 0;
110  int time_s = 0;
111 
112  for (int i = 1; i < argc; i++) {
113  if (std::string(argv[i]) == "--device" && i + 1 < argc) {
114  opt_device = std::string(argv[i + 1]);
115  i++;
116  }
117  else if (std::string(argv[i]) == "--baud" && i + 1 < argc) {
118  opt_baudrate = std::atoi(argv[i + 1]);
119  i++;
120  }
121  else if (std::string(argv[i]) == "--channel" && i + 1 < argc) {
122  opt_channel = std::atoi(argv[i + 1]);
123  i++;
124  }
125  else if (std::string(argv[i]) == "--range-pwm" && i + 2 < argc) {
126  opt_pwm_min = static_cast<unsigned short>(vpMath::rad(std::atoi(argv[i + 1])));
127  opt_pwm_max = static_cast<unsigned short>(vpMath::rad(std::atoi(argv[i + 2])));
128  i += 2;
129  }
130  else if (std::string(argv[i]) == "--range-angles" && i + 2 < argc) {
131  opt_angle_min = static_cast<float>(std::atof(argv[i + 1]));
132  opt_angle_max = static_cast<float>(std::atof(argv[i + 2]));
133  i += 2;
134  }
135  else if (std::string(argv[i]) == "--calibrate") {
136  opt_calibrate = true;
137  }
138  else if (std::string(argv[i]) == "--verbose" || std::string(argv[i]) == "-v") {
139  opt_verbose = true;
140  }
141  else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
142  usage(argv, 0, opt_device, opt_baudrate, opt_channel, opt_pwm_min, opt_pwm_max, opt_angle_min, opt_angle_max);
143  return EXIT_SUCCESS;
144  }
145  else {
146  usage(argv, i, opt_device, opt_baudrate, opt_channel, opt_pwm_min, opt_pwm_max, opt_angle_min, opt_angle_max);
147  return EXIT_FAILURE;
148  }
149  }
150 
151  try {
152  // Creating the servo object on channel 0
153  vpPololu servo(opt_device, opt_baudrate, opt_channel, opt_verbose);
154 
155  std::cout << "Pololu board is " << (servo.connected() ? "connected" : "disconnected") << std::endl;
156 
157  if (opt_calibrate) {
158  std::cout << "Proceed to calibration to determine pwm min and max values..." << std::endl;
159  std::cout << "WARNING: Calibration will move the servo at channel " << opt_channel << "!" << std::endl;
160  std::cout << "Press Enter to move to min and max pwm positions..." << std::endl;
161  std::cin.ignore();
162 
163  unsigned short pwm_min, pwm_max;
164  servo.calibrate(pwm_min, pwm_max);
165  std::cout << "Servo on channel " << opt_channel << " has pwm range [" << pwm_min << ", " << pwm_max << "]" << std::endl;
166  return EXIT_SUCCESS;
167  }
168 
169  servo.setPwmRange(opt_pwm_min, opt_pwm_max);
170  servo.setAngularRange(opt_angle_min, opt_angle_max);
171 
172  // Getting the ranges of the servo
173  servo.getRangePwm(opt_pwm_min, opt_pwm_max);
174  std::cout << "Position range (pwm): " << opt_pwm_min << " " << opt_pwm_max << std::endl;
175  servo.getRangeAngles(opt_angle_min, opt_angle_max);
176  std::cout << "Position range (deg): " << vpMath::deg(opt_angle_min) << " " << vpMath::deg(opt_angle_max) << std::endl;
177 
178  // Servo will first move to min pwm range wait 3 seconds and move to max pwm range
179  std::cout << "Move to min position (pwm): " << opt_pwm_min << " at max velocity" << std::endl;
180  servo.setPwmPosition(opt_pwm_min, 0);
181  std::this_thread::sleep_for(std::chrono::seconds(3));
182  std::cout << "Servo reached position (pwm): " << servo.getPwmPosition() << std::endl;
183 
184  std::cout << "Move to max position (pwm): " << opt_pwm_max << " at max velocity" << std::endl;
185  servo.setPwmPosition(opt_pwm_max, 0);
186  std::this_thread::sleep_for(std::chrono::seconds(3));
187  std::cout << "Servo reached position (pwm): " << servo.getPwmPosition() << std::endl;
188 
189  // Servo will first move to min angle wait 3 seconds and move to max angle
190  std::cout << "Move to min position (deg): " << vpMath::deg(opt_angle_min) << " at max velocity" << std::endl;
191  servo.setAngularPosition(opt_angle_min, 0);
192  std::this_thread::sleep_for(std::chrono::seconds(3));
193  std::cout << "Servo reached position (deg): " << vpMath::deg(servo.getAngularPosition()) << std::endl;
194 
195  std::cout << "Move to max position (deg): " << vpMath::deg(opt_angle_max) << " at max velocity" << std::endl;
196  servo.setAngularPosition(opt_angle_max, 0);
197  std::this_thread::sleep_for(std::chrono::seconds(3));
198  std::cout << "Servo reached position (deg): " << vpMath::deg(servo.getAngularPosition()) << std::endl;
199 
200  // Servo will move to 0 angle at a max velocity in rad/s
201  std::cout << "Move to zero position (deg): " << vpMath::deg(0) << " at max velocity" << std::endl;
202  servo.setAngularPosition(0, 0);
203  std::this_thread::sleep_for(std::chrono::seconds(3));
204  last_angle = servo.getAngularPosition();
205  std::cout << "Servo reached position (deg): " << vpMath::deg(last_angle) << std::endl;
206 
207  // Servo will first move to min angle at a given velocity in rad/s
208  std::cout << "Move to min position (deg): " << vpMath::deg(opt_angle_min) << " at " << vpMath::deg(opt_positioning_velocity) << " deg/s" << std::endl;
209  servo.setAngularPosition(opt_angle_min, opt_positioning_velocity);
210  // Estimate time to reach position
211  time_s = static_cast<int>(std::abs((opt_angle_min - last_angle) / opt_positioning_velocity) + 2);
212 
213  std::this_thread::sleep_for(std::chrono::seconds(time_s));
214  last_angle = servo.getAngularPosition();
215  std::cout << "Servo reached position (deg): " << vpMath::deg(last_angle) << std::endl;
216 
217  std::cout << "Move to max position (deg): " << vpMath::deg(opt_angle_max) << " at " << vpMath::deg(opt_positioning_velocity) << " deg/s" << std::endl;
218  servo.setAngularPosition(opt_angle_max, opt_positioning_velocity);
219  // Estimate time to reach position
220  time_s = static_cast<int>(std::abs((opt_angle_max - last_angle) / opt_positioning_velocity) + 2);
221  std::this_thread::sleep_for(std::chrono::seconds(time_s));
222  last_angle = servo.getAngularPosition();
223  std::cout << "Servo reached position (deg): " << vpMath::deg(last_angle) << std::endl;
224 
225  return EXIT_SUCCESS;
226  }
227  catch (const vpException &e) {
228  std::cout << e.getMessage() << std::endl;
229  return EXIT_FAILURE;
230  }
231 }
232 
233 #else
234 int main()
235 {
236  std::cout << "ViSP doesn't support Pololu 3rd party library" << std::endl;
237 }
238 #endif
error that can be emitted by ViSP classes.
Definition: vpException.h:59
const char * getMessage() const
Definition: vpException.cpp:64
static double rad(double deg)
Definition: vpMath.h:127
static double deg(double rad)
Definition: vpMath.h:117
Interface for the Pololu Maestro USB Servo Controllers.
Definition: vpPololu.h:75