Visual Servoing Platform  version 3.6.1 under development (2024-04-19)
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 
74 class VISP_EXPORT vpPololu
75 {
76 public:
84  vpPololu(bool verbose = false);
85 
96  vpPololu(const std::string &device, int baudrate = 38400, int channel = 0, bool verbose = false);
97 
101  virtual ~vpPololu();
102 
110  void calibrate(unsigned short &pwm_min, unsigned short &pwm_max);
111 
119  void connect(const std::string &device, int baudrate, int channel);
120 
126  bool connected() const;
127 
133  float getAngularPosition() const;
134 
140  unsigned short getPwmPosition() const;
141 
151  void getRangeAngles(float &minAngle, float &maxAngle) const;
152 
162  void getRangePwm(unsigned short &min, unsigned short &max);
163 
172  void setAngularPosition(float pos_rad, float vel_rad_s = 0.f);
173 
183  inline void setAngularRange(float min_angle, float max_angle)
184  {
185  m_min_angle = min_angle;
186  m_max_angle = max_angle;
187  m_range_angle = m_max_angle - m_min_angle;
188  }
189 
195  void setAngularVelocity(float vel_rad_s);
196 
206  void setPwmPosition(unsigned short pos_pwm, unsigned short speed_pwm = 0);
207 
217  inline void setPwmRange(unsigned short min_pwm, unsigned short max_pwm)
218  {
219  m_min_pwm = min_pwm;
220  m_max_pwm = max_pwm;
221  m_range_pwm = m_max_pwm - m_min_pwm;
222  }
223 
231  void setPwmVelocity(short pwm_vel);
232 
238  void setVerbose(bool verbose)
239  {
240  m_verbose = verbose;
241  }
242 
246  void stopVelocityCmd();
247 
261  float pwmToRad(unsigned short pwm) const;
262 
272  unsigned short radToPwm(float angle) const;
273 
283  short radSToSpeed(float speed_rad_s) const;
284 
294  float speedToRadS(short speed) const;
296 
297 private:
298  static RPMSerialInterface *m_interface; // Only one interface should be used even when controlling multiple servos
299  static int m_nb_servo; // Object counter to handel serial interface destruction
300 
301  int m_channel;
302  bool m_apply_velocity_cmd;
303  bool m_stop_velocity_cmd_thread;
304 
305  unsigned short m_vel_speed;
306  unsigned short m_vel_target_position;
307 
308  unsigned short m_vel_speed_prev;
309  unsigned short m_vel_target_position_prev;
310 
311  std::mutex m_mutex_velocity_cmd;
312 
313  // ranges
314  unsigned short m_min_pwm = 4095;
315  unsigned short m_max_pwm = 7905;
316  unsigned short m_range_pwm = m_max_pwm - m_min_pwm;
317  float m_min_angle = -40;
318  float m_max_angle = 40;
319  float m_range_angle = abs(m_min_angle) + abs(m_max_angle);
320 
321  bool m_verbose;
322 
329  void VelocityCmdThread();
330 };
331 
332 #endif
333 #endif
Interface for the Pololu Maestro USB Servo Controllers.
Definition: vpPololu.h:75
void setVerbose(bool verbose)
Definition: vpPololu.h:238
void setAngularRange(float min_angle, float max_angle)
Definition: vpPololu.h:183
void setPwmRange(unsigned short min_pwm, unsigned short max_pwm)
Definition: vpPololu.h:217