-
Notifications
You must be signed in to change notification settings - Fork 12
/
fanuc_adapter.cpp
335 lines (284 loc) · 8.05 KB
/
fanuc_adapter.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
/*
* Copyright (c) 2008, AMT – The Association For Manufacturing Technology (“AMT”)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the AMT nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* DISCLAIMER OF WARRANTY. ALL MTCONNECT MATERIALS AND SPECIFICATIONS PROVIDED
* BY AMT, MTCONNECT OR ANY PARTICIPANT TO YOU OR ANY PARTY ARE PROVIDED "AS IS"
* AND WITHOUT ANY WARRANTY OF ANY KIND. AMT, MTCONNECT, AND EACH OF THEIR
* RESPECTIVE MEMBERS, OFFICERS, DIRECTORS, AFFILIATES, SPONSORS, AND AGENTS
* (COLLECTIVELY, THE "AMT PARTIES") AND PARTICIPANTS MAKE NO REPRESENTATION OR
* WARRANTY OF ANY KIND WHATSOEVER RELATING TO THESE MATERIALS, INCLUDING, WITHOUT
* LIMITATION, ANY EXPRESS OR IMPLIED WARRANTY OF NONINFRINGEMENT,
* MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* LIMITATION OF LIABILITY. IN NO EVENT SHALL AMT, MTCONNECT, ANY OTHER AMT
* PARTY, OR ANY PARTICIPANT BE LIABLE FOR THE COST OF PROCURING SUBSTITUTE GOODS
* OR SERVICES, LOST PROFITS, LOSS OF USE, LOSS OF DATA OR ANY INCIDENTAL,
* CONSEQUENTIAL, INDIRECT, SPECIAL OR PUNITIVE DAMAGES OR OTHER DIRECT DAMAGES,
* WHETHER UNDER CONTRACT, TORT, WARRANTY OR OTHERWISE, ARISING IN ANY WAY OUT OF
* THIS AGREEMENT, USE OR INABILITY TO USE MTCONNECT MATERIALS, WHETHER OR NOT
* SUCH PARTY HAD ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.
*/
#include "stdafx.h"
#include <math.h>
#include "internal.hpp"
#include "fanuc_adapter.h"
#include <sstream>
// Michaloski
#include "Logger.h"
#include "EnumProc.h"
#include "Globals.h"
#include "StdStringFcn.h"
#ifdef F15i
#include "FS15D.h"
#endif
#ifdef iSERIES
#include "iSeries.h"
#endif
#ifdef F15M
#include "F15B.h"
#endif
#define LOADS
static void trans_func( unsigned int u, EXCEPTION_POINTERS* pExp )
{
std::string errmsg = StdStringFormat("Fanuc Shdr trans_func - Code = 0x%x\n",
pExp->ExceptionRecord->ExceptionCode) ;
Logger.LogMessage(std::string("FanucMTConnectAdapter::gatherDeviceData Exception Raised\n" + errmsg).c_str(), Logger.LERROR);
throw std::exception(errmsg.c_str());
}
FanucMTConnectAdapter::FanucMTConnectAdapter( int aPort, const char *aDeviceIP, int aDevicePort, int delay) :
Adapter(aPort, delay),
mMode("controllermode"),
mPathFeedrate("path_feedratefrt"),
mPathFeedrateOvr("path_feedrateovr"),
mPower("power"),
mExecution("execution"),
mProgram("program"),
mLine("line"),
mXact("Xabs"),
mYact("Yabs"),
mZact("Zabs"),
mAact("Adeg"),
mBact("Bdeg"),
mCact("Cdeg"),
mXload("Xlod_percent"),
mYload("Ylod_percent"),
mZload("Zlod_percent"),
mAload("Alod_percent"),
mBload("Blod_percent"),
mCload("Clod_percent"),
mSpindleSpeed("Srpm"),
mSpindleSpeedLoad("Slod_percent"),
mSpindleSpeedOvr("Sovr")
//mAlarm("alarm"), // removed in version 1.2
{
Logger.LogMessage("FanucMTConnectAdapter::FanucMTConnectAdapter\n", Logger.LDEBUG);
mConnected=false;
//addDatum(mAlarm);
addDatum(mPower);
addDatum(mExecution);
addDatum(mLine);
addDatum(mProgram);
addDatum(mXact);
addDatum(mYact);
addDatum(mZact);
addDatum(mAact);
addDatum(mBact);
addDatum(mCact);
addDatum(mXload);
addDatum(mYload);
addDatum(mZload);
addDatum(mAload);
addDatum(mBload);
addDatum(mCload);
addDatum(mSpindleSpeed);
addDatum(mPathFeedrate);
addDatum(mMode);
addDatum(mSpindleSpeedLoad);
addDatum(mPathFeedrateOvr);
addDatum(mSpindleSpeedOvr);
mDevicePort = aDevicePort;
mDeviceIP = aDeviceIP;
_nAxes=3;
#ifdef F15M
_F15M = new CF15B(this);
#endif
#ifdef F15i
_FS15D = new CFS15D(this);
#endif
#ifdef iSERIES
_iSeries = new CiSeries(this);
#endif
}
void FanucMTConnectAdapter::initialize(int aArgc, const char *aArgv[])
{
OutputDebugString("FanucMTConnectAdapter::initialize()\n");
MTConnectService::initialize(aArgc, aArgv);
if (aArgc > 1) {
}
}
void FanucMTConnectAdapter::start()
{
OutputDebugString("FanucMTConnectAdapter::start()\n");
startServer();
}
void FanucMTConnectAdapter::stop()
{
OutputDebugString("FanucMTConnectAdapter::stop()\n");
disconnect();
stopServer();
}
void FanucMTConnectAdapter::disconnect()
{
#ifdef F15i
_FS15D->disconnect();
#endif
#ifdef F15M
_F15M->disconnect();
#endif
#ifdef iSeries
_iSeries->disconnect();
#endif
}
FanucMTConnectAdapter::~FanucMTConnectAdapter()
{
disconnect();
}
#pragma comment(lib,"psapi")
bool FanucMTConnectAdapter::IsCNCRunning()
{
// Check for local machine cnc pid running.
if(Globals.szFanucExe.size() >0)
{
if(!CProcessIterator::FindProcess((LPCSTR) Globals.szFanucExe.c_str()))
return 0;
}
// Check for remote machine cnc pid running - ouch!
return 1;
}
void FanucMTConnectAdapter::gatherDeviceData()
{
Logger.LogMessage("FanucMTConnectAdapter::gatherDeviceData\n", Logger.HEAVYDEBUG);
_set_se_translator( trans_func ); // correct thread?
try {
#if 0
if(!this->IsCNCRunning())
{
::Sleep(2000);
return;
}
#endif
::Sleep(1000);
#ifdef F15i
if (!mConnected)
_FS15D->connect();
if (mConnected)
{
//if(Globals.FanucVersion == "15i")
{
_FS15D->getVersion();
_FS15D->getInfo();
_FS15D->getLine();
_FS15D->getPositions();
_FS15D->getStatus();
_FS15D->getSpeeds();
}
}
#else
#ifdef F15M
if (!mConnected)
_F15M->connect();
if (mConnected)
{
//if(Globals.FanucVersion == "15M")
{
_F15M->saveStateInfo();
_F15M->getVersion();
_F15M->getInfo();
_F15M->getLine();
_F15M->getPositions();
_F15M->getStatus();
// hack in getSpeeds
_F15M->getSpeeds();
}
}
#else
if (!mConnected)
_iSeries->connect();
if (mConnected)
{
//else
{
_iSeries->getVersion();
_iSeries->getInfo();
_iSeries->getLine();
_iSeries->getPositions();
_iSeries->getStatus();
_iSeries->getSpeeds();
}
}
#endif
#endif
}
catch(...)
{
Logger.LogMessage("FanucMTConnectAdapter::gatherDeviceData Exception Raised\n", Logger.WARNING);
disconnect();
}
}
//static std::string DumpODBPOS(ODBPOS & pos)
//{
// std::string tmp;
// tmp+= StrFormat("Abs Data=%d Dec=%d Unit=%d Display=%d Axis=%c Suff=%c\n",
// pos.abs.data, pos.abs.dec, pos.abs.unit, pos.abs.disp, pos.abs.name, pos.abs.suff);
// tmp+= StrFormat("Mach Data=%d Dec=%d Unit=%d Display=%d Axis=%c Suff=%c\n",
// pos.abs.data, pos.mach.dec, pos.mach.unit, pos.mach.disp, pos.mach.name, pos.mach.suff);
// tmp+= StrFormat("Rel Data=%d Dec=%d Unit=%d Display=%d Axis=%c Suff=%c\n",
// pos.rel.data, pos.rel.dec, pos.rel.unit, pos.rel.disp, pos.rel.name, pos.rel.suff);
// tmp+= StrFormat("Dist Data=%d Dec=%d Unit=%d Display=%d Axis=%c Suff=%c\n",
// pos.dist.data, pos.dist.dec, pos.dist.unit, pos.dist.disp, pos.dist.name, pos.dist.suff);
// return tmp;
//}
std::string FanucMTConnectAdapter::getProgramName(char * buf)
{
Logger.LogMessage("FanucMTConnectAdapter::getProgramName Enter\n", 3);
int prognum;
std::string progname;
std::stringstream ss(buf);
std::string sLine;
while(std::getline(ss, sLine, '\n'))
{
// look for O # which must be there
if(1 != sscanf(sLine.c_str(), "O%d", &prognum))
continue;
progname=StdStringFormat("O%d", prognum);
#if 0
// if find comment inside (...) return as program name
int n = sLine.find("(");
int m= sLine.find(")");
if(std::string::npos !=n && std::string::npos !=m)
return sLine.substr(n+1,m-n-1);
// else could be standalone on next line
if(std::getline(ss, sLine, '\n'))
{
n = sLine.find("(");
m= sLine.find(")");
if(std::string::npos !=n && std::string::npos !=m)
return sLine.substr(n+1,m-n-1);
}
#endif
return progname;
}
Logger.LogMessage("FanucMTConnectAdapter::getProgramName Leave\n", 3);
return "";
}