JMotor
Loading...
Searching...
No Matches
JMotorControllerBasic.h
Go to the documentation of this file.
1#ifndef J_MOTOR_CONTROLLER_BASIC_OPEN_H
2#define J_MOTOR_CONTROLLER_BASIC_OPEN_H
6#include <Arduino.h>
7
13protected:
14 float velocity;
15 float setVal;
18 float velLimit;
19 unsigned long lastRunMicros;
20
24
25public:
33 JMotorControllerBasic(JMotorDriver& _driver, JMotorCompensator& _compensator, float _velLimit = INFINITY, float _accelLimit = INFINITY)
34 : driver(_driver)
35 , compensator(_compensator)
36 {
38 driverInRange = true;
39 velocity = 0;
40 accelLimit = max(_accelLimit, (float)0.0);
41 velLimit = max(_velLimit, (float)0.0);
43 setVal = 0;
44 lastRunMicros = 0;
45 }
46
47 void setAccelLimit(float _accelLimit)
48 {
49 accelLimit = max(_accelLimit, (float)0.0);
50 }
51
52 void setVelLimit(float _velLimit)
53 {
54 velLimit = max(_velLimit, (float)0.0);
55 }
56
57 void setVel(float vel, bool _run = true)
58 {
59 velocity = vel;
60 velocityTarget = vel;
61 if (_run)
63 }
64
65 void setVelTarget(float vel, bool _run = true)
66 {
67 velocityTarget = vel;
68 if (_run)
70 }
71
73 {
74 return velocityTarget;
75 }
76
77 float getVel()
78 {
79 return velocity;
80 }
81
83 {
84 return setVal;
85 }
86
87 void run()
88 {
89 float time = (micros() - lastRunMicros) / 1000000.0;
90 lastRunMicros = micros();
91 if (time == 0) {
92 return;
93 }
94 if (getEnable()) {
95 if (velocity != velocityTarget) {
96 velocity += constrain(velocityTarget - velocity, -accelLimit * time, accelLimit * time);
97 }
100 velocity = constrain(velocity, -velLimit, velLimit);
101 if (driver.getEnable()) {
104 }
105 }
106 }
107
108 bool setEnable(bool _enable)
109 {
110 if (!_enable && getEnable()) {
111 velocity = 0;
112 velocityTarget = 0;
113 }
114 lastRunMicros = micros();
115 return driver.setEnable(_enable);
116 }
117
119 {
120 return driverInRange;
121 }
122
123 bool enable()
124 {
125 return setEnable(true);
126 }
127
128 bool disable()
129 {
130 return setEnable(false);
131 }
132
134 {
135 return driver.getMinRange();
136 }
137
142
144 {
145 return driver.getEnable();
146 }
147
148 float getMaxVel()
149 {
150 return min(velLimit, compensator.getMaxVel());
151 }
152
153 float getMinVel()
154 {
155 return min(velLimit, compensator.getMinVel());
156 }
157
158 void setMaxDriverRangeAmount(float _driverRangeAmount)
159 {
160 compensator.setMaxDriverRangeAmount(_driverRangeAmount);
161 }
162
164 {
165 return velLimit;
166 }
167
169 {
170 return accelLimit;
171 }
172};
173#endif
This class defines a common interface for converting from speed to driver input. It should compensate...
Definition JMotorCompensator.h:9
virtual float getMaxVel()
How fast of a motor speed setting would get adjusted to full motor power.
void setDriverRange(float _driverRange)
set what value makes the driver output 100% of supply voltage
Definition JMotorCompensator.h:19
virtual float compensate(float val)
calculate
virtual float getMinVel()
Slowest speed motor can go.
void setMaxDriverRangeAmount(float _driverRangeAmount)
set what fraction of driverRange can be used
Definition JMotorCompensator.h:27
float getMaxDriverRangeAmount()
get what fraction of driverRange can be used
Definition JMotorCompensator.h:64
This class defines a common interface for classes which control velocity of a motor controlled by a J...
Definition JMotorControllerBase.h:7
A motor controller object that uses a JMotorCompensator to set velocity of a motor.
Definition JMotorControllerBasic.h:12
bool enable()
enable motor controller
Definition JMotorControllerBasic.h:123
void setVelLimit(float _velLimit)
set maximum motor speed
Definition JMotorControllerBasic.h:52
JMotorDriver & driver
Definition JMotorControllerBasic.h:21
void setVelTarget(float vel, bool _run=true)
set target velocity for controller (acceleration limited)
Definition JMotorControllerBasic.h:65
unsigned long lastRunMicros
Definition JMotorControllerBasic.h:19
bool setEnable(bool _enable)
change whether motor controller is enabled
Definition JMotorControllerBasic.h:108
void run()
update driver
Definition JMotorControllerBasic.h:87
bool driverInRange
Definition JMotorControllerBasic.h:22
float accelLimit
Definition JMotorControllerBasic.h:17
bool disable()
disable motor controller
Definition JMotorControllerBasic.h:128
float velocityTarget
Definition JMotorControllerBasic.h:16
JMotorCompensator & compensator
Definition JMotorControllerBasic.h:23
JMotorControllerBasic(JMotorDriver &_driver, JMotorCompensator &_compensator, float _velLimit=INFINITY, float _accelLimit=INFINITY)
constructor
Definition JMotorControllerBasic.h:33
float getDriverMaxRange()
if getDriverSetVal exceeds this, isDriverInRange will go false
Definition JMotorControllerBasic.h:138
float getVelTarget()
get target velocity for controller
Definition JMotorControllerBasic.h:72
float velLimit
Definition JMotorControllerBasic.h:18
float getDriverSetVal()
returns the value the motor driver is being set to
Definition JMotorControllerBasic.h:82
void setVel(float vel, bool _run=true)
set velocity for controller
Definition JMotorControllerBasic.h:57
float setVal
Definition JMotorControllerBasic.h:15
float getMinVel()
slowest speed motor can go
Definition JMotorControllerBasic.h:153
float velocity
Definition JMotorControllerBasic.h:14
bool isDriverInRange()
true if motor driver is being set to within its range, false if driver is at a maximum
Definition JMotorControllerBasic.h:118
float getVel()
get current velocity of motor
Definition JMotorControllerBasic.h:77
void setMaxDriverRangeAmount(float _driverRangeAmount)
set what fraction of driverRange can be used
Definition JMotorControllerBasic.h:158
float getVelLimit()
returns value of velLimit
Definition JMotorControllerBasic.h:163
bool getEnable()
is the controller enabled
Definition JMotorControllerBasic.h:143
float getAccelLimit()
returns value of accelLimit
Definition JMotorControllerBasic.h:168
float getMaxVel()
How fast of a motor speed setting would get adjusted to full motor power.
Definition JMotorControllerBasic.h:148
float getDriverMinRange()
if getDriverSetVal goes below this, isDriverInRange will go false
Definition JMotorControllerBasic.h:133
void setAccelLimit(float _accelLimit)
set maximum rate that motor speed can be changed at
Definition JMotorControllerBasic.h:47
defines common interface for all types of JMotorDrivers
Definition JMotorDriver.h:10
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