-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathABBDriver.cpp
294 lines (250 loc) · 8.41 KB
/
ABBDriver.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
//
// ABBDriver.cpp
// urModernDriverTest
//
// Created by dantheman on 2/20/16.
//
// Copyright (c) 2016, 2021 Daniel Moore, Madeline Gannon, and The Frank-Ratchye STUDIO for Creative Inquiry All rights reserved.
////
#include "ABBDriver.h"
using namespace ofxRobotArm;
ABBDriver::ABBDriver(){
ofLog()<<"ABBDriver"<<endl;
currentSpeed.assign(numJoints, 0.0);
acceleration = 0.0;
robot = NULL;
bStarted =false;
// -0.25, 0.25, 0.13, 2.5, 0.45, -2.59
vector<double> foo;
foo.assign(numJoints, 0.0001);
poseRaw.setup(foo);
toolPoseRaw.setup(foo);
poseProcessed.setup(foo);
poseRaw.getBack().assign(numJoints, 0.0001);
poseProcessed.getBack().assign(numJoints, 0.0001);
toolPoseRaw.getBack().assign(numJoints, 0.0001);
numDeccelSteps = 120;
}
ABBDriver::~ABBDriver(){
if(robot->isInitialized()){
disconnect();
}
}
void ABBDriver::stopThread(){
if(isConnected()){
disconnect();
}
if(isThreadRunning()){
ofThread::stopThread();
}
}
void ABBDriver::toggleTeachMode(){
lock();
if(bTeachModeEnabled){
bTeachModeEnabled = false;
}else{
bTeachModeEnabled = true;
}
unlock();
}
vector<double> ABBDriver::getInitPose(){
vector<double> foo;
foo.assign(6, 0.0001);
return foo;
}
void ABBDriver::setTeachMode(bool enabled){
lock();
if(enabled){
bTeachModeEnabled = true;
}else{
bTeachModeEnabled = false;
}
unlock();
}
void ABBDriver::setAllowReconnect(bool bDoReconnect){
bTryReconnect = bDoReconnect;
}
void ABBDriver::setup(){
}
void ABBDriver::setup(string ipAddress, double minPayload, double maxPayload){
}
void ABBDriver::setup(string ipAddress, int port, double minPayload, double maxPayload){
}
void ABBDriver::setup(int port, double minPayload, double maxPayload){
ofLog(OF_LOG_NOTICE) << "ABBDriver :: setup : " << port << endl;
poseProcessed.swapBack();
poseRaw.swapBack();
toolPoseRaw.swapBack();
bStarted = false;
bTriedOnce = false;
abb::egm::BaseConfiguration configuration;
robot = std::make_unique<abb::egm::EGMControllerInterface>(io_service, port, configuration);
if(!robot->isInitialized())
{
ofLog(OF_LOG_ERROR)<<"EGM interface failed to initialize (e.g. due to port already bound)"<<endl;
}else{
ofLog(OF_LOG_NOTICE)<<"EGM interface initialized"<<endl;
}
thread_group.create_thread(boost::bind(&boost::asio::io_service::run, &io_service));
wait = true;
}
void ABBDriver::start(){
ofLog(OF_LOG_NOTICE)<<"Starting ABBDriver Controller"<<endl;
startThread();
}
bool ABBDriver::isConnected() {
if( ofThread::isThreadRunning() ) {
bool tConn = false;
if(lock()) {
tConn = bStarted;
unlock();
}
return tConn;
}
return false;
}
void ABBDriver::disconnect(){
io_service.stop();
thread_group.join_all();
stopThread();
}
bool ABBDriver::isDataReady(){
if(bDataReady){
bDataReady = false;
return true;
}else{
return false;
}
}
vector<double> ABBDriver::getToolPointRaw(){
vector<double> ret;
lock();
toolPoseRaw.swapFront();
ret = toolPoseRaw.getFront();
unlock();
return ret;
}
vector<double> ABBDriver::getCurrentPose(){
vector<double> ret;
lock();
poseRaw.swapFront();
ret = poseRaw.getFront();
unlock();
return ret;
}
ofVec4f ABBDriver::getCalculatedTCPOrientation(){
ofVec4f ret;
lock();
ret = ofVec4f(dtoolPoint.orientation.x(), dtoolPoint.orientation.y(), dtoolPoint.orientation.z(), dtoolPoint.orientation.w());
unlock();
return ret;
}
ofxRobotArm::Pose ABBDriver::getToolPose(){
ofxRobotArm::Pose ret;
lock();
ret = tool;
unlock();
return ret;
}
void ABBDriver::setSpeed(vector<double> speeds, double accel){
lock();
currentSpeed = speeds;
acceleration = accel;
bMove = true;
bMoveWithPos = false;
unlock();
}
void ABBDriver::setPose(vector<double> pose){
lock();
currentPose = pose;
bMove = true;
bMoveWithPos = true;
deccelCount = numDeccelSteps+4;
bStop = false;
unlock();
}
void ABBDriver::setToolOffset(ofVec3f localPos){
}
void ABBDriver::threadedFunction(){
while(isThreadRunning()){
timer.tick();
if(!bStarted && !bTriedOnce) {
if( robot ) {
if(wait){
if(robot->isConnected())
{
if(robot->getStatus().rapid_execution_state() == abb::egm::wrapper::Status_RAPIDExecutionState_RAPID_UNDEFINED)
{
ofLogWarning()<<"RAPID execution state is UNDEFINED (might happen first time after controller start/restart). Try to restart the RAPID program.";
}
else
{
wait = robot->getStatus().rapid_execution_state() != abb::egm::wrapper::Status_RAPIDExecutionState_RAPID_RUNNING;
}
}else{
ofLogError()<<"NOT CONNECTRED"<<endl;
abb::egm::wrapper::Status s = robot->getStatus();
}
}else{
bStarted = true;
bTriedOnce = true;
}
}
}else{
if(robot->waitForMessage(500))
{
// Read the message received from the EGM client.
robot->read(&input);
sequence_number = input.header().sequence_number();
actualPose.CopyFrom(input.feedback().robot().joints().position());
for(int i = 0 ; i < actualPose.values_size(); i++){
poseRaw.getBack()[i] = ofDegToRad(actualPose.values(i));
}
currentPoseRadian = poseRaw.getBack();
poseProcessed.getBack() = poseRaw.getBack();
if(sequence_number == 0)
{
output.Clear();
initial_positions.CopyFrom(input.feedback().robot().joints().position());
output.mutable_robot()->mutable_joints()->mutable_position()->CopyFrom(initial_positions);
}
else
{
initial_positions.CopyFrom(input.feedback().robot().joints().position());
output.mutable_robot()->mutable_joints()->mutable_position()->CopyFrom(initial_positions);
time = sequence_number/((double) egm_rate);
if(bMoveWithPos){
//if we aren't moving but deccelCount isn't 0 lets deccelerate
if( (bMove && currentPose.size()>0)|| (currentPose.size()>0 && deccelCount>0) ){
timeNow = ofGetElapsedTimef();
if( bMove || timeNow-lastTimeSentMove >= 1.0/60.0){
currentPose = getAchievablePosition(currentPose);
if(output.mutable_robot()->mutable_joints()->mutable_position()->values_size() == currentPose.size())
{
for(int i = 0 ; i < currentPose.size(); i++){
output.mutable_robot()->mutable_joints()->mutable_position()->set_values(i, ofRadToDeg(currentPose[i]));
}
}else{
ofLog()<<"OUTPUT NOT BIG ENOUGH size "<<output.mutable_robot()->mutable_joints()->mutable_position()->values_size()<<endl;
}
if(!bMove){
deccelCount--;
if( deccelCount < 0){
deccelCount = 0;
}
}
lastTimeSentMove = timeNow;
}
bMove = false;
}
}
}
// Write references back to the EGM client.
robot->write(output);
toolPoseRaw.swapBack();
poseRaw.swapBack();
poseProcessed.swapBack();
}
}
}
}