-
Notifications
You must be signed in to change notification settings - Fork 1
/
m_srv_dist.1.lua
111 lines (110 loc) · 3.11 KB
/
m_srv_dist.1.lua
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
hc_server_responce=("No command "..pl.." found\nOptions dist_[get]_[cont]_[stop]")
if (pl=="dist_get") then --stop
--measure()
hc_server_responce=("distance:"..distance);
elseif (pl=="dist_get_cont") then --stop
CONTINUOUS = true
measure()
m.dist_state="on"
hc_server_responce=("continuous enabled");
elseif (pl=="dist_get_cont_stop") then --stop
CONTINUOUS = false
m.dist_state="off"
hc_server_responce=("continuous disabled");
end
if m.dist=="enabled" then
m.dist="started"
READING_INTERVAL = math.ceil(((max_dist *2/ 340*1000) + TRIG_INTERVAL)*1.2)
-- initialize global variables
time_start = 0
time_stop = 0
distance = 0
readings = {}
-- start a measure cycle
function measure()
readings = {}
tmr.start(0)
end
-- called when measure is done
function done_measuring()
-- print("Distance: "..string.format("%.3f", distance).." Readings: "..#readings)
distance=distance*100
--if distance_last and (distance >= distance_last+20 or distance <= distance_last-20) then
-- distance=distance_last
--else
-- distance_last=distance
--end
-- print(distance)
if distance >100 then
--remote_cmd("192.168.51.1","car_frwd")
--remote_cmd("192.168.51.1","car_speed_99")
elseif distance <=55 then
--remote_cmd("192.168.51.1","car_left")
elseif distance <=90 then
--remote_cmd("192.168.51.1","car_speed_85")
end
if CONTINUOUS then
node.task.post(measure)
end
end
-- distance calculation, called by the echo_callback function on falling edge.
function calculate()
-- echo time (or high level time) in seconds
local echo_time = (time_stop - time_start) / 1000000
-- got a valid reading
if echo_time > 0 then
-- distance = echo time (or high level time) in seconds* velocity of sound (340M/S) / 2
distance = echo_time *340 / 2
table.insert(readings, distance)
end
-- got all readings
--if #readings >= AVG_READINGS then
tmr.stop(0)
-- calculate the average of the readings
-- distance = 0
-- dist_temp=0
-- dist_count=0
-- for k,v in pairs(readings) do
-- dist_count=dist_count+1
-- if v==0 then
-- dist_count=dist_count-1
-- elseif dist_count==1 then
-- distance = distance + v
-- else
-- dist_temp=distance+v
-- if v >= (dist_temp/dist_count)-0.1 and v <= (dist_temp/dist_count)+0.1 then
-- distance = distance + v
-- else
-- dist_count=dist_count-1
-- end
-- end
-- end
-- distance = distance / #readings
--distance = distance / dist_count
node.task.post(done_measuring)
--end
end
-- echo callback function called on both rising and falling edges
function echo_callback(level)
if level == 1 then
time_start = tmr.now()
else
time_stop = tmr.now()
calculate()
end
end
-- send trigger signal
function trigger()
gpio.write(TRIG_PIN, gpio.HIGH)
tmr.delay(TRIG_INTERVAL)
gpio.write(TRIG_PIN, gpio.LOW)
end
-- configure pins
gpio.mode(TRIG_PIN, gpio.OUTPUT)
gpio.mode(ECHO_PIN, gpio.INT)
-- trigger timer
tmr.register(0, READING_INTERVAL, tmr.ALARM_AUTO, trigger)
-- set callback function to be called both on rising and falling edges
gpio.trig(ECHO_PIN, "both", echo_callback)
end
end