Skip to content

Commit 431f766

Browse files
it-klingerjic23
authored andcommitted
iio: srf04: fix wrong limitation in distance measuring
The measured time value in the driver is limited to the maximum distance which can be read by the sensor. This limitation was wrong and is fixed by this patch. It also takes into account that we are supporting a variety of sensors today and that the recently added sensors have a higher maximum distance range. Changes in v2: - Added a Tested-by Suggested-by: Zbyněk Kocur <[email protected]> Tested-by: Zbyněk Kocur <[email protected]> Signed-off-by: Andreas Klinger <[email protected]> Cc:<[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 24e1eb5 commit 431f766

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

drivers/iio/proximity/srf04.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ static int srf04_read(struct srf04_data *data)
110110
udelay(data->cfg->trigger_pulse_us);
111111
gpiod_set_value(data->gpiod_trig, 0);
112112

113-
/* it cannot take more than 20 ms */
113+
/* it should not take more than 20 ms until echo is rising */
114114
ret = wait_for_completion_killable_timeout(&data->rising, HZ/50);
115115
if (ret < 0) {
116116
mutex_unlock(&data->lock);
@@ -120,7 +120,8 @@ static int srf04_read(struct srf04_data *data)
120120
return -ETIMEDOUT;
121121
}
122122

123-
ret = wait_for_completion_killable_timeout(&data->falling, HZ/50);
123+
/* it cannot take more than 50 ms until echo is falling */
124+
ret = wait_for_completion_killable_timeout(&data->falling, HZ/20);
124125
if (ret < 0) {
125126
mutex_unlock(&data->lock);
126127
return ret;
@@ -135,19 +136,19 @@ static int srf04_read(struct srf04_data *data)
135136

136137
dt_ns = ktime_to_ns(ktime_dt);
137138
/*
138-
* measuring more than 3 meters is beyond the capabilities of
139-
* the sensor
139+
* measuring more than 6,45 meters is beyond the capabilities of
140+
* the supported sensors
140141
* ==> filter out invalid results for not measuring echos of
141142
* another us sensor
142143
*
143144
* formula:
144-
* distance 3 m
145-
* time = ---------- = --------- = 9404389 ns
146-
* speed 319 m/s
145+
* distance 6,45 * 2 m
146+
* time = ---------- = ------------ = 40438871 ns
147+
* speed 319 m/s
147148
*
148149
* using a minimum speed at -20 °C of 319 m/s
149150
*/
150-
if (dt_ns > 9404389)
151+
if (dt_ns > 40438871)
151152
return -EIO;
152153

153154
time_ns = dt_ns;
@@ -159,20 +160,20 @@ static int srf04_read(struct srf04_data *data)
159160
* with Temp in °C
160161
* and speed in m/s
161162
*
162-
* use 343 m/s as ultrasonic speed at 20 °C here in absence of the
163+
* use 343,5 m/s as ultrasonic speed at 20 °C here in absence of the
163164
* temperature
164165
*
165166
* therefore:
166-
* time 343
167-
* distance = ------ * -----
168-
* 10^6 2
167+
* time 343,5 time * 106
168+
* distance = ------ * ------- = ------------
169+
* 10^6 2 617176
169170
* with time in ns
170171
* and distance in mm (one way)
171172
*
172-
* because we limit to 3 meters the multiplication with 343 just
173+
* because we limit to 6,45 meters the multiplication with 106 just
173174
* fits into 32 bit
174175
*/
175-
distance_mm = time_ns * 343 / 2000000;
176+
distance_mm = time_ns * 106 / 617176;
176177

177178
return distance_mm;
178179
}

0 commit comments

Comments
 (0)