-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDensoRobot.cpp
More file actions
268 lines (222 loc) · 8.28 KB
/
DensoRobot.cpp
File metadata and controls
268 lines (222 loc) · 8.28 KB
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
#include "PJLib.h"
#include "CAO_i.c"
/* GENERAL NOTES: */
/* - The L in front of a CComBSTR will use a unicode string at compile time (no L will use ANSI) */
namespace PJLib
{
DensoRobot::DensoRobot(string robotIP, double robotSpeed)
{
this->isConnected = false;
this->robotIP = robotIP;
this->robotSpeed = robotSpeed;
}
void DensoRobot::Connect()
{
if(!isConnected)
{
CoInitialize(0);
// Setup Robot
this->hr = S_OK;
/* Create CaoEngine */
hr = CoCreateInstance(CLSID_CaoEngine, NULL, CLSCTX_LOCAL_SERVER, IID_ICaoEngine, (void **)&pEngine);
if(FAILED(hr)) throw exception("Failed to create CaoEngine!");
cout << SCODE(hr) << endl;
if(SCODE(hr) == 0x80000807)
{
cout << "Too much robot connections." << endl;
}
/* Retrieve CaoWorkspace collection */
hr = pEngine->get_Workspaces(&pWss);
if(FAILED(hr)) throw exception("Failed to retrieve CaoWorkspace collection!");
/* Retrieve CaoWorkspace */
hr = pWss->Item(CComVariant(0L), &pWs);
if(FAILED(hr)) throw exception("Failed to retrieve CaoWorkspace!");
/* Create CaoController */
string robotIPString;
robotIPString.append("Conn=eth:");
robotIPString.append(this->robotIP);
robotIPString.append(",@EVENT_DISABLE=False");
CComBSTR ipOption = robotIPString.c_str();
hr = pWs->AddController(CComBSTR(L"ctrl"), CComBSTR(L"CaoProv.DENSO.NetwoRC"), CComBSTR(L""), ipOption, &pController);
if (FAILED(hr)) throw exception("Failed to create CaoController!");
/* Create Robot */
hr = pController->AddRobot(CComBSTR(L"PJRobot"), CComBSTR(L""), &pRobot);
if (FAILED(hr)) throw exception("Failed to create PJRobot!");
/* Get the robot variables */
hr = pRobot->AddVariable(CComBSTR(L"@CURRENT_POSITION"), CComBSTR(""), ¤tPosition);
if (FAILED(hr)) throw exception("Variable could not be created: 'currentPosition'.");
hr = pRobot->AddVariable(CComBSTR(L"@CURRENT_ANGLE"), CComBSTR(""), ¤tAngle);
if (FAILED(hr)) throw exception("Variable could not be created: 'currentAngle'.");
hr = pRobot->AddVariable(CComBSTR(L"@SERVO_ON"), CComBSTR(""), &isServoOn);
if (FAILED(hr)) throw exception("Variable could not be created: 'isServoOn'.");
hr = pRobot->AddVariable(CComBSTR(L"@CURRENT_TOOL"), CComBSTR(""), ¤tTool);
if (FAILED(hr)) throw exception("Variable could not be created: 'currentTool'.");
hr = pRobot->AddVariable(CComBSTR(L"@CURRENT_WORK"), CComBSTR(""), ¤tWork);
if (FAILED(hr)) throw exception("Variable could not be created: 'currentWork'.");
//hr = pRobot->AddVariable(CComBSTR(L"@ERROR_DESCRIPTION"), CComBSTR(""), &error);
//if (FAILED(hr)) throw exception("Variable could not be created: 'error'.");
/* Set the robSlave task & get the status*/
hr = pController->AddTask(CComBSTR(L"ROBSLAVE"), CComBSTR(L""), &robSlaveTask);
if (FAILED(hr)) throw exception("RobSlave task could not be created.");
hr = robSlaveTask->AddVariable(CComBSTR(L"@STATUS"), CComBSTR(L""), &robSlaveTaskStatus);
if (FAILED(hr)) throw exception("Variable could not be created: 'robSlaveTaskStatus'.");
/* Get the controller variables */
hr = pController->AddVariable(CComBSTR(L"S10"), CComBSTR(L""), &pS10);
if (FAILED(hr)) throw exception("Variable could not be created: 'pS10'.");
hr = pController->AddVariable(CComBSTR(L"IO30"), CComBSTR(L""), &vacuumPin);
if (FAILED(hr)) throw exception("Variable could not be created: 'vacuumPin'.");
/* Set the boolean */
this->isConnected = true;
/* Start the robot slave */
hr = robSlaveTask->Start(1L, CComBSTR(L""));
if (FAILED(hr)) throw exception("RobSlave could not be started!");
}
// TO DO
/*
{
if ( ex.Message.Contains( "license" ) || ex.Message.Contains( "License" ) )
{
System.Diagnostics.Process[] caoProc = System.Diagnostics.Process.GetProcessesByName( "CAO" );
if ( caoProc != null )
{
if ( caoProc.Length != 0 )
{
foreach ( System.Diagnostics.Process proc in caoProc )
{
proc.Kill();
}
}
}
}
}
*/
}
void DensoRobot::Disconnect()
{
if(this->isConnected)
{
/* Stop the robot */
robSlaveTask->Stop(1L, CComBSTR(L""));
/* turn off the motor */
CComVariant pVal;
pRobot->Execute(CComBSTR(L"Motor"), CComVariant(L"0"), &pVal);
/* Stop robSlave?? TO DO */
hr = robSlaveTask->Stop(1L, CComBSTR(L""));
if (FAILED(hr)) throw exception("RobSlave could not be stopped!");
/* Set the connected indicator to false */
this->isConnected = false;
}
if (pRobot) pRobot->Release();
if (pS10) pS10->Release();
if (pController) pController->Release();
if (pWs) pWs->Release();
if (pWss) pWss->Release();
if (pEngine) pEngine->Release();
CoUninitialize();
}
void DensoRobot::Start()
{
/* Start the motor */
CComVariant pVal; // probably empty
hr = pRobot->Execute(CComBSTR(L"Motor"), CComVariant(L"1"), &pVal);
if(FAILED(hr)) throw exception("Failed to start the robot motor!");
/* Set the external robot speed TO DO*/
CComVariant parameters;
parameters.vt = VT_ARRAY | VT_VARIANT;
SAFEARRAYBOUND bounds[1];
bounds[0].lLbound = 0;
bounds[0].cElements = 3;
parameters.parray = SafeArrayCreate(VT_VARIANT, 1, bounds);
CComVariant varSpeed;
varSpeed.vt = VT_R4;
varSpeed.fltVal = (float)this->robotSpeed;
CComVariant varAcceleration;
varAcceleration.vt = VT_R4;
varAcceleration.fltVal = (float)(this->robotSpeed*this->robotSpeed/100);
CComVariant varDeceleration;
varDeceleration.vt = VT_R4;
varDeceleration.fltVal = (float)(this->robotSpeed*this->robotSpeed/100);
long index = 0;
SafeArrayPutElement(parameters.parray, &index, &varSpeed);
index = 1;
SafeArrayPutElement(parameters.parray, &index, &varAcceleration);
index = 2;
SafeArrayPutElement(parameters.parray, &index, &varDeceleration);
CComVariant vntDummy;
hr = pRobot->Execute(CComBSTR(L"ExtSpeed"), parameters, &vntDummy);
if(FAILED(hr)) throw exception("Failed to set the external robot speed!");
parameters.Clear();
}
void DensoRobot::Stop()
{
}
void DensoRobot::MoveToLIN(RobotPoint p)
{
/* Create the pose in a variant format */
string stringPose;
stringPose.append("@E ");
stringPose.append(p.ToString());
CComVariant variantPose = stringPose.c_str();
if(this->isConnected)
{
/* MOVE: 1 is point to point (easiest for the robot joints), 2 is linear (awesome) */
hr = pRobot->Move(2L, variantPose, CComBSTR(L"CONT"));
if(FAILED(hr)) throw exception("Failed to move the robot arm!");
}
}
void DensoRobot::MoveToPTP(RobotPoint p)
{
/* Create the pose in a variant format */
string stringPose;
stringPose.append("@E ");
stringPose.append(p.ToString());
CComVariant variantPose = stringPose.c_str();
if(this->isConnected)
{
/* MOVE: 1 is point to point (easiest for the robot joints), 2 is linear (awesome) */
hr = pRobot->Move(1L, variantPose, CComBSTR(L"CONT"));
if(FAILED(hr)) throw exception("Failed to move the robot arm!");
}
}
RobotPoint DensoRobot::GetCurrentPos()
{
CComVariant varValue;
long i, longlBound, longUBound;
float *positionArray = new float[6];
hr = this->currentPosition->get_Value(&varValue);
if(FAILED(hr)) throw exception ("Could not get the current position!");
SAFEARRAY *psa = varValue.parray;
SafeArrayGetLBound(psa, 1, &longlBound);
SafeArrayGetUBound(psa, 1, &longUBound);
int n = 0;
for(i = longlBound; i <= longUBound; i++)
{
float fltResult;
SafeArrayGetElement(psa, &i, &fltResult);
positionArray[n] = fltResult;
n++;
}
RobotPoint robotPoint((float)positionArray[0],
(float)positionArray[1],
(float)positionArray[2],
(float)positionArray[3],
(float)positionArray[4],
(float)positionArray[5],
(float)positionArray[6]);
return robotPoint;
}
void DensoRobot::ToolOn()
{
hr = vacuumPin->put_Value(CComVariant(true));
if(FAILED(hr)) throw exception("Failed to turn on the vacuüm pin!");
}
void DensoRobot::ToolOff()
{
hr = vacuumPin->put_Value(CComVariant(false));
if(FAILED(hr)) throw exception("Failed to turn off the vacuüm pin!");
}
DensoRobot::~DensoRobot()
{
this->Disconnect();
}
}