JMotor
Loading...
Searching...
No Matches
JMotorControllerOpen.h
Go to the documentation of this file.
1#ifndef J_MOTOR_CONTROLLER_OPEN_H
2#define J_MOTOR_CONTROLLER_OPEN_H
3#include "JMotorController.h"
5#include <Arduino.h>
6#include <Derivs_Limiter.h>
11protected:
12 bool posMode;
14 float position;
16 float posDelta;
17 unsigned long lastRunOpenMicros;
18 unsigned long minMotorPulseTime;
19 unsigned long lastPosDeltaWrite;
20
21public:
26 Derivs_Limiter dL;
35 JMotorControllerOpen(JMotorDriver& _driver, JMotorCompensator& _compensator, float _velLimit = INFINITY, float _accelLimit = INFINITY, unsigned long _minMotorPulseTime = 0)
36 : JMotorControllerBasic(_driver, _compensator, _velLimit, _accelLimit)
37 , dL(Derivs_Limiter(_velLimit, _accelLimit))
38 {
39 position = 0;
41 posMode = false;
42 smoothedMode = true;
43 posDelta = 0;
45 minMotorPulseTime = _minMotorPulseTime;
47 }
48
49 void run()
50 {
51 float time = (micros() - lastRunOpenMicros) / 1000000.0;
52 lastRunOpenMicros = micros();
53 if (time == 0) {
54 return;
55 }
56 if (getEnable()) {
57 if (posMode) {
58 if (!smoothedMode) { // setPosSetpoint() mode
59 positionTarget += time * posDelta;
60 if (micros() - lastPosDeltaWrite > minMotorPulseTime) {
61 float posDeltaTime = (micros() - lastPosDeltaWrite) / 1000000.0;
62 lastPosDeltaWrite = micros();
63 if (position == positionTarget) {
65 } else if (abs(positionTarget - position) <= getMinVel() * posDeltaTime) {
67 } else if (abs(positionTarget - position) < getMaxVel() * posDeltaTime) {
68 JMotorControllerBasic::setVel((positionTarget - position) / posDeltaTime, false);
70 } else { // far away
72 position += velocity * posDeltaTime;
73 }
74 }
76
77 } else { // setPosTarget() mode
78 dL.setPositionVelocity(position, velocity);
79 dL.setVelLimit(min(velLimit, getMaxVel()));
80 dL.calc(positionTarget);
81 position = dL.getPosition();
82 // TODO: it would be better to use posSetpoint mode
83 JMotorControllerBasic::setVel(dL.getVelocity(), true);
84 }
85 } else { // not pos mode
87 if (abs(velocity) > getMinVel()) {
88 position += velocity * time;
89 }
90 }
91 }
92 }
93
94 bool setEnable(bool _enable)
95 {
96 if (!_enable && getEnable()) {
97 posDelta = 0;
99 }
100 dL.resetTime();
101 return JMotorControllerBasic::setEnable(_enable);
102 }
103
104 bool setPosTarget(float _posTarget, bool _run = true)
105 {
106 smoothedMode = true;
107 if (posMode == false) {
108 dL.resetTime();
109 }
110
111 if (position != _posTarget && velocity == 0) {
112 dL.setVelocity((_posTarget > position) ? getMinVel() : -getMinVel());
113 }
114 posMode = true;
115 if (positionTarget != _posTarget) {
116 positionTarget = _posTarget;
117 if (_run)
119 return true;
120 }
121 if (_run)
123 return false;
124 }
125 bool setPosSetpoint(float _posSetpoint, bool _run = true)
126 {
127 smoothedMode = false;
128 posMode = true;
129 posDelta = 0;
130 if (positionTarget != _posSetpoint) {
131 positionTarget = _posSetpoint;
132 if (_run)
134 return true;
135 }
136 if (_run)
138 return false;
139 }
140
141 bool setPosDelta(float _posDelta, bool _run = true, bool _resetPos = false)
142 {
143 smoothedMode = false;
144 posMode = true;
145 bool ret = (posDelta != _posDelta);
146 posDelta = _posDelta;
147 if (_resetPos) {
148 resetPos();
149 }
150 if (_run)
152 return ret;
153 }
154
156 {
157 return positionTarget;
158 }
160 {
161 return position;
162 }
163 float getPos()
164 {
165 return position;
166 }
167 float resetPos()
168 {
169 dL.setPosition(0);
170 if (posMode)
172 float temp = position;
173 position = 0;
174 return temp;
175 }
176
178 {
179 return posMode;
180 }
181
182 // override basicOpen functions
183 void setVel(float vel, bool _run = true)
184 {
185 posMode = false;
186 velocity = vel;
187 velocityTarget = vel;
188 if (_run)
190 }
191 void setVelTarget(float vel, bool _run = true)
192 {
193 posMode = false;
194 velocityTarget = vel;
195 if (_run)
197 }
198 void setVelLimit(float _velLimit)
199 {
201 dL.setVelLimit(velLimit);
202 }
203 void setAccelLimit(float _accelLimit)
204 {
206 dL.setAccelLimit(accelLimit);
207 }
214 void setVelAccelLimits(float _vel, float _accel)
215 {
216 setVelLimit(_vel);
217 setAccelLimit(_accel);
218 }
219
224 void setMinMotorPulseTime(unsigned long _minMotorPulseTime)
225 {
226 minMotorPulseTime = _minMotorPulseTime;
227 }
228};
229
230#endif
This class defines a common interface for converting from speed to driver input. It should compensate...
Definition JMotorCompensator.h:9
A motor controller object that uses a JMotorCompensator to set velocity of a motor.
Definition JMotorControllerBasic.h:12
void setVelLimit(float _velLimit)
set maximum motor speed
Definition JMotorControllerBasic.h:52
bool setEnable(bool _enable)
change whether motor controller is enabled
Definition JMotorControllerBasic.h:108
void run()
update driver
Definition JMotorControllerBasic.h:87
float accelLimit
Definition JMotorControllerBasic.h:17
float velocityTarget
Definition JMotorControllerBasic.h:16
float velLimit
Definition JMotorControllerBasic.h:18
void setVel(float vel, bool _run=true)
set velocity for controller
Definition JMotorControllerBasic.h:57
float getMinVel()
slowest speed motor can go
Definition JMotorControllerBasic.h:153
float velocity
Definition JMotorControllerBasic.h:14
bool getEnable()
is the controller enabled
Definition JMotorControllerBasic.h:143
float getMaxVel()
How fast of a motor speed setting would get adjusted to full motor power.
Definition JMotorControllerBasic.h:148
void setAccelLimit(float _accelLimit)
set maximum rate that motor speed can be changed at
Definition JMotorControllerBasic.h:47
This class defines a common interface for classes which control velocity and position of a motor cont...
Definition JMotorController.h:8
This class controls velocity and position of a motor without any encoder feedback.
Definition JMotorControllerOpen.h:10
void setVelTarget(float vel, bool _run=true)
set target velocity for controller (acceleration limited)
Definition JMotorControllerOpen.h:191
void setVelAccelLimits(float _vel, float _accel)
set velocity and acceleration limits for motor
Definition JMotorControllerOpen.h:214
void setVel(float vel, bool _run=true)
set velocity for controller
Definition JMotorControllerOpen.h:183
float getPosTarget()
get position set as target (for smoothed position setting mode)
Definition JMotorControllerOpen.h:155
bool setPosTarget(float _posTarget, bool _run=true)
set target position (motor drives towards position, following acceleration profile)
Definition JMotorControllerOpen.h:104
bool posMode
Definition JMotorControllerOpen.h:12
bool setPosDelta(float _posDelta, bool _run=true, bool _resetPos=false)
alternative method for setting velocity that uses setPosSetpoint
Definition JMotorControllerOpen.h:141
bool isPosModeNotVelocity()
true if controller is in position target mode, false if in velocity mode
Definition JMotorControllerOpen.h:177
float positionTarget
Definition JMotorControllerOpen.h:15
bool setEnable(bool _enable)
change whether motor controller is enabled
Definition JMotorControllerOpen.h:94
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 JMotorControllerOpen.h:125
float posDelta
Definition JMotorControllerOpen.h:16
float resetPos()
reset what position the controller thinks it's in
Definition JMotorControllerOpen.h:167
void setMinMotorPulseTime(unsigned long _minMotorPulseTime)
At low speeds in setPosDelta the motor cycles between min speed and stopped. What is the minimum puls...
Definition JMotorControllerOpen.h:224
float position
Definition JMotorControllerOpen.h:14
unsigned long lastRunOpenMicros
Definition JMotorControllerOpen.h:17
void run()
update driver
Definition JMotorControllerOpen.h:49
unsigned long minMotorPulseTime
Definition JMotorControllerOpen.h:18
void setAccelLimit(float _accelLimit)
set maximum rate that motor speed can be changed at
Definition JMotorControllerOpen.h:203
Derivs_Limiter dL
use functions from JMotorControllerOpen whenever possible, but the Derivs_Limiter object is public in...
Definition JMotorControllerOpen.h:26
float getPosSetpoint()
get position controller is currently trying to get to
Definition JMotorControllerOpen.h:159
void setVelLimit(float _velLimit)
set maximum motor speed
Definition JMotorControllerOpen.h:198
JMotorControllerOpen(JMotorDriver &_driver, JMotorCompensator &_compensator, float _velLimit=INFINITY, float _accelLimit=INFINITY, unsigned long _minMotorPulseTime=0)
Constructor for an object that controls a motor, and can smooth the motion of the motor and go approx...
Definition JMotorControllerOpen.h:35
bool smoothedMode
Definition JMotorControllerOpen.h:13
float getPos()
get what position the motor is currently at
Definition JMotorControllerOpen.h:163
unsigned long lastPosDeltaWrite
Definition JMotorControllerOpen.h:19
defines common interface for all types of JMotorDrivers
Definition JMotorDriver.h:10