00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <cob_forcetorque/CanESD.h>
00010
00011
00012
00013 CanESD::CanESD(const char* cIniFile, bool bObjectMode)
00014 {
00015 m_bObjectMode = bObjectMode;
00016 m_bIsTXError = false;
00017
00018 initIntern();
00019 }
00020
00021
00026 CanESD::~CanESD()
00027 {
00028 std::cout << "Closing CAN handle" << std::endl;
00029 canClose(m_Handle);
00030
00031 }
00032
00033
00034
00035 void CanESD::initIntern()
00036 {
00037 int ret=0;
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 ret = 0;
00060 int iCanNet = 0;
00061
00062
00063 int iBaudrateVal = NTCAN_BAUD_250;
00064
00065
00066 std::cout << "Initializing CAN network with id =" << iCanNet << ", baudrate=" << iBaudrateVal << std::endl;
00067
00068 int iRet;
00069 if( m_bObjectMode )
00070
00071 iRet = canOpen(iCanNet, NTCAN_MODE_OBJECT, 10000, 10000, 1000, 0, &m_Handle);
00072 else
00073
00074 iRet = canOpen(iCanNet, 0, 10000, 10000, 1000, 0, &m_Handle);
00075 sleep(0.3);
00076
00077 if(iRet == NTCAN_SUCCESS)
00078 std::cout << "CanESD::CanESD(), init ok" << std::endl;
00079 else
00080 std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 iRet = canSetBaudrate(m_Handle, iBaudrateVal);
00099 if(iRet == NTCAN_SUCCESS)
00100 std::cout << "CanESD::CanESD(), canSetBaudrate ok" << std::endl;
00101 else
00102 std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
00103 sleep(0.3);
00104
00105 long lArg;
00106 iRet = canIoctl(m_Handle, NTCAN_IOCTL_FLUSH_RX_FIFO, NULL);
00107
00108
00109
00110
00111 for( int i=0; i<=0x7FF; ++i ) {
00112 iRet = canIdAdd( m_Handle, i );
00113 if(iRet != NTCAN_SUCCESS)
00114 std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(iRet) << std::endl;
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 sleep(0.3);
00197
00198 m_LastID = -1;
00199
00200
00201 }
00202
00203
00210 bool CanESD::transmitMsg(CanMsg CMsg, bool bBlocking)
00211 {
00212 CMSG NTCANMsg;
00213 NTCANMsg.id = CMsg.m_iID;
00214 NTCANMsg.len = CMsg.m_iLen;
00215
00216 for(int i=0; i<8; i++)
00217 {
00218 NTCANMsg.data[i] = CMsg.getAt(i);
00219
00220 }
00221
00222 int ret;
00223
00224 int32_t len;
00225 bool bRet = true;
00226
00227 len = 1;
00228
00229
00230 if (bBlocking)
00231 ret = canWrite(m_Handle, &NTCANMsg, &len,+ NULL);
00232 else
00233 ret = canSend(m_Handle, &NTCANMsg, &len);
00234
00235
00236
00237 if( ret != NTCAN_SUCCESS)
00238 {
00239 std::cout << "error in CANESD::transmitMsg: " << GetErrorStr(ret) << std::endl;
00240 bRet = false;
00241 }
00242
00243 m_LastID = (int)NTCANMsg.data[0];
00244
00245
00246
00247
00248 m_bIsTXError = !bRet;
00249 return bRet;
00250 }
00251
00252
00253 bool CanESD::receiveMsgRetry(CanMsg* pCMsg, int iNrOfRetry)
00254 {
00255 int id = pCMsg->m_iID;
00256 CMSG NTCANMsg;
00257 NTCANMsg.len = 8;
00258
00259
00260 int32_t len;
00261 int i, ret;
00262 bool bRet = true;
00263
00264 i=0;
00265
00266 len = 1;
00267
00268
00269
00270 do
00271 {
00272 len = 1;
00273
00274 ret = canTake(m_Handle, &NTCANMsg, &len);
00275
00276
00277 i++;
00278 sleep(0.01);
00279 }
00280
00281
00282 while((len == 0) && (i < iNrOfRetry));
00283
00284
00285
00286 if(i == iNrOfRetry)
00287
00288 {
00289 if( ret != NTCAN_SUCCESS )
00290 std::cout << "error in CANESD::receiveMsgRetry: " << GetErrorStr(ret) << std::endl;
00291
00292
00293
00294
00295
00296
00297 bRet = false;
00298 }
00299 else
00300 {
00301 pCMsg->m_iID = NTCANMsg.id;
00302 pCMsg->m_iLen = NTCANMsg.len;
00303 pCMsg->set(NTCANMsg.data[0], NTCANMsg.data[1], NTCANMsg.data[2], NTCANMsg.data[3],
00304 NTCANMsg.data[4], NTCANMsg.data[5], NTCANMsg.data[6], NTCANMsg.data[7]);
00305 }
00306
00307
00308
00309 return bRet;
00310 }
00311
00312
00313 bool CanESD::receiveMsg(CanMsg* pCMsg)
00314 {
00315 CMSG NTCANMsg;
00316 NTCANMsg.len = 8;
00317
00318 int ret;
00319
00320 int32_t len;
00321 bool bRet = true;
00322
00323 len = 1;
00324
00325
00326 NTCANMsg.data[0] = 0;
00327 NTCANMsg.data[1] = 0;
00328 NTCANMsg.data[2] = 0;
00329 NTCANMsg.data[3] = 0;
00330 NTCANMsg.data[4] = 0;
00331 NTCANMsg.data[5] = 0;
00332 NTCANMsg.data[6] = 0;
00333 NTCANMsg.data[7] = 0;
00334 NTCANMsg.msg_lost = 0;
00335 NTCANMsg.id = 0;
00336 NTCANMsg.len = 0;
00337
00338 pCMsg->set(0,0,0,0,0,0,0,0);
00339
00340
00341 if( !isObjectMode() ) {
00342 pCMsg->m_iID = 0;
00343 } else {
00344 NTCANMsg.id = pCMsg->m_iID;
00345 }
00346
00347
00348 ret = canRead(m_Handle, &NTCANMsg, &len, NULL);
00349
00350
00351 if( !isObjectMode() ) {
00352 if( (len == 1) && (ret == NTCAN_SUCCESS) )
00353 {
00354
00355 pCMsg->m_iID = NTCANMsg.id;
00356 pCMsg->m_iLen = NTCANMsg.len;
00357 pCMsg->set(NTCANMsg.data[0], NTCANMsg.data[1], NTCANMsg.data[2], NTCANMsg.data[3],
00358 NTCANMsg.data[4], NTCANMsg.data[5], NTCANMsg.data[6], NTCANMsg.data[7]);
00359 bRet = true;
00360 }
00361 else
00362 {
00363
00364 if( ret != NTCAN_SUCCESS)
00365 {
00366
00367 std::cout << "error in CANESD::receiveMsg: " << GetErrorStr(ret) << std::endl;
00368 }
00369
00370 pCMsg->m_iID = NTCANMsg.id;
00371 pCMsg->set(0,0,0,0,0,0,0,0);
00372
00373
00374 bRet = false;
00375 }
00376 } else {
00377
00378 if( len == 16 ) {
00379
00380 pCMsg->m_iID = NTCANMsg.id;
00381 pCMsg->set(0,0,0,0,0,0,0,0);
00382 bRet = false;
00383 } else {
00384 pCMsg->m_iID = NTCANMsg.id;
00385 pCMsg->m_iLen = NTCANMsg.len;
00386 pCMsg->set(NTCANMsg.data[0], NTCANMsg.data[1], NTCANMsg.data[2], NTCANMsg.data[3],
00387 NTCANMsg.data[4], NTCANMsg.data[5], NTCANMsg.data[6], NTCANMsg.data[7]);
00388 bRet = true;
00389 }
00390 }
00391
00392 if( NTCANMsg.msg_lost != 0 )
00393 std::cout << (int)(NTCANMsg.msg_lost) << " messages lost!" << std::endl;
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403 return bRet;
00404 }
00405
00406
00418 int CanESD::canIdAddGroup(HANDLE handle, int id)
00419 {
00420 int result = NTCAN_SUCCESS;
00421 int i = 0;
00422 int iRes = 0;
00423 int cmd_id = invert(id);
00424
00425
00426 for( i=0; i<8; ++i) {
00427 iRes = canIdAdd(m_Handle, cmd_id+i);
00428
00429 if( iRes != NTCAN_SUCCESS ) {
00430 std::cout << "Adding CAN ID " << cmd_id+i << " failed with errorcode: " << iRes << " " << GetErrorStr(iRes) << std::endl;
00431 result = iRes;
00432 }
00433 }
00434
00435
00436 return result;
00437 }
00438
00439
00440 std::string CanESD::GetErrorStr(int ntstatus) const
00441 {
00442 switch (ntstatus)
00443 {
00444 case NTCAN_SUCCESS: return "NTCAN_SUCCESS";
00445 case NTCAN_RX_TIMEOUT: return "NTCAN_RX_TIMEOUT";
00446 case NTCAN_TX_TIMEOUT: return "NTCAN_TX_TIMEOUT";
00447 case NTCAN_TX_ERROR: return "NTCAN_TX_ERROR";
00448 case NTCAN_CONTR_OFF_BUS: return "NTCAN_CONTR_OFF_BUS";
00449 case NTCAN_CONTR_BUSY: return "NTCAN_CONTR_BUSY";
00450 case NTCAN_CONTR_WARN: return "NTCAN_CONTR_WARN";
00451 case NTCAN_NO_ID_ENABLED: return "NTCAN_NO_ID_ENABLED";
00452 case NTCAN_ID_ALREADY_ENABLED: return "NTCAN_ID_ALREADY_ENABLED";
00453 case NTCAN_ID_NOT_ENABLED: return "NTCAN_ID_NOT_ENABLED";
00454
00455 case NTCAN_INVALID_FIRMWARE: return "NTCAN_INVALID_FIRMWARE";
00456 case NTCAN_MESSAGE_LOST: return "NTCAN_MESSAGE_LOST";
00457 case NTCAN_INVALID_HARDWARE: return "NTCAN_INVALID_HARDWARE";
00458
00459 case NTCAN_PENDING_WRITE: return "NTCAN_PENDING_WRITE";
00460 case NTCAN_PENDING_READ: return "NTCAN_PENDING_READ";
00461 case NTCAN_INVALID_DRIVER: return "NTCAN_INVALID_DRIVER";
00462
00463 case NTCAN_INVALID_PARAMETER: return "NTCAN_INVALID_PARAMETER";
00464 case NTCAN_INVALID_HANDLE: return "NTCAN_INVALID_HANDLE";
00465 case NTCAN_NET_NOT_FOUND: return "NTCAN_NET_NOT_FOUND";
00466 case NTCAN_INSUFFICIENT_RESOURCES: return "NTCAN_INSUFFICIENT_RESOURCES";
00467
00468 case NTCAN_OPERATION_ABORTED: return "NTCAN_OPERATION_ABORTED";
00469 }
00470 char msg[100];
00471 sprintf(msg, "unknown error code %d", ntstatus);
00472 return msg;
00473 }
00474
00484 int CanESD::readEvent()
00485 {
00486 EVMSG evmsg;
00487 int iRet = 0;
00488 int ret;
00489
00490
00491 ret = canReadEvent(m_Handle, &evmsg, NULL);
00492
00493
00494 if(ret == NTCAN_SUCCESS)
00495 {
00496 if( (int)evmsg.evid == NTCAN_EV_CAN_ERROR ) {
00497 switch( evmsg.evdata.s[0] ) {
00498 case 0x00:
00499 iRet = 0;
00500 break;
00501 case 0xC0:
00502 iRet = -6;
00503 std::cout << "BUS OFF" << std::endl;
00504 break;
00505 case 0x40:
00506 iRet = -7;
00507 std::cout << "ERROR PASSIVE" << std::endl;
00508 break;
00509 }
00510 if( evmsg.evdata.s[3] != 0 ) {
00511 iRet = -3;
00512 std::cout << "Lost " << (int)evmsg.evdata.s[3] << " messages" << std::endl;
00513 } else if( evmsg.evdata.s[5] != 0 ) {
00514 iRet = -5;
00515 std::cout << "Lost " << (int)evmsg.evdata.s[5] << " messages from fifo" << std::endl;
00516 }
00517 }
00518 }
00519 return iRet;
00520 }
00521