-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_interface_calibration.rb
executable file
·282 lines (246 loc) · 7.35 KB
/
web_interface_calibration.rb
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env mruby
#daemon(true, true)
module YAML
def YAML.load_file(f)
YAML.load(File.open(f,"rb").read)
end
end
config = YAML.load_file("laser_pos.yml")
# Reset 0 axes for position output
# rescaling from -100 to + 100
def rescale(value)
return (value * 200.0/255.0 - 100.0).round(0)
end
# Calculate output on dedzone basis
# center => center point for
def deadzone(value, center, dz)
value = rescale(value) - center
return ( value.abs < dz/2 ? 0 : (value > 0 ? 1 : -1) * (value.abs - dz/2))
end
def control(ary, config)
ret = [deadzone(ary[0], config[:center_x], config[:dz_x]), deadzone(ary[1], config[:center_y], config[:dz_y])]
ret.map! { |e| (e.abs > 100 ? (e > 0 ? 1 : -1) * 100 : e ).to_i }
return ret.reverse
end
Signal.trap(:INT) do
puts "Terminating communication"
running = false
end
# WebServer
class WSArgError < ArgumentError; end
class WSRunError < RuntimeError; end
class MWebSerialServer
def initialize(config, port = nil)
if File.exist?(config) then
@config = YAML.load_file(config)
raise WSArgError, "Cannot setup hostname" if not @config[:hostname]
raise WSArgError, "Cannot setup port" if not @config[:port]
raise WSArgError, "Cannot setup index" if not @config[:index]
else
raise WSArgError, "Cannot find configuration file #{config}"
end
puts inspect
end
def serve(opt_block = nil)
@server = TCPServer.new(@config[:hostname], @config[:port])
if @server
while (session = @server.accept)
request = session.gets
if request.is_a?(String)
@resource = request.gsub(/GET\ \//, '').gsub(/\ HTTP.*/, '').chomp
yield(@resource, session)
end
end
close
else
puts @config
raise WSRunError, "Cannot find @server??"
end
ensure
close
end
def send_api(request)
name, tokens = parse_query(request)
if methods.include?(name.to_sym)
ret = self.send(name, *tokens)
return ret
else
return nil
end
end
def close
puts " = Closing server..."
if @server
@server.close
end
puts "BYE!"
end
def parse_query(s)
tokens = s.split("/")
return tokens[0..1].join("_"), tokens[2..-1]
end
def api(s, &block)
name, tokens = parse_query(s)
MWebSerialServer.define_method(name) { |*tokens| yield(tokens) }
puts " = Define new API -> #{name}(#{tokens.flatten})" if methods.include?(name.to_sym)
end
def inspect
return <<-EOS
=======================================================
Mechatronix Web2Serial Server - a /dev/null creation ;)
=======================================================
= Configuration
Webserver = http://#{@config[:hostname]}:#{@config[:port]}
Index page = #{@config[:index]}
= Requests ============================================
EOS
end
end
if __FILE__ == $0 then
begin
cam = RaspiCam.new(config[:frame_width]).open
ser = SerialPort.new(config[:serialport], config[:baudrate]).open
ws = MWebSerialServer.new("laser_pos.yml")
#ENABLE_PIN = 3
#Raspberry::Core.pin_mode(ENABLE_PIN, Raspberry::INPUT)
#while Raspberry::Core.digital_read(ENABLE_PIN) == Raspberry::LOW do
# sleep(0.1)
#end
sleep 0.5
ser.command('/') {|txt| p txt}
#### API definitions ###################
ws.api("api/up") do
ret = ""
ser.command("500V") { |txt| ret = txt }
ret
end
ws.api("api/down") do
ret = ""
ser.command("500v") { |txt| ret = txt }
ret
end
ws.api("api/left") do
ret = ""
ser.command("500Y") { |txt| ret = txt }
ret
end
ws.api("api/right") do
ret = ""
ser.command("500y") { |txt| ret = txt }
ret
end
ws.api("api/brake") do
ret = ""
ser.command("0V0Y") { |txt| ret = txt }
ret
end
ws.api("api/stop") do
ret = ""
ser.command("/") { |txt| ret = txt }
ret
end
ws.api("api/laser") do
ret = ""
ser.command("A") { |txt| ret = txt }
ret
end
ws.api("api/man") do
ret = ""
ser.command("A") { |txt| ret = txt }
ret
end
ws.api("api/query") do
ret = ""
ser.command("?") { |txt| ret = txt }
ret
end
ws.api("api/photo") do
config = YAML.load_file("laser_pos.yml")
frame_width = config[:frame_width]
frame_height = 3/4 * frame_width
p1 = [config[:center_x] - config[:dz_x]/2, config[:center_y] + config[:dz_y]/2]
p2 = [config[:center_x] + config[:dz_x]/2, config[:center_y] - config[:dz_y]/2]
p1.map! {|e| ((e + 100) * 255.0/200.0).round }
p2.map! {|e| ((e + 100) * 255.0/200.0).round }
p1[0] = (p1[0] * frame_width/255.0).round
p2[0] = (p2[0] * frame_width/255.0).round
p1[1] = (p1[1] * frame_height/255.0).round
p2[1] = (p2[1] * frame_height/255.0).round
p1[1] = frame_height - p1[1]
p2[1] = frame_height - p2[1]
cam.set_rect(p1[0], p1[1], p2[0], p2[1])
cam.save_image config[:save_image]
cam.reset_rect
"updated photo"
end
# laser_loop = Proc.new do
# cmd = {}
# begin
# pos = control(cam.position, config)
# cmd[:x] = "#{pos[0].abs}#{pos[0] > 0 ? 'X' : 'x'}"
# cmd[:c] = "#{pos[1].abs}#{pos[1] > 0 ? 'c' : 'C'}"
# rescue RaspicamError
# cmd[:x] = '0x'
# cmd[:c] = '0c'
# rescue => e
# p e
# end
# cmd_str = cmd[:x] + cmd[:c]
# puts "cmd = #{cmd_str}"
# # Communicating via Serial
# begin
# #if Raspberry::Core.read_digital(ENABLE_PIN) == Raspberry::HIGH
# ser.command(cmd_str)
# #else
# # print '.'
# #end
# rescue Exception => e
# p e
# end
# end
########################################
#ws.serve(laser_loop) { |response, session, opt_block|
ws.serve { |response, session|
#puts response
case response
when ""
session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
session.print File.open(config[:index], "rb").read
session.close
when /^static\//
if File.exist?(response)
session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
session.print File.open(response, "rb").read
session.close
else
session.print "HTTP/1.1 404/Object Not Found\r\nMWebServer /dev/null\r\n\r\n"
session.print "404 - Resource cannot be found."
session.close
end
when "favicon.ico"
session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
session.print File.open("static/favicon.ico", "rb").read
session.close
else
if /api/.match(response) then
reply = ws.send_api(response)
if reply then
session.print "HTTP/1.1 200/OK\r\nContent-type:text/html\r\n\r\n"
#serial_response = ser.command(reply)
#session.print serial_response
puts "Reply = #{reply}"
session.print reply
session.close
else
session.print "HTTP/1.1 404/Object Not Found\r\nMWebServer /dev/null\r\n\r\n"
session.close
end
else
session.print "HTTP/1.1 404/Object Not Found\r\nMWebServer /dev/null\r\n\r\n"
session.print "404 - Resource cannot be found."
session.close
end
end
}
end
end