source: pacpussensors/trunk/CanGateway/Win32CanInterface.cpp@ 67

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

CANGATEWAY changed

File size: 7.0 KB
Line 
1/*********************************************************************
2// created: 2008/2/11 - 11:59
3// filename: Win32CanInterface.cpp
4//
5// author: Gerald Dherbomez
6// Copyright Heudiasyc UMR UTC/CNRS 6599
7//
8// version: $Id: $
9//
10// purpose: Windows specific management of the Can Interface
11//
12*********************************************************************/
13
14#include "Win32CanInterface.h"
15
16#include "Pacpus/PacpusTools/ShMem.h"
17
18namespace pacpus {
19
20using namespace std;
21
22/************************************************************************/
23/// Constructor
24Win32CanInterface::Win32CanInterface()
25{
26 continue_ = true;
27 counter_ = 0;
28 receivedFramesArraySize_ = 0;
29 canDriver_ = NULL;
30}
31
32/************************************************************************/
33/// Destructor
34Win32CanInterface::~Win32CanInterface()
35{
36}
37
38/************************************************************************/
39/// Opens the CAN interface of the number given in argument
40bool Win32CanInterface::openInterface(const int number, const unsigned int speed)
41{
42 canDriver_ = new CanDriver(number, speed);
43
44 //connection to CAN bus
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
55bool 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
71/// todo: close only the port identified by number
72/// make a function that close the driver or close the driver only if
73/// there is no more pending connections
74bool Win32CanInterface::closeInterface(const int /*number*/)
75{
76 if(canDriver_->cleanUpPort() != 0)
77 {
78 cout << "CAN disconnection fails" << endl;
79 delete canDriver_;
80 return false;
81 }
82 else
83 {
84 delete canDriver_;
85 return true;
86 }
87}
88
89/************************************************************************/
90/// The main loop of the class
91void Win32CanInterface::run()
92{
93 continue_ = true;
94 counter_ = 0;
95
96 if (!receivedFramesArraySize_)
97 qFatal("receivedFramesArraySize_ not initialized, division by zero may occur if you continue. Context:%s:L%d",__FILE__, __LINE__);
98
99 cout << "Win32CanInterface starts" << endl;
100
101 switch (source_)
102 {
103 case VectorCard:
104 vectorLoop();
105 break;
106 case SharedMemory:
107 shMemLoop();
108 break;
109 case PeakCard:
110 peakLoop();
111 break;
112 case igepCard:
113 igepLoop();
114 break;
115 case XLVectorCard:
116 vectorXlLoop();
117 break;
118 default:
119 break;
120 }
121 counter_ = 0;
122 cout << "Win32CanInterface thread stopped...\n" << endl;
123}
124
125
126/************************************************************************/
127/// The loop used for waiting CAN data from Vector card
128void Win32CanInterface::vectorLoop()
129{
130 while (continue_)
131 {
132 // Wait incoming data from the CAN bus
133 if (canDriver_->receiveFrame(frame_) == 0) {
134 receivedFrames_[counter_].time = road_time();
135 receivedFrames_[counter_].timerange = 0;
136 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
137 semaphore_->release();
138 counter_++;
139 counter_ = counter_ % receivedFramesArraySize_;
140 }
141 }
142}
143
144/************************************************************************/
145/// The loop used for waiting CAN data from XL Vector card
146void Win32CanInterface::vectorXlLoop()
147{
148 while(continue_) {
149 // Wait incoming data from the CAN bus
150 if (canDriver_->receiveFrame(frame_) == 0) {
151 receivedFrames_[counter_].time = road_time();
152 receivedFrames_[counter_].timerange = 0;
153 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame));
154 semaphore_->release();
155 counter_++;
156 counter_ = counter_ % receivedFramesArraySize_;
157 }
158 }
159}
160
161/************************************************************************/
162/* The loop used for waiting CAN data from a shared memory
163/************************************************************************/
164void Win32CanInterface::shMemLoop()
165{
166 shMem_ = new ShMem("CARMEN_CAN_2200", sizeof(TimestampedCanFrame));
167 while (continue_)
168 {
169 // Wait incoming data from the shared memory
170 if ( shMem_->wait(100) )
171 {
172 TimestampedCanFrame* ptr = (TimestampedCanFrame*)(shMem_->read());
173 memcpy(&frame_, &(ptr->frame), sizeof(CanFrame));
174 receivedFrames_[counter_].time = ptr->time;
175 receivedFrames_[counter_].timerange = ptr->timerange;
176 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
177 semaphore_->release();
178 counter_++;
179 counter_ = counter_ % receivedFramesArraySize_;
180 }
181 } // END while (continue_)
182
183 delete shMem_;
184}
185
186/************************************************************************/
187/// The loop used for waiting CAN data from Peak card
188void Win32CanInterface::peakLoop()
189{
190 std::cout << "In peak loop" << std::endl;
191 while(continue_)
192 {
193 // Wait incoming data from the CAN bus
194 if ( canDriver_->receiveFrame(frame_) == 0 ) {
195 receivedFrames_[counter_].time = road_time();
196 receivedFrames_[counter_].timerange = 0;
197 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
198 semaphore_->release();
199 counter_++;
200 counter_ = counter_ % receivedFramesArraySize_;
201 }
202 }
203}
204
205/************************************************************************/
206/// The loop used for waiting CAN data from igep card
207void Win32CanInterface::igepLoop()
208{
209 std::cout << "In igep loop" << std::endl;
210
211 while(continue_)
212 {
213 // Wait incoming data from the CAN bus
214 if ( canDriver_->receiveFrame(frame_) == 0 ) {
215 receivedFrames_[counter_].time = road_time();
216 receivedFrames_[counter_].timerange = 0;
217 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
218 semaphore_->release();
219 counter_++;
220 counter_ = counter_ % receivedFramesArraySize_;
221 }
222 }
223}
224/************************************************************************/
225/// Stops the thread
226void Win32CanInterface::stop()
227{
228 continue_ = false;
229}
230
231/************************************************************************/
232/// Defines the place where the incoming frames will be copied
233void Win32CanInterface::setExchangeBuffer(TimestampedCanFrame * framesArray, const int framesArraySize)
234{
235 receivedFrames_ = framesArray;
236 receivedFramesArraySize_ = framesArraySize;
237}
238
239void Win32CanInterface::setSource(DataSource source)
240{
241 source_ = source;
242}
243
244} // namespace pacpus
Note: See TracBrowser for help on using the repository browser.