-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathRobotModel.cpp
executable file
·476 lines (413 loc) · 15.1 KB
/
RobotModel.cpp
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
//
// KinectModel.cpp
// urModernDriverTest
//
// Created by dantheman on 2/20/16.
// Copyright (c) 2016, Daniel Moore, Madaline Gannon, and The Frank-Ratchye STUDIO for Creative Inquiry All rights reserved.
//
#include "RobotModel.h"
using namespace ofxRobotArm;
RobotModel::RobotModel()
{
pose = vector<ofxRobotArm::Pose>();
}
RobotModel::~RobotModel()
{
}
void RobotModel::setup(string path)
{
loadURDF(path);
}
void RobotModel::setup(string path, RobotType type)
{
this->type = type;
loadURDF(path);
// parseURDF(path);
}
ofNode RobotModel::getForwardPose(){
return forwardPose;
}
ofNode RobotModel::getTCPNode(){
return tcpNode;
}
void RobotModel::loadURDF(string path)
{
ofxXmlSettings xml;
if (xml.load(ofToDataPath(path)))
{
ofLog(OF_LOG_NOTICE) << "Loading URDF:" << ofToDataPath(path) << endl;
if (!xml.pushTag("robot"))
{
ofLogFatalError() << "CANNOT FIND ROBOT TAG" << endl;
}
int numLinks = xml.getNumTags("link");
if (numLinks <= 0)
{
ofLogFatalError() << "CANNOT FIND ROBOT LINKS" << endl;
}
ofLog(OF_LOG_NOTICE) << "URDF::NUM LINKS " << numLinks << endl;
int numJoints = xml.getNumTags("joint");
if (numJoints <= 0)
{
ofLogFatalError() << "CANNOT FIND ROBOT JOINTS" << endl;
}
ofLog(OF_LOG_NOTICE) << "URDF::NUM JOINTS " << numJoints << endl;
pose.resize(numJoints);
jointMin.resize(numJoints);
jointMax.resize(numJoints);
nodes.resize(numJoints);
poseRadians.resize(numJoints);
for (int i = 0; i < numJoints; i++)
{
string type = xml.getAttribute("joint", "type", "revolute", i);
string name = xml.getAttribute("joint", "name", "", i);
// if(ofIsStringInString(type, "revolute")){
if(xml.pushTag("joint", i)){
string xyz = xml.getAttribute("origin", "xyz", "0.0 0.0 0.0", 0);
string rot = xml.getAttribute("origin", "rpy", "0.0 0.0 0.0", 0);
jointMin[i] = xml.getAttribute("limit", "lower", -TWO_PI, 0);
jointMax[i] = xml.getAttribute("limit", "upper", TWO_PI, 0);
string axis = xml.getAttribute("axis", "xyz", "0 0 0", 0);
Pose p;
p.name = name;
p.type = type;
vector<string> pos = ofSplitString(xyz, " ");
p.position = ofVec3f(ofToFloat(pos[0]), ofToFloat(pos[1]), ofToFloat(pos[2]));
vector<string> ax = ofSplitString(axis, " ");
p.axis = ofVec3f(ofToFloat(ax[0]), ofToFloat(ax[1]), ofToFloat(ax[2]));
vector<string> ro = ofSplitString(rot, " ");
p.rotOffset = ofVec3f(ofToFloat(ro[0]), ofToFloat(ro[1]), ofToFloat(ro[2]));
p.rotation = 0;
p.orientation.makeRotate(ofRadToDeg(ofToFloat(ro[0])), ofVec3f(1, 0, 0),
ofRadToDeg(ofToFloat(ro[1])), ofVec3f(0, 1, 0),
ofRadToDeg(ofToFloat(ro[2])), ofVec3f(0, 0, 1));
pose[i] = p;
xml.popTag();
if(i > 0){
pose[i].offset = pose[i].position - pose[i-1].position;
nodes[i].setParent(nodes[i-1]);
}
nodes[i].setPosition(pose[i].position * 1000);
nodes[i].setOrientation(pose[i].orientation);
}
}
// }
toolNode.setParent(nodes[nodes.size()-1]);
originNode.setPosition(ofVec3f(0, 0, 0));
nodes[0].setParent(originNode);
setForwardPose(toolNode);
setTCPPose(pose[pose.size()-1]);
for (int i = 0; i < numLinks; i++)
{
if (xml.pushTag("link", i))
{
if(xml.pushTag("inertial"))
{
string xyz = xml.getAttribute("origin", "xyz", "0.0 0.0 0.0", 0);
vector<string> pos = ofSplitString(xyz, " ");
pose[i].link_offset = ofVec3f(ofToFloat(pos[0]), ofToFloat(pos[1]), ofToFloat(pos[2]));
xml.popTag();
}
if (xml.pushTag("visual"))
{
if (xml.pushTag("geometry"))
{
string path = ofToDataPath(xml.getAttribute("mesh", "filename", "", 0), true);
if (path != "")
{
ofLog(OF_LOG_NOTICE) << "LOADING " << path << endl;
ofxAssimpModelLoader loader;
if (loader.loadModel(path, true))
{
ofLog(OF_LOG_NOTICE) << "LOADED 3D FILE" << endl;
ofMesh m;
for (int i = 0; i < loader.getMeshCount(); i++)
{
m.append(loader.getMesh(i));
}
ofLog(OF_LOG_NOTICE) << m.getNumVertices() << endl;
meshes.push_back(m);
}
else if(ofIsStringInString(path, "stl")){
ofxSTLImporter importer;
importer.loadSTL(path);
meshes.push_back(importer.getMesh());
}
else
{
ofLog(OF_LOG_NOTICE) << "NOT LOADED!" << endl;
}
}
xml.popTag();
}
xml.popTag();
}
xml.popTag();
}
}
}
}
void RobotModel::setOrigin(ofVec3f pos, ofQuaternion orientation)
{
originNode.setGlobalPosition(pos);
originNode.setGlobalOrientation(orientation);
}
void RobotModel::setForwardPose(ofNode pose)
{
forwardPose.setGlobalPosition(pose.getGlobalPosition());
forwardPose.setGlobalOrientation(pose.getGlobalOrientation());
}
void RobotModel::setTCPPose(Pose pose)
{
tool = pose;
ofMatrix4x4 mat = toolNode.getLocalTransformMatrix();
ofVec3f pos = toolNode.getPosition();
tool.position = tool.position - pos * mat;
tcpNode.setPosition(tool.position);
tcpNode.setOrientation(pose.orientation);
}
ofQuaternion RobotModel::getToolPointQuaternion()
{
return toolNode.getGlobalOrientation();
}
ofNode RobotModel::getTool()
{
return toolNode;
}
void RobotModel::setToolOffset(ofVec3f localOffset)
{
this->localOffset = localOffset;
toolNode.setPosition(this->localOffset);
}
Pose RobotModel::getModifiedTCPPose()
{
return tool;
}
void RobotModel::setPose(vector<double> pose)
{
int i = 0;
for (auto pdouble : pose)
{
this->pose[i].rotation = ofRadToDeg(pdouble+this->pose[i].rotOffset.length());
poseRadians[i] = pdouble+this->pose[i].rotOffset.length();
this->pose[i].orientation.makeRotate(this->pose[i].rotation, this->pose[i].axis);
nodes[i].setOrientation(this->pose[i].orientation);
i++;
}
}
void RobotModel::setEndEffector(string filename)
{
string path = ofToDataPath("models/" + filename);
loader.clear();
if (loader.loadModel("models/" + filename))
{
toolMesh = loader.getMesh(0);
}
else
{
ofLogFatalError() << "PLEASE PLACE THE 3D FILES OF THE END EFFECTOR IN data/models/" << filename << endl;
}
}
void RobotModel::clearEndEffector()
{
toolMesh.clear();
}
void RobotModel::setToolMesh(ofMesh mesh)
{
toolMesh = mesh;
}
// ----------------------------------------------------------
void RobotModel::drawSkeleton()
{
ofPushStyle();
{
int i = 0;
float dist = 0;
for (auto joint : nodes)
{
ofVec3f p = joint.getGlobalPosition();
ofColor colorOne = ofColor(ofColor::aqua);
ofColor colorTwo = ofColor(ofColor::magenta);
// draw each link
ofPushStyle();
{
float t = i / float(nodes.size());
if (pose[i].axis != ofVec3f(0, 1, 0))
{
ofPushMatrix();
{
ofMultMatrix(joint.getGlobalTransformMatrix());
ofMatrix4x4 mat;
mat.makeRotationMatrix(pose[i].rotation, pose[i].axis);
ofMultMatrix(mat);
ofScale(70, 70, 70);
ofSetLineWidth(3);
ofVec3f forward = ofVec3f(0, 1, 0).getPerpendicular(pose[i].axis);
ofSetColor(colorOne.getLerped(colorTwo, t));
drawArc(jointMin[i] * RAD_TO_DEG, jointMax[i] * RAD_TO_DEG, forward, pose[i].axis);
forward.rotate(pose[i].rotation, pose[i].axis);
ofDrawLine(ofVec3f(), forward);
ofSetColor(colorOne.getLerped(colorTwo, t), 100);
drawArc(jointMin[i] * RAD_TO_DEG, pose[i].rotation, ofVec3f(0, 1, 0).getPerpendicular(pose[i].axis), pose[i].axis, true);
}
ofPopMatrix();
}
else if (pose[i].axis != ofVec3f(0, 0, 1))
{
ofPushMatrix();
{
ofMultMatrix(joint.getGlobalTransformMatrix());
ofMatrix4x4 mat;
mat.makeRotationMatrix(pose[i].rotation, pose[i].axis);
ofMultMatrix(mat);
ofScale(70, 70, 70);
ofSetLineWidth(3);
ofSetColor(colorOne.getLerped(colorTwo, t));
ofVec3f forward = ofVec3f(0, 0, 1).getPerpendicular(pose[i].axis);
drawArc(jointMin[i] * RAD_TO_DEG, jointMax[i] * RAD_TO_DEG, forward, pose[i].axis);
forward.rotate(pose[i].rotation, pose[i].axis);
ofDrawLine(ofVec3f(), forward);
ofSetColor(colorOne.getLerped(colorTwo, t), 100);
drawArc(jointMin[i] * RAD_TO_DEG, pose[i].rotation, ofVec3f(0, 0, 1).getPerpendicular(pose[i].axis), pose[i].axis, true);
}
ofPopMatrix();
}
if (i > 0)
{
ofSetColor(colorOne.getLerped(colorTwo, t));
// draw each joint
joint.draw();
ofSetLineWidth(5);
ofDrawLine(nodes[i - 1].getGlobalPosition(), p);
dist = p.distance(nodes[i - 1].getGlobalPosition());
}
}
ofPopStyle();
// show length of each link
ofSetColor(255, 200);
if (i == 0)
ofDrawBitmapString(dist, p.getInterpolated(ofVec3f(), .5));
else
ofDrawBitmapString(dist, p.getInterpolated(nodes[i - 1].getGlobalPosition(), .5));
// show joint id
ofSetColor(255, 200);
ofDrawBitmapString(ofToString(i), p.x + 5, p.y, p.z + 5);
// show angle at joint
ofDrawBitmapString("angle: " + ofToString(pose[i].rotation), p + ofVec3f(0, 0, 20));
if (ofGetKeyPressed(OF_KEY_CONTROL) && i == 5)
{
ofSetColor(colorOne);
ofDrawBitmapString("pos: " + ofToString(p), p + ofVec3f(0, 0, 40));
}
if (i == nodes.size()-1)
{
ofSetColor(255, 0, 0, 100);
toolNode.draw();
}
i++;
}
}
ofPopStyle();
}
void RobotModel::drawMesh(ofColor color, bool bDrawDebug)
{
ofEnableDepthTest();
{
ofPushStyle();
{
ofColor face = ofColor(color);
ofColor wireframe = ofColor(ofColor::black);
int i = 0;
for (auto mesh : meshes)
{
if (i == 0)
{
ofPushMatrix();
{
ofTranslate(nodes[i].getPosition());
ofScale(1000, 1000, 1000);
ofSetColor(face);
mesh.drawFaces();
ofSetColor(wireframe, 100);
mesh.drawWireframe();
}
ofPopMatrix();
}
else
{
ofPushMatrix();
{
ofMultMatrix(nodes[i-1].getGlobalTransformMatrix());
ofScale(1000, 1000, 1000);
ofSetColor(face);
mesh.drawFaces();
ofSetColor(wireframe, 100);
mesh.drawWireframe();
}
ofPopMatrix();
}
i++;
}
}
ofPopStyle();
}
ofDisableDepthTest();
}
void RobotModel::draw(ofColor color, bool bDrawDebug)
{
ofPushMatrix();
{
ofPushStyle();
{
ofSetColor(255, 255, 255);
if (bDrawDebug)
{
ofPushStyle();
{
ofDrawAxis(1000);
ofSetColor(255, 255, 0);
ofDrawSphere(tool.position * ofVec3f(1000, 1000, 1000), 40);
ofSetColor(225, 225, 225);
}
ofPopStyle();
}
}
ofPopStyle();
}
ofPopMatrix();
}
int RobotModel::getNumJoints(){
return pose.size();
}
void RobotModel::drawArc(float aStartAngleDegrees, float aEndAngleDegrees, ofVec3f aForwardAxis, ofVec3f aSideAxis, bool fill)
{
float startDegrees = aStartAngleDegrees;
float endDegrees = aEndAngleDegrees;
float tangleDiff = endDegrees - startDegrees;
int iterations = fabs(tangleDiff) / 4;
if (iterations < 2)
iterations = 2;
float currDegree = startDegrees;
float tstep = tangleDiff / (float)iterations;
ofVec3f cvec = aForwardAxis;
ofMesh tmesh;
if (fill)
{
tmesh.setMode(OF_PRIMITIVE_TRIANGLE_FAN);
}
else
{
tmesh.setMode(OF_PRIMITIVE_LINE_STRIP);
}
tmesh.addVertex(ofVec3f());
for (int i = 0; i <= iterations; i++)
{
cvec = aForwardAxis;
cvec.rotate(currDegree, aSideAxis);
tmesh.addVertex(cvec);
currDegree += tstep;
currDegree = ofWrapDegrees(currDegree);
}
tmesh.addVertex(ofVec3f());
tmesh.draw();
}