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 |
|
---|
18 | namespace pacpus {
|
---|
19 |
|
---|
20 | using namespace std;
|
---|
21 |
|
---|
22 | /************************************************************************/
|
---|
23 | /// Constructor
|
---|
24 | Win32CanInterface::Win32CanInterface()
|
---|
25 | {
|
---|
26 | continue_ = true;
|
---|
27 | counter_ = 0;
|
---|
28 | receivedFramesArraySize_ = 0;
|
---|
29 | }
|
---|
30 |
|
---|
31 | /************************************************************************/
|
---|
32 | /// Destructor
|
---|
33 | Win32CanInterface::~Win32CanInterface()
|
---|
34 | {
|
---|
35 | }
|
---|
36 |
|
---|
37 | /************************************************************************/
|
---|
38 | /// Opens the CAN interface of the number given in argument
|
---|
39 | bool 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
|
---|
54 | bool 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
|
---|
73 | bool 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
|
---|
90 | void 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 igepCard:
|
---|
112 | igepLoop();
|
---|
113 | break;
|
---|
114 | case XLVectorCard:
|
---|
115 | vectorXlLoop();
|
---|
116 | break;
|
---|
117 | default:
|
---|
118 | break;
|
---|
119 | }
|
---|
120 | counter_ = 0;
|
---|
121 | cout << "Win32CanInterface thread stopped...\n" << endl;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | /************************************************************************/
|
---|
126 | /// The loop used for waiting CAN data from Vector card
|
---|
127 | void Win32CanInterface::vectorLoop()
|
---|
128 | {
|
---|
129 | while (continue_)
|
---|
130 | {
|
---|
131 | // Wait incoming data from the CAN bus
|
---|
132 | if (canDriver_->receiveFrame(frame_) == 0) {
|
---|
133 | receivedFrames_[counter_].time = road_time();
|
---|
134 | receivedFrames_[counter_].timerange = 0;
|
---|
135 | memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
|
---|
136 | semaphore_->release();
|
---|
137 | counter_++;
|
---|
138 | counter_ = counter_ % receivedFramesArraySize_;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | /************************************************************************/
|
---|
144 | /// The loop used for waiting CAN data from XL Vector card
|
---|
145 | void Win32CanInterface::vectorXlLoop()
|
---|
146 | {
|
---|
147 | while(continue_) {
|
---|
148 | // Wait incoming data from the CAN bus
|
---|
149 | if (canDriver_->receiveFrame(frame_) == 0) {
|
---|
150 | receivedFrames_[counter_].time = road_time();
|
---|
151 | receivedFrames_[counter_].timerange = 0;
|
---|
152 | memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame));
|
---|
153 | semaphore_->release();
|
---|
154 | counter_++;
|
---|
155 | counter_ = counter_ % receivedFramesArraySize_;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /************************************************************************/
|
---|
161 | /* The loop used for waiting CAN data from a shared memory
|
---|
162 | /************************************************************************/
|
---|
163 | void Win32CanInterface::shMemLoop()
|
---|
164 | {
|
---|
165 | shMem_ = new ShMem("CARMEN_CAN_2200", sizeof(TimestampedCanFrame));
|
---|
166 | while (continue_)
|
---|
167 | {
|
---|
168 | // Wait incoming data from the shared memory
|
---|
169 | if ( shMem_->wait(100) )
|
---|
170 | {
|
---|
171 | TimestampedCanFrame* ptr = (TimestampedCanFrame*)(shMem_->read());
|
---|
172 | memcpy(&frame_, &(ptr->frame), sizeof(CanFrame));
|
---|
173 | receivedFrames_[counter_].time = ptr->time;
|
---|
174 | receivedFrames_[counter_].timerange = ptr->timerange;
|
---|
175 | memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
|
---|
176 | semaphore_->release();
|
---|
177 | counter_++;
|
---|
178 | counter_ = counter_ % receivedFramesArraySize_;
|
---|
179 | }
|
---|
180 | } // END while (continue_)
|
---|
181 |
|
---|
182 | delete shMem_;
|
---|
183 | }
|
---|
184 |
|
---|
185 | /************************************************************************/
|
---|
186 | /// The loop used for waiting CAN data from Peak card
|
---|
187 | void Win32CanInterface::peakLoop()
|
---|
188 | {
|
---|
189 | std::cout << "In peak loop" << std::endl;
|
---|
190 | while(continue_)
|
---|
191 | {
|
---|
192 | // Wait incoming data from the CAN bus
|
---|
193 | if ( canDriver_->receiveFrame(frame_) == 0 ) {
|
---|
194 | receivedFrames_[counter_].time = road_time();
|
---|
195 | receivedFrames_[counter_].timerange = 0;
|
---|
196 | memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
|
---|
197 | semaphore_->release();
|
---|
198 | counter_++;
|
---|
199 | counter_ = counter_ % receivedFramesArraySize_;
|
---|
200 | }
|
---|
201 | }
|
---|
202 | }
|
---|
203 |
|
---|
204 | /************************************************************************/
|
---|
205 | /// The loop used for waiting CAN data from igep card
|
---|
206 | void Win32CanInterface::igepLoop()
|
---|
207 | {
|
---|
208 | std::cout << "In igep loop" << std::endl;
|
---|
209 |
|
---|
210 | while(continue_)
|
---|
211 | {
|
---|
212 | // Wait incoming data from the CAN bus
|
---|
213 | if ( canDriver_->receiveFrame(frame_) == 0 ) {
|
---|
214 | receivedFrames_[counter_].time = road_time();
|
---|
215 | receivedFrames_[counter_].timerange = 0;
|
---|
216 | memcpy(&(receivedFrames_[counter_].frame), &frame_, sizeof(CanFrame) );
|
---|
217 | semaphore_->release();
|
---|
218 | counter_++;
|
---|
219 | counter_ = counter_ % receivedFramesArraySize_;
|
---|
220 | }
|
---|
221 | }
|
---|
222 | }
|
---|
223 | /************************************************************************/
|
---|
224 | /// Stops the thread
|
---|
225 | void Win32CanInterface::stop()
|
---|
226 | {
|
---|
227 | continue_ = false;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /************************************************************************/
|
---|
231 | /// Defines the place where the incoming frames will be copied
|
---|
232 | void Win32CanInterface::setExchangeBuffer(TimestampedCanFrame * framesArray, const int framesArraySize)
|
---|
233 | {
|
---|
234 | receivedFrames_ = framesArray;
|
---|
235 | receivedFramesArraySize_ = framesArraySize;
|
---|
236 | }
|
---|
237 |
|
---|
238 | void Win32CanInterface::setSource(DataSource source)
|
---|
239 | {
|
---|
240 | source_ = source;
|
---|
241 | }
|
---|
242 |
|
---|
243 | } // namespace pacpus
|
---|