Commit 52c1d877 authored by isaac.thompson's avatar isaac.thompson

Finished Python 3 changes, fixed existing commands. Also added new WeDo commands

parent dcba4b6b
from __future__ import print_function
from http.server import BaseHTTPRequestHandler, HTTPServer from http.server import BaseHTTPRequestHandler, HTTPServer
import socketserver import socketserver
import socket import socket
...@@ -6,7 +7,8 @@ import sys ...@@ -6,7 +7,8 @@ import sys
import ev3dev.ev3 as ev3 import ev3dev.ev3 as ev3
if len(sys.argv) != 2: if len(sys.argv) != 2:
print>>sys.stderr,'Syntax: %s <port>' % (sys.argv[0]) def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
sys.exit(1) sys.exit(1)
class Handler(BaseHTTPRequestHandler): class Handler(BaseHTTPRequestHandler):
...@@ -18,7 +20,7 @@ class Handler(BaseHTTPRequestHandler): ...@@ -18,7 +20,7 @@ class Handler(BaseHTTPRequestHandler):
return return
f = open('%s/mode' % dir, 'w') f = open('%s/mode' % dir, 'w')
print>>f,mode f.write(mode)
f.close() f.close()
self.color_sensor_mode = mode self.color_sensor_mode = mode
...@@ -27,8 +29,7 @@ class Handler(BaseHTTPRequestHandler): ...@@ -27,8 +29,7 @@ class Handler(BaseHTTPRequestHandler):
self.send_response(200) self.send_response(200)
self.send_header('Content-Type', 'text/plain') self.send_header('Content-Type', 'text/plain')
self.end_headers() self.end_headers()
self.wfile.write(reply) self.wfile.write(bytes(reply, 'UTF-8'))
self.wfile.close()
def find_sensor(self, driver_name): def find_sensor(self, driver_name):
for s in range(0, 4): for s in range(0, 4):
...@@ -42,18 +43,25 @@ class Handler(BaseHTTPRequestHandler): ...@@ -42,18 +43,25 @@ class Handler(BaseHTTPRequestHandler):
def do_GET(self): def do_GET(self):
parts = self.path[1:].split('/') parts = self.path[1:].split('/')
print (parts) print (parts)
if len(parts) == 4 and parts[0] == 'motor': if len(parts) == 4 and parts[0] == 'mediummotor':
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 = ev3.MediumMotor('out' + parts[1]) d = ev3.MediumMotor('out' + parts[1])
d.run_timed(time_sp=3000, speed_sp=500) d.run_timed(time_sp=int(parts[2]), speed_sp=int(parts[3]))
#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') self.respond('OK')
elif len(parts) == 4 and parts[0] == 'largemotor':
print ('Run %s to %s at speed %s' % (parts[1], parts[2], parts[3]))
d = ev3.LargeMotor('out' + parts[1])
d.run_timed(time_sp=int(parts[2]), speed_sp=int(parts[3]))
#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.respond(bytes('OK', 'UTF-8'))
elif len(parts) == 2 and parts[0] == 'ir-sensor' and parts[1] == 'proximity': elif len(parts) == 2 and parts[0] == 'ir-sensor' and parts[1] == 'proximity':
dir = self.find_sensor('lego-ev3-ir') dir = self.find_sensor('lego-ev3-ir')
f = open('%s/value0' % dir, 'r') f = open('%s/value0' % dir, 'r')
...@@ -73,6 +81,37 @@ class Handler(BaseHTTPRequestHandler): ...@@ -73,6 +81,37 @@ class Handler(BaseHTTPRequestHandler):
v = f.readline().strip() v = f.readline().strip()
f.close() f.close()
self.respond(v) self.respond(v)
elif len(parts) == 3 and parts[0] == 'mediumtimed':
print('Run %s at speed %s' % (parts[1],parts[2]))
d = ev3.MediumMotor('out' + parts[1])
d.run_timed(time_sp=int(parts[2]), speed_sp=500)
self.respond('OK')
elif len(parts) == 3 and parts[0] == 'largetimed':
print('Run %s at speed %s' % (parts[1],parts[2]))
d = ev3.LargeMotor('out' + parts[1])
d.run_timed(time_sp=int(parts[2]), speed_sp=500)
self.respond('OK')
elif len(parts) == 2 and parts[0] == 'mediumon':
print('Run %s' % (parts[1]))
d = ev3.MediumMotor('out' + parts[1])
d.run_forever()
self.respond('OK')
elif len(parts) == 2 and parts[0] == 'largeon':
print('Run %s' % (parts[1]))
d = ev3.LargeMotor('out' + parts[1])
d.run_forever()
self.respond('OK')
elif len(parts) == 2 and parts[0] == 'mediumoff':
print('Stop %s' % (parts[1]))
d = ev3.MediumMotor('out' + parts[1])
d.stop()
self.respond('OK')
elif len(parts) == 2 and parts[0] == 'largeoff':
print('Stop %s' % (parts[1]))
d = ev3.LargeMotor('out' + parts[1])
d.stop()
self.respond('OK')
class TCPServer(socketserver.TCPServer): class TCPServer(socketserver.TCPServer):
def server_bind(self): def server_bind(self):
......
...@@ -16,6 +16,7 @@ EV3_IP = '10.42.0.3' ...@@ -16,6 +16,7 @@ EV3_IP = '10.42.0.3'
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self): def do_GET(self):
if self.path == '/snap-ev3': if self.path == '/snap-ev3':
print "Opening XML"
f = open('snap-ev3.xml', 'rb') f = open('snap-ev3.xml', 'rb')
self.send_response(200) self.send_response(200)
self.send_header("Content-type", 'text/xml') self.send_header("Content-type", 'text/xml')
...@@ -45,8 +46,8 @@ class TCPServer(SocketServer.TCPServer): ...@@ -45,8 +46,8 @@ class TCPServer(SocketServer.TCPServer):
self.socket.bind(self.server_address) self.socket.bind(self.server_address)
print "Starting listener on EV3" print "Starting listener on EV3"
#os.system('scp listen.py %s@%s:' % (EV3_USER, EV3_IP)) os.system('scp listen.py %s@%s:' % (EV3_USER, EV3_IP))
#os.system('ssh %s@%s -- nohup python listen.py %d &' % (EV3_USER, EV3_IP, EV3_PORT)) #os.system('ssh %s@%s -- nohup python3 listen.py %d &' % (EV3_USER, EV3_IP, EV3_PORT))
httpd = TCPServer(("", SNAP_PORT), Handler) httpd = TCPServer(("", SNAP_PORT), Handler)
print "http://snap.berkeley.edu/snapsource/snap.html#open:http://localhost:1330/snap-ev3" print "http://snap.berkeley.edu/snapsource/snap.html#open:http://localhost:1330/snap-ev3"
......
<blocks app="Snap! 4.0, http://snap.berkeley.edu" version="1"> <blocks app="Snap! 4.0, http://snap.berkeley.edu" version="1">
<block-definition category="control" s="move motor %&apos;motor&apos; %&apos;degrees&apos; degrees at %&apos;speed&apos; speed" type="command"> <block-definition category="control" s="move largemotor %&apos;motor&apos; %&apos;milliseconds&apos; milliseconds at %&apos;speed&apos; speed" type="command">
<inputs> <inputs>
<input type="%txt">a</input> <input type="%txt">A<options>A
B
C
D
E
F
G
H
</options>
</input>
<input type="%n">360</input> <input type="%n">360</input>
<input type="%n">100</input> <input type="%n">100</input>
</inputs> </inputs>
...@@ -12,10 +21,10 @@ ...@@ -12,10 +21,10 @@
<block s="reportURL"> <block s="reportURL">
<block s="reportJoinWords"> <block s="reportJoinWords">
<list> <list>
<l>localhost:1330/motor/</l> <l>localhost:1330/largemotor/</l>
<block var="motor"/> <block var="motor"/>
<l>/</l> <l>/</l>
<block var="degrees"/> <block var="milliseconds"/>
<l>/</l> <l>/</l>
<block var="speed"/> <block var="speed"/>
</list> </list>
...@@ -26,6 +35,299 @@ ...@@ -26,6 +35,299 @@
</block> </block>
</script> </script>
</block-definition> </block-definition>
<block-definition category="control" s="move mediummotor %&apos;motor&apos; %&apos;milliseconds&apos; milliseconds at %&apos;speed&apos; speed" type="command">
<inputs>
<input type="%txt">A<options>A
B
C
D
E
F
G
H
</options>
</input>
<input type="%n">360</input>
<input type="%n">100</input>
</inputs>
<script>
<block s="doRun">
<block s="reifyReporter">
<autolambda>
<block s="reportURL">
<block s="reportJoinWords">
<list>
<l>localhost:1330/mediummotor/</l>
<block var="motor"/>
<l>/</l>
<block var="milliseconds"/>
<l>/</l>
<block var="speed"/>
</list>
</block>
</block>
</autolambda>
</block>
</block>
</script>
</block-definition>
<block-definition category="control" s="move mediummotor %&apos;motor&apos; %&apos;milliseconds&apos; milliseconds" type="command">
<inputs>
<input type="%txt">A<options>A
B
C
D
E
F
G
H
</options>
</input>
<input type="%n">360</input>
</inputs>
<script>
<block s="doRun">
<block s="reifyReporter">
<autolambda>
<block s="reportURL">
<block s="reportJoinWords">
<list>
<l>localhost:1330/mediumtimed/</l>
<block var="motor"/>
<l>/</l>
<block var = "milliseconds"/>
</list>
</block>
</block>
</autolambda>
</block>
</block>
</script>
</block-definition>
<block-definition category="control" s="move largemotor %&apos;motor&apos; %&apos;milliseconds&apos; milliseconds" type="command">
<inputs>
<input type="%txt">A<options>A
B
C
D
E
F
G
H
</options>
</input>
<input type="%n">360</input>
</inputs>
<script>
<block s="doRun">
<block s="reifyReporter">
<autolambda>
<block s="reportURL">
<block s="reportJoinWords">
<list>
<l>localhost:1330/largetimed/</l>
<block var="motor"/>
<l>/</l>
<block var = "milliseconds"/>
</list>
</block>
</block>
</autolambda>
</block>
</block>
</script>
</block-definition>
<block-definition category="control" s="turn on mediummotor %&apos;motor&apos;" type="command">
<inputs>
<input type="%txt">A<options>A
B
C
D
E
F
G
H
</options>
</input>
</inputs>
<script>
<block s="doRun">
<block s="reifyReporter">
<autolambda>
<block s="reportURL">
<block s="reportJoinWords">
<list>
<l>localhost:1330/mediumon/</l>
<block var="motor"/>
</list>
</block>
</block>
</autolambda>
</block>
</block>
</script>
</block-definition>
<block-definition category="control" s="turn off mediummotor %&apos;motor&apos;" type="command">
<inputs>
<input type="%txt">A<options>A
B
C
D
E
F
G
H
</options>
</input>
</inputs>
<script>
<block s="doRun">
<block s="reifyReporter">
<autolambda>
<block s="reportURL">
<block s="reportJoinWords">
<list>
<l>localhost:1330/mediumoff/</l>
<block var="motor"/>
</list>
</block>
</block>
</autolambda>
</block>
</block>
</script>
</block-definition>
<block-definition category="control" s="turn off largemotor %&apos;motor&apos;" type="command">
<inputs>
<input type="%txt">A<options>A
B
C
D
E
F
G
H
</options>
</input>
</inputs>
<script>
<block s="doRun">
<block s="reifyReporter">
<autolambda>
<block s="reportURL">
<block s="reportJoinWords">
<list>
<l>localhost:1330/largeoff/</l>
<block var="motor"/>
</list>
</block>
</block>
</autolambda>
</block>
</block>
</script>
</block-definition>
<block-definition category="control" s="turn on largemotor %&apos;motor&apos;" type="command">
<inputs>
<input type="%txt">A<options>A
B
C
D
E
F
G
H
</options>
</input>
</inputs>
<script>
<block s="doRun">
<block s="reifyReporter">
<autolambda>
<block s="reportURL">
<block s="reportJoinWords">
<list>
<l>localhost:1330/largeon/</l>
<block var="motor"/>
</list>
</block>
</block>
</autolambda>
</block>
</block>
</script>
</block-definition>
<block-definition s="Being Touched?" type="reporter" category ="operators">
<script>
<block s="doIfElse">
<block s="reportEquals">
<custom-block s="touch sensor level"/>
<l>1</l>
</block>
<script>
<block s="doReport">
<l>True</l>
</block>
</script>
<script>
<block s="doReport">
<l>False</l>
</block>
</script>
</block>
</script>
</block-definition>
<block-definition s="How Far Away?" type="reporter" category="operators">
<header/>
<code/>
<inputs/>
<script>
<block s="doIf">
<block s="reportLessThan">
<custom-block s="IR sensor proximity"/>
<l>33</l>
</block>
<script>
<block s="doReport">
<l>Close</l>
</block>
</script>
</block>
<block s="doIfElse">
<block s="reportAnd">
<block s="reportLessThan">
<custom-block s="IR sensor proximity"/>
<l>66</l>
</block>
<block s="reportLessThan">
<l>33</l>
<custom-block s="IR sensor proximity"/>
</block>
</block>
<script>
<block s="doReport">
<l>Medium</l>
</block>
</script>
<script>
<block s="doReport">
<l>Far Away</l>
</block>
</script>
</block>
</script>
</block-definition>
<block-definition category="control" s="speak %&apos;words&apos;" type="command"> <block-definition category="control" s="speak %&apos;words&apos;" type="command">
<inputs> <inputs>
<input type="%txt">hello</input> <input type="%txt">hello</input>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment