Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
Lego_Robots
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Matthew Baxter
Lego_Robots
Commits
a8dad347
Commit
a8dad347
authored
Jun 29, 2015
by
Carl Hetherington
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add IR sensor support.
parent
76f316f1
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
10 deletions
+42
-10
listen.py
listen.py
+25
-7
snap-ev3.py
snap-ev3.py
+6
-3
snap-ev3.xml
snap-ev3.xml
+11
-0
No files found.
listen.py
View file @
a8dad347
...
@@ -10,6 +10,22 @@ if len(sys.argv) != 2:
...
@@ -10,6 +10,22 @@ if len(sys.argv) != 2:
sys
.
exit
(
1
)
sys
.
exit
(
1
)
class
Handler
(
SimpleHTTPServer
.
SimpleHTTPRequestHandler
):
class
Handler
(
SimpleHTTPServer
.
SimpleHTTPRequestHandler
):
def
respond
(
self
,
reply
):
self
.
send_response
(
200
)
self
.
send_header
(
'Content-Type'
,
'text/plain'
)
self
.
end_headers
()
self
.
wfile
.
write
(
reply
)
self
.
wfile
.
close
()
def
find_sensor
(
self
,
driver_name
):
for
s
in
range
(
0
,
4
):
dir
=
'/sys/class/lego-sensor/sensor
%
d'
%
s
f
=
open
(
'
%
s/driver_name'
%
dir
,
'r'
)
if
f
.
readline
()
.
strip
()
==
driver_name
:
return
dir
return
None
def
do_GET
(
self
):
def
do_GET
(
self
):
parts
=
self
.
path
[
1
:]
.
split
(
'/'
)
parts
=
self
.
path
[
1
:]
.
split
(
'/'
)
print
parts
print
parts
...
@@ -17,17 +33,19 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
...
@@ -17,17 +33,19 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
print
'Run
%
s to
%
s at speed
%
s'
%
(
parts
[
1
],
parts
[
2
],
parts
[
3
])
print
'Run
%
s to
%
s at speed
%
s'
%
(
parts
[
1
],
parts
[
2
],
parts
[
3
])
d
=
MediumMotor
(
parts
[
1
])
d
=
MediumMotor
(
parts
[
1
])
d
.
run_position_limited
(
int
(
parts
[
2
]),
int
(
parts
[
3
]),
absolute
=
False
)
d
.
run_position_limited
(
int
(
parts
[
2
]),
int
(
parts
[
3
]),
absolute
=
False
)
self
.
respond
(
'OK'
)
elif
len
(
parts
)
==
2
and
parts
[
0
]
==
'say'
:
elif
len
(
parts
)
==
2
and
parts
[
0
]
==
'say'
:
phrase
=
parts
[
1
]
phrase
=
parts
[
1
]
phrase
=
phrase
.
replace
(
'
%20
'
,
' '
)
phrase
=
phrase
.
replace
(
'
%20
'
,
' '
)
print
'Say
%
s'
%
phrase
print
'Say
%
s'
%
phrase
os
.
system
(
'espeak -a 200 -s 130 -v en-sc --stdout "
%
s" | aplay'
%
phrase
)
os
.
system
(
'espeak -a 200 -s 130 -v en-sc --stdout "
%
s" | aplay'
%
phrase
)
self
.
respond
(
'OK'
)
self
.
send_response
(
200
)
elif
len
(
parts
)
==
2
and
parts
[
0
]
==
'ir-sensor'
and
parts
[
1
]
==
'proximity'
:
self
.
send_header
(
'Content-Type'
,
'text/html'
)
dir
=
self
.
find_sensor
(
'lego-ev3-ir'
)
self
.
end_headers
()
f
=
open
(
'
%
s/value0'
%
dir
,
'r'
)
self
.
wfile
.
write
(
'OK'
)
v
=
f
.
readline
()
.
strip
()
self
.
wfile
.
close
()
f
.
close
()
self
.
respond
(
v
)
class
TCPServer
(
SocketServer
.
TCPServer
):
class
TCPServer
(
SocketServer
.
TCPServer
):
def
server_bind
(
self
):
def
server_bind
(
self
):
...
...
snap-ev3.py
View file @
a8dad347
...
@@ -25,11 +25,14 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
...
@@ -25,11 +25,14 @@ class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
self
.
copyfile
(
f
,
self
.
wfile
)
self
.
copyfile
(
f
,
self
.
wfile
)
f
.
close
()
f
.
close
()
else
:
else
:
urllib2
.
urlopen
(
"http://10.42.0.51:
%
d
%
s"
%
(
EV3_PORT
,
self
.
path
),
timeout
=
5
)
.
read
()
response
=
urllib2
.
urlopen
(
"http://10.42.0.51:
%
d
%
s"
%
(
EV3_PORT
,
self
.
path
),
timeout
=
5
)
.
read
()
print
'
%
s ->
%
s'
%
(
self
.
path
,
response
)
self
.
send_response
(
200
)
self
.
send_response
(
200
)
self
.
send_header
(
'Content-Type'
,
'text/html'
)
self
.
send_header
(
'Content-Type'
,
'application/octet-stream'
)
self
.
send_header
(
'Content-Length'
,
str
(
len
(
response
)))
self
.
send_header
(
'Access-Control-Allow-Origin'
,
'*'
)
self
.
end_headers
()
self
.
end_headers
()
self
.
wfile
.
write
(
'OK'
)
self
.
wfile
.
write
(
response
)
self
.
wfile
.
close
()
self
.
wfile
.
close
()
class
TCPServer
(
SocketServer
.
TCPServer
):
class
TCPServer
(
SocketServer
.
TCPServer
):
...
...
snap-ev3.xml
View file @
a8dad347
...
@@ -47,4 +47,15 @@
...
@@ -47,4 +47,15 @@
</block>
</block>
</script>
</script>
</block-definition>
</block-definition>
<block-definition
category=
"sensing"
s=
"IR sensor proximity"
type=
"reporter"
>
<inputs></inputs>
<script>
<block
s=
"doReport"
>
<block
s=
"reportURL"
>
<l>
localhost:1330/ir-sensor/proximity
</l>
</block>
</block>
</script>
<watcher
s=
"IR sensor proximity"
style=
"normal"
/>
</block-definition>
</blocks>
</blocks>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment