JMotor
Loading...
Searching...
No Matches
JServoController.h
Go to the documentation of this file.
1#ifndef J_SERVO_CONTROLLER_H
2#define J_SERVO_CONTROLLER_H
3#include "Derivs_Limiter.h"
6#include <Arduino.h>
7// see bottom of file for #includes of subclasses and other JServoController relevant files
13protected:
21 unsigned long disableTimeout;
43 bool reverse;
47 unsigned long lastMovedMillis;
51 bool enabled;
56
57public:
63 Derivs_Limiter dL;
64
84 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)
85 : servo(_servo)
86 , dL(Derivs_Limiter(max(velLimit, float(0.0)), accelLimit, decelLimit, _pos, _pos, 0, _preventGoingWrongWay, _preventGoingTooFast, min(_minAngleLimit, _maxAngleLimit), max(_minAngleLimit, _maxAngleLimit), _stoppingDecelLimit))
87 {
88 enabled = false;
89 sleeping = false;
91 minAngleLimit = _minAngleLimit;
92 maxAngleLimit = _maxAngleLimit;
93 disableTimeout = _disableTimeout;
94 reverse = _reverse;
95 minSetAngle = _minSetAngle;
96 maxSetAngle = _maxSetAngle;
97 servo.setServoValues(minServoVal, maxServoVal);
98 }
102 virtual void run()
103 {
104 if (enabled)
105 dL.calc();
106 if (dL.getPositionDelta() != 0.0) {
107 lastMovedMillis = millis();
108 }
109 if (disableTimeout != 0) {
111 if (sleeping) {
112 if (servo.getEnable() == true) {
113 servo.disable();
114 }
115 } else { // activate
116 if (servo.getEnable() == false && enabled) {
117 servo.enable();
118 }
119 }
120 } else {
121 sleeping = false;
122 }
123 if (servo.getEnable() && enabled) {
124 writeAngleToServo(dL.getPosition());
125 }
126 }
132 void setAngleImmediate(float angle, bool _run = true)
133 {
134 angle = constrain(angle, min(minAngleLimit, maxAngleLimit), max(minAngleLimit, maxAngleLimit));
135 if (dL.getTarget() != angle) {
136 wake();
137 }
138 dL.setTarget(angle);
139 dL.setPosition(angle);
140 if (_run)
141 run();
142 }
148 void setAngleImmediateInc(float angleDiff, bool _run = true)
149 {
150 float angle = constrain(getPos() + angleDiff, min(minAngleLimit, maxAngleLimit), max(minAngleLimit, maxAngleLimit));
151 if (dL.getTarget() != angle) {
152 wake();
153 }
154 dL.setTarget(angle);
155 dL.setPosition(angle);
156 if (_run)
157 run();
158 }
164 void setAngleSmoothed(float angle, bool _run = true)
165 {
166 angle = constrain(angle, min(minAngleLimit, maxAngleLimit), max(minAngleLimit, maxAngleLimit));
167 if (dL.getTarget() != angle)
168 wake();
169 dL.setTarget(angle);
170 if (_run)
171 run();
172 }
177 void setDisableTimeout(unsigned long _timeout)
178 {
179 disableTimeout = _timeout;
180 }
181
182 unsigned long getDisableTimeout()
183 {
184 return disableTimeout;
185 }
190 unsigned long getLastMovedMillis()
191 {
192 return lastMovedMillis;
193 }
198 unsigned long getMillisSinceMoved()
199 {
200 return millis() - lastMovedMillis;
201 }
207 void setLastMovedMillis(unsigned long mil)
208 {
209 lastMovedMillis = mil;
210 }
217 virtual bool setEnable(bool _enable)
218 {
219
220 if (_enable && !enabled) {
221 wake();
222 restartRun();
223 dL.setVelocity(0);
224 }
225 servo.setEnable(_enable);
226 if (enabled != _enable) {
227 enabled = _enable;
228 return true;
229 } else
230 return false;
231 }
235 bool enable()
236 {
237 return setEnable(true);
238 }
242 bool disable()
243 {
244 return setEnable(false);
245 }
251 {
252 return enabled;
253 }
259 {
260 return dL.getTarget();
261 }
266 float getPos()
267 {
268 return dL.getPosition();
269 }
275 {
276 return dL.getVelocity();
277 }
283 {
284 return dL.isPosAtTarget();
285 }
291 {
292 return dL.isPosNotAtTarget();
293 }
299 {
300 return dL.distToTarget();
301 }
306 {
307 dL.resetTime();
308 }
314 {
315 return !sleeping;
316 }
321 void wake()
322 {
323 lastMovedMillis = millis();
324 }
325 void setReverse(bool rev)
326 {
327 if (rev != reverse) {
328 reverse = rev;
329 }
330 }
332 {
333 return reverse;
334 }
335 void setAngleLimits(float _minAngleLimit, float _maxAngleLimit)
336 {
337 setMinAngleLimit(_minAngleLimit);
338 setMaxAngleLimit(_maxAngleLimit);
339 }
340 void setMinAngleLimit(float _minAngleLimit)
341 {
342 if (minAngleLimit != _minAngleLimit) {
343 minAngleLimit = _minAngleLimit;
344 }
345 dL.setPosLimits(min(minAngleLimit, maxAngleLimit), max(minAngleLimit, maxAngleLimit));
346 }
347 void setMaxAngleLimit(float _maxAngleLimit)
348 {
349 if (maxAngleLimit != _maxAngleLimit) {
350 maxAngleLimit = _maxAngleLimit;
351 }
352 dL.setPosLimits(min(minAngleLimit, maxAngleLimit), max(minAngleLimit, maxAngleLimit));
353 }
355 {
356 return minAngleLimit;
357 }
359 {
360 return maxAngleLimit;
361 }
363 {
365 }
366
367 void setSetAngles(float _minSetAngle, float _maxSetAngle)
368 {
369 setMinSetAngle(_minSetAngle);
370 setMaxSetAngle(_maxSetAngle);
371 }
372 void setMinSetAngle(float _minSetAngle)
373 {
374 if (minSetAngle != _minSetAngle) {
375 minSetAngle = _minSetAngle;
376 }
377 }
378 void setMaxSetAngle(float _maxSetAngle)
379 {
380 if (maxSetAngle != _maxSetAngle) {
381 maxSetAngle = _maxSetAngle;
382 }
383 }
384
386 {
387 return dL.getAccelLimit();
388 }
390 {
391 return dL.getDecelLimit();
392 }
394 {
395 return dL.getVelLimit();
396 }
397 void setAccelLimit(float accelLim)
398 {
399 dL.setAccelLimit(accelLim);
400 }
401 void setAccelAndDecelLimits(float accelLim, float decelLim = NAN)
402 {
403 dL.setAccelAndDecelLimits(accelLim, decelLim);
404 }
405 void setDecelLimit(float decelLim = NAN)
406 {
407 dL.setDecelLimit(decelLim);
408 }
409 void setVelLimit(float velLim)
410 {
411 dL.setVelLimit(velLim);
412 }
413 void setVelAccelLimits(float velLim, float accelLim, float decelLim = NAN)
414 {
415 dL.setVelAccelLimits(velLim, accelLim, decelLim);
416 }
423 void setPosition(float pos, bool _run = true)
424 {
425 if (pos != dL.getPosition()) {
426 wake();
427 dL.setPosition(pos);
428 }
429 if (_run)
430 run();
431 }
432
437 void setMinServoValue(int value)
438 {
439 if (servo.getMinServoValue() != value) {
440 servo.setMinServoValue(value);
441 }
442 }
447 void setMaxServoValue(int value)
448 {
449 if (servo.getMaxServoValue() != value) {
450 servo.setMaxServoValue(value);
451 }
452 }
453
459 void setServoRangeValues(int min, int max)
460 {
461 setMinServoValue(min);
462 setMaxServoValue(max);
463 }
464
470 {
471 return servo.getMinServoValue();
472 }
478 {
479 return servo.getMaxServoValue();
480 }
486 {
487 return servo.getServoValueRange();
488 }
489
495 {
496 return servo.getSetMicroseconds();
497 }
503 {
504 return servo.getConstrainRange();
505 }
510 void setConstrainRange(bool _constrainRange)
511 {
512 servo.setConstrainRange(_constrainRange);
513 }
514
516 void setVel(float vel, bool _run = true)
517 {
518 dL.setVelConstant(vel);
519 if (_run) {
520 run();
521 }
522 }
523 void setVelTarget(float vel, bool _run = true)
524 {
525 dL.setVelTarget(vel);
526 if (_run) {
527 run();
528 }
529 }
531 {
532 return dL.getVelTarget();
533 }
534 float getVel()
535 {
536 return dL.getVelocity();
537 }
539 {
540 // TODO: fix placeholder (JServoController and JServoControllerAdvanced seem to have some issues with sharing some functions but having different driver types
541 return NAN;
542 }
544 {
545 // TODO: fix placeholder
546 return true;
547 }
549 {
550 return min(minAngleLimit, maxAngleLimit);
551 }
553 {
554 return max(minAngleLimit, maxAngleLimit);
555 }
556 float getMaxVel()
557 {
558 return dL.getVelLimit();
559 }
560 float getMinVel()
561 {
562 return 0;
563 }
564 void setMaxDriverRangeAmount(float _driverRangeAmount)
565 {
566 // TODO: might not make sense for a servo, but should this do anything?
567 }
568 bool setPosTarget(float _posTarget, bool _run = true)
569 {
570 float tempTarget = getPosTarget();
571 setAngleSmoothed(_posTarget, _run);
572 return tempTarget != getPosTarget();
573 }
574 bool setPosSetpoint(float _posSetpoint, bool _run = true)
575 {
576 float tempPos = getPos();
577 setAngleImmediate(_posSetpoint, _run);
578 return tempPos != getPos();
579 }
580 bool setPosDelta(float _posDelta, bool _run = true, bool _resetPos = false)
581 {
582 setVel(_posDelta, _run);
583 // TODO: resetting the position doesn't make much sense for a servo, so it isn't handled here.
584 return true;
585 }
587 {
588 return getPos();
589 }
590 float resetPos()
591 {
592 // TODO: not handled by a servo
593 return 0;
594 }
596 {
597 return dL.isPosModeNotVelocity();
598 }
599
600protected:
601 void
603 {
604 ang = constrain(ang, min(minAngleLimit, maxAngleLimit), max(minAngleLimit, maxAngleLimit));
605 if ((reverse) != (minAngleLimit < maxAngleLimit)) {
607 } else { // not reversed
609 }
610 if (enabled && !sleeping) {
611 servo.set(ang);
612 }
613 }
614 float floatMap(float x, float in_min, float in_max, float out_min, float out_max)
615 {
616 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
617 }
618};
619
623#include "JServoCurrentSensor.h"
624
625#endif
This class defines a common interface for classes which control velocity and position of a motor cont...
Definition JMotorController.h:8
virtual bool set(float val)
set motor power
virtual bool setEnable(bool _enable)
use to enable or disable a motor, and sets up pin states
virtual float getMaxRange()
high end of the range
virtual float getMinRange()
low end of the range
virtual bool getEnable()
get the enable state of the driver
bool disable()
disable motor
Definition JMotorDriver.h:63
bool enable()
enable motor
Definition JMotorDriver.h:53
class that can be used to specify a driver that controls specifically a servo
Definition JMotorDriverServo.h:8
void setServoValues(int min, int max)
set the settings for short and long servo pulses at the same time
Definition JMotorDriverServo.h:64
int getServoValueRange()
returns difference (in microseconds) between longest and shortest servo pulse settings
Definition JMotorDriverServo.h:55
void setMinServoValue(int value)
microseconds for shortest servo pulse
Definition JMotorDriverServo.h:23
void setMaxServoValue(int value)
microseconds for longest servo pulse
Definition JMotorDriverServo.h:31
int getMaxServoValue()
returns setting for microseconds for longest servo pulse
Definition JMotorDriverServo.h:47
int getSetMicroseconds()
returns how many microseconds the servo signal pulse was most recently set to
Definition JMotorDriverServo.h:74
int getMinServoValue()
returns setting for microseconds for shortest servo pulse
Definition JMotorDriverServo.h:39
bool getConstrainRange()
returns whether the range of set() will be constrained to within -1 and 1
Definition JMotorDriverServo.h:82
void setConstrainRange(bool _constrainRange)
set whether the range of set() will be constrained to within -1 and 1
Definition JMotorDriverServo.h:90
class for controlling JMotorDriverServo, with angle calibration and accel and velocity limiting
Definition JServoController.h:12
bool setPosSetpoint(float _posSetpoint, bool _run=true)
set position for motor to drive towards position as fast as possible (setpoint for control loop if av...
Definition JServoController.h:574
void setPosition(float pos, bool _run=true)
sets servo position, leaves target where it was
Definition JServoController.h:423
void setAngleSmoothed(float angle, bool _run=true)
set servo angle target, servo will move to target but at limited velocity and acceleration
Definition JServoController.h:164
unsigned long getMillisSinceMoved()
returns the number of milliseconds since the servo last moved
Definition JServoController.h:198
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 a...
Definition JServoController.h:84
bool setPosTarget(float _posTarget, bool _run=true)
set target position (motor drives towards position, following acceleration profile)
Definition JServoController.h:568
void setVelLimit(float velLim)
set maximum motor speed
Definition JServoController.h:409
void setAngleImmediate(float angle, bool _run=true)
set servo angle immediately, without velocity or acceleration limiting
Definition JServoController.h:132
void setReverse(bool rev)
Definition JServoController.h:325
void setMaxServoValue(int value)
microseconds for longest servo pulse
Definition JServoController.h:447
float getPosTarget()
what position is the servo moving towards or at
Definition JServoController.h:258
float getVelLimit()
returns value of velLimit
Definition JServoController.h:393
virtual void run()
call this in your main loop
Definition JServoController.h:102
float getDecelLimit()
Definition JServoController.h:389
bool isDriverInRange()
true if motor driver is being set to within its range, false if driver is at a maximum
Definition JServoController.h:543
bool sleeping
has it been longer than disableTimeout since the servo has moved?
Definition JServoController.h:55
void setConstrainRange(bool _constrainRange)
set whether the range of set() will be constrained to within -1 and 1
Definition JServoController.h:510
void setAccelAndDecelLimits(float accelLim, float decelLim=NAN)
Definition JServoController.h:401
float floatMap(float x, float in_min, float in_max, float out_min, float out_max)
Definition JServoController.h:614
void setMaxAngleLimit(float _maxAngleLimit)
Definition JServoController.h:347
float getDriverMaxRange()
if getDriverSetVal exceeds this, isDriverInRange will go false
Definition JServoController.h:552
void setSetAngles(float _minSetAngle, float _maxSetAngle)
Definition JServoController.h:367
float maxAngleLimit
higher limit for angle setpoint
Definition JServoController.h:31
int getServoValueRange()
returns difference (in microseconds) between longest and shortest servo pulse settings
Definition JServoController.h:485
bool getReverse()
Definition JServoController.h:331
int getMinServoRangeValue()
returns setting for microseconds for shortest servo pulse
Definition JServoController.h:469
void setAngleImmediateInc(float angleDiff, bool _run=true)
increment servo angle and set immediately, without velocity or acceleration limiting
Definition JServoController.h:148
float minAngleLimit
lower limit for angle setpoint
Definition JServoController.h:26
void setAccelLimit(float accelLim)
set maximum rate that motor speed can be changed at
Definition JServoController.h:397
void setDecelLimit(float decelLim=NAN)
Definition JServoController.h:405
unsigned long getDisableTimeout()
Definition JServoController.h:182
void restartRun()
if you stopped calling run() for a while, call this before restarting run() to avoid a big jump in mo...
Definition JServoController.h:305
int getMaxServoRangeValue()
returns setting for microseconds for longest servo pulse
Definition JServoController.h:477
float getDriverMinRange()
if getDriverSetVal goes below this, isDriverInRange will go false
Definition JServoController.h:548
float getMaxAngleLimit()
Definition JServoController.h:358
bool getConstrainRange()
returns whether the range of set() will be constrained to within -1 and 1
Definition JServoController.h:502
void setMaxDriverRangeAmount(float _driverRangeAmount)
set what fraction of driverRange can be used
Definition JServoController.h:564
void setAngleLimits(float _minAngleLimit, float _maxAngleLimit)
Definition JServoController.h:335
float getVelTarget()
get target velocity for controller
Definition JServoController.h:530
float resetPos()
reset what position the controller thinks it's in
Definition JServoController.h:590
bool setPosDelta(float _posDelta, bool _run=true, bool _resetPos=false)
alternative method for setting velocity that uses setPosSetpoint
Definition JServoController.h:580
unsigned long getLastMovedMillis()
returns the value of millis() when the servo last moved
Definition JServoController.h:190
float getMinAngleLimit()
Definition JServoController.h:354
float minSetAngle
when servodriver is set to getMinValue what angle is the servo?
Definition JServoController.h:35
float getAngleLimitRange()
Definition JServoController.h:362
float getPos()
what position is the servo actually being set to? (slowly approaches target if smoothing is used)
Definition JServoController.h:266
int getSetMicroseconds()
returns how many microseconds the servo signal pulse was most recently set to
Definition JServoController.h:494
void setVelAccelLimits(float velLim, float accelLim, float decelLim=NAN)
Definition JServoController.h:413
bool reverse
reverse range of servo
Definition JServoController.h:43
void setVel(float vel, bool _run=true)
set velocity for controller
Definition JServoController.h:516
void writeAngleToServo(float ang)
Definition JServoController.h:602
unsigned long disableTimeout
variable for how long to wait if not moved before disabling (default: 0, never disable)
Definition JServoController.h:21
bool disable()
equivalent to setEnable(false)
Definition JServoController.h:242
bool enable()
equivalent to setEnable(true)
Definition JServoController.h:235
bool getEnable()
returns whether the servo is enabled or not
Definition JServoController.h:250
void setMinSetAngle(float _minSetAngle)
Definition JServoController.h:372
float getDriverSetVal()
returns the value the motor driver is being set to
Definition JServoController.h:538
float getPosSetpoint()
get position controller is currently trying to get to
Definition JServoController.h:586
float getMaxVel()
How fast of a motor speed setting would get adjusted to full motor power.
Definition JServoController.h:556
bool isPosAtTarget()
true if position=target, false otherwise
Definition JServoController.h:282
unsigned long lastMovedMillis
variable used to keep track of how long the servo has been still for
Definition JServoController.h:47
virtual bool setEnable(bool _enable)
enable or disable the servo (start or stop sending a signal to the servo)
Definition JServoController.h:217
void setMinAngleLimit(float _minAngleLimit)
Definition JServoController.h:340
float getMinVel()
slowest speed motor can go
Definition JServoController.h:560
float getVelocity()
what rate is servo position being changed at?
Definition JServoController.h:274
void wake()
if the servo has been disabled because of inactivity, calling this function simulates the servo being...
Definition JServoController.h:321
Derivs_Limiter dL
instance of Derivs_Limiter class used to smoothly move servo
Definition JServoController.h:63
bool isPosModeNotVelocity()
true if controller is in position target mode, false if in velocity mode
Definition JServoController.h:595
void setServoRangeValues(int min, int max)
set the settings for short and long servo pulses at the same time
Definition JServoController.h:459
void setDisableTimeout(unsigned long _timeout)
default: 0, after how many milliseconds of no movement should the servo be disabled?...
Definition JServoController.h:177
float getVel()
get current velocity of motor
Definition JServoController.h:534
void setMaxSetAngle(float _maxSetAngle)
Definition JServoController.h:378
void setLastMovedMillis(unsigned long mil)
allows for changing the variable that stores when the servo last moved
Definition JServoController.h:207
bool isPosNotAtTarget()
equivalent to !isPosAtTarget()
Definition JServoController.h:290
void setVelTarget(float vel, bool _run=true)
set target velocity for controller (acceleration limited)
Definition JServoController.h:523
void setMinServoValue(int value)
microseconds for shortest servo pulse
Definition JServoController.h:437
JMotorDriverServo & servo
reference to driver that's a subclass of JMotorDriverServo
Definition JServoController.h:17
bool getActive()
returns whether the servo is "awake" (whether signals are being sent)
Definition JServoController.h:313
float maxSetAngle
when servodriver is set to getMaxValue what angle is the servo?
Definition JServoController.h:39
float distToTarget()
returns target-position
Definition JServoController.h:298
float getAccelLimit()
returns value of accelLimit
Definition JServoController.h:385
bool enabled
keep track of whether controller is enabled (since even when controller is enabled driver might be di...
Definition JServoController.h:51