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