Changeset 44 in pacpussensors for trunk/CanGateway/CanGateway.cpp
- Timestamp:
- Apr 11, 2014, 4:22:07 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/CanGateway/CanGateway.cpp
r41 r44 25 25 #include <QList> 26 26 27 28 29 27 #include "../CanGateway/CanDecoderBase.h" 30 28 … … 34 32 /// Construct the factory 35 33 ComponentFactory<CanGateway> sFactory("CanGateway"); 34 36 35 37 36 //////////////////////////////////////////////////////////////////////////////// … … 56 55 void CanGateway::startActivity() 57 56 { 58 59 57 counter_ = 0; 58 THREAD_ALIVE = true; 60 59 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(); 105 102 } 106 103 … … 112 109 void CanGateway::stopActivity() 113 110 { 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 } 129 128 } 130 129 … … 136 135 ComponentBase::COMPONENT_CONFIGURATION CanGateway::configureComponent(XmlComponentConfig config) 137 136 { 138 139 140 141 142 143 channel_ = param.getProperty("channel").toInt() - 1;144 speed_ = param.getProperty("speed").toInt() * 1000;145 source_ = param.getProperty("source");146 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 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; 156 155 } 157 156 … … 177 176 void CanGateway::run() 178 177 { 179 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 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(); 187 186 188 189 190 191 192 193 194 195 196 197 if (recording)198 { 199 200 201 202 203 204 205 // printf("id:%x\n",incomingCanFrames_[counter_].frame.id);206 207 208 209 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: 210 209 // unknown identifier 211 210 break; 212 211 }; 213 212 214 213 */ 215 214 216 217 218 219 220 }221 222 if (recording)223 215 dispatchCanFrame(incomingCanFrames_[counter_]); 216 217 counter_++; 218 counter_ = counter_ % INCOMINGCANFRAMES_SIZE; 219 } 220 221 if (isRecording()) 222 { 224 223 rawCanFile_.close(); 225 224 } 226 225 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.