forked from zeroflag/punyforth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathping.forth
More file actions
29 lines (26 loc) · 1.05 KB
/
Copy pathping.forth
File metadata and controls
29 lines (26 loc) · 1.05 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
: emit-pulse ( trigger-pin -- )
dup GPIO_OUT gpio-mode
dup GPIO_LOW gpio-write
2 us
dup GPIO_HIGH gpio-write
10 us
GPIO_LOW gpio-write ;
: listen-echo ( echo-pin timeout-us -- ms )
over GPIO_IN gpio-mode
GPIO_HIGH rot pulse-len ;
\ Measures the pulse generated by ultrasonic ranging module (tested with: HC-SR04 sensors)
\ Works the following way:
\ (1) Using IO trigger for at least 10us high level signal,
\ (2) The Module automatically sends eight 40 kHz and detect whether there is a pulse signal back.
\ (3) IF the signal back, time of high output IO duration is the time from sending ultrasonic to returning.
\ Distance = (high level time×velocity of sound (340M/S) / 2,
\ Usage example: PIN_ECHO 100 timeout>cm PIN_TRIGGER ping range>cm
: ping ( echo-pin timeout-us trigger-pin -- pulse-duration-us )
os-enter-critical
{ emit-pulse listen-echo } catch
os-exit-critical
throw ;
: timeout>cm ( us -- cm ) 60 * ;
: timeout>inch ( us -- cm ) 150 * ;
: range>cm ( us -- cm ) 58 / ;
: range>inch ( us -- inch ) 148 / ;