-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathLegacyRobotController.cpp
executable file
·439 lines (373 loc) · 11.5 KB
/
LegacyRobotController.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
#include "LegacyRobotController.h"
//
// Copyright (c) 2016, 2021 Daniel Moore, Madeline Gannon, and The Frank-Ratchye STUDIO for Creative Inquiry All rights reserved.
////
using namespace ofxRobotArm;
LegacyRobotController::LegacyRobotController()
{
}
LegacyRobotController::~LegacyRobotController()
{
}
void LegacyRobotController::setup(string ipAddress, int port, string urdfPath, RobotType robotType, IKType ikType, bool offline)
{
this->robotType = robotType;
setAddress(ipAddress);
setPort(port);
createRobot(this->robotType);
loadURDF(urdfPath);
setupRobot(offline);
initKinematics(ikType);
setupParams();
}
void LegacyRobotController::setAddress(string ipAddress){
this->ipAddress = ipAddress;
}
void LegacyRobotController::setPort(int port){
this->port = port;
}
void LegacyRobotController::createRobot(RobotType type)
{
robotType = type;
if (robotType == UR3 || robotType == UR5 || robotType == UR10)
{
robot = new URDriver();
}
else if (robotType == IRB120 || robotType == IRB4600 || robotType == IRB6700)
{
robot = new ABBDriver();
}
else if (robotType == XARM7)
{
robot = new XARMDriver(type);
}
else if(robotType == PANDA){
robot = new PandaDriver();
}
}
void LegacyRobotController::loadURDF(string urdfpath)
{
desiredModel.setup(urdfpath, robotType);
actualModel.setup(urdfpath, robotType);
}
void LegacyRobotController::setupRobot(bool offline)
{
if (!offline && robot != nullptr)
{
robot->setAllowReconnect(bDoReconnect);
if (robotType == UR3 || robotType == UR5 || robotType == UR10)
{
robot->setup(ipAddress, 0, 1);
}
else if (robotType == IRB120 || robotType == IRB4600 || robotType == IRB6700)
{
robot->setup(port, 0, 1);
}
else if (robotType == XARM7)
{
robot->setup(ipAddress, 0, 1);
}
}
}
void LegacyRobotController::disconnectRobot()
{
if (robot != nullptr)
{
robot->disconnect();
}
}
void LegacyRobotController::setupParams()
{
robotArmParams.setName("Robot Controls");
robotArmParams.add(bLive.set("Live", false));
robotArmParams.add(bTeachMode.set("Enable Teach Mode", false));
robotArmParams.add(bDoReconnect.set("TryReconnect", false));
robotArmParams.add(bSmoothPose.set("Smooth Pose", true));
robotArmParams.add(bOverrideNthJoint.set("Override Nth Joint", false));
robotArmParams.add(nthJoint.set("Nth Joint", 0, -TWO_PI, TWO_PI));
robotArmParams.add(origin.set("Origin", ofVec3f(0, 0, 0), ofVec3f(-500, -500, -500), ofVec3f(500, 500, 500)));
joints.setName("Joint Pos");
targetJoints.setName("Target Joints");
jointsIK.setName("IK Solver");
for (unsigned int i = 0; i < currentPose.size(); i++)
{
pCurrentPose.push_back(ofParameter<double>());
joints.add(pCurrentPose.back().set("actual joint " + ofToString(i), 0, -360, 360));
}
for (unsigned int i = 0; i < currentPose.size(); i++)
{
pTargetPose.push_back(ofParameter<double>());
targetJoints.add(pTargetPose.back().set("target joint " + ofToString(i), 0, -360, 360));
pIkPose.push_back(ofParameter<double>());
jointsIK.add(pIkPose.back().set("ik joint " + ofToString(i), 0, -360, 360));
}
joints.add(tcpPosition.set("Actual Robot TCP POS", ofVec3f(0, 0, 0), ofVec3f(-1, -1, -1), ofVec3f(1, 1, 1)));
joints.add(tcpOrientation.set("Actual Robot TCP ORIENT", ofVec4f(0, 0, 0, 1), ofVec4f(-1, -1, -1, -1), ofVec4f(1, 1, 1, 1)));
joints.add(calcTCPOrientation.set("Relaxed Robot TCP ORIENT", ofVec4f(0, 0, 0, 1), ofVec4f(-1, -1, -1, -1), ofVec4f(1, 1, 1, 1)));
joints.add(forwardTCPOrientation.set("Forward TCP ORIENT", ofVec4f(0, 0, 0, 1), ofVec4f(-1, -1, -1, -1), ofVec4f(1, 1, 1, 1)));
joints.add(forwardTCPPosition.set("Forward TCP Pos", ofVec3f(0, 0, 0), ofVec3f(-1, -1, -1), ofVec3f(1, 1, 1)));
}
void LegacyRobotController::setNthJoint(double pose)
{
nthJoint.set(pose);
}
void LegacyRobotController::setRobotOrigin(ofVec3f origin, ofQuaternion orientation)
{
this->origin = origin;
desiredModel.setOrigin(this->origin, orientation);
actualModel.setOrigin(this->origin, orientation);
}
void LegacyRobotController::initKinematics(ofxRobotArm::IKType ikType)
{
vector<double> pose = robot->getInitPose();
jointWeights.assign(pose.size(), 1.0f);
smoothedPose.assign(pose.size(), 0.0f);
for(int i = 0 ; i < pose.size(); i++){
ofParameter<double> smooth;
smooth.set("Smooth-"+ofToString(i), 0.1, 0.001, 1.0);
robotArmParams.add(smooth);
smoothedWeights.push_back(smooth);
}
inverseKinematics.setup(robotType, ikType, pose, &actualModel);
ofMatrix4x4 forwardIK = inverseKinematics.forwardKinematics(pose);
forwardNode.setGlobalPosition(forwardIK.getTranslation());
forwardNode.setGlobalOrientation(forwardIK.getRotate());
initPose.position = forwardIK.getTranslation();
initPose.orientation = forwardIK.getRotate();
int i = 0;
for (auto joint : smoothedPose)
{
joint = pose[i];
i++;
}
}
void LegacyRobotController::setIKType(ofxRobotArm::IKType ikType){
inverseKinematics.setIKType(ikType);
}
void LegacyRobotController::setPoseExternally(bool externally){
bSetPoseExternally = externally;
}
void LegacyRobotController::toggleLive()
{
bLive = !bLive;
}
bool LegacyRobotController::isLive()
{
return bLive.get();
}
bool LegacyRobotController::isConnected()
{
return robot->isConnected();
}
void LegacyRobotController::setEnableMovement(bool move){
bLive = move;
}
void LegacyRobotController::setHomePose(vector<double> pose)
{
homePose = pose;
}
bool LegacyRobotController::isPoseControlledExternally()
{
return bSetPoseExternally;
}
void LegacyRobotController::setEndEffector(string filename)
{
actualModel.setEndEffector(filename);
desiredModel.setEndEffector(filename);
}
void LegacyRobotController::setToolOffset(ofVec3f local)
{
desiredModel.setToolOffset(local/1000);
actualModel.setToolOffset(local/1000);
}
void LegacyRobotController::startConnection()
{
// Start the connection to the actual robot over TCP/IP
if(robot != nullptr){
robot->start();
}
}
vector<double> LegacyRobotController::getCurrentPose()
{
return robot->getCurrentPose();
}
//------------------------------------------------------------------
void LegacyRobotController::toggleTeachMode()
{
robot->toggleTeachMode();
}
void LegacyRobotController::setTeachMode(bool teachMode)
{
robot->setTeachMode(teachMode);
}
#pragma mark - IK
void LegacyRobotController::updateIK(Pose pose)
{
targetPoses = inverseKinematics.inverseKinematics(pose, initPose);
if(targetPoses.size() > 0){
targetPose = targetPoses[0];
}
ofQuaternion rot = initPose.orientation.inverse() * pose.orientation;
calcTCPOrientation = ofVec4f(rot.x(), rot.y(), rot.z(), rot.w());
int i = 0;
for (auto p : targetPose)
{
float tpose = (float)p;
if (isnan(tpose))
{
tpose = 0.f;
}
p = tpose;
i++;
}
if (bOverrideNthJoint)
{
targetPose[targetPose.size() - 1] = nthJoint.get();
}
}
#pragma mark - Update
void LegacyRobotController::update()
{
updateRobotData();
if(!bSetPoseExternally)updateIK(desiredModel.getModifiedTCPPose());
updateMovement();
}
void LegacyRobotController::update(vector<double> _pose)
{
updateRobotData();
targetPose = _pose;
updateMovement();
if (targetPose.size() > 0)
{
ofMatrix4x4 forwardIK = inverseKinematics.forwardKinematics(targetPose);
ofNode fN;
fN.setGlobalPosition(forwardIK.getTranslation());
fN.setGlobalOrientation(forwardIK.getRotate());
desiredModel.setPose(targetPose);
desiredModel.setForwardPose(fN);
}
}
#pragma mark - Data
//READS AND SETS IMPORTANT GUI INFO AND THE CURRENT POSE OF THE ARM
void LegacyRobotController::updateRobotData()
{
// pass the current joints from the robot to the kinematic solver
currentPose = getCurrentPose();
if(currentPose.size() > 0){
actualModel.setPose(currentPose);
ofMatrix4x4 forwardIK = inverseKinematics.forwardKinematics(currentPose);
ofVec3f translation = forwardIK.getTranslation();
forwardNode.setGlobalPosition(translation);
forwardNode.setGlobalOrientation(forwardIK.getRotate());
forwardPose.position = translation;
forwardPose.orientation = forwardIK.getRotate();
forwardTCPPosition = forwardIK.getTranslation()*1000;
forwardTCPOrientation = forwardIK.getRotate().asVec4();
actualModel.setForwardPose(forwardNode);
actualTCP = robot->getToolPose();
tcpPosition = actualTCP.position;
actualModel.setTCPPose(actualTCP);
tcpOrientation = actualTCP.orientation.asVec4();
int i = 0;
for (auto p : pCurrentPose)
{
p = ofRadToDeg((float)currentPose[i++]);
}
}
}
#pragma mark - Movements
void LegacyRobotController::updateMovement()
{
tcp_plane.update(target);
if (bSmoothPose)
{
int i = 0;
for (auto p : targetPose)
{
smoothedPose[i] = ofLerp(smoothedPose[i], p, smoothedWeights[i]);
targetPose[i] = smoothedPose[i];
i++;
}
}
if(bHome && homePose.size() == targetPose.size())
{
int i = 0;
for(auto p: targetPose)
{
p = ofLerp(currentPose[i], homePose[i], smoothedWeights[i]);
i++;
}
}
if (targetPose.size() > 0)
{
ofMatrix4x4 forwardIK = inverseKinematics.forwardKinematics(targetPose);
ofNode fN;
fN.setGlobalPosition(forwardIK.getTranslation());
fN.setGlobalOrientation(forwardIK.getRotate());
desiredModel.setPose(targetPose);
desiredModel.setForwardPose(fN);
}
if (bLive)
{
robot->setPose(targetPose);
stopPosition = targetPose;
stopCount = 30;
}
else
{
if (stopCount > 0 && stopPosition.size() == targetPose.size() && stopPosition.size() > 0)
{
for (int d = 0; d < stopPosition.size(); d++)
{
stopPosition[d] *= 0.9998;
}
robot->setPose(stopPosition);
cout << " Doing stop count " << stopCount << endl;
stopCount--;
}
}
}
void LegacyRobotController::setDesiredPose(ofNode target)
{
// convert from mm to m
targetTCP.position = (target.getGlobalPosition()) / 1000.0;
targetTCP.orientation = target.getGlobalOrientation();
desiredModel.setTCPPose(targetTCP);
this->target = target;
}
ofNode LegacyRobotController::getTCPNode()
{
return actualModel.getTool();
}
ofNode LegacyRobotController::getForwardNode(){
return forwardNode;
}
void LegacyRobotController::close()
{
if (robot->isThreadRunning())
{
robot->stopThread();
}
}
#pragma mark - drawing
void LegacyRobotController::drawActual(ofColor color, bool debug)
{
ofPushMatrix();
{
actualModel.drawMesh(color, debug);
actualModel.draw(color, debug);
// actualModel.drawSkeleton();
}
ofPopMatrix();
}
void LegacyRobotController::drawDesired(ofColor color)
{
ofPushMatrix();
{
desiredModel.drawMesh(color, false);
desiredModel.draw(color, false);
desiredModel.drawSkeleton();
}
ofPopMatrix();
tcp_plane.draw();
}