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

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

ajout des capteurs CanGateway et Alasca

File size: 6.3 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 "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}
30
31/************************************************************************/
32/// Destructor
33Win32CanInterface::~Win32CanInterface()
34{
35}
36
37/************************************************************************/
38/// Opens the CAN interface of the number given in argument
39bool Win32CanInterface::openInterface(const int number, const unsigned int speed)
40{
41 canDriver_ = new CanDriver(number, speed);
42
43 //connection to CAN bus
44 if(canDriver_->initPort() != 0)
45 {
46 cout << "CAN connection fails" << endl;
47 return false;
48 }
49 else
50 return true;
51}
52/************************************************************************/
53/// Open the CAN interface of the number given in argument
54bool Win32CanInterface::openInterface(char * port, char * accessMode)
55{
56 canDriver_ = new CanDriver(port, accessMode);
57 //connection to CAN bus
58 if(canDriver_->initPort() != 0)
59 {
60 cout << "CAN connection fails" << endl;
61 return false;
62 }
63 else{
64 return true;
65 }
66}
67
68/************************************************************************/
69/// Close the CAN interface
70/// todo: close only the port identified by number
71/// make a function that close the driver or close the driver only if
72/// there is no more pending connections
73bool Win32CanInterface::closeInterface(const int /*number*/)
74{
75 if(canDriver_->cleanUpPort() != 0)
76 {
77 cout << "CAN disconnection fails" << endl;
78 delete canDriver_;
79 return false;
80 }
81 else
82 {
83 delete canDriver_;
84 return true;
85 }
86}
87
88/************************************************************************/
89/// The main loop of the class
90void Win32CanInterface::run()
91{
92 continue_ = true;
93 counter_ = 0;
94
95 if (!receivedFramesArraySize_)
96 qFatal("receivedFramesArraySize_ not initialized, division by zero may occur if you continue. Context:%s:L%d",__FILE__, __LINE__);
97
98 cout << "Win32CanInterface starts" << endl;
99
100 switch (source_)
101 {
102 case VectorCard:
103 vectorLoop();
104 break;
105 case SharedMemory:
106 shMemLoop();
107 break;
108 case PeakCard:
109 peakLoop();
110 break;
111 case XLVectorCard:
112 vectorXlLoop();
113 break;
114 default:
115 break;
116 }
117 counter_ = 0;
118 cout << "Win32CanInterface thread stopped...\n" << endl;
119}
120
121
122/************************************************************************/
123/// The loop used for waiting CAN data from Vector card
124void Win32CanInterface::vectorLoop()
125{
126 while (continue_)
127 {
128 // Wait incoming data from the CAN bus
129 if (canDriver_->receiveFrame(frame_) == 0) {
130 receivedFrames_[counter_].time = road_time();
131 receivedFrames_[counter_].timerange = 0;
132 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
133 semaphore_->release();
134 counter_++;
135 counter_ = counter_ % receivedFramesArraySize_;
136 }
137 }
138}
139
140/************************************************************************/
141/// The loop used for waiting CAN data from XL Vector card
142void Win32CanInterface::vectorXlLoop()
143{
144 while(continue_) {
145 // Wait incoming data from the CAN bus
146 if (canDriver_->receiveFrame(frame_) == 0) {
147 receivedFrames_[counter_].time = road_time();
148 receivedFrames_[counter_].timerange = 0;
149 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame));
150 semaphore_->release();
151 counter_++;
152 counter_ = counter_ % receivedFramesArraySize_;
153 }
154 }
155}
156
157/************************************************************************/
158/* The loop used for waiting CAN data from a shared memory
159/************************************************************************/
160void Win32CanInterface::shMemLoop()
161{
162 shMem_ = new ShMem("CARMEN_CAN_2200", sizeof(TimestampedCanFrame));
163 while (continue_)
164 {
165 // Wait incoming data from the shared memory
166 if ( shMem_->wait(100) )
167 {
168 TimestampedCanFrame* ptr = (TimestampedCanFrame*)(shMem_->read());
169 memcpy(&frame_, &(ptr->frame), sizeof(CanFrame));
170 receivedFrames_[counter_].time = ptr->time;
171 receivedFrames_[counter_].timerange = ptr->timerange;
172 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
173 semaphore_->release();
174 counter_++;
175 counter_ = counter_ % receivedFramesArraySize_;
176 }
177 } // END while (continue_)
178
179 delete shMem_;
180}
181
182/************************************************************************/
183/// The loop used for waiting CAN data from Peak card
184void Win32CanInterface::peakLoop()
185{
186 std::cout << "In peak loop" << std::endl;
187 while(continue_)
188 {
189 // Wait incoming data from the CAN bus
190 if ( canDriver_->receiveFrame(frame_) == 0 ) {
191 receivedFrames_[counter_].time = road_time();
192 receivedFrames_[counter_].timerange = 0;
193 memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
194 semaphore_->release();
195 counter_++;
196 counter_ = counter_ % receivedFramesArraySize_;
197 }
198 }
199}
200
201/************************************************************************/
202/// Stops the thread
203void Win32CanInterface::stop()
204{
205 continue_ = false;
206}
207
208/************************************************************************/
209/// Defines the place where the incoming frames will be copied
210void Win32CanInterface::setExchangeBuffer(TimestampedCanFrame * framesArray, const int framesArraySize)
211{
212 receivedFrames_ = framesArray;
213 receivedFramesArraySize_ = framesArraySize;
214}
215
216void Win32CanInterface::setSource(DataSource source)
217{
218 source_ = source;
219}
220
221} // namespace pacpus
Note: See TracBrowser for help on using the repository browser.