-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
79 lines (63 loc) · 1.85 KB
/
Copy pathmain.cpp
File metadata and controls
79 lines (63 loc) · 1.85 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
#include <Arduino.h>
#include "DAVIS6410.hpp"
// ------------ DEFINICION DE CONSTANTES ------------
// Definicion de pines
const uint8_t vvPin = 23; // Negro
const uint8_t dvPin = 34; // Verde
// Definicion de tiempos
const uint16_t TMedicion = 1000;
// ------------ DEFINICION DE VARIABLES ------------
unsigned long tActual = 0; // Tiempo actual de millis()
unsigned long tPrevio = 0;
uint8_t sampleTime = 1;
uint16_t dvOffset = 0;
// ------------ DEFINICION DE INTERRUPCIONES ------------
volatile int count = 0;
volatile int freq;
hw_timer_t * timer = NULL;
portMUX_TYPE synch = portMUX_INITIALIZER_UNLOCKED;
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
// Función de interrupción
void IRAM_ATTR isr() {
portENTER_CRITICAL(&synch);
count++; // Cada vez que se llama a la función el contador se incrementa
portEXIT_CRITICAL(&synch);
}
// Función de interrupción de timer
void IRAM_ATTR onTimer() {
portENTER_CRITICAL_ISR(&timerMux);
freq = count; // Se almacena el valor de la frecuencia
count = 0; // Se reinicia el contador.
portEXIT_CRITICAL_ISR(&timerMux);
}
// ------------ SETUP ------------
void setup() {
// DEBUGGING
Serial.begin(115200);
// PINES
pinMode(vvPin, INPUT);
pinMode(dvPin, INPUT);
// SETUP DE INTERRUPCIONES
attachInterrupt(vvPin, isr, FALLING);
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &onTimer, true);
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);
}
// ------------ LOOP ------------
void loop() {
double vel;
uint16_t dir;
tActual = millis();
if (tActual > tPrevio + TMedicion) {
vel = getSpeed(freq, sampleTime);
dir = getDirection(dvPin, dvOffset);
Serial.print("Vel. [m/s]\tDir.[°]\n");
Serial.print(vel);
Serial.print("\t");
Serial.print(dir);
Serial.print("\n");
calcularDireccion(dir);
tPrevio = tActual;
}
}