forked from Pitt-RAS/micromouse-2016
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRangeSensor.cpp
84 lines (65 loc) · 1.99 KB
/
RangeSensor.cpp
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
#include "Arduino.h"
#include "conf.h"
#include "RangeSensor.h"
RangeSensor::RangeSensor(int temp_pin, int lowT, int highT) {
pin_ = temp_pin;
low_threshold_ = lowT;
high_threshold_ = highT;
memset(raw_queue_, 0, sizeof(raw_queue_));
memset(history_queue_, 0, sizeof(history_queue_));
}
void RangeSensor::refreshRawQueue() {
for (int i = 0; i < RANGE_QUEUE_NUM_TO_CLEAR; i++) {
raw_queue_sum_ -= raw_queue_[(raw_queue_index_+ i) % RANGE_QUEUE_MAX_LENGTH];
raw_queue_[raw_queue_index_] = 0;
raw_queue_length_--;
}
}
void RangeSensor::clearRawQueue() {
raw_queue_index_ = 0;
raw_queue_sum_ = 0;
raw_queue_length_ = 0;
memset(raw_queue_, 0, sizeof(raw_queue_));
}
void RangeSensor::clearHistory() {
history_queue_index_ = 0;
memset(history_queue_, 0, sizeof(history_queue_));
}
//Update Raw Range Queue so ranges are accurate later when needed by higher level
void RangeSensor::updateRange() {
raw_queue_sum_ -= raw_queue_[raw_queue_index_];
raw_queue_[raw_queue_index_] = (int) 20760 / (analogRead(pin_) - 11);
raw_queue_sum_ += raw_queue_[raw_queue_index_];
raw_queue_index_ = (raw_queue_index_ + 1) % RANGE_QUEUE_MAX_LENGTH;
if(raw_queue_length_ != RANGE_QUEUE_MAX_LENGTH) {
raw_queue_length_++;
}
}
//Force to take new reading and adjust Queue
int RangeSensor::getRange() {
int raw_queue_average_ = raw_queue_sum_ / raw_queue_length_;
history_queue_[history_queue_index_] = raw_queue_average_;
history_queue_index_ = (history_queue_index_ + 1) % HISTORY_QUEUE_MAX_LENGTH ;
if(sawWall) {
sawWall = raw_queue_average_ < high_threshold_;
}
else {
sawWall = raw_queue_average_ < low_threshold_;
}
return raw_queue_average_;
}
int RangeSensor::getRange(int index) {
if(!index) {
return getRange();
}
if(history_queue_index_-index >= 0) {
return history_queue_[history_queue_index_-index];
}
else {
return history_queue_[HISTORY_QUEUE_MAX_LENGTH+history_queue_index_-index];
}
}
bool RangeSensor::isWall() {
getRange();
return sawWall;
}