-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoveMotor.cpp
80 lines (64 loc) · 2.31 KB
/
RoveMotor.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
#include "RoveMotor.h"
void RoveMotor::configInvert(const bool invert) {
m_inverted = invert;
}
void RoveMotor::configMaxOutputs(const int16_t reverseMaxDecipercent, const int16_t forwardMaxDecipercent) {
if (reverseMaxDecipercent >= -1000 && reverseMaxDecipercent <= 0) {
m_reverseMaxDecipercent = reverseMaxDecipercent;
}
if (forwardMaxDecipercent <= 1000 && forwardMaxDecipercent >= 0) {
m_forwardMaxDecipercent = forwardMaxDecipercent;
}
}
void RoveMotor::configMinOutputs(const int16_t reverseMinDecipercent, const int16_t forwardMinDecipercent) {
if (reverseMinDecipercent >= -1000 && reverseMinDecipercent <= 0) {
m_reverseMinDecipercent = reverseMinDecipercent;
}
if (forwardMinDecipercent <= 1000 && forwardMinDecipercent >= 0) {
m_forwardMinDecipercent = forwardMinDecipercent;
}
}
void RoveMotor::configRampRate(const uint16_t maxRamp) {
m_maxRamp = maxRamp;
}
int16_t RoveMotor::lastDecipercent() const {
return m_lastDecipercent;
}
#if defined(ARDUINO)
#include "Arduino.h"
int16_t RoveMotor::applyConfigs(int16_t decipercent) const {
uint32_t timestamp = millis();
if (decipercent != 0) { // A demand of 0 always outputs 0
// Apply invert
if (m_inverted) {
decipercent = -decipercent;
}
// Apply ramp
if (m_maxRamp != 0) {
int32_t ramp = (timestamp - m_lastDriveTimestamp) * m_maxRamp/1000;
if (ramp == 0) {
return m_lastDecipercent;
}
if ((decipercent - m_lastDecipercent) > ramp) {
decipercent = m_lastDecipercent + ramp;
}
else if ((decipercent - m_lastDecipercent) < -ramp) {
decipercent = m_lastDecipercent - ramp;
}
}
// Apply outer bounds
if (decipercent < m_reverseMaxDecipercent) {
decipercent = m_reverseMaxDecipercent;
} else if (decipercent > m_forwardMaxDecipercent) {
decipercent = m_forwardMaxDecipercent;
}
}
m_lastDriveTimestamp = timestamp;
m_lastDecipercent = decipercent;
// Apply inner bounds
if (decipercent > m_reverseMinDecipercent && decipercent < m_forwardMinDecipercent) {
decipercent = 0;
}
return decipercent;
}
#endif