pcan_transmit.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_transmit.h"
00017 
00018 
00019 
00020 void pcan_transmit::hlpMsg()
00021 {
00028         printf("pcan_transmit - a small node, based on transmitest , which is subscribed to a ROS topic and sends CAN messages.\n");
00029         printf("usage:   pcan_transmit {[-f=devicenode]  | {[-b=BTR0BTR1]  [-?]}} \n");
00030         printf("options: -f - devicenode, default=%s\n", DEFAULT_NODE);
00031         printf("         -b - BTR0BTR1 code in hex, e.g.\n");
00032         printf("                0x0014  =   1 MBit/s\n");
00033         printf("                0x001C  = 500 kBit/s\n");
00034         printf("                0x011C  = 250 kBit/s\n");
00035         printf("                0x031C  = 125 kBit/s\n");
00036         printf("                0x432F  = 100 kBit/s\n");
00037         printf("                0x472F  =  50 kBit/s\n");
00038         printf("                0x532F  =  20 kBit/s\n");
00039         printf("                0x672F  =  10 kBit/s\n");
00040         printf("                0x7F7F  =   5 kBit/s\n");
00041         printf("                (default: 1 Mbit).\n");
00042         printf("        -? or --help - this help\n");
00043         printf("\n");
00044 }
00045 
00046 
00047 
00048 
00049 pcan_transmit::pcan_transmit()
00050 {
00057         //Initialisation
00058         h = 0 ;
00059         pcan_sub = n.subscribe("pcan_transmitted", 1, &pcan_transmit::transmit, this);
00060 }
00061 
00062 void pcan_transmit::init(int argc, char **argv)
00063 {
00072         int   nExtended = CAN_INIT_TYPE_ST;
00073         int nType;
00074         __u32 dwPort;
00075         __u16 wIrq;
00076         char *ptr;
00077         __u16 wBTR0BTR1 = 0x0014;
00078         // parameter wBTR0BTR1
00079         // bitrate codes of BTR0/BTR1 registers
00080         //#define CAN_BAUD_1M     0x0014  //   1 MBit/s
00081         //#define CAN_BAUD_500K   0x001C  // 500 kBit/s
00082         //#define CAN_BAUD_250K   0x011C  // 250 kBit/s
00083         //#define CAN_BAUD_125K   0x031C  // 125 kBit/s
00084         //#define CAN_BAUD_100K   0x432F  // 100 kBit/s
00085         //#define CAN_BAUD_50K    0x472F  //  50 kBit/s
00086         //#define CAN_BAUD_20K    0x532F  //  20 kBit/s
00087         //#define CAN_BAUD_10K    0x672F  //  10 kBit/s
00088         //#define CAN_BAUD_5K     0x7F7F  //   5 kBit/s
00089         const char  *szDevNode = DEFAULT_NODE;
00090         bool bDevNodeGiven = false;
00091         bool bTypeGiven = false;
00092         char txt[VERSIONSTRING_LEN];
00093 
00094         // decode command line arguments
00095                 for (int i = 1; i < argc; i++)
00096                 {
00097                     char c;
00098 
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_transmit: 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_transmit: 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_transmit: driver version = %s\n", txt);
00159                      else {
00160                        perror("pcan_transmit: 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, nExtended);
00167                        if (errno)
00168                        {
00169                          perror("pcan_transmit: CAN_Init()");
00170                          do_exit(errno, h);;
00171                        }
00172                      }
00173 
00174 }
00175 TPCANMsg pcan_transmit::StringToTPCANRdMsg(std_msgs::String msg)
00176 {
00184         TPCANMsg m;
00185         std::string can_message = msg.data;
00186         std::stringstream ss;
00187         std::istringstream iss(can_message);
00188         std::vector<std::string> tokens;
00189         std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), std::back_inserter<std::vector<std::string> >(tokens));
00190         if(tokens.size()>2)
00191         {
00192                 m.ID = (DWORD) std::strtol(tokens.at(0).data(),NULL,0);
00193                 m.LEN = (BYTE) std::strtol(tokens.at(1).data(),NULL,0);
00194         }
00195         if(tokens.size()>3 && tokens.size()<11)
00196         {
00197                 for(int i = 2; i < tokens.size(); i++)
00198                 {
00199                         m.DATA[i-2] = (BYTE) std::strtol(tokens.at(i).data(),NULL,0);
00200                 }
00201 
00202         }
00203         return m;
00204 }
00205 
00206 
00207 void pcan_transmit::transmit(const std_msgs::String msg)
00208 {
00216         TPCANMsg m = StringToTPCANRdMsg(msg);
00217 
00218         //send the message
00219         //CheckTPCANMsg(m) &&
00220         if (  CAN_Write(h, &m))
00221         {
00222                 perror("pcan_transmit: CAN_Write()");
00223         }
00224 }
00225 
00226 


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