Changeset 44 in pacpussensors for trunk/CanGateway/CanGateway.cpp


Ignore:
Timestamp:
Apr 11, 2014, 4:22:07 PM (11 years ago)
Author:
phudelai
Message:

CANGATEWAY modified for Pacpus 0.1.x

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CanGateway/CanGateway.cpp

    r41 r44  
    2525#include <QList>
    2626
    27 
    28 
    2927#include "../CanGateway/CanDecoderBase.h"
    3028
     
    3432/// Construct the factory
    3533ComponentFactory<CanGateway> sFactory("CanGateway");
     34
    3635
    3736////////////////////////////////////////////////////////////////////////////////
     
    5655void CanGateway::startActivity()
    5756{
    58   counter_ = 0;
    59   THREAD_ALIVE = true;
     57        counter_ = 0;
     58        THREAD_ALIVE = true;
    6059 
    61   // set the exhange parameters for incoming CAN frames
    62   canIf_.setExchangeBuffer(incomingCanFrames_, INCOMINGCANFRAMES_SIZE);
    63   canIf_.setSignalSempahore(&semaphore_);
    64   if (source_ == "shMem")
    65     canIf_.setSource(Win32CanInterface::SharedMemory);
    66   else if (source_ == "vector")
    67   {
    68     canIf_.setSource(Win32CanInterface::VectorCard);
    69     // open the interface
    70     if (!canIf_.openInterface(channel_, speed_))
    71       qFatal("Failed to open the CAN interface num %d at speed %d",channel_,speed_);
    72   }
    73   else if (source_ == "vectorXL")
    74   {
    75           canIf_.setSource(Win32CanInterface::XLVectorCard);
    76     // open the interface
    77     if (!canIf_.openInterface(channel_, speed_))
    78       qFatal("Failed to open the CAN interface num %d at speed %d",channel_,speed_);
    79   }
    80   else if (source_ == "peak")
    81   {
    82     canIf_.setSource(Win32CanInterface::PeakCard);
    83     // open interface
    84     if (canIf_.openInterface(port_, accessMode_)==0)
    85       qFatal("Failed to open the CAN interface port %s in %s mode",port_, accessMode_);
    86   }
    87   else if (source_ == "igep")
    88   {
    89     canIf_.setSource(Win32CanInterface::igepCard);
    90     // open interface
    91     if (canIf_.openInterface(port_, accessMode_)==0)
    92       qFatal("Failed to open the CAN interface port %s in %s mode",port_, accessMode_);
    93   }
    94   else
    95   {
    96     qCritical("Error in the source property of the component, bad value");
    97     return;
    98   }
    99 
    100   // start the 2 threads: reception thread and decoding thread
    101   canIf_.start();
    102   start();
    103 
    104 
     60        // set the exhange parameters for incoming CAN frames
     61        canIf_.setExchangeBuffer(incomingCanFrames_, INCOMINGCANFRAMES_SIZE);
     62        canIf_.setSignalSempahore(&semaphore_);
     63        if (source_ == "shMem")
     64                canIf_.setSource(Win32CanInterface::SharedMemory);
     65        else if (source_ == "vector")
     66        {
     67                canIf_.setSource(Win32CanInterface::VectorCard);
     68                // open the interface
     69                if (!canIf_.openInterface(channel_, speed_))
     70                        qFatal("Failed to open the CAN interface num %d at speed %d",channel_,speed_);
     71        }
     72        else if (source_ == "vectorXL")
     73        {
     74                canIf_.setSource(Win32CanInterface::XLVectorCard);
     75                // open the interface
     76                if (!canIf_.openInterface(channel_, speed_))
     77                        qFatal("Failed to open the CAN interface num %d at speed %d",channel_,speed_);
     78        }
     79        else if (source_ == "peak")
     80        {
     81                canIf_.setSource(Win32CanInterface::PeakCard);
     82                // open interface
     83                if (canIf_.openInterface(port_, accessMode_)==0)
     84                        qFatal("Failed to open the CAN interface port %s in %s mode",port_, accessMode_);
     85        }
     86        else if (source_ == "igep")
     87        {
     88                canIf_.setSource(Win32CanInterface::igepCard);
     89                // open interface
     90                if (canIf_.openInterface(port_, accessMode_)==0)
     91                        qFatal("Failed to open the CAN interface port %s in %s mode",port_, accessMode_);
     92        }
     93        else
     94        {
     95                qCritical("Error in the source property of the component, bad value");
     96                return;
     97        }
     98
     99        // start the 2 threads: reception thread and decoding thread
     100        canIf_.start();
     101        start();
    105102}
    106103
     
    112109void CanGateway::stopActivity()
    113110{
    114   counter_ = 0;
    115   canIf_.stop();
    116   if ((source_ == "vector")||(source_=="peak")||(source_=="vectorXL")||(source_=="igep"))
    117     canIf_.closeInterface(channel_);
    118   canIf_.wait();
    119 
    120   // we stop the decoding thread
    121   THREAD_ALIVE = false;
    122   semaphore_.release(); // to release the waiting of new CAN frame
    123 
    124   if (!wait(1000))
    125   {
    126     terminate();
    127     qDebug() << "The thread" << componentName << "seems blocked, it has been killed";
    128   }
     111        counter_ = 0;
     112        canIf_.stop();
     113
     114        if ((source_ == "vector")||(source_=="peak")||(source_=="vectorXL")||(source_=="igep"))
     115                canIf_.closeInterface(channel_);
     116       
     117        canIf_.wait();
     118
     119        // we stop the decoding thread
     120        THREAD_ALIVE = false;
     121        semaphore_.release(); // to release the waiting of new CAN frame
     122
     123        if (!wait(1000))
     124        {
     125                terminate();
     126                qDebug() << "The thread" << name() << "seems blocked, it has been killed";
     127        }
    129128}
    130129
     
    136135ComponentBase::COMPONENT_CONFIGURATION CanGateway::configureComponent(XmlComponentConfig config)
    137136{
    138   // FIXME: use string instead of char[]
    139   // Peak driver, default values
    140   strcpy(port_,"/dev/pcanusb0");
    141   strcpy(accessMode_,"ReadOnly");
    142 
    143   channel_ = param.getProperty("channel").toInt() - 1;
    144   speed_ = param.getProperty("speed").toInt() * 1000;
    145   source_ = param.getProperty("source");
    146   if (source_ =="peak")
    147   {
    148     if (param.getProperty("port")!= NULL)
    149         strcpy(port_,param.getProperty("port").toStdString().c_str());
    150     if (param.getProperty("mode")!= NULL)
    151         strcpy(accessMode_,param.getProperty("mode").toStdString().c_str());       
    152   }
    153   recording = (param.getProperty("recording") == "true" ? true : false);
    154 
    155   return ComponentBase::CONFIGURED_OK;
     137        // FIXME: use string instead of char[]
     138        // Peak driver, default values
     139        strcpy(port_,"/dev/pcanusb0");
     140        strcpy(accessMode_,"ReadOnly");
     141
     142        channel_ = config.getProperty("channel").toInt() - 1;
     143        speed_ = config.getProperty("speed").toInt() * 1000;
     144        source_ = config.getProperty("source");
     145        if (source_ =="peak")
     146        {
     147                if (config.getProperty("port")!= NULL)
     148                        strcpy(port_, config.getProperty("port").toStdString().c_str());
     149                if (config.getProperty("mode")!= NULL)
     150                        strcpy(accessMode_, config.getProperty("mode").toStdString().c_str());       
     151        }
     152        setRecording( config.getProperty("recording") == "true" ? true : false );
     153
     154        return ComponentBase::CONFIGURED_OK;
    156155}
    157156
     
    177176void CanGateway::run()
    178177{
    179   counter_ = 0;
    180   qDebug() << componentName << "thread is started";
    181 
    182   if (recording) {
    183     rawCanFile_.open(componentName.toStdString() + "_rawcan.dbt", WriteMode, CARMEN_CAN_RAW , sizeof( CanFrame ) );
    184   }
    185 
    186   tic();
     178        counter_ = 0;
     179        qDebug() << name() << "thread is started";
     180
     181        if (isRecording()) {
     182                rawCanFile_.open(name().toStdString() + "_rawcan.dbt", WriteMode, CARMEN_CAN_RAW , sizeof( CanFrame ) );
     183        }
     184
     185        tic();
    187186   
    188    while (THREAD_ALIVE)
    189    {
    190           semaphore_.acquire();
    191           if (!THREAD_ALIVE)
    192           {
    193                   continue; // user asks stopping the thread
    194           }
    195 
    196     //displayData(incomingCanFrames_[counter_].frame.data, incomingCanFrames_[counter_].frame.dlc, incomingCanFrames_[counter_].frame.id);
    197     if (recording)
    198         {
    199         rawCanFile_.writeRecord(incomingCanFrames_[counter_].time, incomingCanFrames_[counter_].timerange,
    200                                 reinterpret_cast<const char *>(&(incomingCanFrames_[counter_].frame)), sizeof(CanFrame));
    201     }
    202 
    203     setState(ComponentBase::MONITOR_OK);
    204 
    205 //      printf("id:%x\n",incomingCanFrames_[counter_].frame.id);
    206 
    207    /* switch (incomingCanFrames_[counter_].frame.id)
    208     {
    209     default:
     187        while (THREAD_ALIVE)
     188        {
     189                semaphore_.acquire();
     190                if (!THREAD_ALIVE)
     191                {
     192                        continue; // user asks stopping the thread
     193                }
     194
     195        //displayData(incomingCanFrames_[counter_].frame.data, incomingCanFrames_[counter_].frame.dlc, incomingCanFrames_[counter_].frame.id);
     196        if (isRecording())
     197        {
     198                rawCanFile_.writeRecord(incomingCanFrames_[counter_].time, incomingCanFrames_[counter_].timerange,
     199                                                                reinterpret_cast<const char *>(&(incomingCanFrames_[counter_].frame)), sizeof(CanFrame));
     200        }
     201
     202        setState(ComponentBase::MONITOR_OK);
     203
     204        //      printf("id:%x\n",incomingCanFrames_[counter_].frame.id);
     205
     206        /* switch (incomingCanFrames_[counter_].frame.id)
     207        {
     208        default:
    210209                // unknown identifier
    211         break;
     210                break;
    212211        };
    213212
    214213        */
    215214
    216   dispatchCanFrame(incomingCanFrames_[counter_]);
    217 
    218   counter_++;
    219   counter_ = counter_ % INCOMINGCANFRAMES_SIZE;
    220 }
    221 
    222   if (recording)
    223   {
     215        dispatchCanFrame(incomingCanFrames_[counter_]);
     216
     217        counter_++;
     218        counter_ = counter_ % INCOMINGCANFRAMES_SIZE;
     219        }
     220
     221        if (isRecording())
     222        {
    224223      rawCanFile_.close();
    225224        }
    226225 
    227   qDebug() << componentName << "thread is stopped";
    228 }
    229 
    230 
     226        qDebug() << name() << "thread is stopped";
     227}
     228
     229
Note: See TracChangeset for help on using the changeset viewer.