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

Last change on this file since 91 was 91, checked in by DHERBOMEZ Gérald, 9 years ago

Small corrections during tests: CAN reading OK under Windows with Kvaser Leaf Light.

File size: 6.3 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#include "../CanGateway/CanDecoderBase.h"
28
29
30
31////////////////////////////////////////////////////////////////////////////////
32/// Construct the factory
33ComponentFactory<CanGateway> sFactory("CanGateway");
34
35
36////////////////////////////////////////////////////////////////////////////////
37/// Constructor
38CanGateway::CanGateway(QString name)
39 : ComponentBase(name)
40{
41 counter_ = 0;
42// tcpServer_ = NULL;
43 //subscribers_ = new QMultiHash<int, ComponentBase *>;
44}
45
46////////////////////////////////////////////////////////////////////////////////
47/// Destructor
48CanGateway::~CanGateway()
49{
50}
51
52/************************************************************************/
53/* Start function
54/************************************************************************/
55void CanGateway::startActivity()
56{
57 counter_ = 0;
58 THREAD_ALIVE = true;
59
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 if (source_ == "kvaser")
94 {
95 canIf_.setSource(Win32CanInterface::KvaserCard);
96 // open interface
97 if (canIf_.openInterface(channel_,speed_)==0)
98 qFatal("Failed to open the CAN interface num %d at speed %d",channel_,speed_);
99 }
100 else
101 {
102 qCritical("Error in the source property of the component, bad value");
103 return;
104 }
105
106 // start the 2 threads: reception thread and decoding thread
107 canIf_.start();
108 start();
109}
110
111
112
113/************************************************************************/
114/* Stop function
115/************************************************************************/
116void CanGateway::stopActivity()
117{
118 counter_ = 0;
119 canIf_.stop();
120
121 if ((source_ == "vector")||(source_=="peak")||(source_=="vectorXL")||(source_=="igep")||(source_=="kvaser"))
122 canIf_.closeInterface(channel_);
123
124 canIf_.wait();
125
126 // we stop the decoding thread
127 THREAD_ALIVE = false;
128 semaphore_.release(); // to release the waiting of new CAN frame
129
130 if (!wait(1000))
131 {
132 terminate();
133 qDebug() << "The thread" << name() << "seems blocked, it has been killed";
134 }
135}
136
137
138
139/************************************************************************/
140/* Configuration of the component
141/************************************************************************/
142ComponentBase::COMPONENT_CONFIGURATION CanGateway::configureComponent(XmlComponentConfig config)
143{
144 // FIXME: use string instead of char[]
145 // Peak driver, default values
146 strcpy(port_,"/dev/pcanusb0");
147 strcpy(accessMode_,"ReadOnly");
148
149 channel_ = config.getProperty("channel").toInt() - 1;
150 speed_ = config.getProperty("speed").toInt() * 1000;
151 source_ = config.getProperty("source");
152 if (source_ =="peak")
153 {
154 if (config.getProperty("port")!= NULL)
155 strcpy(port_, config.getProperty("port").toStdString().c_str());
156 if (config.getProperty("mode")!= NULL)
157 strcpy(accessMode_, config.getProperty("mode").toStdString().c_str());
158 }
159 setRecording( config.getProperty("recording") == "true" ? true : false );
160
161 verbose_ = config.getProperty("verbose") == "true" ? true : false;
162
163 return ComponentBase::CONFIGURED_OK;
164}
165
166
167bool CanGateway::sendFrame(const CanFrame frame)
168{
169 if (canIf_.canDriver_ == NULL)
170 return false;
171 else
172 {
173 if (canIf_.canDriver_->sendFrame(frame))
174 return true;
175 else
176 return false;
177 }
178}
179
180
181
182/************************************************************************/
183/* The main loop of the thread
184/************************************************************************/
185void CanGateway::run()
186{
187 counter_ = 0;
188 qDebug() << name() << "thread is started";
189
190 if (isRecording()) {
191 rawCanFile_.open(name().toStdString() + "_rawcan.dbt", WriteMode, CARMEN_CAN_RAW , sizeof( CanFrame ) );
192 }
193
194 tic();
195
196 while (THREAD_ALIVE)
197 {
198 semaphore_.acquire();
199 if (!THREAD_ALIVE)
200 {
201 continue; // user asks stopping the thread
202 }
203
204 if (verbose_) displayData(incomingCanFrames_[counter_].frame.data, incomingCanFrames_[counter_].frame.dlc, incomingCanFrames_[counter_].frame.id);
205 if (isRecording())
206 {
207 rawCanFile_.writeRecord(incomingCanFrames_[counter_].time, incomingCanFrames_[counter_].timerange,
208 reinterpret_cast<const char *>(&(incomingCanFrames_[counter_].frame)), sizeof(CanFrame));
209 }
210
211 setState(ComponentBase::MONITOR_OK);
212
213 // printf("id:%x\n",incomingCanFrames_[counter_].frame.id);
214
215 /* switch (incomingCanFrames_[counter_].frame.id)
216 {
217 default:
218 // unknown identifier
219 break;
220 };
221
222 */
223
224 dispatchCanFrame(incomingCanFrames_[counter_]);
225
226 counter_++;
227 counter_ = counter_ % INCOMINGCANFRAMES_SIZE;
228 }
229
230 if (isRecording())
231 {
232 rawCanFile_.close();
233 }
234
235 qDebug() << name() << "thread is stopped";
236}
237
238
Note: See TracBrowser for help on using the repository browser.