JMotor
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
JServoController Class Reference

class for controlling JMotorDriverServo, with angle calibration and accel and velocity limiting More...

#include <JServoController.h>

Inheritance diagram for JServoController:
Inheritance graph
[legend]
Collaboration diagram for JServoController:
Collaboration graph
[legend]

Public Member Functions

 JServoController (JMotorDriverServo &_servo, bool _reverse=false, float velLimit=INFINITY, float accelLimit=INFINITY, float decelLimit=NAN, unsigned long _disableTimeout=0, float _minAngleLimit=0, float _maxAngleLimit=180, float _pos=90, float _minSetAngle=0, float _maxSetAngle=180, int minServoVal=544, int maxServoVal=2400, bool _preventGoingWrongWay=true, bool _preventGoingTooFast=true, float _stoppingDecelLimit=INFINITY)
 Constructor for JServoController, a class for controlling JMotorDriverServo, with angle calibration and accel and velocity limiting.
 
virtual void run ()
 call this in your main loop
 
void setAngleImmediate (float angle, bool _run=true)
 set servo angle immediately, without velocity or acceleration limiting
 
void setAngleImmediateInc (float angleDiff, bool _run=true)
 increment servo angle and set immediately, without velocity or acceleration limiting
 
void setAngleSmoothed (float angle, bool _run=true)
 set servo angle target, servo will move to target but at limited velocity and acceleration
 
void setDisableTimeout (unsigned long _timeout)
 default: 0, after how many milliseconds of no movement should the servo be disabled? 0=never disable
 
unsigned long getDisableTimeout ()
 
unsigned long getLastMovedMillis ()
 returns the value of millis() when the servo last moved
 
unsigned long getMillisSinceMoved ()
 returns the number of milliseconds since the servo last moved
 
void setLastMovedMillis (unsigned long mil)
 allows for changing the variable that stores when the servo last moved
 
virtual bool setEnable (bool _enable)
 enable or disable the servo (start or stop sending a signal to the servo)
 
bool enable ()
 equivalent to setEnable(true)
 
bool disable ()
 equivalent to setEnable(false)
 
bool getEnable ()
 returns whether the servo is enabled or not
 
float getPosTarget ()
 what position is the servo moving towards or at
 
float getPos ()
 what position is the servo actually being set to? (slowly approaches target if smoothing is used)
 
float getVelocity ()
 what rate is servo position being changed at?
 
bool isPosAtTarget ()
 true if position=target, false otherwise
 
bool isPosNotAtTarget ()
 equivalent to !isPosAtTarget()
 
float distToTarget ()
 returns target-position
 
void restartRun ()
 if you stopped calling run() for a while, call this before restarting run() to avoid a big jump in movement
 
bool getActive ()
 returns whether the servo is "awake" (whether signals are being sent)
 
void wake ()
 if the servo has been disabled because of inactivity, calling this function simulates the servo being told to move and wakes it up
 
void setReverse (bool rev)
 
bool getReverse ()
 
void setAngleLimits (float _minAngleLimit, float _maxAngleLimit)
 
void setMinAngleLimit (float _minAngleLimit)
 
void setMaxAngleLimit (float _maxAngleLimit)
 
float getMinAngleLimit ()
 
float getMaxAngleLimit ()
 
float getAngleLimitRange ()
 
void setSetAngles (float _minSetAngle, float _maxSetAngle)
 
void setMinSetAngle (float _minSetAngle)
 
void setMaxSetAngle (float _maxSetAngle)
 
float getAccelLimit ()
 returns value of accelLimit
 
float getDecelLimit ()
 
float getVelLimit ()
 returns value of velLimit
 
void setAccelLimit (float accelLim)
 set maximum rate that motor speed can be changed at
 
void setAccelAndDecelLimits (float accelLim, float decelLim=NAN)
 
void setDecelLimit (float decelLim=NAN)
 
void setVelLimit (float velLim)
 set maximum motor speed
 
void setVelAccelLimits (float velLim, float accelLim, float decelLim=NAN)
 
void setPosition (float pos, bool _run=true)
 sets servo position, leaves target where it was
 
void setMinServoValue (int value)
 microseconds for shortest servo pulse
 
void setMaxServoValue (int value)
 microseconds for longest servo pulse
 
void setServoRangeValues (int min, int max)
 set the settings for short and long servo pulses at the same time
 
int getMinServoRangeValue ()
 returns setting for microseconds for shortest servo pulse
 
int getMaxServoRangeValue ()
 returns setting for microseconds for longest servo pulse
 
int getServoValueRange ()
 returns difference (in microseconds) between longest and shortest servo pulse settings
 
int getSetMicroseconds ()
 returns how many microseconds the servo signal pulse was most recently set to
 
bool getConstrainRange ()
 returns whether the range of set() will be constrained to within -1 and 1
 
void setConstrainRange (bool _constrainRange)
 set whether the range of set() will be constrained to within -1 and 1
 
void setVel (float vel, bool _run=true)
 set velocity for controller
 
void setVelTarget (float vel, bool _run=true)
 set target velocity for controller (acceleration limited)
 
float getVelTarget ()
 get target velocity for controller
 
float getVel ()
 get current velocity of motor
 
float getDriverSetVal ()
 returns the value the motor driver is being set to
 
bool isDriverInRange ()
 true if motor driver is being set to within its range, false if driver is at a maximum
 
float getDriverMinRange ()
 if getDriverSetVal goes below this, isDriverInRange will go false
 
float getDriverMaxRange ()
 if getDriverSetVal exceeds this, isDriverInRange will go false
 
float getMaxVel ()
 How fast of a motor speed setting would get adjusted to full motor power.
 
float getMinVel ()
 slowest speed motor can go
 
void setMaxDriverRangeAmount (float _driverRangeAmount)
 set what fraction of driverRange can be used
 
bool setPosTarget (float _posTarget, bool _run=true)
 set target position (motor drives towards position, following acceleration profile)
 
bool setPosSetpoint (float _posSetpoint, bool _run=true)
 set position for motor to drive towards position as fast as possible (setpoint for control loop if available)
 
bool setPosDelta (float _posDelta, bool _run=true, bool _resetPos=false)
 alternative method for setting velocity that uses setPosSetpoint
 
float getPosSetpoint ()
 get position controller is currently trying to get to
 
float resetPos ()
 reset what position the controller thinks it's in
 
bool isPosModeNotVelocity ()
 true if controller is in position target mode, false if in velocity mode
 

Public Attributes

Derivs_Limiter dL
 instance of Derivs_Limiter class used to smoothly move servo
 

Protected Member Functions

void writeAngleToServo (float ang)
 
float floatMap (float x, float in_min, float in_max, float out_min, float out_max)
 

Protected Attributes

JMotorDriverServoservo
 reference to driver that's a subclass of JMotorDriverServo
 
unsigned long disableTimeout
 variable for how long to wait if not moved before disabling (default: 0, never disable)
 
float minAngleLimit
 lower limit for angle setpoint
 
float maxAngleLimit
 higher limit for angle setpoint
 
float minSetAngle
 when servodriver is set to getMinValue what angle is the servo?
 
float maxSetAngle
 when servodriver is set to getMaxValue what angle is the servo?
 
bool reverse
 reverse range of servo
 
unsigned long lastMovedMillis
 variable used to keep track of how long the servo has been still for
 
bool enabled
 keep track of whether controller is enabled (since even when controller is enabled driver might be disabled if timedout and sleeping)
 
bool sleeping
 has it been longer than disableTimeout since the servo has moved?
 

Detailed Description

class for controlling JMotorDriverServo, with angle calibration and accel and velocity limiting

Note
after creating a ServoController object with a ServoDriver, using the Driver's methods directly is not recommended

Constructor & Destructor Documentation

◆ JServoController()

JServoController::JServoController ( JMotorDriverServo _servo,
bool  _reverse = false,
float  velLimit = INFINITY,
float  accelLimit = INFINITY,
float  decelLimit = NAN,
unsigned long  _disableTimeout = 0,
float  _minAngleLimit = 0,
float  _maxAngleLimit = 180,
float  _pos = 90,
float  _minSetAngle = 0,
float  _maxSetAngle = 180,
int  minServoVal = 544,
int  maxServoVal = 2400,
bool  _preventGoingWrongWay = true,
bool  _preventGoingTooFast = true,
float  _stoppingDecelLimit = INFINITY 
)
inline

Constructor for JServoController, a class for controlling JMotorDriverServo, with angle calibration and accel and velocity limiting.

Parameters
_servo(JMotorDriverServo&) reference to an instance of a class that's a subclass of JMotorDriverServo
_reverse(bool) default: false, use to reverse direction of servo
velLimit(float) default: INFINITY, maximum velocity you want the servo to move at in limited mode
accelLimit(float) default: INFINITY, maximum acceleration you want the servo to move at in limited mode
decelLimit(float) default: NAN, maximum deceleration you want the servo to move at in limited mode, NAN= use accelLimit
_disableTimeout(unsigned long) default: 0, after how many milliseconds of no movement should the servo be disabled? 0=never disable
_minAngleLimit(float) minimum angle limit for servo
_maxAngleLimit(float) maximum angle limit for servo
_pos(float) default: 90, value to set servo position to at startup
_minSetAngle(float) default: 0, when servo driver is set to its minimum (probably -1) what angle does the servo go to?
_maxSetAngle(float) default: 180, when servo driver is set to its maximum (probably 1) what angle does the servo go to?
minServoVal(int) default: 544, microseconds for servo signal pulse for minimum angle
maxServoVal(int) default: 2400, microseconds for servo signal pulse for maximum angle
_preventGoingWrongWay(bool) default: true, immediately stop if going away from target
_preventGoingTooFast(bool) default: true, immediately slow down if set to a speed above velLimit
_stoppingDecelLimit(float) default: INFINITY, how much extra deceleration can be used to stop the servo in time (if the target moves towards the servo's position)

Member Function Documentation

◆ disable()

bool JServoController::disable ( )
inlinevirtual

equivalent to setEnable(false)

Implements JMotorControllerBase.

◆ distToTarget()

float JServoController::distToTarget ( )
inline

returns target-position

Return values
(float)

◆ enable()

bool JServoController::enable ( )
inlinevirtual

equivalent to setEnable(true)

Implements JMotorControllerBase.

◆ floatMap()

float JServoController::floatMap ( float  x,
float  in_min,
float  in_max,
float  out_min,
float  out_max 
)
inlineprotected

◆ getAccelLimit()

float JServoController::getAccelLimit ( )
inlinevirtual

returns value of accelLimit

Return values
(float)

Implements JMotorControllerBase.

◆ getActive()

bool JServoController::getActive ( )
inline

returns whether the servo is "awake" (whether signals are being sent)

Return values
(bool)

◆ getAngleLimitRange()

float JServoController::getAngleLimitRange ( )
inline

◆ getConstrainRange()

bool JServoController::getConstrainRange ( )
inline

returns whether the range of set() will be constrained to within -1 and 1

Return values
(bool)

◆ getDecelLimit()

float JServoController::getDecelLimit ( )
inline

◆ getDisableTimeout()

unsigned long JServoController::getDisableTimeout ( )
inline

◆ getDriverMaxRange()

float JServoController::getDriverMaxRange ( )
inlinevirtual

if getDriverSetVal exceeds this, isDriverInRange will go false

Return values
(float)

Implements JMotorControllerBase.

◆ getDriverMinRange()

float JServoController::getDriverMinRange ( )
inlinevirtual

if getDriverSetVal goes below this, isDriverInRange will go false

Return values
(float)

Implements JMotorControllerBase.

◆ getDriverSetVal()

float JServoController::getDriverSetVal ( )
inlinevirtual

returns the value the motor driver is being set to

Return values
(float)

Implements JMotorControllerBase.

◆ getEnable()

bool JServoController::getEnable ( )
inlinevirtual

returns whether the servo is enabled or not

Return values
(bool)

Implements JMotorControllerBase.

◆ getLastMovedMillis()

unsigned long JServoController::getLastMovedMillis ( )
inline

returns the value of millis() when the servo last moved

Return values
(unsignedlong)

◆ getMaxAngleLimit()

float JServoController::getMaxAngleLimit ( )
inline

◆ getMaxServoRangeValue()

int JServoController::getMaxServoRangeValue ( )
inline

returns setting for microseconds for longest servo pulse

Return values
(int)

◆ getMaxVel()

float JServoController::getMaxVel ( )
inlinevirtual

How fast of a motor speed setting would get adjusted to full motor power.

Note
may change as battery voltage changes
Return values
(float)

Implements JMotorControllerBase.

◆ getMillisSinceMoved()

unsigned long JServoController::getMillisSinceMoved ( )
inline

returns the number of milliseconds since the servo last moved

Return values
(unsignedlong)

◆ getMinAngleLimit()

float JServoController::getMinAngleLimit ( )
inline

◆ getMinServoRangeValue()

int JServoController::getMinServoRangeValue ( )
inline

returns setting for microseconds for shortest servo pulse

Return values
(int)

◆ getMinVel()

float JServoController::getMinVel ( )
inlinevirtual

slowest speed motor can go

Implements JMotorControllerBase.

◆ getPos()

float JServoController::getPos ( )
inlinevirtual

what position is the servo actually being set to? (slowly approaches target if smoothing is used)

Return values
(float)

Implements JMotorController.

◆ getPosSetpoint()

float JServoController::getPosSetpoint ( )
inlinevirtual

get position controller is currently trying to get to

Return values
(float)

Implements JMotorController.

◆ getPosTarget()

float JServoController::getPosTarget ( )
inlinevirtual

what position is the servo moving towards or at

Return values
(float)

Implements JMotorController.

◆ getReverse()

bool JServoController::getReverse ( )
inline

◆ getServoValueRange()

int JServoController::getServoValueRange ( )
inline

returns difference (in microseconds) between longest and shortest servo pulse settings

Return values
(int)

◆ getSetMicroseconds()

int JServoController::getSetMicroseconds ( )
inline

returns how many microseconds the servo signal pulse was most recently set to

Return values
(int)

◆ getVel()

float JServoController::getVel ( )
inlinevirtual

get current velocity of motor

Return values
(float)

Implements JMotorControllerBase.

◆ getVelLimit()

float JServoController::getVelLimit ( )
inlinevirtual

returns value of velLimit

Return values
(float)

Implements JMotorControllerBase.

◆ getVelocity()

float JServoController::getVelocity ( )
inline

what rate is servo position being changed at?

Return values
(float)

◆ getVelTarget()

float JServoController::getVelTarget ( )
inlinevirtual

get target velocity for controller

Return values
(float)current velocity target

Implements JMotorControllerBase.

◆ isDriverInRange()

bool JServoController::isDriverInRange ( )
inlinevirtual

true if motor driver is being set to within its range, false if driver is at a maximum

Return values
(bool)

Implements JMotorControllerBase.

◆ isPosAtTarget()

bool JServoController::isPosAtTarget ( )
inline

true if position=target, false otherwise

Return values
(bool)

◆ isPosModeNotVelocity()

bool JServoController::isPosModeNotVelocity ( )
inlinevirtual

true if controller is in position target mode, false if in velocity mode

Return values
(bool)

Implements JMotorController.

◆ isPosNotAtTarget()

bool JServoController::isPosNotAtTarget ( )
inline

equivalent to !isPosAtTarget()

Return values
(bool)

◆ resetPos()

float JServoController::resetPos ( )
inlinevirtual

reset what position the controller thinks it's in

Note
don't drive too far without resetting this so that the limited precision of floats doesn't become a problem.
Return values
(float)returns old position

Implements JMotorController.

◆ restartRun()

void JServoController::restartRun ( )
inline

if you stopped calling run() for a while, call this before restarting run() to avoid a big jump in movement

◆ run()

virtual void JServoController::run ( )
inlinevirtual

call this in your main loop

Implements JMotorControllerBase.

Reimplemented in JServoControllerAdvanced, JServoControllerGentle, and JServoControllerStallProtected.

◆ setAccelAndDecelLimits()

void JServoController::setAccelAndDecelLimits ( float  accelLim,
float  decelLim = NAN 
)
inline

◆ setAccelLimit()

void JServoController::setAccelLimit ( float  _accelLimit)
inlinevirtual

set maximum rate that motor speed can be changed at

Note
set to INFINITY to disable acceleration limiting
Parameters
_accelLimit(float)

Implements JMotorControllerBase.

◆ setAngleImmediate()

void JServoController::setAngleImmediate ( float  angle,
bool  _run = true 
)
inline

set servo angle immediately, without velocity or acceleration limiting

Parameters
angle(float) angle to set
_run(bool) default: true, true = call run() in this function, false = you'll call run() yourself

◆ setAngleImmediateInc()

void JServoController::setAngleImmediateInc ( float  angleDiff,
bool  _run = true 
)
inline

increment servo angle and set immediately, without velocity or acceleration limiting

Parameters
angleDiff(float) angle change to set
_run(bool) default: true, true = call run() in this function, false = you'll call run() yourself

◆ setAngleLimits()

void JServoController::setAngleLimits ( float  _minAngleLimit,
float  _maxAngleLimit 
)
inline

◆ setAngleSmoothed()

void JServoController::setAngleSmoothed ( float  angle,
bool  _run = true 
)
inline

set servo angle target, servo will move to target but at limited velocity and acceleration

Parameters
angle(float) angle to set
_run(bool) default: true, true = call run() in this function, false = you'll call run() yourself

◆ setConstrainRange()

void JServoController::setConstrainRange ( bool  _constrainRange)
inline

set whether the range of set() will be constrained to within -1 and 1

Parameters
_constrainRange(bool)

◆ setDecelLimit()

void JServoController::setDecelLimit ( float  decelLim = NAN)
inline

◆ setDisableTimeout()

void JServoController::setDisableTimeout ( unsigned long  _timeout)
inline

default: 0, after how many milliseconds of no movement should the servo be disabled? 0=never disable

Parameters
_timeout(unsigned long)

◆ setEnable()

virtual bool JServoController::setEnable ( bool  _enable)
inlinevirtual

enable or disable the servo (start or stop sending a signal to the servo)

Note
some (nicer digital) servos may not turn off their motors when the signal is stopped, but cheap servos usually do
Parameters
_enable(bool) true=enable, false=disable
Return values
(bool)true if state changed, otherwise false

Implements JMotorControllerBase.

Reimplemented in JServoControllerAdvanced.

◆ setLastMovedMillis()

void JServoController::setLastMovedMillis ( unsigned long  mil)
inline

allows for changing the variable that stores when the servo last moved

Note
use with caution: if you just want to make the servo start receiving signals again use wake()
Parameters
mil(unsigned long)

◆ setMaxAngleLimit()

void JServoController::setMaxAngleLimit ( float  _maxAngleLimit)
inline

◆ setMaxDriverRangeAmount()

void JServoController::setMaxDriverRangeAmount ( float  _driverRangeAmount)
inlinevirtual

set what fraction of driverRange can be used

Parameters
_driverRangeAmount(float)

Implements JMotorControllerBase.

◆ setMaxServoValue()

void JServoController::setMaxServoValue ( int  value)
inline

microseconds for longest servo pulse

Parameters
value(int)

◆ setMaxSetAngle()

void JServoController::setMaxSetAngle ( float  _maxSetAngle)
inline

◆ setMinAngleLimit()

void JServoController::setMinAngleLimit ( float  _minAngleLimit)
inline

◆ setMinServoValue()

void JServoController::setMinServoValue ( int  value)
inline

microseconds for shortest servo pulse

Parameters
value(int)

◆ setMinSetAngle()

void JServoController::setMinSetAngle ( float  _minSetAngle)
inline

◆ setPosDelta()

bool JServoController::setPosDelta ( float  _posDelta,
bool  _run = true,
bool  _resetPos = false 
)
inlinevirtual

alternative method for setting velocity that uses setPosSetpoint

Note
handles velocities below getMinVel(), and may have better distance accuracy, doesn't smooth velocity
Parameters
_posDelta(float) basically velocity
_resetPos(bool) default=false, keep setting current position to zero
_run(bool) default:true, true = call run() in this function, false = you'll call run() yourself
Return values
(bool)did the setting change?

Implements JMotorController.

◆ setPosition()

void JServoController::setPosition ( float  pos,
bool  _run = true 
)
inline

sets servo position, leaves target where it was

Parameters
pos(float)
_run(bool) default: true, true = call run() in this function, false = you'll call run() yourself

◆ setPosSetpoint()

bool JServoController::setPosSetpoint ( float  _posSetpoint,
bool  _run = true 
)
inlinevirtual

set position for motor to drive towards position as fast as possible (setpoint for control loop if available)

Note
run() needs to be called in order for the motor to move
Parameters
_posSetpoint(float) position setpoint
_run(bool) default:true, true = call run() in this function, false = you'll call run() yourself
Return values
(bool)did the setpoint change?

Implements JMotorController.

◆ setPosTarget()

bool JServoController::setPosTarget ( float  _posTarget,
bool  _run = true 
)
inlinevirtual

set target position (motor drives towards position, following acceleration profile)

Parameters
_posTarget(float) target position
_run(bool) default:true, true = call run() in this function, false = you'll call run() yourself
Return values
(bool)did the target change?

Implements JMotorController.

◆ setReverse()

void JServoController::setReverse ( bool  rev)
inline

◆ setServoRangeValues()

void JServoController::setServoRangeValues ( int  min,
int  max 
)
inline

set the settings for short and long servo pulses at the same time

Parameters
min(int) microseconds, often 544
max(int) microseconds, often 2400

◆ setSetAngles()

void JServoController::setSetAngles ( float  _minSetAngle,
float  _maxSetAngle 
)
inline

◆ setVel()

void JServoController::setVel ( float  vel,
bool  _run = true 
)
inlinevirtual

set velocity for controller

Note
this function can be called repeatedly, or just once if run() is called frequently
Parameters
vel(float)
_run(bool) default:true, true = call run() in this function, false = you'll call run() yourself

Implements JMotorControllerBase.

◆ setVelAccelLimits()

void JServoController::setVelAccelLimits ( float  velLim,
float  accelLim,
float  decelLim = NAN 
)
inline

◆ setVelLimit()

void JServoController::setVelLimit ( float  _velLimit)
inlinevirtual

set maximum motor speed

Note
set to INFINITY to disable limit
Parameters
_velLimit(float)

Implements JMotorController.

◆ setVelTarget()

void JServoController::setVelTarget ( float  vel,
bool  _run = true 
)
inlinevirtual

set target velocity for controller (acceleration limited)

Note
this function can be called repeatedly, or just once if run() is called frequently
Parameters
vel(float) target velocity
_run(bool) default:true, true = call run() in this function, false = you'll call run() yourself

Implements JMotorControllerBase.

◆ wake()

void JServoController::wake ( )
inline

if the servo has been disabled because of inactivity, calling this function simulates the servo being told to move and wakes it up

Note
lastMovedMillis is set to millis() even though there was no movement

◆ writeAngleToServo()

void JServoController::writeAngleToServo ( float  ang)
inlineprotected

Member Data Documentation

◆ disableTimeout

unsigned long JServoController::disableTimeout
protected

variable for how long to wait if not moved before disabling (default: 0, never disable)

◆ dL

Derivs_Limiter JServoController::dL

instance of Derivs_Limiter class used to smoothly move servo

Note
use ServoController functions whenever possible rather than directly accessing this.
https://github.com/joshua-8/Derivs_Limiter

◆ enabled

bool JServoController::enabled
protected

keep track of whether controller is enabled (since even when controller is enabled driver might be disabled if timedout and sleeping)

◆ lastMovedMillis

unsigned long JServoController::lastMovedMillis
protected

variable used to keep track of how long the servo has been still for

◆ maxAngleLimit

float JServoController::maxAngleLimit
protected

higher limit for angle setpoint

Note
(if less than minAngleLimit, direction is reversed)

◆ maxSetAngle

float JServoController::maxSetAngle
protected

when servodriver is set to getMaxValue what angle is the servo?

◆ minAngleLimit

float JServoController::minAngleLimit
protected

lower limit for angle setpoint

Note
(if greater than maxAngleLimit, direction is reversed)

◆ minSetAngle

float JServoController::minSetAngle
protected

when servodriver is set to getMinValue what angle is the servo?

◆ reverse

bool JServoController::reverse
protected

reverse range of servo

◆ servo

JMotorDriverServo& JServoController::servo
protected

reference to driver that's a subclass of JMotorDriverServo

◆ sleeping

bool JServoController::sleeping
protected

has it been longer than disableTimeout since the servo has moved?


The documentation for this class was generated from the following file: