-
Notifications
You must be signed in to change notification settings - Fork 5
/
rak12007_us.cpp
150 lines (137 loc) · 4.39 KB
/
rak12007_us.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
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
/**
* @file rak12007_us.cpp
* @author Bernd Giesecke ([email protected])
* @brief Initialization and reading of RAK12007 US sensor
* @version 0.1
* @date 2023-12-18
*
* @copyright Copyright (c) 2023
*
*/
#include "app.h"
#define TIME_OUT 24125 // max measure distance is 4m,the velocity of sound is 331.6m/s in 0℃,TIME_OUT=4*2/331.6*1000000=24215us
float ratio = 346.6 / 1000 / 2; // velocity of sound =331.6+0.6*25℃(m/s),(Indoor temperature about 25℃)
bool has_rak12007 = false;
bool init_rak12007(void)
{
// Initialize GPIO
pinMode(ECHO, INPUT); // Echo Pin of Ultrasonic Sensor is an Input
pinMode(TRIG, OUTPUT); // Trigger Pin of Ultrasonic Sensor is an Output
pinMode(PD, OUTPUT); // power done control pin is an Output
digitalWrite(TRIG, LOW);
digitalWrite(PD, LOW); // Power up the sensor
delay(500);
has_rak12007 = read_rak12007(false);
if (!has_rak12007)
{
MYLOG("US", "Timeout on first measurement, assuming no sensor");
}
digitalWrite(PD, HIGH); // Power down the sensor
return has_rak12007;
}
bool read_rak12007(bool add_payload)
{
uint32_t respond_time;
uint64_t measure_time = 0;
uint32_t distance = 0;
uint16_t valid_measures = 0;
digitalWrite(PD, LOW); // Power up the sensor
digitalWrite(TRIG, LOW);
delay(500);
// Do 10 measurements to get an average
for (int reading = 0; reading < 10; reading++)
{
digitalWrite(TRIG, HIGH);
delayMicroseconds(20); // pull high time need over 10us
digitalWrite(TRIG, LOW);
respond_time = pulseInLong(ECHO, HIGH, 2000000L); // microseconds
delay(33);
if ((respond_time > 0) && (respond_time < TIME_OUT)) // ECHO pin max timeout is 33000us according it's datasheet
{
#ifdef _VARIANT_RAK4630_
measure_time += respond_time * 0.7726; // Time calibration factor is 0.7726,to get real time microseconds for 4631 board
#else
measure_time += respond_time; // No time calibration
#endif
valid_measures++;
}
else
{
MYLOG("US", "Timeout");
}
MYLOG("US", "Respond time is %d valid measures %d", respond_time, valid_measures);
delay(500);
}
measure_time = measure_time / valid_measures;
if ((measure_time > 0) && (measure_time < TIME_OUT)) // ECHO pin max timeout is 33000us according it's datasheet
{
// Calculate measured distance
distance = measure_time * ratio; // Test distance = (high level time × velocity of sound (340M/S) / 2
bool overflow = false;
bool low_level = false;
// Check for low level
if (distance > g_custom_parameters.tank_depth_mm / 3 * 2) // 2/3rd of tank depth
{
low_level = true;
}
// Check for overflow
if (distance < 50)
{
overflow = true;
}
MYLOG("US", "Distance is %ld mm", distance);
// Calculate level from measured Distance
uint16_t level = g_custom_parameters.tank_depth_mm - distance;
MYLOG("US", "Waterlevel is %ld mm", level);
if (add_payload)
{
// Add level to the payload (in cm !)
g_solution_data.addAnalogInput(LPP_CHANNEL_WLEVEL, (float)(level / 10.0));
#if MY_DEBUG == 1
MYLOG("PAYL", "After adding water level");
uint8_t *payload_temp = g_solution_data.getBuffer();
for (int i = 0; i < g_solution_data.getSize(); i++)
{
Serial.printf("%02X", payload_temp[i]);
}
Serial.print("\r\n");
#endif
g_solution_data.addPresence(LPP_CHANNEL_WL_LOW, low_level);
#if MY_DEBUG == 1
MYLOG("PAYL", "After adding low level");
payload_temp = g_solution_data.getBuffer();
for (int i = 0; i < g_solution_data.getSize(); i++)
{
Serial.printf("%02X", payload_temp[i]);
}
Serial.print("\r\n");
#endif
g_solution_data.addPresence(LPP_CHANNEL_WL_HIGH, overflow);
#if MY_DEBUG == 1
MYLOG("PAYL", "After adding overflow");
payload_temp = g_solution_data.getBuffer();
for (int i = 0; i < g_solution_data.getSize(); i++)
{
Serial.printf("%02X", payload_temp[i]);
}
Serial.print("\r\n");
#endif
}
digitalWrite(PD, HIGH); // Power down the sensor
digitalWrite(TRIG, HIGH);
return true;
}
else
{
if (add_payload)
{
// Report an error with both overflow and low level alert set
g_solution_data.addAnalogInput(LPP_CHANNEL_WLEVEL, 0.0);
g_solution_data.addPresence(LPP_CHANNEL_WL_LOW, true);
g_solution_data.addPresence(LPP_CHANNEL_WL_HIGH, true);
}
}
digitalWrite(PD, HIGH); // Power down the sensor
digitalWrite(TRIG, HIGH);
return false;
}