RBE1001Lib
Motor.h
Go to the documentation of this file.
1 /*
2  * Motor.h
3  *
4  * Created on: May 31, 2020
5  * Author: hephaestus
6  */
7 
8 #ifndef LIBRARIES_RBE1001LIB_SRC_MOTOR_H_
9 #define LIBRARIES_RBE1001LIB_SRC_MOTOR_H_
10 #include <ESP32Servo.h>
11 #include <ESP32Encoder.h>
12 #include <Arduino.h>
13 #define MAX_POSSIBLE_MOTORS 4
14 #define ENCODER_CPR 12.0f
15 #define GEAR_BOX_RATIO 120.0f
16 #define QUADRATUE_MULTIPLYER 1.0f
17 #define TICKS_TO_DEGREES ((QUADRATUE_MULTIPLYER / (ENCODER_CPR * GEAR_BOX_RATIO / 360.0)) * -1)
18 #define I_TERM_SIZE 120.0f
19 
20 const float DELTA_EFFORT = 0.0025;
21 
23 {
27  BEZIER = 4,
29 };
43 class Motor
44 {
45 private:
57  bool isAttached = false;
63  /*
64  * this stores the previous count of the encoder last time the velocity was calculated
65  */
66  int prevousCount = 0;
70  float cachedSpeed = 0;
74  float setpoint = 0;
78  float kP = 0.02;
82  float kI = 0.01;
86  float kD = 0;
90  float runntingITerm = 0;
91  /*
92  * effort of the motor
93  * @param a value from -1 to 1 representing effort
94  * 0 is brake
95  * 1 is full speed clockwise
96  * -1 is full speed counter clockwise
97  * @note this should only be called from the PID thread
98  */
99  void setEffortLocal(float effort);
103  static void allocateTimer(int PWMgenerationTimer);
108  bool closedLoopControl = true;
112  float targetEffort = 0;
116  float currentEffort = 0;
120  float duration = 0;
124  float startTime = 0;
128  float endSetpoint = 0;
132  float startSetpoint = 0;
136  float unitDuration = 1;
141 
152  float BEZIER_P0 = 0.25;
158  float BEZIER_P1 = 0.75;
159 
163  float TRAPEZOIDAL_time = 0;
164 
165 public:
169  int MotorPWMPin = -1;
173  int directionFlag = -1;
177  int MotorEncAPin = -1;
181  int MotorEncBPin = -1;
190  int64_t nowEncoder = 0;
194  static bool timersAllocated;
201 
202 
203 
217  Motor(int pwmPin, int dirPin, int encAPin, int encBPin);
218  virtual ~Motor();
219 
227  void attach();
228  /*
229  * \brief effort of the motor, proportional to PWM
230  *
231  * @param effort a value from -1 to 1 representing effort
232  * 0 is brake
233  * 1 is full speed clockwise
234  * -1 is full speed counter clockwise
235  */
236  void setEffort(float effort);
237  /*
238  * effort of the motor
239  * @param percent a value from -100 to 100 representing effort
240  * 0 is brake
241  * 100 is full speed clockwise
242  * -100 is full speed counter clockwise
243  */
244  void setEffortPercent(float percent)
245  {
246  if (!isAttached)
247  attach();
248  setEffort(percent * 0.01);
249  }
250  /*
251  * effort of the motor
252  * @return a value from -1 to 1 representing effort
253  * 0 is brake
254  * 1 is full speed clockwise
255  * -1 is full speed counter clockwise
256  */
257  float getEffort();
258  /*
259  * effort of the motor
260  * @return a value from -100 to 100 representing effort
261  * 0 is brake
262  * 100 is full speed clockwise
263  * -100 is full speed counter clockwise
264  */
266  {
267  if (!isAttached)
268  attach();
269  return getEffort() * 100;
270  }
278  float getDegreesPerSecond();
285  float getCurrentDegrees();
291  void loop();
299  void setSetpointWithTime(float newTargetInDegrees, long miliseconds,
300  interpolateMode mode);
318  void setSpeed(float newDegreesPerSecond);
325  void setSpeed(float newDegreesPerSecond, long miliseconds);
333  void moveTo(float newTargetInDegrees, float speedDegPerSec);
343  void moveFor(float deltaTargetInDegrees, float speedDegPerSec);
344 
351  void blockUntilMoveIsDone();
359  float startMoveFor(float deltaTargetInDegrees, float speedDegPerSec);
360 
366  void setSetpoint(float newTargetInDegrees)
367  {
368  if (!isAttached)
369  attach();
370  setSetpointWithTime(newTargetInDegrees, 0, LINEAR_INTERPOLATION);
371  }
372 
380  void setSetpointWithLinearInterpolation(float newTargetInDegrees,
381  long miliseconds)
382  {
383  if (!isAttached)
384  attach();
385  setSetpointWithTime(newTargetInDegrees, miliseconds,
387  }
388 
396  void setSetpointWithSinusoidalInterpolation(float newTargetInDegrees,
397  long miliseconds)
398  {
399  if (!isAttached)
400  attach();
401  setSetpointWithTime(newTargetInDegrees, miliseconds,
403  }
413  void setSetpointWithBezierInterpolation(float newTargetInDegrees,
414  long miliseconds, float Control_0 = 0.5, float Control_1 = 1.0)
415  {
416  if (!isAttached)
417  attach();
418  BEZIER_P0 = Control_0;
419  BEZIER_P1 = Control_1;
420  setSetpointWithTime(newTargetInDegrees, miliseconds,
421  BEZIER);
422  }
433  void setSetpointWithTrapezoidalInterpolation(float newTargetInDegrees,
434  long miliseconds, float trapazoidalTime)
435  {
436  if (!isAttached)
437  attach();
438  if (trapazoidalTime * 2 > miliseconds)
439  {
440  setSetpointWithSinusoidalInterpolation(newTargetInDegrees,
441  miliseconds);
442  return;
443  }
444  TRAPEZOIDAL_time = trapazoidalTime;
445  setSetpointWithTime(newTargetInDegrees, miliseconds, TRAPEZOIDAL);
446  }
450  void setGains(float p, float i, float d);
451  void setGainsP(float p);
452  void setGainsI(float i);
453  void setGainsD(float d);
454 
455  float getGainsP()
456  {
457  if (!isAttached)
458  attach();
459  return kP;
460  }
461  float getGainsI()
462  {
463  if (!isAttached)
464  attach();
465  return kI;
466  }
467  float getGainsD()
468  {
469  if (!isAttached)
470  attach();
471  return kD;
472  }
473 
484  bool isMotorDoneWithMove();
485 };
486 
487 //extern Motor left_motor;
488 //extern Motor right_motor;
489 
490 #endif /* LIBRARIES_RBE1001LIB_SRC_MOTOR_H_ */
void setEffortLocal(float effort)
Definition: Motor.cpp:529
float getGainsI()
Definition: Motor.h:461
float getEffort()
Definition: Motor.cpp:516
int MotorEncBPin
Definition: Motor.h:181
int MotorEncAPin
Definition: Motor.h:177
int prevousCount
Definition: Motor.h:66
void setSetpoint(float newTargetInDegrees)
Definition: Motor.h:366
float startMoveFor(float deltaTargetInDegrees, float speedDegPerSec)
Definition: Motor.cpp:192
void setEffortPercent(float percent)
Definition: Motor.h:244
float kP
Definition: Motor.h:78
Motor(int pwmPin, int dirPin, int encAPin, int encBPin)
A PID Motor class using FreeRTOS threads, ESP32Encoder and ESP32PWM.
Definition: Motor.cpp:126
void setSetpointWithTime(float newTargetInDegrees, long miliseconds, interpolateMode mode)
Definition: Motor.cpp:150
virtual ~Motor()
Definition: Motor.cpp:136
void setSetpointWithLinearInterpolation(float newTargetInDegrees, long miliseconds)
Definition: Motor.h:380
interpolateMode mode
Definition: Motor.h:140
float targetEffort
Definition: Motor.h:112
float setpoint
Definition: Motor.h:74
Definition: Motor.h:27
void setGainsI(float i)
Definition: Motor.cpp:439
float kD
Definition: Motor.h:86
void setSpeed(float newDegreesPerSecond)
Definition: Motor.cpp:295
void setGainsP(float p)
Definition: Motor.cpp:431
float currentEffort
Definition: Motor.h:116
float duration
Definition: Motor.h:120
float runntingITerm
Definition: Motor.h:90
void setGainsD(float d)
Definition: Motor.cpp:447
const float DELTA_EFFORT
Definition: Motor.h:20
void attach()
Attach the motors hardware.
Definition: Motor.cpp:456
float getGainsP()
Definition: Motor.h:455
int directionFlag
Definition: Motor.h:173
void setGains(float p, float i, float d)
Definition: Motor.cpp:418
void setEffort(float effort)
Definition: Motor.cpp:495
int interruptCountForVelocity
Definition: Motor.h:62
#define MAX_POSSIBLE_MOTORS
Definition: Motor.h:13
bool closedLoopControl
Definition: Motor.h:108
float kI
Definition: Motor.h:82
interpolateMode
Definition: Motor.h:22
float endSetpoint
Definition: Motor.h:128
float getCurrentDegrees()
Definition: Motor.cpp:568
float BEZIER_P0
BEZIER Control Point 0.
Definition: Motor.h:152
float startSetpoint
Definition: Motor.h:132
float getEffortPercent()
Definition: Motor.h:265
float milisecondPosIncrementForVelocity
Definition: Motor.h:146
void moveTo(float newTargetInDegrees, float speedDegPerSec)
Definition: Motor.cpp:182
float TRAPEZOIDAL_time
the amount of time to ramp up and ramp down the speed
Definition: Motor.h:163
A PID Motor class using FreeRTOS threads, ESP32Encoder and ESP32PWM.
Definition: Motor.h:43
float cachedSpeed
Definition: Motor.h:70
void loop()
Definition: Motor.cpp:347
static Motor * list[MAX_POSSIBLE_MOTORS]
Definition: Motor.h:200
float BEZIER_P1
BEZIER Control Point 1.
Definition: Motor.h:158
ESP32PWM * pwm
Definition: Motor.h:49
bool isMotorDoneWithMove()
Check to see if the motor is done with a move.
Definition: Motor.cpp:213
static bool timersAllocated
Definition: Motor.h:194
void setSetpointWithTrapezoidalInterpolation(float newTargetInDegrees, long miliseconds, float trapazoidalTime)
Definition: Motor.h:433
ESP32Encoder * encoder
Definition: Motor.h:53
static void allocateTimer(int PWMgenerationTimer)
Definition: Motor.cpp:115
void setSetpointWithBezierInterpolation(float newTargetInDegrees, long miliseconds, float Control_0=0.5, float Control_1=1.0)
Definition: Motor.h:413
float getDegreesPerSecond()
Definition: Motor.cpp:551
void moveFor(float deltaTargetInDegrees, float speedDegPerSec)
Definition: Motor.cpp:269
float getGainsD()
Definition: Motor.h:467
void setSetpointWithSinusoidalInterpolation(float newTargetInDegrees, long miliseconds)
Definition: Motor.h:396
int MotorPWMPin
Definition: Motor.h:169
void blockUntilMoveIsDone()
wait for the motor to arrive at a setpoint
Definition: Motor.cpp:249
float unitDuration
Definition: Motor.h:136
bool isAttached
Definition: Motor.h:57
float getInterpolationUnitIncrement()
Definition: Motor.cpp:32
float startTime
Definition: Motor.h:124
int64_t nowEncoder
Definition: Motor.h:190