[1] | 1 | /*********************************************************************
|
---|
| 2 | // created: 2008/2/11 - 11:59
|
---|
| 3 | // filename: Win32CanInterface.cpp
|
---|
| 4 | //
|
---|
| 5 | // author: Gerald Dherbomez
|
---|
[89] | 6 | // Copyright Heudiasyc UMR UTC/CNRS 7253
|
---|
[116] | 7 | //
|
---|
[1] | 8 | // version: $Id: $
|
---|
| 9 | //
|
---|
[89] | 10 | // purpose: Management of the Can Interface
|
---|
[1] | 11 | //
|
---|
| 12 | *********************************************************************/
|
---|
| 13 |
|
---|
| 14 | #include "Win32CanInterface.h"
|
---|
| 15 |
|
---|
[94] | 16 | // #include "Pacpus/PacpusTools/ShMem.h" // runtime crash because PosixShMem
|
---|
[1] | 17 |
|
---|
| 18 | namespace pacpus {
|
---|
| 19 |
|
---|
| 20 | using namespace std;
|
---|
| 21 |
|
---|
| 22 | /************************************************************************/
|
---|
| 23 | /// Constructor
|
---|
| 24 | Win32CanInterface::Win32CanInterface()
|
---|
| 25 | {
|
---|
| 26 | continue_ = true;
|
---|
| 27 | counter_ = 0;
|
---|
| 28 | receivedFramesArraySize_ = 0;
|
---|
[41] | 29 | canDriver_ = NULL;
|
---|
[1] | 30 | }
|
---|
| 31 |
|
---|
| 32 | /************************************************************************/
|
---|
| 33 | /// Destructor
|
---|
| 34 | Win32CanInterface::~Win32CanInterface()
|
---|
| 35 | {
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | /************************************************************************/
|
---|
[116] | 39 | /// Opens the CAN interface of the number given in argument
|
---|
[1] | 40 | bool Win32CanInterface::openInterface(const int number, const unsigned int speed)
|
---|
| 41 | {
|
---|
| 42 | canDriver_ = new CanDriver(number, speed);
|
---|
[116] | 43 |
|
---|
| 44 | //connection to CAN bus
|
---|
[1] | 45 | if(canDriver_->initPort() != 0)
|
---|
| 46 | {
|
---|
| 47 | cout << "CAN connection fails" << endl;
|
---|
| 48 | return false;
|
---|
| 49 | }
|
---|
| 50 | else
|
---|
| 51 | return true;
|
---|
| 52 | }
|
---|
| 53 | /************************************************************************/
|
---|
| 54 | /// Open the CAN interface of the number given in argument
|
---|
| 55 | bool Win32CanInterface::openInterface(char * port, char * accessMode)
|
---|
| 56 | {
|
---|
| 57 | canDriver_ = new CanDriver(port, accessMode);
|
---|
| 58 | //connection to CAN bus
|
---|
| 59 | if(canDriver_->initPort() != 0)
|
---|
| 60 | {
|
---|
| 61 | cout << "CAN connection fails" << endl;
|
---|
| 62 | return false;
|
---|
| 63 | }
|
---|
| 64 | else{
|
---|
| 65 | return true;
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /************************************************************************/
|
---|
| 70 | /// Close the CAN interface
|
---|
[116] | 71 | /// todo: close only the port identified by number
|
---|
| 72 | /// make a function that close the driver or close the driver only if
|
---|
[1] | 73 | /// there is no more pending connections
|
---|
| 74 | bool Win32CanInterface::closeInterface(const int /*number*/)
|
---|
| 75 | {
|
---|
[116] | 76 | if(canDriver_ != NULL) {
|
---|
| 77 | if(canDriver_->cleanUpPort() != 0)
|
---|
| 78 | {
|
---|
| 79 | cout << "CAN disconnection fails" << endl;
|
---|
| 80 | delete canDriver_;
|
---|
| 81 | return false;
|
---|
| 82 | }
|
---|
| 83 | else
|
---|
| 84 | {
|
---|
| 85 | delete canDriver_;
|
---|
| 86 | return true;
|
---|
| 87 | }
|
---|
[1] | 88 | }
|
---|
| 89 | else
|
---|
| 90 | return true;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /************************************************************************/
|
---|
| 94 | /// The main loop of the class
|
---|
| 95 | void Win32CanInterface::run()
|
---|
| 96 | {
|
---|
| 97 | continue_ = true;
|
---|
| 98 | counter_ = 0;
|
---|
| 99 |
|
---|
| 100 | if (!receivedFramesArraySize_)
|
---|
[116] | 101 | qFatal("receivedFramesArraySize_ not initialized, division by zero may occur if you continue. Context:%s:L%d",__FILE__, __LINE__);
|
---|
[1] | 102 |
|
---|
| 103 | cout << "Win32CanInterface starts" << endl;
|
---|
| 104 |
|
---|
| 105 | switch (source_)
|
---|
| 106 | {
|
---|
| 107 | case VectorCard:
|
---|
| 108 | case PeakCard:
|
---|
[4] | 109 | case igepCard:
|
---|
[1] | 110 | case XLVectorCard:
|
---|
[89] | 111 | case KvaserCard:
|
---|
[116] | 112 | defaultLoop();
|
---|
[1] | 113 | break;
|
---|
[116] | 114 | default:
|
---|
[1] | 115 | break;
|
---|
| 116 | }
|
---|
| 117 | counter_ = 0;
|
---|
| 118 | cout << "Win32CanInterface thread stopped...\n" << endl;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
| 122 | /************************************************************************/
|
---|
| 123 | /// The loop used for waiting CAN data from Vector card
|
---|
[116] | 124 | // Default for vector, vectorXL, kvaser, Peak, Igep
|
---|
[1] | 125 |
|
---|
[116] | 126 | void Win32CanInterface::defaultLoop()
|
---|
[1] | 127 | {
|
---|
| 128 | while(continue_) {
|
---|
[116] | 129 | // Wait incoming data from the CAN bus
|
---|
[1] | 130 | if (canDriver_->receiveFrame(frame_) == 0) {
|
---|
| 131 | receivedFrames_[counter_].time = road_time();
|
---|
| 132 | receivedFrames_[counter_].timerange = 0;
|
---|
| 133 | memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame));
|
---|
| 134 | semaphore_->release();
|
---|
[116] | 135 | counter_++;
|
---|
[1] | 136 | counter_ = counter_ % receivedFramesArraySize_;
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /************************************************************************/
|
---|
| 142 | /* The loop used for waiting CAN data from a shared memory
|
---|
| 143 | /************************************************************************/
|
---|
[94] | 144 | // void Win32CanInterface::shMemLoop() // runtime crash because PosixShMem
|
---|
| 145 | // {
|
---|
| 146 | // shMem_ = new ShMem("CARMEN_CAN_2200", sizeof(TimestampedCanFrame));
|
---|
| 147 | // while (continue_)
|
---|
| 148 | // {
|
---|
| 149 | // // Wait incoming data from the shared memory
|
---|
| 150 | // if ( shMem_->wait(100) )
|
---|
| 151 | // {
|
---|
| 152 | // TimestampedCanFrame* ptr = (TimestampedCanFrame*)(shMem_->read());
|
---|
| 153 | // memcpy(&frame_, &(ptr->frame), sizeof(CanFrame));
|
---|
| 154 | // receivedFrames_[counter_].time = ptr->time;
|
---|
| 155 | // receivedFrames_[counter_].timerange = ptr->timerange;
|
---|
| 156 | // memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
|
---|
| 157 | // semaphore_->release();
|
---|
[116] | 158 | // counter_++;
|
---|
[94] | 159 | // counter_ = counter_ % receivedFramesArraySize_;
|
---|
| 160 | // }
|
---|
| 161 | // } // END while (continue_)
|
---|
[116] | 162 |
|
---|
[94] | 163 | // delete shMem_;
|
---|
| 164 | // }
|
---|
[1] | 165 |
|
---|
| 166 | /************************************************************************/
|
---|
| 167 | /// Stops the thread
|
---|
| 168 | void Win32CanInterface::stop()
|
---|
| 169 | {
|
---|
| 170 | continue_ = false;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /************************************************************************/
|
---|
| 174 | /// Defines the place where the incoming frames will be copied
|
---|
| 175 | void Win32CanInterface::setExchangeBuffer(TimestampedCanFrame * framesArray, const int framesArraySize)
|
---|
[116] | 176 | {
|
---|
[1] | 177 | receivedFrames_ = framesArray;
|
---|
| 178 | receivedFramesArraySize_ = framesArraySize;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | void Win32CanInterface::setSource(DataSource source)
|
---|
| 182 | {
|
---|
| 183 | source_ = source;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | } // namespace pacpus
|
---|