-
Notifications
You must be signed in to change notification settings - Fork 5
/
walkSequenceSolver.js
359 lines (295 loc) · 8.93 KB
/
walkSequenceSolver.js
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import { solveHexapodParams } from "./ik/hexapodSolver"
/* * *
Return format:
poseSequence = {
leftMiddle: {
alpha: [],
beta: [],
gamma: [],
}
....
}
gaitType = ["ripple", "tripod"]
walkMode = ["rotating", "walking"]
* * */
const getWalkSequence = (
dimensions,
params = {
tx: 0,
tz: 0,
rx: 0,
ry: 0,
legStance: 0,
hipStance: 25,
stepCount: 5,
hipSwing: 25,
liftSwing: 40,
},
gaitType = "tripod",
walkMode = "walking"
) => {
const { hipStance, rx, ry, tx, tz, legStance } = params
const rawIKparams = {
tx,
ty: 0,
tz,
legStance,
hipStance,
rx,
ry,
rz: 0,
}
const [ikSolver] = solveHexapodParams(dimensions, rawIKparams, true)
if (!ikSolver.foundSolution || ikSolver.hasLegsOffGround) {
return null
}
const { hipSwing, liftSwing, stepCount } = params
const [aHipSwing, aLiftSwing] = [Math.abs(hipSwing), Math.abs(liftSwing)]
const hipSwings =
walkMode === "rotating"
? getHipSwingRotate(aHipSwing)
: getHipSwingForward(aHipSwing)
return gaitType === "ripple"
? rippleSequence(ikSolver.pose, aLiftSwing, hipSwings, stepCount)
: tripodSequence(ikSolver.pose, aLiftSwing, hipSwings, stepCount)
}
/* *
powerStroke aka stancePhase
returnStroke aka swingPhase
1. > startPowerStroke / endReturnStroke < 6.
\
\
*--*--*
/ | \ /
* -- * -- * /
\ \ | /
\ *--*--*
2. > middlePowerStroke / middleReturnStroke < 5.
--- *--*--*
/ | \
* -- * -- * ---
\ | /
---- *--*--*
3. > endPowerStroke / startReturnStroke < 4.
/ *--*--*
/ / | \
* -- * -- *
\ | / \
*--*--* \
/
/
TRIPOD1 - leftFront, rightMiddle, leftBack
|----- powerStroke ----------|------- returnStroke -------|
|-- liftUp --|-- shoveDown --|
TRIPOD2 rightFront, leftMiddle, rightBack
|------- returnStroke -------|----- powerStroke ----------|
|-- liftUp --|-- shoveDown --|
* */
const tripodSequence = (pose, aLiftSwing, hipSwings, stepCount, walkMode) => {
const { forwardAlphaSeqs, liftBetaSeqs, liftGammaSeqs } = buildTripodSequences(
pose,
aLiftSwing,
hipSwings,
stepCount,
walkMode
)
const doubleStepCount = 2 * stepCount
const tripodA = tripodASequence(
forwardAlphaSeqs,
liftGammaSeqs,
liftBetaSeqs,
doubleStepCount
)
const tripodB = tripodBSequence(
forwardAlphaSeqs,
liftGammaSeqs,
liftBetaSeqs,
doubleStepCount
)
return { ...tripodA, ...tripodB }
}
const tripodASequence = (
forwardAlphaSeqs,
liftGammaSeqs,
liftBetaSeqs,
doubleStepCount
) =>
["leftFront", "rightMiddle", "leftBack"].reduce((sequences, legPosition) => {
const forward = forwardAlphaSeqs[legPosition]
const gammaLiftUp = liftGammaSeqs[legPosition]
const betaLiftUp = liftBetaSeqs[legPosition]
const gammaSeq = [
...gammaLiftUp,
...gammaLiftUp.slice().reverse(),
...fillArray(gammaLiftUp[0], doubleStepCount),
]
const betaSeq = [
...betaLiftUp,
...betaLiftUp.slice().reverse(),
...fillArray(betaLiftUp[0], doubleStepCount),
]
sequences[legPosition] = {
alpha: [...forward, ...forward.slice().reverse()],
gamma: gammaSeq,
beta: betaSeq,
}
return sequences
}, {})
const tripodBSequence = (
forwardAlphaSeqs,
liftGammaSeqs,
liftBetaSeqs,
doubleStepCount
) =>
["rightFront", "leftMiddle", "rightBack"].reduce((sequences, legPosition) => {
const forward = forwardAlphaSeqs[legPosition]
const gammaLiftUp = liftGammaSeqs[legPosition]
const betaLiftUp = liftBetaSeqs[legPosition]
const gammaSeq = [
...fillArray(gammaLiftUp[0], doubleStepCount),
...gammaLiftUp,
...gammaLiftUp.slice().reverse(),
]
const betaSeq = [
...fillArray(betaLiftUp[0], doubleStepCount),
...betaLiftUp,
...betaLiftUp.slice().reverse(),
]
sequences[legPosition] = {
alpha: [...forward.slice().reverse(), ...forward],
gamma: gammaSeq,
beta: betaSeq,
}
return sequences
}, {})
/* * *
RIPPLE SEQUENCE
a - lift-up
b - shove-down
[1, 2, 3, 4] - retract / power stroke sequence
left-back |-- a --|-- b --| 1 | 2 | 3 | 4 |
left-middle | 3 | 4 |-- a --|-- b --| 1 | 2 |
left-front | 1 | 2 | 3 | 4 |-- a --|-- b --|
right-front | 4 |-- a --|-- b --| 1 | 2 | 3 |
right-back | 1 | 2 | 3 |-- a --|-- b --| 4 |
right-middle |-- b --| 1 | 2 | 3 | 4 |-- a --|
* * */
const rippleSequence = (startPose, aLiftSwing, hipSwings, stepCount) => {
const legPositions = Object.keys(startPose)
let sequences = {}
legPositions.forEach(position => {
const { alpha, beta, gamma } = startPose[position]
const betaLift = buildSequence(beta, aLiftSwing, stepCount)
const gammaLift = buildSequence(gamma, -aLiftSwing / 2, stepCount)
const delta = hipSwings[position]
const fw1 = buildSequence(alpha - delta, delta, stepCount)
const fw2 = buildSequence(alpha, delta, stepCount)
const halfDelta = delta / 2
const bk1 = buildSequence(alpha + delta, -halfDelta, stepCount)
const bk2 = buildSequence(alpha + halfDelta, -halfDelta, stepCount)
const bk3 = buildSequence(alpha, -halfDelta, stepCount)
const bk4 = buildSequence(alpha - halfDelta, -halfDelta, stepCount)
// prettier-ignore
sequences[position] = buildRippleLegSequence(
position, betaLift, gammaLift, fw1, fw2, bk1, bk2, bk3, bk4
)
})
return sequences
}
const buildRippleLegSequence = (position, bLift, gLift, fw1, fw2, bk1, bk2, bk3, bk4) => {
const stepCount = fw1.length
const revGLift = gLift.slice().reverse()
const revBLift = bLift.slice().reverse()
const b0 = bLift[0]
const g0 = gLift[0]
// n stands for neutral
const bN = fillArray(b0, stepCount)
const gN = fillArray(g0, stepCount)
const alphaSeq = [fw1, fw2, bk1, bk2, bk3, bk4]
const betaSeq = [bLift, revBLift, bN, bN, bN, bN]
const gammaSeq = [gLift, revGLift, gN, gN, gN, gN]
const moduloMap = {
leftBack: 0,
rightFront: 1,
leftMiddle: 2,
rightBack: 3,
leftFront: 4,
rightMiddle: 5,
}
return {
alpha: modSequence(moduloMap[position], alphaSeq),
beta: modSequence(moduloMap[position], betaSeq),
gamma: modSequence(moduloMap[position], gammaSeq),
}
}
const modSequence = (mod, seq) => {
const sequence = [...seq, ...seq]
return sequence.slice(mod, mod + 6).flat()
}
const buildTripodSequences = (startPose, aLiftSwing, hipSwings, stepCount, walkMode) => {
const doubleStepCount = 2 * stepCount
const legPositions = Object.keys(startPose)
let forwardAlphaSeqs = {}
let liftBetaSeqs = {}
let liftGammaSeqs = {}
legPositions.forEach(legPosition => {
const { alpha, beta, gamma } = startPose[legPosition]
const deltaAlpha = hipSwings[legPosition]
forwardAlphaSeqs[legPosition] = buildSequence(
alpha - deltaAlpha,
2 * deltaAlpha,
doubleStepCount
)
liftBetaSeqs[legPosition] = buildSequence(beta, aLiftSwing, stepCount)
liftGammaSeqs[legPosition] = buildSequence(gamma, -aLiftSwing / 2, stepCount)
})
return {
forwardAlphaSeqs,
liftBetaSeqs,
liftGammaSeqs,
}
}
const buildSequence = (startVal, delta, stepCount) => {
const step = delta / stepCount
let currentItem = startVal
let array = []
for (let i = 0; i < stepCount; i++) {
currentItem += step
array.push(currentItem)
}
return array
}
const getHipSwingForward = aHipSwing => {
return {
leftFront: -aHipSwing,
rightMiddle: aHipSwing,
leftBack: -aHipSwing,
rightFront: aHipSwing,
leftMiddle: -aHipSwing,
rightBack: aHipSwing,
}
}
const getHipSwingRotate = aHipSwing => {
return {
leftFront: aHipSwing,
rightMiddle: aHipSwing,
leftBack: aHipSwing,
rightFront: aHipSwing,
leftMiddle: aHipSwing,
rightBack: aHipSwing,
}
}
const fillArray = (value, len) => {
if (len === 0) {
return []
}
let a = [value]
while (a.length * 2 <= len) {
a = a.concat(a)
}
if (a.length < len) {
a = a.concat(a.slice(0, len - a.length))
}
return a
}
export default getWalkSequence