CanPeakSys.cpp
Go to the documentation of this file.
00001 /****************************************************************
00002  *
00003  * Copyright (c) 2010
00004  *
00005  * Fraunhofer Institute for Manufacturing Engineering   
00006  * and Automation (IPA)
00007  *
00008  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00009  *
00010  * Project name: care-o-bot
00011  * ROS stack name: cob_drivers
00012  * ROS package name: cob_generic_can
00013  * Description:
00014  *                                                              
00015  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00016  *                      
00017  * Author: Christian Connette, email:christian.connette@ipa.fhg.de
00018  * Supervised by: Christian Connette, email:christian.connette@ipa.fhg.de
00019  *
00020  * Date of creation: Feb 2009
00021  * ToDo: Remove dependency to inifiles_old -> Inifile.h
00022  *
00023  * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00024  *
00025  * Redistribution and use in source and binary forms, with or without
00026  * modification, are permitted provided that the following conditions are met:
00027  *
00028  *     * Redistributions of source code must retain the above copyright
00029  *       notice, this list of conditions and the following disclaimer.
00030  *     * Redistributions in binary form must reproduce the above copyright
00031  *       notice, this list of conditions and the following disclaimer in the
00032  *       documentation and/or other materials provided with the distribution.
00033  *     * Neither the name of the Fraunhofer Institute for Manufacturing 
00034  *       Engineering and Automation (IPA) nor the names of its
00035  *       contributors may be used to endorse or promote products derived from
00036  *       this software without specific prior written permission.
00037  *
00038  * This program is free software: you can redistribute it and/or modify
00039  * it under the terms of the GNU Lesser General Public License LGPL as 
00040  * published by the Free Software Foundation, either version 3 of the 
00041  * License, or (at your option) any later version.
00042  * 
00043  * This program is distributed in the hope that it will be useful,
00044  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00045  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00046  * GNU Lesser General Public License LGPL for more details.
00047  * 
00048  * You should have received a copy of the GNU Lesser General Public 
00049  * License LGPL along with this program. 
00050  * If not, see <http://www.gnu.org/licenses/>.
00051  *
00052  ****************************************************************/
00053 
00054 #include <cob_generic_can/CanPeakSys.h>
00055 #include <stdlib.h>
00056 #include <cerrno>
00057 #include <cstring>
00058 #include <sys/types.h>
00059 #include <sys/stat.h>
00060 #include <fcntl.h>
00061 //-----------------------------------------------
00062 
00063 const int CanPeakSys::c_iInterrupt = 7;
00064 const int CanPeakSys::c_iPort = 0x378;
00065 
00066 //-----------------------------------------------
00067 CanPeakSys::CanPeakSys(const char* cIniFile)
00068 {
00069         m_bInitialized = false;
00070 
00071         // read IniFile
00072         m_IniFile.SetFileName(cIniFile, "CanPeakSys.cpp");
00073         init();
00074 }
00075 
00076 //-----------------------------------------------
00077 CanPeakSys::~CanPeakSys()
00078 {
00079         if (m_bInitialized)
00080         {
00081                 CAN_Close(m_handle);
00082         }
00083 }
00084 
00085 //-----------------------------------------------
00086 void CanPeakSys::init()
00087 {
00088  std::string sCanDevice; 
00089  if( m_IniFile.GetKeyString( "TypeCan", "DevicePath", &sCanDevice, false) != 0) {
00090                 sCanDevice = "/dev/pcan32";
00091         } else std::cout << "CAN-device path read from ini-File: " << sCanDevice << std::endl;
00092         m_handle = LINUX_CAN_Open(sCanDevice.c_str(), O_RDWR);
00093         
00094 
00095         if (! m_handle)
00096         {
00097                 // Fatal error
00098                 std::cout << "Cannot open CAN-dongle on parallel port: " << strerror(errno) << std::endl;
00099                 sleep(3);
00100                 exit(0);
00101         }
00102         
00103         
00104         int ret = CAN_ERR_OK;
00105         int iBaudrateVal = 0;
00106         m_IniFile.GetKeyInt( "CanCtrl", "BaudrateVal", &iBaudrateVal, true);
00107         
00108         switch(iBaudrateVal)
00109         {
00110         case 0:
00111                 ret = CAN_Init(m_handle, CAN_BAUD_1M, CAN_INIT_TYPE_ST);
00112                 break;
00113         case 2:
00114                 ret = CAN_Init(m_handle, CAN_BAUD_500K, CAN_INIT_TYPE_ST);
00115                 break;
00116         case 4:
00117                 ret = CAN_Init(m_handle, CAN_BAUD_250K, CAN_INIT_TYPE_ST);
00118                 break;
00119         case 6:
00120                 ret = CAN_Init(m_handle, CAN_BAUD_125K, CAN_INIT_TYPE_ST);
00121                 break;
00122         case 9:
00123                 ret = CAN_Init(m_handle, CAN_BAUD_50K, CAN_INIT_TYPE_ST);
00124                 break;
00125         case 11:
00126                 ret = CAN_Init(m_handle, CAN_BAUD_20K, CAN_INIT_TYPE_ST);
00127                 break;
00128         case 13:
00129                 ret = CAN_Init(m_handle, CAN_BAUD_10K, CAN_INIT_TYPE_ST);
00130                 break;
00131         }
00132 
00133         if(ret)
00134         {
00135                 std::cout << "CanPeakSys::CanPeakSys(), error in init" << std::endl;
00136         }
00137         else
00138         {
00139                 std::cout << "CanPeakSys::CanpeakSys(), init ok" << std::endl;
00140                 m_bInitialized = true;
00141         }
00142 }
00143 
00144 //-------------------------------------------
00145 bool CanPeakSys::transmitMsg(CanMsg CMsg, bool bBlocking)
00146 {
00147         TPCANMsg TPCMsg;
00148         bool bRet = true;
00149 
00150         if (m_bInitialized == false) return false;
00151 
00152         // copy CMsg to TPCmsg
00153         TPCMsg.LEN = CMsg.m_iLen;
00154         TPCMsg.ID = CMsg.m_iID;
00155         TPCMsg.MSGTYPE = CMsg.m_iType;
00156         for(int i=0; i<8; i++)
00157                 TPCMsg.DATA[i] = CMsg.getAt(i);
00158         
00159         // write msg
00160         int iRet;
00161         iRet = CAN_Write(m_handle, &TPCMsg);
00162         iRet = CAN_Status(m_handle);
00163 
00164         if(iRet < 0)
00165         {
00166                 std::cout << "CanPeakSys::transmitMsg, errorcode= " << nGetLastError() << std::endl;
00167                 bRet = false;
00168         }
00169 
00170 
00171         return bRet;
00172 }
00173 
00174 //-------------------------------------------
00175 bool CanPeakSys::receiveMsg(CanMsg* pCMsg)
00176 {
00177         TPCANRdMsg TPCMsg;
00178         TPCMsg.Msg.LEN = 8;
00179         TPCMsg.Msg.MSGTYPE = 0;
00180         TPCMsg.Msg.ID = 0;
00181         
00182         int iRet = CAN_ERR_OK;
00183         bool bRet = false;
00184 
00185 
00186         if (m_bInitialized == false) return false;
00187 
00188         iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, 0);
00189 
00190         if (iRet == CAN_ERR_OK)
00191         {
00192                 pCMsg->m_iID = TPCMsg.Msg.ID;
00193                 pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3],
00194                         TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]);
00195                 bRet = true;
00196         }
00197         else if (CAN_Status(m_handle) != CAN_ERR_QRCVEMPTY)
00198         {
00199                 std::cout << "CanPeakSys::receiveMsg ERROR: iRet = " << iRet << std::endl;
00200                 pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0);
00201         }
00202         else
00203         {
00204                 // make sure there's never an undefined state (even when can drivers fail)
00205                 pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0);
00206         }
00207 
00208         return bRet;
00209 }
00210 
00211 //-------------------------------------------
00212 bool CanPeakSys::receiveMsgRetry(CanMsg* pCMsg, int iNrOfRetry)
00213 {
00214         int i, iRet;
00215 
00216         TPCANRdMsg TPCMsg;
00217         TPCMsg.Msg.LEN = 8;
00218         TPCMsg.Msg.MSGTYPE = 0;
00219         TPCMsg.Msg.ID = 0;
00220 
00221         if (m_bInitialized == false) return false;
00222 
00223         // wait until msg in buffer
00224         bool bRet = true;
00225         iRet = CAN_ERR_OK;
00226         i=0;
00227         do
00228         {
00229                 iRet = LINUX_CAN_Read_Timeout(m_handle, &TPCMsg, 0);
00230 
00231                 if(iRet == CAN_ERR_OK)
00232                         break;
00233 
00234                 i++;
00235                 usleep(100000);
00236         }
00237         while(i < iNrOfRetry);
00238 
00239         // eval return value
00240         if(iRet != CAN_ERR_OK)
00241         {
00242                 std::cout << "CanPeakSys::receiveMsgRetry: " << strerror(errno) << std::endl;
00243                 pCMsg->set(0, 0, 0, 0, 0, 0, 0, 0);
00244                 bRet = false;
00245         }
00246         else
00247         {
00248                 pCMsg->m_iID = TPCMsg.Msg.ID;
00249                 pCMsg->set(TPCMsg.Msg.DATA[0], TPCMsg.Msg.DATA[1], TPCMsg.Msg.DATA[2], TPCMsg.Msg.DATA[3],
00250                         TPCMsg.Msg.DATA[4], TPCMsg.Msg.DATA[5], TPCMsg.Msg.DATA[6], TPCMsg.Msg.DATA[7]);
00251         }
00252 
00253         return bRet;
00254 }
00255 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends


cob_generic_can
Author(s): Christian Connette
autogenerated on Fri Mar 1 2013 17:46:39