-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobo_TCP.py
executable file
·63 lines (61 loc) · 1.63 KB
/
robo_TCP.py
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
# Echo client program
import socket
import struct
import nxt.locator
from nxt.motor import *
print "looking for robot"
b = nxt.locator.find_one_brick()
print "robot Found!!"
HOST = raw_input("Host: ")
PORT = 5660 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
m_x = Motor(b, PORT_B)
m_y = Motor(b, PORT_C)
speed = 75
yspeed = 35
xspeed = 35
yval = 0
xval = 0
add = float(0.8)
while (True):
try:
ox = xval
oy = yval
data = s.recvfrom(1024)[0].split(",")
xval = data[0]
yval = data[1]
print (xval, yval)
xval = float(xval)
yval = float(yval)
#print xval
if xval == 0 and ox != 0: m_x.idle()
elif xval > 0:
try:
m_x.run(-xspeed*(abs(xval)+add))
except nxt.motor.BlockedException:
m_x.turn(speed, dataX)
elif xval < 0:
try:
m_x.run(xspeed*(abs(xval)+add))
except nxt.motor.BlockedException:
m_x.turn(-speed, dataX)
if yval == 0 and oy !=0: m_y.idle()
elif yval < 0:
try:
m_y.run(yspeed*(abs(yval)+add))
except nxt.motor.BlockedException:
m_y.turn(-speed, dataX)
elif yval > 0:
try:
m_y.run(-yspeed*(abs(yval)+add))
except nxt.motor.BlockedException:
m_y.turn(speed, dataX)
except Exception, e: #KeyboardInterrupt:
print data
#print xval
#print yval
#s.close()
#m_x.idle()
#m_y.idle()
raise e