source: pacpussensors/trunk/CanGateway/CanGateway.cpp@ 41

Last change on this file since 41 was 41, checked in by phudelai, 10 years ago

CANGATEWAY changed

File size: 6.2 KB
Line 
1/********************************************************************
2// created: 2008/2/11 - 12:55
3// filename: CanGateway.cpp
4//
5// author: Gerald Dherbomez
6// Copyright Heudiasyc UMR UTC/CNRS 7253
7//
8// version: $Id: $
9//
10// purpose: Decoding of the CAN bus
11//
12*********************************************************************/
13
14
15#include "Pacpus/kernel/ComponentFactory.h"
16#include "Pacpus/kernel/DbiteFileTypes.h"
17
18using namespace pacpus;
19
20#include "CanGateway.h"
21
22#include <QDebug>
23
24#include <QSemaphore>
25#include <QList>
26
27
28
29#include "../CanGateway/CanDecoderBase.h"
30
31
32
33////////////////////////////////////////////////////////////////////////////////
34/// Construct the factory
35ComponentFactory<CanGateway> sFactory("CanGateway");
36
37////////////////////////////////////////////////////////////////////////////////
38/// Constructor
39CanGateway::CanGateway(QString name)
40 : ComponentBase(name)
41{
42 counter_ = 0;
43// tcpServer_ = NULL;
44 //subscribers_ = new QMultiHash<int, ComponentBase *>;
45}
46
47////////////////////////////////////////////////////////////////////////////////
48/// Destructor
49CanGateway::~CanGateway()
50{
51}
52
53/************************************************************************/
54/* Start function
55/************************************************************************/
56void CanGateway::startActivity()
57{
58 counter_ = 0;
59 THREAD_ALIVE = true;
60
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
105}
106
107
108
109/************************************************************************/
110/* Stop function
111/************************************************************************/
112void CanGateway::stopActivity()
113{
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 }
129}
130
131
132
133/************************************************************************/
134/* Configuration of the component
135/************************************************************************/
136ComponentBase::COMPONENT_CONFIGURATION CanGateway::configureComponent(XmlComponentConfig config)
137{
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;
156}
157
158
159bool CanGateway::sendFrame(const CanFrame frame)
160{
161 if (canIf_.canDriver_ == NULL)
162 return false;
163 else
164 {
165 if (canIf_.canDriver_->sendFrame(frame))
166 return true;
167 else
168 return false;
169 }
170}
171
172
173
174/************************************************************************/
175/* The main loop of the thread
176/************************************************************************/
177void CanGateway::run()
178{
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();
187
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:
210 // unknown identifier
211 break;
212 };
213
214 */
215
216 dispatchCanFrame(incomingCanFrames_[counter_]);
217
218 counter_++;
219 counter_ = counter_ % INCOMINGCANFRAMES_SIZE;
220}
221
222 if (recording)
223 {
224 rawCanFile_.close();
225 }
226
227 qDebug() << componentName << "thread is stopped";
228}
229
230
Note: See TracBrowser for help on using the repository browser.