-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaits.py
executable file
·216 lines (191 loc) · 6.37 KB
/
gaits.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
#!/usr/bin/env python
"""
The node publishes joint position commands to effort position controllers.
The controllers should already be spawned and running and communicating
with the appropriate robot hardware interface.
"""
import rospy
import socket
import sys
from std_msgs.msg import Float64
from threading import Thread
import numpy as np
movement = 0
time_goal= 0
mode = 0
def read_movement():
global movement
global time_goal
if mode == 0:
input_message ="""Type to select a new movement. Possible values:
0:=Slither Forward
1:=Turn Left
2:=Turn Right
3:=Roll Left
4:=Roll Right
5:=Stop
Type 'exit' to quit.\n"""
while not rospy.is_shutdown():
user_input = raw_input(input_message)
try:
new_movement = int(user_input)
if new_movement<0 or new_movement>5:
new_movement = 5
movement = new_movement
if new_movement==1 or new_movement==2:
### 150 = 30 degrees
time_goal = 150
except ValueError:
if user_input == 'exit':
sys.exit()
print "Invalid Input"
else:
s = socket.socket()
host = socket.gethostname()
port = 10500
s.connect((host,port))
while not rospy.is_shutdown():
new_movement = s.recv(8)
try:
new_movement = int(new_movement)
if new_movement<0 or new_movement>5:
new_movement = 5
movement = new_movement
if new_movement==1 or new_movement==2:
### 150 = 30 degrees
time_goal = 50
except ValueError:
raise ValueError('Movement must be an int')
s.close()
class JointCmds:
"""
The class provides a dictionary mapping joints to command values.
"""
def __init__( self, num_mods):
self.num_modules = num_mods
self.jnt_cmd_dict = {}
self.joints_list = []
self.t = 0.0
self.movement = 0
for i in range(self.num_modules) :
leg_str='S_'
if i < 10 :
leg_str += '0' + str(i)
else :
leg_str += str(i)
self.joints_list += [leg_str]
def update( self, dt, new_movement):
self.t +=dt
self.movement = new_movement
global time_goal
global movement
##0= Slither Forward
if self.movement == 0:
A_o = 80 * (np.pi/180)
A_e = 50 * (np.pi/180)
d_o = np.pi/3.0
d_e = 2 * d_o
w_o = 2
w_e = 4
for i,jnt in enumerate(self.joints_list):
#odd
if i%2:
P = ((i+1)/self.num_modules) * 0.3 + 0.7
theta = w_o * self.t + d_o * i
alpha = P * A_o * np.sin(theta)
#even
else:
P = ((i+1)/self.num_modules) * 0.7 + 0.3
theta = w_e * self.t + d_e * i
alpha = P * A_e * np.sin(theta)
self.jnt_cmd_dict[jnt] = alpha
return self.jnt_cmd_dict
##1:= Turn Left
elif self.movement == 1:
time_goal -= 1
if time_goal == 0:
movement = 0
spat_freq = 0.25
A = - np.pi/6.0
d = 1
for i, jnt in enumerate(self.joints_list) :
if i<8:
self.jnt_cmd_dict[jnt] = A*np.sin( 2.0*np.pi*(d*self.t + i*spat_freq) )
else:
self.jnt_cmd_dict[jnt] = -A*np.sin( 2.0*np.pi*(-d*self.t + i*spat_freq) )
return self.jnt_cmd_dict
##2:= Turn Right
elif self.movement == 2:
time_goal -= 1
if time_goal == 0:
movement = 0
spat_freq = 0.25
A = np.pi/6.0
d = -1
for i, jnt in enumerate(self.joints_list) :
if i<8:
self.jnt_cmd_dict[jnt] = A*np.sin( 2.0*np.pi*(d*self.t + i*spat_freq) )
else:
self.jnt_cmd_dict[jnt] = -A*np.sin( 2.0*np.pi*(-d*self.t + i*spat_freq) )
return self.jnt_cmd_dict
##3:= Roll Left
elif self.movement == 3:
spat_freq = 0.25
A = np.pi/6.0
d = -1
for i, jnt in enumerate(self.joints_list) :
self.jnt_cmd_dict[jnt] = A*np.sin( 2.0*np.pi*(d*self.t + i*spat_freq) )
return self.jnt_cmd_dict
##4:= Roll Right
elif self.movement == 4:
spat_freq = 0.25
A = - np.pi/6.0
d = 1
for i, jnt in enumerate(self.joints_list) :
self.jnt_cmd_dict[jnt] = A*np.sin( 2.0*np.pi*(d*self.t + i*spat_freq) )
return self.jnt_cmd_dict
##>=5:= Stop
else:
for i,jnt in enumerate(self.joints_list):
self.jnt_cmd_dict[jnt] = 0
return self.jnt_cmd_dict
def publish_commands( num_modules, hz ):
pub={}
ns_str = '/snake'
cont_str = 'eff_pos_controller'
for i in range(num_modules) :
leg_str='S_'
if i < 10 :
leg_str += '0' + str(i)
else :
leg_str += str(i)
pub[leg_str] = rospy.Publisher( ns_str + '/' + leg_str + '_'
+ cont_str + '/command',
Float64, queue_size=10 )
rospy.init_node('snake_controller', anonymous=True)
rate = rospy.Rate(hz)
jntcmds = JointCmds(num_mods=num_modules)
thread = Thread(target=read_movement)
thread.start()
while not rospy.is_shutdown():
jnt_cmd_dict = jntcmds.update(1./hz, movement)
for jnt in jnt_cmd_dict.keys() :
pub[jnt].publish( jnt_cmd_dict[jnt] )
rate.sleep()
thread.join()
if __name__ == "__main__":
try:
if len(sys.argv) == 1:
mode = 0
else:
try:
mode = int(sys.argv[1])
except ValueError:
sys.exit("Bad Argument")
if mode != 0:
mode = 1
num_modules = 16
hz = 100
publish_commands( num_modules, hz )
except rospy.ROSInterruptException:
pass