-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoboHeartDRV8836.cpp
101 lines (76 loc) · 2.44 KB
/
RoboHeartDRV8836.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
96
97
98
99
100
101
/*!
* @file RoboHeartDRV8836.cpp
*
* Arduino library for the RoboHeart.
*
*/
#include "RoboHeartDRV8836.h"
#define FILE_IDENTIFIER \
"DRV8836" // Define identifier before including DebuggerMsgs.h
#include "DebuggerMsgs.h"
int clampSpeed(int speed, int minSpeed, int maxSpeed) {
if (speed < minSpeed) {
return minSpeed;
} else if (speed > maxSpeed) {
return maxSpeed;
} else {
return speed;
}
}
RoboHeartDRV8836::RoboHeartDRV8836() {}
RoboHeartDRV8836::RoboHeartDRV8836(Stream& debug) : _debug(&debug) {}
RoboHeartDRV8836::~RoboHeartDRV8836() {}
void RoboHeartDRV8836::begin(int in1Pin, int in2Pin, int nsleepPin,
int in1Channel, int in2Channel) {
_nSleepPin = nsleepPin;
_in1Pin = in1Pin;
_in2Pin = in2Pin;
_in1Channel = in1Channel;
_in2Channel = in2Channel;
// Configure the pins
pinMode(_in1Pin, OUTPUT);
pinMode(_in2Pin, OUTPUT);
// Calling only once the channels are set
configPWM();
// IN/IN MODE BY DEFAULT, IT SUPPORTS COASTING
pinMode(_nSleepPin, OUTPUT); // nSLEEP
digitalWrite(_nSleepPin, HIGH);
}
void RoboHeartDRV8836::configPWM(int freq, int resolution) {
RETURN_WARN_IF_EQUAL(_in1Pin, -1)
_pwmFreq = freq;
_pwmResolution = resolution;
ledcAttachChannel(_in1Pin, _pwmFreq, _pwmResolution, _in1Channel);
ledcAttachChannel(_in2Pin, _pwmFreq, _pwmResolution, _in2Channel);
_pwmMaxDutyCycle = (int)(pow(2, _pwmResolution) - 1);
}
void RoboHeartDRV8836::sleep(bool sleep) {
RETURN_WARN_IF_EQUAL(_in1Pin, -1)
ledcWrite(_in1Pin, 0);
ledcWrite(_in2Pin, 0);
digitalWrite(_nSleepPin, !sleep);
}
void RoboHeartDRV8836::coast() {
RETURN_WARN_IF_EQUAL(_in1Pin, -1)
ledcWrite(_in1Pin, 0);
ledcWrite(_in2Pin, 0);
}
void RoboHeartDRV8836::forward(int speed) {
RETURN_WARN_IF_EQUAL(_in1Pin, -1)
_speed = clampSpeed(speed, 0, _pwmMaxDutyCycle);
ledcWrite(_in1Pin, _speed);
ledcWrite(_in2Pin, 0);
}
void RoboHeartDRV8836::reverse(int speed) {
RETURN_WARN_IF_EQUAL(_in1Pin, -1)
_speed = clampSpeed(speed, 0, _pwmMaxDutyCycle);
ledcWrite(_in1Pin, 0);
ledcWrite(_in2Pin, _speed);
}
void RoboHeartDRV8836::brake() {
RETURN_WARN_IF_EQUAL(_in1Pin, -1)
ledcWrite(_in1Pin, _pwmMaxDutyCycle);
ledcWrite(_in2Pin, _pwmMaxDutyCycle);
}
int RoboHeartDRV8836::getSpeed() { return _speed; }
int RoboHeartDRV8836::getMaxDutyCycle() { return _pwmMaxDutyCycle; }