-
Notifications
You must be signed in to change notification settings - Fork 1
/
MotorController.cpp
95 lines (85 loc) · 2.43 KB
/
MotorController.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <Arduino.h>
#include "MotorController.h"
MotorController::MotorController(unsigned int forwardsPin, unsigned int backwardsPin, bool pin1IsDirection){
this->forwardPin = forwardsPin;
this->backwardPin = backwardsPin;
pinMode(forwardPin, OUTPUT);
pinMode(backwardPin, OUTPUT);
pin1Direction = pin1IsDirection;
enabledRampRate = false;
}
MotorController::MotorController(unsigned int forwardPin, unsigned int backwardsPin, int enablePin)
: MotorController::MotorController(forwardPin,backwardsPin, false)
{
this->enablePin = (unsigned int)enablePin;
pinMode(enablePin, OUTPUT);
}
void MotorController::setRampRate(double toSet){
rampRate = toSet;
if (rampRate <= 0.01){
rampRate = 0.01;
}
}
void MotorController::enableRampRate(){
enabledRampRate = true;
}
void MotorController::disableRampRate(){
enabledRampRate = false;
timeAtLastSet = 0;
}
double MotorController::get(){
return setValue;
}
void MotorController::set(double powerPercent){
if (powerPercent > 1) powerPercent = 1;
else if (powerPercent < -1) powerPercent = -1;
if (timeAtLastSet != 0 || !enabledRampRate){
double adjustedRamp = rampRate * (millis()-timeAtLastSet)/1000.0;
if (!enabledRampRate || abs(setValue - powerPercent) <= adjustedRamp)
setValue = powerPercent;
else if (powerPercent < setValue)
setValue -= adjustedRamp;
else if (powerPercent > setValue){
setValue += adjustedRamp;
}
if (setValue > 0){
if (enablePin >= 0){
digitalWrite(forwardPin, 1);
digitalWrite(backwardPin, 0);
}
else{
if (!pin1Direction){
analogWrite(forwardPin, PWMRANGE*setValue);
analogWrite(backwardPin, 0);
}
else{
digitalWrite(forwardPin, 1);
analogWrite(backwardPin, setValue);
}
}
}
else if (setValue < 0){
if (enablePin >= 0){
digitalWrite(forwardPin, 0);
digitalWrite(backwardPin, 1);
}
else{
if (!pin1Direction){
analogWrite(forwardPin, 0);
analogWrite(backwardPin, PWMRANGE*-setValue);
}
else{
digitalWrite(forwardPin, 0);
analogWrite(backwardPin, setValue);
}
}
}
else{
digitalWrite(forwardPin,0);
digitalWrite(backwardPin,0);
}
double setVal = PWMRANGE*setValue;
if (enablePin >= 0) analogWrite(enablePin, abs(setVal));
}
timeAtLastSet = millis();
}