can_core.cpp
Go to the documentation of this file.
00001 #include <iostream>
00002 #include <iomanip>
00003 #include <cstring>
00004 #include <cstdio>
00005 #include <stdint.h>
00006 #include "ros/ros.h"
00007 #include "can_core.h"
00008 #include "tinycan/CanMsg.h"
00009 #include "can_drv.h"
00010 #include "config.h"
00011 #include "global.h"
00012 
00013 namespace tinycan {
00014 
00015 /*----------------------------------------------------------------------
00016  * Can()
00017  * Constructor.
00018  *--------------------------------------------------------------------*/
00019 
00020 Can::Can()
00021 {
00022   device_idx = 0;
00023   return;
00024 }
00025 
00026 /*----------------------------------------------------------------------
00027  * ~Can()
00028  * Destructor.
00029  *--------------------------------------------------------------------*/
00030 
00031 Can::~Can()
00032 {
00033   CanDownDriver();
00034   // **** DLL entladen
00035   UnloadDriver();
00036 }
00037 
00038 /*----------------------------------------------------------------------
00039  * init()
00040  * Init tinycan device.
00041  *--------------------------------------------------------------------*/
00042 
00043 int Can::init()
00044 {
00045   int err;
00046 
00047   if ((err = LoadDriver(TREIBER_NAME)) < 0)
00048   {
00049     std::cout << "error load driver! code " << err <<std::endl;
00050     return false;
00051   }
00052 
00053   if ((err = CanInitDriver((char*)TREIBER_INIT)) != 0)
00054   {
00055     std::cout << "error init driver! code " << err <<std::endl;
00056     return false;
00057   }
00058 
00059   if ((err = CanDeviceOpen(device_idx, DEVICE_OPEN)) != 0)
00060   {
00061     std::cout << "error device open! code " << err <<std::endl;
00062     return false;
00063   }
00064 
00065   /******************************/
00066   /*  CAN Speed einstellen      */
00067   /******************************/
00068   // **** Übertragungsgeschwindigkeit auf 250kBit/s einstellen
00069   CanSetSpeed(device_idx, CAN_SPEED);
00070 
00071   // **** CAN Bus Start, alle FIFOs, Filter, Puffer und Fehler löschen
00072   CanSetMode(device_idx, OP_CAN_START, CAN_CMD_ALL_CLEAR);
00073 
00074   return true;
00075 }
00076 
00077 /*----------------------------------------------------------------------
00078  * buildMsg()
00079  * Build a can message from given data.
00080  *--------------------------------------------------------------------*/
00081 
00082 int Can::buildMsg(TCanMsg *msg, uint8_t id, uint8_t len, char* data)
00083 {
00084   // msg Variable Initialisieren
00085   msg->MsgFlags = 0L; // Alle Flags löschen, Stanadrt Frame Format,
00086                       // keine RTR, Datenlänge auf 0
00087   msg->Id = id;
00088   msg->MsgLen = len;
00089   memcpy(msg->MsgData, data, len);
00090   return true;
00091 }
00092 
00093 /*----------------------------------------------------------------------
00094  * sendMsg()
00095  * Send a message via tinycan device.
00096  *--------------------------------------------------------------------*/
00097 
00098 int Can::sendMsg(TCanMsg *msg)
00099 {
00100   int err = 0;
00101 
00102   if ((err = CanTransmit(device_idx, msg, 1)) < 0)
00103   {
00104     std::cout << "CanTransmit Error-Code:" << err << std::endl;
00105     return false;
00106   }
00107   return true;
00108 }
00109 
00110 /*----------------------------------------------------------------------
00111  * readMsg()
00112  * Read a can message from tinycan device.
00113  *--------------------------------------------------------------------*/
00114 
00115 int Can::readMsg(TCanMsg *msg)
00116 {
00117   struct TDeviceStatus status;
00118 
00119   CanGetDeviceStatus(device_idx, &status);
00120 
00121   if (status.DrvStatus >= DRV_STATUS_CAN_OPEN)
00122   {
00123     if (status.CanStatus == CAN_STATUS_BUS_OFF)
00124     {
00125       std::cout << "CAN Status BusOff" << std::endl;
00126       CanSetMode(0, OP_CAN_RESET, CAN_CMD_NONE);
00127     }
00128   }
00129   else
00130   {
00131     std::cout << status.DrvStatus << std::endl;
00132     std::cout << "CAN Device nicht geöffnet" << std::endl;
00133     return false;
00134   }
00135 
00136   if (CanReceive(device_idx, msg, 1) > 0)
00137   {
00138     return true;
00139   }
00140   else
00141   {
00142     return false;
00143   }
00144   return true;
00145 }
00146 
00147 /*----------------------------------------------------------------------
00148  * printMsg()
00149  * Print a can message.
00150  *--------------------------------------------------------------------*/
00151 
00152 int Can::printMsg(TCanMsg *msg)
00153 {
00154   if (msg == NULL)
00155     return false;
00156   //std::cout <<  msg->Time.Sec << "." << msg->Time.USec << std::endl;
00157   std::cout << "id:" << msg->Id << " dlc:" << msg->MsgLen << std::endl << "data:" << std::endl;
00158   if (msg->MsgLen)
00159   {
00160     for (int i = 0; i < msg->MsgLen; i++)
00161     {
00162       if (i !=0)
00163         std::cout << "::";
00164       printf("%.2X", msg->MsgData[i]);
00165     }
00166   }
00167   else
00168     std::cout << " keine";
00169   std::cout << std::endl;
00170   return true;
00171 }
00172 
00173 /*----------------------------------------------------------------------
00174  * callbackCanMessage()
00175  * Callback function for can subscriber.
00176  *--------------------------------------------------------------------*/
00177 
00178 void Can::callbackCanMessage(const tinycan::CanMsg::ConstPtr& msg)
00179 {
00180   if (msg == NULL)
00181     return;
00182   TCanMsg tmsg;
00183   tmsg.MsgFlags = 0L; // Alle Flags löschen, Stanadrt Frame Format,
00184                    // keine RTR, Datenlänge auf 0
00185   tmsg.Id = msg->id;
00186   tmsg.MsgLen = msg->len;
00187 
00188   for (int i = 0 ; i < msg->len ; i++)
00189     tmsg.MsgData[i] = (char)(msg->data[i]);
00190   sendMsg(&tmsg);
00191 }
00192 
00193 /*----------------------------------------------------------------------
00194  * publishCanMessage()
00195  * Publish can message.
00196  *--------------------------------------------------------------------*/
00197 
00198 void Can::publishCanMessage(ros::Publisher *pub_message, TCanMsg *tmsg)
00199 {
00200   //ROS_INFO_STREAM("pub can message");
00201   if (tmsg == NULL)
00202     return;
00203   tinycan::CanMsg msg;
00204   msg.id = tmsg->Id;
00205   msg.len = tmsg->MsgLen;
00206   for (int i = 0 ; i < msg.len ; i++)
00207     msg.data[i] = tmsg->MsgData[i];
00208   pub_message->publish(msg);
00209 }
00210 }


tinycan
Author(s): M.Fischer
autogenerated on Thu Jun 6 2019 20:39:28