forked from AvazAlimov/self-driving-car
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MotorManager.cpp
111 lines (96 loc) · 2.35 KB
/
MotorManager.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
102
103
104
105
106
107
108
109
110
111
#include "MotorManager.hpp"
// Initialize motor pins
MotorManager::MotorManager(
int minSpeed,
int maxSpeed,
int speed,
int rotateSpeed
)
{
// Exit app if setup failed
if (wiringPiSetup() == -1) exit(-1);
mMinSpeed = minSpeed;
mMaxSpeed = maxSpeed;
mSpeed = speed;
mRotateSpeed = rotateSpeed;
pinMode(IN1_PIN, SOFT_PWM_OUTPUT);
pinMode(IN2_PIN, SOFT_PWM_OUTPUT);
pinMode(IN3_PIN, SOFT_PWM_OUTPUT);
pinMode(IN4_PIN, SOFT_PWM_OUTPUT);
softPwmCreate(IN1_PIN, mMinSpeed, mMaxSpeed);
softPwmCreate(IN2_PIN, mMinSpeed, mMaxSpeed);
softPwmCreate(IN3_PIN, mMinSpeed, mMaxSpeed);
softPwmCreate(IN4_PIN, mMinSpeed, mMaxSpeed);
}
// Release motor pins
MotorManager::~MotorManager()
{
softPwmWrite(IN1_PIN, mMinSpeed);
softPwmWrite(IN2_PIN, mMinSpeed);
softPwmWrite(IN3_PIN, mMinSpeed);
softPwmWrite(IN4_PIN, mMinSpeed);
pinMode(IN1_PIN, INPUT);
pinMode(IN2_PIN, INPUT);
pinMode(IN3_PIN, INPUT);
pinMode(IN4_PIN, INPUT);
}
void MotorManager::setSpeed(int speed)
{
mSpeed = speed;
}
// Go forward with delay
void MotorManager::goForward()
{
softPwmWrite(IN1_PIN, mSpeed);
softPwmWrite(IN2_PIN, mMinSpeed);
softPwmWrite(IN3_PIN, mSpeed);
softPwmWrite(IN4_PIN, mMinSpeed);
}
// Go backward with delay
void MotorManager::goBackward()
{
softPwmWrite(IN1_PIN, mMinSpeed);
softPwmWrite(IN2_PIN, mSpeed);
softPwmWrite(IN3_PIN, mMinSpeed);
softPwmWrite(IN4_PIN, mSpeed);
}
// Turn right at the point with delay
void MotorManager::turnRight()
{
softPwmWrite(IN1_PIN, mSpeed);
softPwmWrite(IN2_PIN, mMinSpeed);
softPwmWrite(IN3_PIN, mMinSpeed);
softPwmWrite(IN4_PIN, mSpeed);
}
// Turn left at the point with delay
void MotorManager::turnLeft()
{
softPwmWrite(IN1_PIN, mMinSpeed);
softPwmWrite(IN2_PIN, mSpeed);
softPwmWrite(IN3_PIN, mSpeed);
softPwmWrite(IN4_PIN, mMinSpeed);
}
// Turn right with delay
void MotorManager::goRight()
{
softPwmWrite(IN1_PIN, mRotateSpeed);
softPwmWrite(IN2_PIN, mMinSpeed);
softPwmWrite(IN3_PIN, mMinSpeed);
softPwmWrite(IN4_PIN, mMinSpeed);
}
// Turn left with delay
void MotorManager::goLeft()
{
softPwmWrite(IN1_PIN, mMinSpeed);
softPwmWrite(IN2_PIN, mMinSpeed);
softPwmWrite(IN3_PIN, mRotateSpeed);
softPwmWrite(IN4_PIN, mMinSpeed);
}
// Stop motors
void MotorManager::stop()
{
softPwmWrite(IN1_PIN, mMinSpeed);
softPwmWrite(IN2_PIN, mMinSpeed);
softPwmWrite(IN3_PIN, mMinSpeed);
softPwmWrite(IN4_PIN, mMinSpeed);
}