JMotor
Loading...
Searching...
No Matches
JControlLoopBasic.h
Go to the documentation of this file.
1#ifndef J_CONTROL_LOOP_BASIC_H
2#define J_CONTROL_LOOP_BASIC_H
3#include "JControlLoop.h"
8protected:
9 float P;
10 unsigned long timeout;
11 unsigned long lastMovedMillis;
13
14public:
21 JControlLoopBasic(float kP, unsigned long _timeout = 0, bool _noReverseVoltage = false)
22 {
23 P = kP;
24 timeout = _timeout;
26 noReverseVoltage = _noReverseVoltage;
27 }
28 float calc(JMotorControllerClosed* controller)
29 {
30 if (controller->getPosDeltaSetpoint() != 0 || abs(controller->getPosSetpoint() - controller->getPos()) > controller->getDistFromSetpointLimit() || abs(controller->getVel()) > controller->getMinVel()) {
31 lastMovedMillis = millis();
32 }
33 result = 0;
34 if (timeout == 0 || millis() - lastMovedMillis <= timeout) {
35 error = (controller->getPosSetpoint() - controller->getPos());
36 if (noReverseVoltage) {
37 if (controller->getPosDeltaSetpoint() > 0) {
38 ctrlLoopOut = max(P * error, (float)0);
39 } else if (controller->getPosDeltaSetpoint() < 0) {
40 ctrlLoopOut = min(P * error, (float)0);
41 } else {
42 ctrlLoopOut = 0;
43 }
44 } else {
46 }
47 result = ctrlLoopOut + controller->getPosDeltaSetpoint();
48 }
49
50 return result;
51 }
52
53 void setKP(float _p)
54 {
55 P = _p;
56 }
57 float getKP()
58 {
59 return P;
60 }
61 void setTimeout(unsigned long _timeout)
62 {
63 timeout = _timeout;
64 }
65 unsigned long getTimeout()
66 {
67 return timeout;
68 }
69 void resetTime()
70 {
71 lastMovedMillis = millis();
72 }
73};
74#endif
simple control loop for use with JMotorControllerClosed, can turn off motor if nothing has moved for ...
Definition JControlLoopBasic.h:7
float getKP()
Definition JControlLoopBasic.h:57
bool noReverseVoltage
Definition JControlLoopBasic.h:12
unsigned long getTimeout()
Definition JControlLoopBasic.h:65
void setKP(float _p)
Definition JControlLoopBasic.h:53
float P
Definition JControlLoopBasic.h:9
void setTimeout(unsigned long _timeout)
Definition JControlLoopBasic.h:61
JControlLoopBasic(float kP, unsigned long _timeout=0, bool _noReverseVoltage=false)
constructor for control loop for use with JMotorControllerClosed
Definition JControlLoopBasic.h:21
unsigned long timeout
Definition JControlLoopBasic.h:10
unsigned long lastMovedMillis
Definition JControlLoopBasic.h:11
void resetTime()
Definition JControlLoopBasic.h:69
float calc(JMotorControllerClosed *controller)
Definition JControlLoopBasic.h:28
interface for control loop used by JMotorControllerClosed
Definition JControlLoop.h:7
float error
Definition JControlLoop.h:9
float ctrlLoopOut
Definition JControlLoop.h:10
float result
Definition JControlLoop.h:11
closed loop (uses encoder) motor controller
Definition JMotorControllerClosed.h:13
float getPos()
get what position the motor is currently at
Definition JMotorControllerClosed.h:348
float getPosSetpoint()
get position controller is currently trying to get to
Definition JMotorControllerClosed.h:297
float getDistFromSetpointLimit()
Definition JMotorControllerClosed.h:325
float getVel()
get current velocity of motor
Definition JMotorControllerClosed.h:344
float getPosDeltaSetpoint()
Definition JMotorControllerClosed.h:310
float getMinVel()
slowest speed motor can go
Definition JMotorControllerClosed.h:459