-
Notifications
You must be signed in to change notification settings - Fork 1
/
ultrasonic.h
215 lines (172 loc) · 4.93 KB
/
ultrasonic.h
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#pragma once
#ifdef ULTRASONIC
//#define USE_TIMER2
#ifdef USE_TIMER2
#include <eRCaGuy_Timer2_Counter.h>
#endif
#include "sensor.h"
/* ==========PIN MAPPING==========
*
* ANALOG
*
* A0 ========== Thermistor 0 (Top)
* A1 ========== Thermistor 1 (Mid)
* A2 ========== Thermistor 2 (Bot)
*
* DIGITAL
*
* 2 ========== Xbee
* 3 ========== Xbee
* 4 ========== Digital Temp Sensors
* 5 ========== Sonar Power
* 6 ========== Sonar Echo
* 7 ========== Sonar Init
* 8 ========== Future use (Software Serial RX)
* 9 ========== Future use (Software Serial TX)
* 10 ========== SD Card
* 11 ========== SD Card
* 12 ========== SD Card
* 13 ========== SD Card
*
*
* ===============================
*/
/* ====Campbell Handshakes====
*
* "Distance\n"
* "TempTop\n"
* "TempMid\n"
* "TempBot\n"
*
* =====SensComp Connections=====
*
*
* Pin 1 - VDD
* Pin 2 - GND
* Pin 3 - Echo
* Pin 4 - not used
* Pin 5 - Init
* Pin 6 - not used
*
* ===========================
*/
// resistance at 25 degrees C
#define THERMISTORNOMINAL 10000
// temp. for nominal resistance (almost always 25 C)
#define TEMPERATURENOMINAL 25
// how many samples to take and average, more takes longer
// but is more 'smooth'
#define NUMSAMPLES 3
// The beta coefficient of the thermistor (usually 3000-4000)
#define BCOEFFICIENT 3950
// the value of the 'other' resistor as measured by multimeter
#define SERIESRESISTOR 1586
unsigned long getTime() {
#ifdef USE_TIMER2
return timer2.get_count();
#else
return millis();
#endif
}
int tempSamples[NUMSAMPLES];
float thermistor_temp(int option){
uint8_t i;
float average;
for (i=0; i< NUMSAMPLES; i++) {
tempSamples[i] = analogRead(option);
delay(10);
}
// average all the samples out
average = 0;
for (i=0; i< NUMSAMPLES; i++) {
average += tempSamples[i];
}
average /= NUMSAMPLES;
// convert the value to resistance
average = 1023 / average - 1;
average = SERIESRESISTOR / average;
float steinhart;
steinhart = average / THERMISTORNOMINAL; // (R/Ro)
steinhart = log(steinhart); // ln(R/Ro)
steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
steinhart = 1.0 / steinhart; // Invert
steinhart -= 273.15; // convert to C
return steinhart;
}
float microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29.0 / 2.0;
}
float tempCorrection(int pin, float microseconds) {
float temp = thermistor_temp(pin);
float usPerMeter = (1.0/340.0)/100.0;
float sound = 331.4 + 0.607 * temp;
return (sound * microseconds)/1000000.0;
}
float sensor_distance(int thermPin, int SonarPower, int SonarEcho, int SonarInit){
unsigned long duration0, duration1;
float duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(SonarInit, LOW);
digitalWrite(SonarPower, LOW); // currently does nothing
delay(5);
digitalWrite(SonarPower, HIGH);
delay(20);
// int i;
// for (i=0; i< NUMSAMPLES; i++) {
digitalWrite(SonarInit, HIGH);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
duration0 = getTime();
while(digitalRead(SonarEcho) == 0 && (getTime() - duration0) < 1000000.0 && getTime() > duration0){
;
}
duration1 = getTime();
digitalWrite(SonarInit, LOW);
duration = (duration1 - duration0)/2.0;
if(duration1 < duration0 || duration >= 1000000.0){
digitalWrite(SonarPower, LOW);
return 0;
}else if(duration <= 0){
digitalWrite(SonarPower, LOW);
return 0;
}
// }
// convert the time into a distance
//cm = microsecondsToCentimeters(duration);
digitalWrite(SonarPower, LOW);
cm = tempCorrection(thermPin, duration);
//Serial.print(cm);
//Serial.print(" cm");
//Serial.println();
return cm;
}
class UltrasonicSensor : public Sensor {
protected:
int thermPin, sonarPower, sonarEcho, sonarInit;
public:
UltrasonicSensor(int thermPin, int sonarPower, int sonarEcho, int sonarInit) {
this->thermPin = thermPin;
this->sonarPower = sonarPower;
this->sonarEcho = sonarEcho;
this->sonarInit = sonarInit;
#ifdef USE_TIMER2
timer2.setup();
#endif
}
int getNumberOfValues() {
return 1;
}
float getValue(int number) {
return sensor_distance(thermPin, sonarPower, sonarEcho, sonarInit);
}
String getValueName(int num) {
return "Ultrasonic Distance";
}
};
#endif