-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMotor.h
84 lines (66 loc) · 1.66 KB
/
Motor.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef _ACROBOT_MOTOR_H
#define _ACROBOT_MOTOR_H
#include "Utils.h"
namespace ACRobot {
static const int16_t MAX_POWER = 255;
static const int16_t MIN_POWER = -MAX_POWER;
class MotorInterface: public PollingInterface
{
public:
MotorInterface(): _power(0) {}
void setPower(int16_t power) {
if (power > MAX_POWER)
power = MAX_POWER;
if (power < MIN_POWER)
power = MIN_POWER;
set(power);
}
protected:
virtual int16_t& set(int16_t power) { return _power = power; };
int16_t _power;
};
class DCMotor: public MotorInterface
{
public:
DCMotor(uint8_t directPin, uint8_t pwmPin): _new_power(0), _directPin(directPin), _pwmPin(pwmPin)
{
pinMode(directPin, OUTPUT);
pinMode(pwmPin, OUTPUT);
clearDigitalPin(directPin);
clearDigitalPin(pwmPin);
}
const int16_t& operator= (int16_t power) {
setPower(power);
return _new_power;
}
operator int16_t const () { return _power; }
bool poll();
protected:
int16_t& set(int16_t power) { return _new_power = power; };
private:
int16_t _new_power;
uint8_t _directPin;
uint8_t _pwmPin;
};
class DCMotorBreak: public DCMotor
{
public:
DCMotorBreak(uint8_t directPin, uint8_t pwmPin, uint8_t breakPin):
DCMotor(directPin, pwmPin), _breakPin(breakPin)
{
pinMode(breakPin, OUTPUT);
go();
}
const int16_t& operator= (int16_t power) { return set(power); };
operator int16_t const () { return _power; }
void stop() {
setDigitalPin(_breakPin);
}
void go() {
clearDigitalPin(_breakPin);
}
private:
uint8_t _breakPin;
};
} // ACRobot namespace
#endif