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