-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot@192.168.0.1
87 lines (64 loc) · 1.96 KB
/
robot@192.168.0.1
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
import socket
import sys
import time
import ev3dev.ev3 as ev3
print(":: Starting program :: \n")
ma = ev3.LargeMotor('outA')
mb = ev3.LargeMotor('outD')
c = None
print(":: Motors ready :: \n")
def not_supported(*args, **kwargs):
print("Not supported")
def move_forward(time, speed):
ma.run_timed(time_sp=time, speed_sp=speed)
mb.run_timed(time_sp=time, speed_sp=speed)
def move_backwards(time, speed):
ma.run_timed(time_sp=time, speed_sp=-speed)
mb.run_timed(time_sp=time, speed_sp=-speed)
def move_left(time, speed):
ma.run_timed(time_sp=time, speed_sp=speed)
mb.run_timed(time_sp=time, speed_sp=-speed)
def move_right(time, speed):
mb.run_timed(time_sp=time, speed_sp=speed)
ma.run_timed(time_sp=time, speed_sp=-speed)
def stop(*args, **kwargs):
print("STOP hammertime")
ma.stop()
mb.stop()
def switch(argument):
argument = argument.rstrip()
print(argument)
if argument == "forward":
move_forward(500, 500)
elif argument == "back":
move_backwards(500, 500)
elif argument == "right":
move_right(500, 500)
elif argument == "left":
move_left(500, 500)
elif argument == "stop":
stop(500, 500)
else:
not_supported(500, 500)
def initializeServer():
s = socket.socket() # Create a socket object
host = '0.0.0.0' # Get local machine name
port = 44446 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
print(host)
s.listen(5) # Now wait for client connection.
while True:
print("Listening for client")
c, addr = s.accept() # Establish connection with client.
print("Client connected, waiting for input")
handleClient(c)
def handleClient(c):
while True:
try:
argument = c.recv(1024)
switch(argument.decode())
except:
print("Unexpected error:", sys.exc_info()[0])
break
c.close()
initializeServer()