Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
vpPololu.h
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 features for Pololu Maestro Servo Motor.
32  */
33 
34 #ifndef _vpPololu_h_
35 #define _vpPololu_h_
36 
37 #include <visp3/core/vpConfig.h>
38 
39 #if defined(VISP_HAVE_POLOLU) && defined(VISP_HAVE_THREADS)
40 
41 #include <iostream>
42 #include <string>
43 #include <mutex>
44 
45 class RPMSerialInterface;
46 
47 BEGIN_VISP_NAMESPACE
75 class VISP_EXPORT vpPololu
76 {
77 public:
85  vpPololu(bool verbose = false);
86 
97  vpPololu(const std::string &device, int baudrate = 38400, int channel = 0, bool verbose = false);
98 
102  virtual ~vpPololu();
103 
111  void calibrate(unsigned short &pwm_min, unsigned short &pwm_max);
112 
120  void connect(const std::string &device, int baudrate, int channel);
121 
127  bool connected() const;
128 
134  float getAngularPosition() const;
135 
141  unsigned short getPwmPosition() const;
142 
152  void getRangeAngles(float &minAngle, float &maxAngle) const;
153 
163  void getRangePwm(unsigned short &min, unsigned short &max);
164 
173  void setAngularPosition(float pos_rad, float vel_rad_s = 0.f);
174 
184  inline void setAngularRange(float min_angle, float max_angle)
185  {
186  m_min_angle = min_angle;
187  m_max_angle = max_angle;
188  m_range_angle = m_max_angle - m_min_angle;
189  }
190 
196  void setAngularVelocity(float vel_rad_s);
197 
207  void setPwmPosition(unsigned short pos_pwm, unsigned short speed_pwm = 0);
208 
218  inline void setPwmRange(unsigned short min_pwm, unsigned short max_pwm)
219  {
220  m_min_pwm = min_pwm;
221  m_max_pwm = max_pwm;
222  m_range_pwm = m_max_pwm - m_min_pwm;
223  }
224 
232  void setPwmVelocity(short pwm_vel);
233 
239  void setVerbose(bool verbose)
240  {
241  m_verbose = verbose;
242  }
243 
247  void stopVelocityCmd();
248 
262  float pwmToRad(unsigned short pwm) const;
263 
273  unsigned short radToPwm(float angle) const;
274 
284  short radSToSpeed(float speed_rad_s) const;
285 
295  float speedToRadS(short speed) const;
297 
298 private:
299  static RPMSerialInterface *m_interface; // Only one interface should be used even when controlling multiple servos
300  static int m_nb_servo; // Object counter to handel serial interface destruction
301 
302  int m_channel;
303  bool m_apply_velocity_cmd;
304  bool m_stop_velocity_cmd_thread;
305 
306  unsigned short m_vel_speed;
307  unsigned short m_vel_target_position;
308 
309  unsigned short m_vel_speed_prev;
310  unsigned short m_vel_target_position_prev;
311 
312  std::mutex m_mutex_velocity_cmd;
313 
314  // ranges
315  unsigned short m_min_pwm = 4095;
316  unsigned short m_max_pwm = 7905;
317  unsigned short m_range_pwm = m_max_pwm - m_min_pwm;
318  float m_min_angle = -40;
319  float m_max_angle = 40;
320  float m_range_angle = abs(m_min_angle) + abs(m_max_angle);
321 
322  bool m_verbose;
323 
330  void VelocityCmdThread();
331 };
332 END_VISP_NAMESPACE
333 #endif
334 #endif
Interface for the Pololu Maestro USB Servo Controllers.
Definition: vpPololu.h:76
void setVerbose(bool verbose)
Definition: vpPololu.h:239
void setAngularRange(float min_angle, float max_angle)
Definition: vpPololu.h:184
void setPwmRange(unsigned short min_pwm, unsigned short max_pwm)
Definition: vpPololu.h:218