39#include <visp3/core/vpConfig.h>
41#if defined(VISP_HAVE_POLOLU) && defined(VISP_HAVE_THREADS)
48#include <visp3/core/vpMath.h>
49#include <visp3/robot/vpPololu.h>
51#ifdef ENABLE_VISP_NAMESPACE
55void 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);
58void usage(
const char **argv,
int error,
const std::string &device,
int baudrate,
int channel,
59 unsigned short pwm_min,
unsigned short pwm_max,
float angle_min,
float angle_max)
61 std::cout <<
"Synopsis" << std::endl
62 <<
" " << argv[0] <<
" [--device <name>] [--channel <number>] [--calibrate] [--range-pwm <min max> ] [--verbose, -v] [--help, -h]" << std::endl
64 std::cout <<
"Description" << std::endl
65 <<
" --device <name> Device name." << std::endl
66 <<
" Default: " << device << std::endl
68 <<
" --baud <rate> Serial link baud rate." << std::endl
69 <<
" Default: " << baudrate << std::endl
71 <<
" --channel <number> Channel to dial with." << std::endl
72 <<
" Default: " << channel << std::endl
74 <<
" --range-pwm <min max> Set PWM min and max values." << std::endl
75 <<
" You can use \"--calibrate\" to retrieve min and max pwm values."
76 <<
" Default: " << pwm_min <<
" " << pwm_max << std::endl
78 <<
" --range-angles <min max> Set angle min and max values (deg)." << std::endl
81 <<
" --verbose, -v Enable verbosity." << std::endl
83 <<
" --calibrate Start pwm calibration determining min and max admissible values." << std::endl
84 <<
" Once calibration done you can use \"--range-pwm <min max>\" option to set" << std::endl
85 <<
" the corresponding values" << std::endl
87 <<
" --help, -h Print this helper message." << std::endl
90 std::cout <<
"Error" << std::endl
92 <<
"Unsupported parameter " << argv[
error] << std::endl;
96int main(
int argc,
const char **argv)
99 std::string opt_device =
"COM4";
101 std::string opt_device =
"/dev/ttyACM0";
106 int opt_baudrate = 38400;
107 bool opt_verbose =
false;
108 bool opt_calibrate =
false;
109 unsigned short opt_pwm_min = 4000;
110 unsigned short opt_pwm_max = 8000;
111 float opt_angle_min =
static_cast<float>(
vpMath::rad(-45));
112 float opt_angle_max =
static_cast<float>(
vpMath::rad(45));
113 float opt_velocity =
static_cast<float>(
vpMath::rad(5));
114 float last_angle = 0;
116 for (
int i = 1;
i < argc;
i++) {
117 if (std::string(argv[i]) ==
"--device" && i + 1 < argc) {
118 opt_device = std::string(argv[i + 1]);
121 else if (std::string(argv[i]) ==
"--baud" && i + 1 < argc) {
122 opt_baudrate = std::atoi(argv[i + 1]);
125 else if (std::string(argv[i]) ==
"--channel" && i + 1 < argc) {
126 opt_channel = std::atoi(argv[i + 1]);
129 else if (std::string(argv[i]) ==
"--range-pwm" && i + 2 < argc) {
130 opt_pwm_min =
static_cast<unsigned short>(
vpMath::rad(std::atoi(argv[i + 1])));
131 opt_pwm_max =
static_cast<unsigned short>(
vpMath::rad(std::atoi(argv[i + 2])));
134 else if (std::string(argv[i]) ==
"--range-angles" && i + 2 < argc) {
135 opt_angle_min =
static_cast<float>(std::atof(argv[i + 1]));
136 opt_angle_max =
static_cast<float>(std::atof(argv[i + 2]));
139 else if (std::string(argv[i]) ==
"--calibrate") {
140 opt_calibrate =
true;
142 else if (std::string(argv[i]) ==
"--verbose" || std::string(argv[i]) ==
"-v") {
145 else if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
146 usage(argv, 0, opt_device, opt_baudrate, opt_channel, opt_pwm_min, opt_pwm_max, opt_angle_min, opt_angle_max);
150 usage(argv, i, opt_device, opt_baudrate, opt_channel, opt_pwm_min, opt_pwm_max, opt_angle_min, opt_angle_max);
155 std::chrono::seconds sec(1);
161 servo.connect(opt_device, opt_baudrate, opt_channel);
164 std::cout <<
"Proceed to calibration to determine pwm min and max values..." << std::endl;
165 std::cout <<
"WARNING: Calibration will move the servo at channel " << opt_channel <<
"!" << std::endl;
166 std::cout <<
"Press Enter to move to min and max pwm positions..." << std::endl;
169 unsigned short pwm_min, pwm_max;
170 servo.calibrate(pwm_min, pwm_max);
171 std::cout <<
"Servo on channel " << opt_channel <<
" has pwm range [" << pwm_min <<
", " << pwm_max <<
"]" << std::endl;
175 servo.setPwmRange(opt_pwm_min, opt_pwm_max);
176 servo.setAngularRange(opt_angle_min, opt_angle_max);
179 std::cout <<
"Move to zero position (deg): " <<
vpMath::deg(0) <<
" at max velocity" << std::endl;
180 servo.setAngularPosition(0, 0);
181 std::this_thread::sleep_for(std::chrono::seconds(3));
182 last_angle = servo.getAngularPosition();
183 std::cout <<
"Servo reached position (deg): " <<
vpMath::deg(last_angle) << std::endl;
188 std::cout <<
"Move at velocity (pwm): " << vel_pwm <<
" for 3 sec" << std::endl;
189 servo.setPwmVelocity(vel_pwm);
190 std::this_thread::sleep_for(3 * sec);
191 std::cout <<
"Servo reached position (pwm): " << servo.getPwmPosition() << std::endl;
194 std::cout <<
"Move at velocity (pwm): " << vel_pwm <<
" for 3 sec" << std::endl;
195 servo.setPwmVelocity(vel_pwm);
196 std::this_thread::sleep_for(3 * sec);
197 std::cout <<
"Servo reached position (pwm): " << servo.getPwmPosition() << std::endl;
198 std::cout <<
"End of velocity motion" << std::endl;
202 std::cout <<
"Move at velocity (deg/s): " <<
vpMath::deg(opt_velocity) <<
" for 3 sec" << std::endl;
203 servo.setAngularVelocity(opt_velocity);
204 std::this_thread::sleep_for(3 * sec);
205 std::cout <<
"Servo reached position (deg): " << servo.getPwmPosition() << std::endl;
207 std::cout <<
"Move at velocity (deg/s): " <<
vpMath::deg(-opt_velocity) <<
" for 3 sec" << std::endl;
208 servo.setAngularVelocity(-opt_velocity);
209 std::this_thread::sleep_for(3 * sec);
210 std::cout <<
"Servo reached position (deg): " << servo.getPwmPosition() << std::endl;
213 servo.stopVelocityCmd();
215 std::cout <<
"The end" << std::endl;
220 std::cout <<
e.getMessage() << std::endl;
228 std::cout <<
"ViSP doesn't support Pololu 3rd party library" << std::endl;
error that can be emitted by ViSP classes.
static double rad(double deg)
static double deg(double rad)
Interface for the Pololu Maestro USB Servo Controllers.