pcan_receive.cpp
Go to the documentation of this file.
00001 // INCLUDE
00002 
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <errno.h>
00006 #include <unistd.h>   // exit
00007 #include <signal.h>
00008 #include <string.h>
00009 #include <stdlib.h>   // strtoul
00010 #include <fcntl.h>    // O_RDWR
00011 #include "common.h"
00012 #include <ctype.h>
00013 #include "ros/ros.h"
00014 #include "std_msgs/String.h"
00015 #include <sstream>
00016 #include "pcan_receive.h"
00017 
00018 // DEFINES
00019 
00020 #define DEFAULT_NODE "/dev/pcan32"
00021 
00022 void pcan_receive::hlpMsg()
00023 {
00030         printf("pcan_receive - a small node, based on receivetest , which receives CAN messages and publishes them on a topic.\n");
00031         printf("usage:   pcan_receive {[-f=devicenode]  | {[-b=BTR0BTR1]  [-?]}}\n");
00032         printf("options: -f - devicenode, default=%s\n", DEFAULT_NODE);
00033         printf("         -b - BTR0BTR1 code in hex, e.g.\n");
00034         printf("                0x0014  =   1 MBit/s\n");
00035         printf("                0x001C  = 500 kBit/s\n");
00036         printf("                0x011C  = 250 kBit/s\n");
00037         printf("                0x031C  = 125 kBit/s\n");
00038         printf("                0x432F  = 100 kBit/s\n");
00039         printf("                0x472F  =  50 kBit/s\n");
00040         printf("                0x532F  =  20 kBit/s\n");
00041         printf("                0x672F  =  10 kBit/s\n");
00042         printf("                0x7F7F  =   5 kBit/s\n");
00043         printf("                (default: 1 Mbit).\n");
00044         printf("        -? or --help - this help\n");
00045         printf("\n");
00046 
00047 }
00048 
00049 
00050 pcan_receive::pcan_receive()
00051 {
00058         //Initialisation
00059         h = 0;
00060         pcan_pub = n.advertise<std_msgs::String>("pcan_received", 1);
00061 }
00062 
00063 
00064 void pcan_receive::init(int argc, char **argv)
00065 {
00073         int   nExtended = CAN_INIT_TYPE_ST;
00074         int nType;
00075         __u32 dwPort;
00076         __u16 wIrq;
00077         char *ptr;
00078         __u16 wBTR0BTR1 = 0x0014;
00079         // parameter wBTR0BTR1
00080         // bitrate codes of BTR0/BTR1 registers
00081         //#define CAN_BAUD_1M     0x0014  //   1 MBit/s
00082         //#define CAN_BAUD_500K   0x001C  // 500 kBit/s
00083         //#define CAN_BAUD_250K   0x011C  // 250 kBit/s
00084         //#define CAN_BAUD_125K   0x031C  // 125 kBit/s
00085         //#define CAN_BAUD_100K   0x432F  // 100 kBit/s
00086         //#define CAN_BAUD_50K    0x472F  //  50 kBit/s
00087         //#define CAN_BAUD_20K    0x532F  //  20 kBit/s
00088         //#define CAN_BAUD_10K    0x672F  //  10 kBit/s
00089         //#define CAN_BAUD_5K     0x7F7F  //   5 kBit/s
00090         const char  *szDevNode = DEFAULT_NODE;
00091         bool bDevNodeGiven = false;
00092         bool bTypeGiven = false;
00093         char txt[VERSIONSTRING_LEN];
00094         // decode command line arguments
00095                 for (int i = 1; i < argc; i++)
00096                 {
00097                     char c;
00098                     char *ptr;
00099                     ptr = argv[i];
00100 
00101                     while (*ptr == '-')
00102                       ptr++;
00103 
00104                     c = *ptr;
00105                     ptr++;
00106 
00107                     if (*ptr == '=')
00108                       ptr++;
00109 
00110                     switch(tolower(c))
00111                     {
00112                       case '?':
00113                       case 'h':
00114                         hlpMsg();
00115                         do_exit(errno, h);
00116                         break;
00117                       case 'f':
00118                         szDevNode = ptr;
00119                         bDevNodeGiven = true;
00120                         break;
00121                       case 'b':
00122                         wBTR0BTR1 = (__u16)strtoul(ptr, NULL, 16);
00123                         break;
00124                       default:
00125                          errno = EINVAL;
00126                          do_exit(errno, h);;
00127                         break;
00128                     }
00129                 }
00130                   /* open CAN port */
00131                    if ((bDevNodeGiven) || (!bDevNodeGiven && !bTypeGiven))
00132                    {
00133                      h = LINUX_CAN_Open(szDevNode, O_RDWR);
00134                      if (!h)
00135                      {
00136                        printf("pcan_receive: can't open %s\n", szDevNode);
00137                        do_exit(errno, h);;
00138                      }
00139                    }
00140                    else {
00141                      // please use what is appropriate
00142                      // HW_DONGLE_SJA
00143                      // HW_DONGLE_SJA_EPP
00144                      // HW_ISA_SJA
00145                      // HW_PCI
00146                      h = CAN_Open(nType, dwPort, wIrq);
00147                      if (!h)
00148                      {
00149                        printf("pcan_receive: can't open %s device.\n", getNameOfInterface(nType));
00150                        do_exit(errno, h);;
00151                      }
00152                    }
00153                    /* clear status */
00154                      CAN_Status(h);
00155                      // get version info
00156                      errno = CAN_VersionInfo(h, txt);
00157                      if (!errno)
00158                        printf("pcan_receive: driver version = %s\n", txt);
00159                      else {
00160                        perror("pcan_receive: CAN_VersionInfo()");
00161                        do_exit(errno, h);;
00162                      }
00163                      // init to a user defined bit rate
00164                      if (wBTR0BTR1)
00165                      {
00166                        errno = CAN_Init(h, wBTR0BTR1, 0x00);
00167                        if (errno)
00168                        {
00169                          perror("pcan_receive: CAN_Init()");
00170                          do_exit(errno, h );;
00171                        }
00172                      }
00173 
00174 }
00175 void pcan_receive::receive()
00176 {
00184                 // Real code
00185                 TPCANRdMsg m;
00186                 __u32 status;
00187                 if (LINUX_CAN_Read(h, &m))
00188                 {
00189                         perror("pcan_receive: LINUX_CAN_Read()");
00190                 }
00191                 else
00192                 {
00193                         //print_message_ex(&m);
00194                         // check if a CAN status is pending
00195                         if (m.Msg.MSGTYPE & MSGTYPE_STATUS)
00196                         {
00197                                 status = CAN_Status(h);
00198                                 if ((int)status < 0)
00199                                 {
00200                                         errno = nGetLastError();
00201                                         perror("pcan_receive: CAN_Status()");
00202                                 }
00203                                 else
00204                                         printf("pcan_receive: pending CAN status 0x%04x read.\n", (__u16)status);
00205                         }
00206                 }
00207                 pcan_pub.publish(TPCANRdMsgToString(m));
00208 
00209 
00210 }
00211 
00212 
00213 
00214 
00215 
00216 std_msgs::String pcan_receive::TPCANRdMsgToString(TPCANRdMsg m)
00217 {
00225         std_msgs::String msg;
00226         std::stringstream ss;
00227         ss << m.dwTime << ".";
00228         if(m.wUsec>=100)
00229         {
00230                 ss << m.wUsec << " ";
00231         }
00232         else
00233         {
00234                 if(m.wUsec>=10)
00235                 {
00236                         ss << "0" <<m.wUsec << " ";
00237                 }
00238                 else
00239                 {
00240                         ss << "00" <<m.wUsec << " ";
00241                 }
00242         }
00243         char IDENT[4];
00244         sprintf(IDENT, "%02X", m.Msg.ID);
00245         ss << "0x" ;
00246         if(m.Msg.ID<16)
00247         {
00248                 ss <<"0";
00249         }
00250         ss<< IDENT << " ";
00251         char LEN[3];
00252         sprintf(LEN, "%02X", m.Msg.LEN);
00253         ss << "0x" << LEN << " ";
00254         for (int i=0; i<sizeof(m.Msg.DATA); i++)
00255         {
00256                 char DATA[3];
00257                 sprintf(DATA, "%02X", m.Msg.DATA[i]);
00258                 ss <<"0x" << DATA << " ";
00259         }
00260         msg.data=ss.str();
00261         return msg;
00262 }


pcan_topics
Author(s):
autogenerated on Thu Jun 6 2019 17:40:04