Visual Servoing Platform  version 3.6.1 under development (2024-04-28)

#include <visp3/robot/vpPololu.h>

Public Member Functions

 vpPololu (bool verbose=false)
 
 vpPololu (const std::string &device, int baudrate=38400, int channel=0, bool verbose=false)
 
virtual ~vpPololu ()
 
void calibrate (unsigned short &pwm_min, unsigned short &pwm_max)
 
void connect (const std::string &device, int baudrate, int channel)
 
bool connected () const
 
float getAngularPosition () const
 
unsigned short getPwmPosition () const
 
void getRangeAngles (float &minAngle, float &maxAngle) const
 
void getRangePwm (unsigned short &min, unsigned short &max)
 
void setAngularPosition (float pos_rad, float vel_rad_s=0.f)
 
void setAngularRange (float min_angle, float max_angle)
 
void setAngularVelocity (float vel_rad_s)
 
void setPwmPosition (unsigned short pos_pwm, unsigned short speed_pwm=0)
 
void setPwmRange (unsigned short min_pwm, unsigned short max_pwm)
 
void setPwmVelocity (short pwm_vel)
 
void setVerbose (bool verbose)
 
void stopVelocityCmd ()
 

Public Member Functions for Conversion

float pwmToRad (unsigned short pwm) const
 
unsigned short radToPwm (float angle) const
 
short radSToSpeed (float speed_rad_s) const
 
float speedToRadS (short speed) const
 

Detailed Description

Interface for the Pololu Maestro USB Servo Controllers.

See https://www.pololu.com/category/102/maestro-usb-servo-controllers for more details.

This class give a position and velocity control for the servo motors plugged into the board on a given channel. If you want to control two or more servo motors, you need to instanciate a new object for each additional servo motor. An example is given in the vpRobotPololuPtu class that allows to control a pan-tilt unit with two servo motors.

It implements a velocity controller that runs in a separate thread.

The conversion between pwm units and radians positions is done using radToPwm() or pwmToRad(). For velocity control conversion from pwm units and rad/s is done using speedToRadS() and radSToSpeed().

Each servo has a pwm position range that could be retrieved using calibrate() and set using set using setPwmRange().

It is the user responsability to set the corresponding angular range using setAngularRange().

Examples
testPololuPosition.cpp, and testPololuVelocity.cpp.

Definition at line 74 of file vpPololu.h.

Constructor & Destructor Documentation

◆ vpPololu() [1/2]

vpPololu::vpPololu ( bool  verbose = false)

Default constructor.

Parameters
[in]verbose: When true enable verbose mode.

You need to call connect() to setup the serial link with the Pololu board.

Definition at line 53 of file vpPololu.cpp.

◆ vpPololu() [2/2]

vpPololu::vpPololu ( const std::string &  device,
int  baudrate = 38400,
int  channel = 0,
bool  verbose = false 
)

Constructor that enables the serial link with the Pololu board calling internally connect(). The velocity controller thread is created in the constructor and will run independently of the rest of the class. You can set velocity commands using setPwmVelocity() or setAngularVelocity().

Parameters
[in]device: Serial device name to dial with Pololu board.
[in]baudrate: Baudrate used to dial with Pololu board. Note that this parameter is only used on Windows.
[in]channel: Channel to which the servo is connected to the Pololu board.
[in]verbose: When true enable verbose mode.

Definition at line 58 of file vpPololu.cpp.

References connect().

◆ ~vpPololu()

vpPololu::~vpPololu ( )
virtual

Destructor.

Definition at line 90 of file vpPololu.cpp.

Member Function Documentation

◆ calibrate()

void vpPololu::calibrate ( unsigned short &  pwm_min,
unsigned short &  pwm_max 
)

Move servo motor to minimal pwm position and then to maximal pwm position to retrieve min and max pwm values.

Parameters
[out]pwm_min: Min position (pwm).
[out]pwm_max: Max position (pwm).

Definition at line 102 of file vpPololu.cpp.

References vpException::fatalError, and getPwmPosition().

◆ connect()

void vpPololu::connect ( const std::string &  device,
int  baudrate,
int  channel 
)

Open a connection with the Pololu board.

Parameters
[in]device: Serial device name to dial with Pololu board.
[in]baudrate: Baudrate used to dial with Pololu board. Note that this parameter is only used on Windows.
[in]channel: Channel to which the servo is connected to the Pololu board.

Definition at line 65 of file vpPololu.cpp.

References vpRobotException::constructionError.

Referenced by vpPololu(), and vpRobotPololuPtu::vpRobotPololuPtu().

◆ connected()

bool vpPololu::connected ( ) const

Check if the serial connection is still up.

Returns
true is the connection is enabled, false if the board is not connected.

Definition at line 128 of file vpPololu.cpp.

◆ getAngularPosition()

float vpPololu::getAngularPosition ( ) const

Return angular position in rad.

Returns
Current position in rad.

Definition at line 165 of file vpPololu.cpp.

References getPwmPosition(), and pwmToRad().

Referenced by vpRobotPololuPtu::getPosition().

◆ getPwmPosition()

unsigned short vpPololu::getPwmPosition ( ) const

Return PWM position.

Returns
Current PWM position.

Definition at line 144 of file vpPololu.cpp.

References vpException::fatalError.

Referenced by calibrate(), getAngularPosition(), setPwmVelocity(), and stopVelocityCmd().

◆ getRangeAngles()

void vpPololu::getRangeAngles ( float &  minAngle,
float &  maxAngle 
) const

Get min, max and range for angle cmd.

Parameters
[out]minAngle: Min range value for angle control.
[out]maxAngle: Max range value for angle control.
See also
setAngularRange()

Definition at line 173 of file vpPololu.cpp.

◆ getRangePwm()

void vpPololu::getRangePwm ( unsigned short &  min,
unsigned short &  max 
)

Get min, max range for PWM cmd.

Parameters
[out]min: Min value for PWM control.
[out]maxMax value for PWM control.
See also
setPwmRange()

Definition at line 179 of file vpPololu.cpp.

◆ pwmToRad()

float vpPololu::pwmToRad ( unsigned short  pwm) const

Convert a PWM value to an angle in radians.

Parameters
[in]pwm: PWM value.
Returns
Corresponding angle value in radian for the PWM.
See also
radToPwm()

Definition at line 185 of file vpPololu.cpp.

Referenced by getAngularPosition().

◆ radSToSpeed()

short vpPololu::radSToSpeed ( float  speed_rad_s) const

Convert deg/s speed into Pololu's speed.

Parameters
speed_rad_s: Speed converted to rad/s.
Returns
Signed speed in units of (0.25 us)/(10 ms).
See also
speedToRadS()

Definition at line 139 of file vpPololu.cpp.

Referenced by setAngularPosition(), and setAngularVelocity().

◆ radToPwm()

unsigned short vpPololu::radToPwm ( float  angle) const

Convert angles in radians to PWM for servo commands.

Parameters
angle: Angle in radian to convert.
Returns
Corresponding PWM value for the angle.
See also
pwmToRad()

Definition at line 120 of file vpPololu.cpp.

Referenced by setAngularPosition().

◆ setAngularPosition()

void vpPololu::setAngularPosition ( float  pos_rad,
float  vel_rad_s = 0.f 
)

Set the position to reach in angle.

Parameters
[in]pos_rad: Position to reach in radians.
[in]vel_rad_s: Velocity to use for the positioning in rad/s. Default is '0' and will use maximum speed.

Definition at line 193 of file vpPololu.cpp.

References vpRobotException::positionOutOfRangeError, radSToSpeed(), radToPwm(), and setPwmPosition().

Referenced by vpRobotPololuPtu::setPosition().

◆ setAngularRange()

void vpPololu::setAngularRange ( float  min_angle,
float  max_angle 
)
inline

Set min and max axis angles range in rad.

Parameters
[in]min_angle: Min value for angle (rad).
[in]max_angle: Max value for angle (rad).
See also
getRangeAngles()

Definition at line 183 of file vpPololu.h.

Referenced by vpRobotPololuPtu::vpRobotPololuPtu().

◆ setAngularVelocity()

void vpPololu::setAngularVelocity ( float  vel_rad_s)

Set the angular velocity of the motor movements in rad/s.

Parameters
[in]vel_rad_s: Velocity to apply for movement in rad/s.

Definition at line 238 of file vpPololu.cpp.

References radSToSpeed(), and setPwmVelocity().

Referenced by vpRobotPololuPtu::setVelocity().

◆ setPwmPosition()

void vpPololu::setPwmPosition ( unsigned short  pos_pwm,
unsigned short  speed_pwm = 0 
)

Set the position to reach in PWM.

Parameters
[in]pos_pwm: Position in PWM to reach.
[in]speed_pwm: Speed to use for movement in units of (0.25 us)/(10 ms). Default is 0, maximum speed.
Exceptions
WhenPWM out of range.

Definition at line 210 of file vpPololu.cpp.

References vpException::fatalError, and vpRobotException::positionOutOfRangeError.

Referenced by setAngularPosition(), and stopVelocityCmd().

◆ setPwmRange()

void vpPololu::setPwmRange ( unsigned short  min_pwm,
unsigned short  max_pwm 
)
inline

Set min, max PWM cmd.

Parameters
[in]min_pwm: Min value for PWM control.
[in]max_pwm: Max value for PWM control.
See also
getRangePwm()

Definition at line 217 of file vpPololu.h.

Referenced by vpRobotPololuPtu::vpRobotPololuPtu().

◆ setPwmVelocity()

void vpPololu::setPwmVelocity ( short  pwm_vel)

Set the pwm velocity of the motor movements. The motor will move to the edge of the range at the given speed.

Parameters
[in]pwm_vel: PWM velocity to use for movement in units of (0.25 us)/(10 ms). When set to 0, will use the maximum speed.

Definition at line 254 of file vpPololu.cpp.

References getPwmPosition(), and vpRobotException::positionOutOfRangeError.

Referenced by setAngularVelocity().

◆ setVerbose()

void vpPololu::setVerbose ( bool  verbose)
inline

Enable/disable verbose mode.

Parameters
[in]verbose: Set to true to enable verbose mode, false otherwise.

Definition at line 238 of file vpPololu.h.

Referenced by vpRobotPololuPtu::vpRobotPololuPtu().

◆ speedToRadS()

float vpPololu::speedToRadS ( short  speed) const

Convert Pololu's pwm velocity to rad/s velocity.

Parameters
[in]speed: Signed speed in units of (0.25 us)/(10 ms).
Returns
Speed converted to rad/s.
See also
radSToSpeed()

Definition at line 282 of file vpPololu.cpp.

Referenced by vpRobotPololuPtu::getAngularVelocityResolution().

◆ stopVelocityCmd()

void vpPololu::stopVelocityCmd ( )

Stop the velocity command thread.

Definition at line 287 of file vpPololu.cpp.

References getPwmPosition(), and setPwmPosition().

Referenced by vpRobotPololuPtu::stopVelocity(), and vpRobotPololuPtu::~vpRobotPololuPtu().