-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMotor.cpp
More file actions
89 lines (70 loc) · 2.06 KB
/
Motor.cpp
File metadata and controls
89 lines (70 loc) · 2.06 KB
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
#include "Definitions.h"
#include "Motor.h"
#include "Arduino.h"
#define PIN_A 0
#define PIN_B 1
/*
* Tabela para as possibilidades de A e B.
* A B Resultado
* 0 0 Parado
* 0 1 Para trás
* 1 0 Para frente
* 1 1 Parado
*/
Motor MotorDireita(MOTOR_INPUT_1_PIN_A, MOTOR_INPUT_2_PIN_A, MOTOR_ENABLE_A_PWM);
Motor MotorEsquerda(MOTOR_INPUT_3_PIN_B, MOTOR_INPUT_4_PIN_B, MOTOR_ENABLE_B_PWM);
void mtSetVelocidade(int V, int MotorID) {
if (MotorID == MT_ESQUERDA) MotorEsquerda.setVelocidade(V);
else if (MotorID == MT_DIREITA) MotorDireita.setVelocidade(V);
}
void mtViraParaEsquerda() {
MotorDireita.giraFrente();
MotorEsquerda.giraTras();
}
void mtViraParaDireita() {
MotorDireita.giraTras();
MotorEsquerda.giraFrente();
}
void mtVaiParaFrente() {
MotorDireita.giraFrente();
MotorEsquerda.giraFrente();
}
void mtVaiParaTras() {
MotorDireita.giraTras();
MotorEsquerda.giraTras();
}
void mtParar() {
MotorDireita.parar();
MotorEsquerda.parar();
}
// ---------------------------------------------------- Classe Motor ----------------------------------------------------
void setupMotor() {
pinMode(MOTOR_INPUT_1_PIN_A, OUTPUT);
pinMode(MOTOR_INPUT_2_PIN_A, OUTPUT);
pinMode(MOTOR_INPUT_3_PIN_B, OUTPUT);
pinMode(MOTOR_INPUT_4_PIN_B, OUTPUT);
pinMode(MOTOR_ENABLE_A_PWM, OUTPUT);
pinMode(MOTOR_ENABLE_B_PWM, OUTPUT);
MotorDireita.setVelocidade(150);
MotorEsquerda.setVelocidade(150);
}
Motor::Motor(int PinA, int PinB, int PinPWM) {
DirectionPin[PIN_A] = PinA;
DirectionPin[PIN_B] = PinB;
PowerPin = PinPWM;
}
void Motor::parar() {
digitalWrite(DirectionPin[PIN_A], LOW);
digitalWrite(DirectionPin[PIN_B], LOW);
}
void Motor::giraFrente() {
digitalWrite(DirectionPin[PIN_A], HIGH);
digitalWrite(DirectionPin[PIN_B], LOW);
}
void Motor::giraTras() {
digitalWrite(DirectionPin[PIN_A], LOW);
digitalWrite(DirectionPin[PIN_B], HIGH);
}
void Motor::setVelocidade(int V) {
analogWrite(PowerPin, V);
}