-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIFRC.cpp
More file actions
85 lines (80 loc) · 2.59 KB
/
IFRC.cpp
File metadata and controls
85 lines (80 loc) · 2.59 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
#include "IFRC.h"
IFRC::IFRC(): _motoresEsquerda(P_MA_ESQ, P_MB_ESQ, P_MV_ESQ), _motoresDireita(P_MA_DIR, P_MB_DIR, P_MV_DIR){ }
void IFRC::testarMotores() {
Serial.println("Iniciando teste de motores, verifique se estao funcionando corretamente.");
delay(1000);
this->_motoresEsquerda.mover(V_MAX); // Roda os motores com o maximo de potencia
this->_motoresDireita.mover(V_MAX);
delay(1000);
this->_motoresEsquerda.parar();
this->_motoresDireita.parar();
}
void IFRC::processarComandos() {
if(Serial.available()) {
this->_requisicao = Serial.readString();
switch(this->_requisicao.charAt(0)) {
case 'T': // teste de conexao
Serial.println("1");
break;
case 'V': // velocidade media
velocidadeMedia();
break;
case 'A': // velocidade uniformemente variada
velocidadeVariada();
break;
default:
Serial.println("$Comando nao reconhecido.");
break;
}
}
}
void IFRC::velocidadeMedia() {
// Formato da requisicao = "Vtt,vv" onde
// t = tempo em segundos, v = velocidade em centimetros por segundo
uint8_t i = 0, r = 0;
float t = this->_requisicao.substring(1,3).toFloat();
float v = this->_requisicao.substring(4).toFloat();
r = this->_motoresEsquerda.mover(v);
if (r > 0) {
this->_motoresDireita.mover(v);
Serial.print(v);
Serial.print(",");
Serial.println((float)i);
while(i < t) {
i++;
delay(1000);
Serial.print(v);
Serial.print(",");
Serial.println((float)i);
}
}
this->_motoresEsquerda.parar();
this->_motoresDireita.parar();
}
void IFRC::velocidadeVariada() {
// Formato da requisicao = Att,vv,aa onde
// t = tempo em segundos, v = velocidade inicial em centimetros por segundo,
// a = aceleracao em centimetros por segundo ao quadrado
uint8_t r = 0;
float vAtual = 0, tAtualF = 0, sensibilidade = 100;
uint16_t tAtual = 0, t = this->_requisicao.substring(1,3).toInt() * 1000;
uint8_t vInicial = this->_requisicao.substring(4,6).toInt();
uint8_t a = this->_requisicao.substring(7).toInt();
r = this->_motoresEsquerda.mover(vInicial);
if (r > 0) {
for(tAtual = 0; tAtual <= t; tAtual += sensibilidade) {
tAtualF = (float)tAtual / 1000;
vAtual = vInicial + (a * tAtualF); // V = Vo + at
this->_motoresEsquerda.mover(vAtual);
this->_motoresDireita.mover(vAtual);
if(tAtual % 1000 == 0) {
Serial.print(vAtual);
Serial.print(",");
Serial.println(tAtualF);
}
delay(sensibilidade);
}
}
this->_motoresEsquerda.parar();
this->_motoresDireita.parar();
}