-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdetector.cpp
More file actions
139 lines (110 loc) · 3.76 KB
/
Copy pathdetector.cpp
File metadata and controls
139 lines (110 loc) · 3.76 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <iostream>
#include <SDL_audio.h>
#include "detector.hpp"
#include "detector_fsm.hpp"
const double Detector::ft_per_rev = 23.0;
Detector::Detector() :
fsm_{std::make_unique<DetectorFSM>([this] { dip_callback(); })},
data_{Data{0, 0.0, std::chrono::steady_clock::now()}} {}
Detector::~Detector() {
stop();
}
void Detector::start(int audio_device) {
if (not running_) {
audio_device_ = audio_device;
running_ = true;
thread_ = std::thread(&Detector::run, this);
}
}
void Detector::stop() {
if (running_) {
running_ = false;
thread_.join();
}
}
bool Detector::is_running() {
return running_;
}
void Detector::run() {
// Unstable past 30mph @ buffer_size = 2048
constexpr auto buffer_size = 1024;
constexpr auto max_queued = 1024 * 10;
auto desired_spec = SDL_AudioSpec{};
desired_spec.freq = 96000;
desired_spec.samples = max_queued;
desired_spec.format = AUDIO_S16;
desired_spec.channels = 1;
desired_spec.callback = nullptr;
auto spec = SDL_AudioSpec{};
auto device = SDL_OpenAudioDevice(SDL_GetAudioDeviceName(audio_device_, 1), 1, &desired_spec, &spec, 0);
if (device == 0) {
std::cout << SDL_GetError() << std::endl;
return;
}
std::array<int16_t, buffer_size> buffer{};
SDL_PauseAudioDevice(device, 0); // Start capture
while (running_) {
if (SDL_GetQueuedAudioSize(device) < buffer_size * 2) {
// Sleep for ~10ms (960 samples)
std::this_thread::sleep_for(std::chrono::milliseconds{10});
continue;
} else if (SDL_GetQueuedAudioSize(device) > max_queued * 2) {
std::cout << "not handling audio fast enough" << std::endl;
SDL_ClearQueuedAudio(device);
continue;
}
auto ret = SDL_DequeueAudio(device, buffer.data(), buffer_size * 2);
if (ret < buffer_size * 2) {
std::cout << "Not enough bytes: " << SDL_GetError() << std::endl;
continue;
}
int n = 0;
auto buffer_mean = 0.0;
auto buffer_mean_sq_diff = 0.0;
for (auto&& sample : buffer) {
n++;
auto delta = sample - buffer_mean;
buffer_mean += delta / n;
auto delta2 = sample - buffer_mean;
buffer_mean_sq_diff += delta * delta2;
}
auto buffer_variance = buffer_mean_sq_diff / (n - 1);
auto buffer_stdev = std::sqrt(buffer_variance);
{
std::lock_guard<std::mutex> lk(mut_);
fsm_->sm.process_event(FSM::reading{buffer_stdev});
}
}
SDL_CloseAudioDevice(device);
}
Detector::Data Detector::get_data() {
return data_;
}
bool Detector::is_calibrating() {
std::lock_guard<std::mutex> lk(mut_);
return fsm_->sm.is(FSM::Calibrating);
}
void Detector::dip_callback() {
auto time = std::chrono::steady_clock::now();
auto data = data_.load();
auto new_data = Data{++data.count, 0.0, time};
if (data.count > 0) {
using Hours = std::chrono::duration<double, std::ratio<3600>>;
auto delta_t = time - data.time;
new_data.velocity_mph = ft_per_rev / 5280.0 * 1.0 / std::chrono::duration_cast<Hours>(delta_t).count();
}
data_ = new_data;
}
std::chrono::steady_clock::duration Detector::Data::age() const {
return std::chrono::steady_clock::now() - time;
}
double Detector::Data::instantaneous_velocity_mph() const {
using Hours = std::chrono::duration<double, std::ratio<3600>>;
auto possible_velocity_mph =
Detector::ft_per_rev / 5280.0 * 1.0 / std::chrono::duration_cast<Hours>(age()).count();
return std::min(velocity_mph, possible_velocity_mph);
}