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

Last change on this file since 17 was 4, checked in by guyardro, 11 years ago

add igep can driver for CanGateway and add support for igep compilation

File size: 6.0 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
159
160/************************************************************************/
161/* The main loop of the thread
162/************************************************************************/
163void CanGateway::run()
164{
165 counter_ = 0;
166 qDebug() << componentName << "thread is started";
167
168 if (recording) {
169 rawCanFile_.open(componentName.toStdString() + "_rawcan.dbt", WriteMode, CARMEN_CAN_RAW , sizeof( CanFrame ) );
170 }
171
172 tic();
173
174 while (THREAD_ALIVE)
175 {
176 semaphore_.acquire();
177 if (!THREAD_ALIVE)
178 {
179 continue; // user asks stopping the thread
180 }
181
182 //displayData(incomingCanFrames_[counter_].frame.data, incomingCanFrames_[counter_].frame.dlc, incomingCanFrames_[counter_].frame.id);
183 if (recording)
184 {
185 rawCanFile_.writeRecord(incomingCanFrames_[counter_].time, incomingCanFrames_[counter_].timerange,
186 reinterpret_cast<const char *>(&(incomingCanFrames_[counter_].frame)), sizeof(CanFrame));
187 }
188
189 setState(ComponentBase::MONITOR_OK);
190
191// printf("id:%x\n",incomingCanFrames_[counter_].frame.id);
192
193 /* switch (incomingCanFrames_[counter_].frame.id)
194 {
195 default:
196 // unknown identifier
197 break;
198 };
199
200 */
201
202 dispatchCanFrame(incomingCanFrames_[counter_]);
203
204 counter_++;
205 counter_ = counter_ % INCOMINGCANFRAMES_SIZE;
206}
207
208 if (recording)
209 {
210 rawCanFile_.close();
211 }
212
213 qDebug() << componentName << "thread is stopped";
214}
215
216
Note: See TracBrowser for help on using the repository browser.