-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
279 lines (225 loc) · 8.23 KB
/
index.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
var vector = require("vektor").vector,
rotate = require("vektor").rotate;
var ikSolvers = {
ZYY: require("./ikSolvers/ZYY.js")
};
// Wrap our chains into a single object we can control
//
// @param {Object} opts Options: {chains, offset, orientation}
// - opts.chains {Array} An array of chains that makeup this robot
// - opts.robotType {String} One of the predefined robot types.
// - opts.offset {Array} A three element array describing an offset
// for the robot's origin point. This can be used to shift the
// robot's position relative to its origin point (picture the
// robot shifting it's weight along the x/z plane or stretching
// to be taller without moving the end effectors)
// - opts.orientation {Array} A three element array of Numbers
// giving the rotation of the robot's chassis around its
// origin point (make it dance! )
function Robot(opts) {
if (!(this instanceof Robot)) {
return new Robot(opts);
}
this.chains = opts.chains;
this.offset = opts.offset || [0, 0, 0];
if (!opts.orientation) {
opts.orientation = {};
}
this.orientation = {
pitch: opts.orientation.pitch || 0,
yaw: opts.orientation.yaw || 0,
roll: opts.orientation.roll || 0
};
}
// Call the @@normalize function on each of the chains
Robot.prototype["@@normalize"] = function(keyFrameSet) {
keyFrameSet.forEach(function( keyFrames, index ) {
keyFrames = this.chains[index]["@@normalize"]([keyFrames])[0];
keyFrames = this.chains[index].devices["@@normalize"]([keyFrames])[0];
}, this);
return keyFrameSet;
};
// Find the solution for each chain. Make sure that all requied chains
// have a valid solution before rendering the robot movement
Robot.prototype["@@render"] = function(opts) {
var passed = true;
opts = opts || {};
this.chains.forEach( function(chain, index) {
if (typeof opts[index] === "undefined") {
opts[index] = chain.devices.last.target;
}
if (!(chain.solve({position: opts[index], orientation: this.orientation, offset: this.offset }))) {
passed = false;
}
}, this);
if (passed) {
this.chains.forEach( function(chain) {
chain.devices["@@render"](chain.angles);
});
}
};
// Wrap our servos object so that we have all the info and
// methods we need to define and solve our the kinematic system
//
// @param {Object} opts Options: {actuators, systemType, origin, bones }
// - opts.actuators {Servos}: The Servos() object that contains the chain's actuators
// - opts.chainType {String}: One of the pre-defined chain types. Don't see your
// system type? Open an issue, or better yet a Pull Request!
// - opts.origin {Array}: A three-tuple representing the x, y and z offset of the
// chain's origin point from the robot's origin point.
// - opts.links {Object}: An array with the link lengths for our system
// The names vary with the systemType
//
// returns this chain
function Chain(opts) {
if (!(this instanceof Chain)) {
return new Chain(opts);
}
if (opts.constructor) {
this.devices = new opts.constructor(opts.actuators);
} else {
this.devices = opts.actuators;
}
this.chainType = opts.chainType;
this.links = opts.links;
this.origin = opts.origin || [0, 0, 0];
this.position = opts.startAt || [0, 0, 0];
this.require = opts.require || true;
}
// // Move all the servos so our end effector is at the
// // target position.
// //
// // returns the Servos() object this was called upon
// Chain.prototype.render = function( opts ) {
// this.solve();
// this.forEach(function(device, index) {
// // This will call the device's render method
// device["@@render"](this.angles[index]);
// });
// };
// Find the angles needed to position the end effector
// at a desired point
//
// @param {Object} opts Options: {chain, position, orientation, type}
// - opts.chain {Servos*}: Typically a Johnny-Five Servos() instance
// that contains the servos used in the chain. Prior to using this
// method the Servos must be run through tharp.makeChain().
// - opts.origin {Array}: Three tuple of kinematic chain origin relative
// to the robots origin point
// - opts.orientation {Object}: {[pitch][, roll][, yaw]}
// pitch, roll and yaw are given in radians
// - opts.type {String}: The type of chain (see the readme for
// a list of types)
// - opts.immediate {Boolean}: Will move the servos immediately this
// is usually a bad idea since other legs (chains) of your robot
// may not have a valid solution and will not move.
//
// returns a three tuple [x, y, z]
Chain.prototype.solve = function( opts ) {
opts = opts || {};
if (opts) {
this.position = opts.position || this.position;
this.orientation = opts.orientation || this.orientation;
this.offset = opts.offset || this.offset;
}
// Find the end effector position relative to the chain origin
var offsetPosition = this.eePosition({
position: this.position,
origin: this.origin,
offset: this.offset,
orientation: this.orientation
});
this.angles = ikSolvers[this.chainType](this, offsetPosition);
if (opts.immediate) {
this.devices["@@render"](this.angles);
}
// If all the joints could be solved, return true and update last
if (this.angles.indexOf(false) === -1) {
this.devices.last = {
target: this.position
};
return true;
} else {
return false;
}
};
Chain.prototype["@@normalize"] = function(keyFrameSet) {
keyFrameSet.forEach( function(keyFrames) {
var last;
// If first element is null, use last position
if (keyFrames[0] === null) {
keyFrames[0] = { position: this.position };
}
keyFrames.forEach( function(keyFrame, keyFrameIndex) {
// If false is passed, use prior keyFrame value
if (keyFrame === false) {
keyFrames[keyFrameIndex] = last;
keyFrame = last;
}
// Handle false being passed in a three tuple
if (keyFrame !== null && Array.isArray(keyFrame.position)) {
keyFrame.position.forEach ( function(vector, vectorIndex) {
if (vector === false) {
keyFrames[keyFrameIndex].position[vectorIndex] = keyFrames[keyFrameIndex - 1].position[vectorIndex];
}
});
}
if (keyFrame !== null) {
last = keyFrame;
}
});
// If last element is null, use last position
if (keyFrames[keyFrames.length - 1] === null) {
keyFrames[keyFrames.length - 1] = last;
}
}, this);
return keyFrameSet;
};
// Find an end effector's position relative to the kinematic chain origin
//
// @param {Object} opts Options: {position[, origin][, orientation] }
// - opts.position {Array}: Three tuple of end effector position
// relative to the robots origin point
// - opts.origin {Array}: Three tuple of kinematic chain origin relative
// to the robots origin point
// - opts.orientation {Object}: {[pitch][, roll][, yaw]}
// pitch, roll and yaw are given in radians
//
// returns a three tuple [x, y, z]
Chain.prototype.eePosition = function(opts) {
var pos = opts.position || [0, 0, 0];
var oPos = opts.origin || [0, 0, 0];
var orientation = opts.orientation || {};
var roll = orientation.roll || 0;
var pitch = orientation.pitch || 0;
var yaw = orientation.yaw || 0;
var offset = opts.offset || [0, 0, 0];
var xOffset = offset[0] || 0;
var yOffset = offset[1] || 0;
var zOffset = offset[2] || 0;
pos = [pos[0] - xOffset, pos[1] - yOffset, pos[2] - zOffset];
// End effector position
var posVector = new vector(pos);
// Chain origin position
var oPosVector = new vector(oPos);
var rotationMatrix = new rotate.RotY(roll);
posVector = rotationMatrix.dot(posVector);
oPosVector = rotationMatrix.dot(oPosVector);
rotationMatrix = new rotate.RotX(pitch);
posVector = rotationMatrix.dot(posVector);
oPosVector = rotationMatrix.dot(oPosVector);
rotationMatrix = new rotate.RotZ(yaw);
posVector = rotationMatrix.dot(posVector);
oPosVector = rotationMatrix.dot(oPosVector);
// We need to subtract the chain origin from the desired position
// but there is no vector.subtract() so let's invert the origin
// and then add the vectors together
oPosVector = oPosVector.scale(-1);
posVector = posVector.add(oPosVector);
return posVector.v;
};
module.exports = {
Chain: Chain,
Robot: Robot,
ikSolvers: ikSolvers
};