forked from manaspaldhe12/hubo-simple-demo-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion2ach.py
229 lines (195 loc) · 6.68 KB
/
motion2ach.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
222
223
224
225
226
227
import hubo_ach as hubo
import ach
import sys
import time
from ctypes import *
from numpy import *
from numpy import linalg
from time import *
import datetime
def moveRobot(trajectory_matrix, delay = 0, sendDataToAch=1, compliance_mode=1):
#print trajectory_matrix
# The joint array
default_trajectory=matrix(zeros((100,27)))
for row_iterator in range(0,100):
default_trajectory[row_iterator,23]=-(float)(row_iterator)/200
trajectory_matrix=default_trajectory
joint_array=["WST","LHY", "LHR", "LHP", "LKN", "LAP", "LAR", "RHY", "RHR", "RHP", "RKN", "RAP", "RAR", "LSP", "LSR", "LSY", "LEB", "LWY", "LWP", "LWR", "RSP", "RSR", "RSY", "REB", "RWY", "RWP", "RWR"];
no_of_rows=trajectory_matrix.shape[0]
no_of_columns=trajectory_matrix.shape[1]
converted_trajectory_matrix=convertToHuboAch(trajectory_matrix, no_of_rows, no_of_columns, joint_array, writeToFile=1)
if (checkMotionSteps(converted_trajectory_matrix)==False):
sendDataToAch=0;
if (no_of_columns != 27):
print "error: number of coluumns are not 27: " + str(no_of_columns)+" columns found"
# Open Hubo-Ach feed-forward and feed-back (reference and state) channels
s = ach.Channel(hubo.HUBO_CHAN_STATE_NAME)
r = ach.Channel(hubo.HUBO_CHAN_REF_NAME)
s.flush()
r.flush()
# feed-forward will now be refered to as "state"
state = hubo.HUBO_STATE()
# feed-back will now be refered to as "ref"
ref = hubo.HUBO_REF()
for trajectory_iterator in range(0,no_of_rows):
# Get the current feed-forward (state)
[statuss, framesizes] = s.get(state, wait=False, last=False)
#Send the commands
for joint in range (0,32):
#print converted_trajectory_matrix[trajectory_iterator,joint]
ref.ref[joint]=converted_trajectory_matrix[trajectory_iterator,joint]
ref.comply[joint]=compliance_mode;
# Write to the feed-forward channel
if (sendDataToAch==1):
r.put(ref)
sleep(delay)
# Close the connection to the channels
r.close()
s.close()
return 0
def jointMap (jointname, joint_array):
for i in range(0,size(joint_array)):
if (joint_array[i]==jointname):
return i
return -1
def getJointName(joint):
if (joint==0):
return "WST"
elif (joint==1):
return "NKY"
elif (joint==2):
return "NK1"
elif (joint==3):
return "NK2"
elif (joint==4):
return "LSP"
elif (joint==5):
return "LSR"
elif (joint==6):
return "LSY"
elif (joint==7):
return "LEB"
elif (joint==8):
return "LWY"
elif (joint==9):
return "LWR"
elif (joint==10):
return "LWP"
elif (joint==11):
return "RSP"
elif (joint==12):
return "RSR"
elif (joint==13):
return "RSY"
elif (joint==14):
return "REB"
elif (joint==15):
return "RWY"
elif (joint==16):
return "RWR"
elif (joint==17):
return "RWP"
elif (joint==18):
return "NULL"
elif (joint==19):
return "LHY"
elif (joint==20):
return "LHR"
elif (joint==21):
return "LHP"
elif (joint==22):
return "LKN"
elif (joint==23):
return "LAP"
elif (joint==24):
return "LAR"
elif (joint==25):
return "NULL"
elif (joint==26):
return "RHY"
elif (joint==27):
return "RHR"
elif (joint==28):
return "RHP"
elif (joint==29):
return "RKN"
elif (joint==30):
return "RAP"
elif (joint==31):
return "RAR"
def writeToFile(martix):
file_matrix = open('trajectoryGenerated.traj', 'w')
no_of_rows=matrix.shape[0]
no_of_columns=matrix.shape[1]
for row_iterator in range(0, no_of_rows):
row_to_be_written=""
for column_iterator in range(0, no_of_columns):
row_to_be_written=str(matrix[row_iterator, column_iterator])+", "
file_matrix.write(row_to_be_written)
def convertToHuboAch(trajectory_matrix, no_of_rows, no_of_columns, joint_array, writeToFile):
no_of_columns=32
converted_matrix=matrix([[0.0 for col in range(no_of_columns)] for row in range(no_of_rows)])
for joint in range(0,no_of_columns):
jointname=getJointName(joint)
joint_in_our_array=jointMap(jointname, joint_array)
for trajectory_setpoint in range(0,no_of_rows):
if (joint_in_our_array==14):
converted_matrix[trajectory_setpoint,joint]=trajectory_matrix[trajectory_setpoint,joint_in_our_array]
else:
converted_matrix[trajectory_setpoint, joint]=0
writeToFile(converted_matrix)
return converted_matrix
def checkMotionSteps(trajectory_matrix):
no_of_rows=trajectory_matrix.shape[0]
no_of_columns=trajectory_matrix.shape[1]
jump_threshold=0.03
is_fine =True
for column in range (0, no_of_columns):
last_value=0;
for row in range (0, no_of_rows):
if (abs(trajectory_matrix[row,column]-last_value)>jump_threshold):
print str(row)+" th row, "+str(column)+" th column has jump more than threshold"
is_fine=False
last_value=trajectory_matrix[row, column]
return is_fine;
def getForces():
s = ach.Channel(hubo.HUBO_CHAN_STATE_NAME)
s.flush()
state = hubo.HUBO_STATE()
[statuss, framesizes] = s.get(state, wait=False, last=False)
forces=[0,0,0,0,0,0,0,0,0,0,0,0];
forces[0]=state.ft[hubo.HUBO_FT_L_HAND].m_x
forces[1]=state.ft[hubo.HUBO_FT_L_HAND].m_y
forces[2]=state.ft[hubo.HUBO_FT_L_HAND].f_z
forces[3]=state.ft[hubo.HUBO_FT_R_HAND].m_x
forces[4]=state.ft[hubo.HUBO_FT_R_HAND].m_y
forces[5]=state.ft[hubo.HUBO_FT_R_HAND].f_z
forces[6]=state.ft[hubo.HUBO_FT_L_FOOT].m_x
forces[7]=state.ft[hubo.HUBO_FT_L_FOOT].m_y
forces[8]=state.ft[hubo.HUBO_FT_L_FOOT].f_z
forces[9]=state.ft[hubo.HUBO_FT_R_FOOT].m_x
forces[10]=state.ft[hubo.HUBO_FT_R_FOOT].m_y
forces[11]=state.ft[hubo.HUBO_FT_R_FOOT].f_z
t=str(datetime.datetime.now())
s.close()
return [forces, t]
def getEncoders():
s = ach.Channel(hubo.HUBO_CHAN_STATE_NAME)
s.flush()
state = hubo.HUBO_STATE()
[statuss, framesizes] = s.get(state, wait=False, last=False)
encoders= [0.0]*28 #(0.0 for i in range(0,27))
for i in range (0,27):
encoders[i]=state.joint[i].pos
t=str(datetime.datetime.now())
s.close()
return [encoders,t]
if __name__ == "__main__":
default_trajectory=matrix([[0.0 for col in range(27)] for row in range(100)])
f=getForces();
e=getEncoders();
print f
print e
for row_iterator in range(0,100):
default_trajectory[row,23]=-row_iterator/100
sleep(0.1)