-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotorDriver.h
63 lines (52 loc) · 1.3 KB
/
MotorDriver.h
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
#ifndef __MOTORDRIVER__
#define __MOTORDRIVER__
#include "PWM.h"
class MotorDriver {
public:
MotorDriver(int forward_pin, int reverse_pin, int pwm_pin)
{
_forwardPin = forward_pin;
_reversePin = reverse_pin;
_pwmPin = pwm_pin;
}
void begin()
{
pwmMode(_pwmPin, 25000, 10);
pinMode(_forwardPin, OUTPUT);
pinMode(_reversePin, OUTPUT);
}
void go(int32_t duty) {
if (duty < 0) {
digitalWrite(_forwardPin, LOW);
digitalWrite(_reversePin, HIGH);
duty = -duty;
} else {
digitalWrite(_forwardPin, HIGH);
digitalWrite(_reversePin, LOW);
}
pwmWrite(_pwmPin, duty);
}
void forward(uint32_t duty) {
digitalWrite(_forwardPin, HIGH);
digitalWrite(_reversePin, LOW);
pwmWrite(_pwmPin, duty);
}
void reverse(uint32_t duty) {
digitalWrite(_forwardPin, LOW);
digitalWrite(_reversePin, HIGH);
pwmWrite(_pwmPin, duty);
}
void stop(void) {
digitalWrite(_forwardPin, LOW);
digitalWrite(_reversePin, LOW);
pwmWrite(_pwmPin, 0);
}
void brake(void) {
digitalWrite(_forwardPin, HIGH);
digitalWrite(_reversePin, HIGH);
pwmWrite(_pwmPin, 0);
}
private:
int _forwardPin, _reversePin, _pwmPin;
};
#endif