-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.py
executable file
·221 lines (200 loc) · 5.5 KB
/
control.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
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
#!/usr/bin/python
import math
import time
import socket
import sys
from move import move
from ToF import *
STEP_LENGTH = 204
STEP_ANGLE = 20
class Soc_server:
def __init__(self,address,port_number):
self.host = address
print self.host
self.port = int(port_number)
self.serversock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
self.serversock.bind((self.host,self.port))
self.serversock.listen(10)
print "Waiting for connections..."
self.clientsock, self.client_address = self.serversock.accept()
self.mode = 0
self.control = Control()
def main(self):
while True:
if self.mode == 0:
print "Acceptance status"
rcvmsg = self.clientsock.recv(1024)
if rcvmsg == '':
self.clientsock.close()
break
temp_data = rcvmsg.split(',')
self.control.main((temp_data[0],float(temp_data[1])))
if temp_data[0] == 'b':
self.mode = 2
else:
self.mode = 1
if self.mode == 1:
print "send result"
self.clientsock.sendall("0")
self.mode = 0
if self.mode == 2:
fg = self.control.ball_check_fg
print "send ball result",fg
if fg == 1:
self.clientsock.sendall("1")
else:
self.clientsock.sendall("0")
self.mode = 0
class Control:
def __init__(self):
self.ball_check_fg = 0
self.tof = ToF()
def stand_up_before_walk(self):
servo = move()
for i in xrange(16):
temp_str = 'stand_before_walk/test' + str(i) + '.csv'
servo.Action(temp_str,0.01)
def motion_01(self):
servo = move()
for i in xrange(36):
temp_str = 'motion_01r/test' + str(i) + '.csv'
servo.Action(temp_str,0.01)
def stand_up_before_turn(self,direction):
servo = move()
for i in xrange(16):
if direction == 'r':
temp_str = 'stand_before_turn_right/test' + str(i) + '.csv'
if direction == 'l':
temp_str = 'stand_before_turn_left/test' + str(i) + '.csv'
servo.Action(temp_str,0.01)
def ahead(self,value):
n = int(value // STEP_LENGTH) + 1
servo = move()
print "ahead :",value
for i in xrange(n):
for j in xrange(24):
temp_str = 'walk24/test' + str(j) + '.csv'
if i == (n - 1):
if (value - ((n-1) * STEP_LENGTH)) < (STEP_LENGTH*j/24):
break
servo.Action(temp_str,0.01)
servo.Close()
def back(self,value):
n = int(value // STEP_LENGTH) + 1
servo = move()
print "back :",value
for i in xrange(n):
for j in xrange(24):
temp_str = 'back24/test' + str(j) + '.csv'
if i == (n - 1):
if (value - ((n-1) * STEP_LENGTH)) < (STEP_LENGTH*j/24):
break
servo.Action(temp_str,0.01)
servo.Close()
def high(self,value):
n = int(value // STEP_LENGTH) + 1
servo = move()
print "high :",value
for i in xrange(n):
for j in xrange(24):
temp_str = 'walk24high/test' + str(j) + '.csv'
if i == (n - 1):
if (value - ((n-1) * STEP_LENGTH)) < (STEP_LENGTH*j/24):
break
servo.Action(temp_str,0.01)
servo.Close()
def turn_right(self,value):
n = int(value // STEP_ANGLE) + 1
servo = move()
print "right : ",value
for i in xrange(n):
for j in xrange(16):
temp_str = 'turn_right/test' + str(j) + '.csv'
if i == (n - 1):
if (value - ((n-1) * STEP_ANGLE)) < (STEP_ANGLE*j/16):
break
servo.Action(temp_str,0.01)
servo.Close()
def turn_left(self,value):
n = int(value // STEP_ANGLE) + 1
servo = move()
print "left : ",value
for i in xrange(n):
for j in xrange(16):
temp_str = 'turn_left/test' + str(j) + '.csv'
if i == (n - 1):
if (value - ((n-1) * STEP_ANGLE)) < (STEP_ANGLE*j/16):
break
servo.Action(temp_str,0.01)
servo.Close()
def ball_catch(self):
self.ball_check_fg = 0
servo = move()
servo.Action('Ball/BallCatch.csv',1.0)
servo.Close()
if self.tof.ReadDistance() == 2:
self.ball_check_fg = 1
def ball_dust(self):
self.ball_check_fg = 0
servo = move()
#demo_action
for i in xrange(16):
temp_str = "stand_up_normal/test" + str(i) + ".csv"
servo.Action(temp_str,0.01)
for i in xrange(2):
for j in xrange(16):
temp_str = "motion_04/test" + str(j) + ".csv"
servo.Action(temp_str,0.01)
for i in xrange(2):
for j in xrange(16):
temp_str = "motion_02/test" + str(j) + ".csv"
servo.Action(temp_str,0.01)
for i in xrange(2):
for j in xrange(16):
temp_str = "motion_06/test" + str(j) + ".csv"
servo.Action(temp_str,0.01)
for i in xrange(2):
for j in xrange(16):
temp_str = "motion_05/test" + str(j) + ".csv"
servo.Action(temp_str,0.01)
for i in xrange(2):
for j in xrange(16):
temp_str = "motion_02/test" + str(15-j) + ".csv"
servo.Action(temp_str,0.01)
for i in xrange(2):
for j in xrange(16):
temp_str = "motion_07/test" + str(15-j) + ".csv"
servo.Action(temp_str,0.01)
servo.Action('Ball/BallDust.csv',1.0)
servo.Close()
def main(self,action_data):
print action_data
if action_data[0] == 'b':
self.ball_catch()
if action_data[0] == 'm':
self.motion_01()
if action_data[0] == 'd':
self.ball_dust()
if action_data[0] == 'x':
self.stand_up_before_walk()
if action_data[1] > 0:
self.ahead(1000 * action_data[1])
else:
self.back(1000 * -action_data[1])
if action_data[0] == 'h':
self.stand_up_before_walk()
self.high(1000 * action_data[1])
if action_data[0] == 'r':
if action_data[1] > 0:
self.stand_up_before_turn('l')
self.turn_left(action_data[1])
else:
self.stand_up_before_turn('r')
self.turn_right(-action_data[1])
time.sleep(2)
print "Move complete"
if __name__ == "__main__":
args = sys.argv
if len(args) == 3:
soc = Soc_server(args[1],args[2])
soc.main()