-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnav.ino
98 lines (82 loc) · 2.11 KB
/
nav.ino
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
#include <ArduinoJson.h>
#include "mqtt.h"
// set up the mqtt client
IPAddress server(192, 168, 0, 100);
MQTT mqtt = MQTT(server, 1883);
const int ir_pin = 2;
#define TICKS_PER_CALC (10)
volatile int ticks = 0; //_MUST_ be volatile so compiler doesn't fuck with this/optimize it out
volatile unsigned long ticks_total = 0;
volatile unsigned long last_t = 0;
volatile unsigned long last_count_delta = 0;
//function called by interrupt
void intr()
{
ticks += 1;
ticks_total += 1;
if (ticks == TICKS_PER_CALC) {
unsigned long t = millis();
last_count_delta = t - last_t;
ticks = 0;
last_t = t;
}
}
void setup()
{
Serial.begin(9600);
/*flash onboard LED for debug*/
Serial.println();
Serial.println();
Serial.println();
for (int i = 0; i < 10; i++)
{
digitalWrite(13, HIGH);
Serial.print(F("."));
if (i % 10 == 0)
{
Serial.println();
}
delay(10);
digitalWrite(13, LOW);
delay(100);
}
Serial.println();
delay(50);
pinMode(ir_pin, INPUT);
attachInterrupt(digitalPinToInterrupt(ir_pin), intr, FALLING); //have to map the pin to the actual interrupt number
mqtt.init();
mqtt.loop();
mqtt.debug("nav (" __DATE__ " " __TIME__ ")");
Serial.println("nav (" __DATE__ " " __TIME__ ")");
mqtt.loop();
}
#define INTERVAL (100)
unsigned long last_loop = 0;
float rps = 0;
void loop()
{
mqtt.loop();
unsigned long t = millis();
int delta = t - last_loop;
if (delta >= INTERVAL) {
rps = TICKS_PER_CALC / 2 / float(last_count_delta / 1000.0);
unsigned int l_ticks_total = ticks_total;
unsigned int l_last_t = last_t;
if (t - last_t >= 1000) {
// Speed timed out, probably 0
rps = 0;
}
Serial.print(rps);
Serial.print(" ");
Serial.println(rps * 60);
StaticJsonBuffer<MQTT_BUFFER_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["rps"] = rps;
root["last_t"] = l_last_t;
root["ticks"] = l_ticks_total;
root["t"] = t;
root.printTo(mqtt.stringBuffer, sizeof(mqtt.stringBuffer));
mqtt.client.publish("sensor/right/rotation", mqtt.stringBuffer);
last_loop = t;
}
}