source: pacpussensors/trunk/Sick/SickLDMRSSensor.h@ 37

Last change on this file since 37 was 37, checked in by cfougera, 10 years ago

First commit of Sick lidars interfaces.

File size: 8.8 KB
Line 
1/*********************************************************************
2// created: 2014/02/02 - 10:48
3// filename: SickLDMRSSensor.h
4//
5// author: Cyril Fougeray
6// Copyright Heudiasyc UMR UTC/CNRS 6599
7//
8// version: $Id: $
9//
10// purpose: Definition of the SickLDMRSSensor class
11*********************************************************************/
12
13
14
15
16#ifndef SICKLDMRSSENSOR_H
17#define SICKLDMRSSENSOR_H
18
19#include "Pacpus/kernel/ComponentBase.h"
20#include "Pacpus/kernel/DbiteFile.h"
21
22#include "AbstractSickSensor.h"
23#include "SickLDMRSData.h"
24
25#include <fstream>
26#include <string>
27
28// Export macro for SickLDMRS DLL for Windows only
29#ifdef WIN32
30# ifdef SICKLDMRS_EXPORTS
31 // make DLL
32# define SICKLDMRS_API __declspec(dllexport)
33# else
34 // use DLL
35# define SICKLDMRS_API __declspec(dllimport)
36# endif
37#else
38 // On other platforms, simply ignore this
39# define SICKLDMRS_API
40#endif
41
42class QEvent;
43
44namespace pacpus {
45
46class ShMem;
47class SickComponent;
48
49
50
51/// The class carrying Sick LDMRS message.
52/** This class is used so that we can store every information sent by a Sick LDMRS sensor.
53 * First, the raw data is stored in \c body.
54 * These data are then decoded and general information about the message is stored in DataHeader and ScanHeader
55 * (Object data decoding is not implemented yet).
56 * Then, the body field is replaced by a ScanPoint or ScanObject array in order to be stored in DBT/UTC files.
57 */
58class SICKLDMRS_API MessageLDMRS
59{
60public:
61 /// Constructor.
62 MessageLDMRS()
63 {
64 time = 0;
65 timerange = 0;
66 memset(&hData,0,sizeof(hData));
67 memset(&hScan,0,sizeof(hScan));
68 }
69
70 /// Destructor.
71 ~MessageLDMRS(){}
72
73 //! An instance of DataHeader.
74 DataHeader hData;
75
76 //! An instance of ScanHeader (if data type is scan points).
77 ScanHeader hScan;
78
79 //! An array of characters : raw data then array of points or objects, depending on data type.
80 /** This array pointer points to allocated in memory (basically, in heap (malloc)) and then must be freed (free) when the whole message is decoded and stored. */
81 char* body;
82
83 //! Time when the message is received.
84 road_time_t time;
85
86 //! Timerange : roughly, time between measurement of a point and the processing step (not implemented).
87 road_timerange_t timerange;
88};
89
90
91
92//! The class implenting receiving, decoding and storing process of Sick LD-MRS data.
93/**
94 * This class can be used as a particular thread to acquire data from Sick LDMRS sensors.
95 * The Ethernet interface is used to get data from the sensor. Thus, the goal of this class is to
96 * get packets and decode them. Also, it offers the possibility to store all relevant information in
97 * two files (.dbt and .utc).
98 * It can be managed by SickComponent objects.
99 */
100class SickLDMRSSensor : public AbstractSickSensor
101{
102 Q_OBJECT
103public:
104 /// Constructor
105 SickLDMRSSensor(QObject *parent);
106
107 /**
108 * @brief SickLDMRSSensor constructor.
109 * @param parent Basically, a SickComponent object.
110 * @param name Name of the sensor in order to write on .dbt and .utc files and to recognize every sensors used.
111 * @param ip The IP address of the remote Sick LDMRS sensor.
112 * @param port The port of the remote Sick LDMRS sensor.
113 * @param recording If \c true, data is recorded into dbt + utc files. Data is not recorded otherwise.
114 */
115 SickLDMRSSensor(QObject *parent, QString name, QString ip, int port, int recording);
116
117 /// Destructor
118 ~SickLDMRSSensor();
119
120 void run() {}
121
122 void stopActivity(); /*!< To stop the processing thread */
123 void startActivity(); /*!< To start the processing thread */
124
125 /**
126 * @brief splitPacket reconstitute incoming data and find messages.
127 * @param packet Raw data coming from the sensor.
128 * @param length Length of the data.
129 * @param time Time of the last received data.
130 *
131 * Analyse the ethernet packet received from the Sick sensor and try to find a
132 * complete message (scan data message or object message)
133 * If a message has been found it is added at the end of the message list
134 * else the pending bytes are stored to be analyzed by further incoming data.
135 */
136 void splitPacket(const char * packet, const int length, road_time_t time);
137
138
139 /**
140 * @brief Process/decode a message.
141 * @param msg The message is encapsulated into a MessageLDMRS
142 * @return Type of the message
143 *
144 * Process the raw data of the message and update the MessageLDMRS object passed : it fills the 2 headers (message and scan)
145 * and replace the body field of the MessageLDRMS object by an array of ScanPoint.
146 * - @b Warning : the process of object data type is not implemented yet !
147 */
148 unsigned long processMessage(MessageLDMRS & msg);
149
150 //! Find the position of the magic word into the array and returns this index.
151 /*!
152 * \param message Array of characters, raw data received from sensor.
153 * \param length Length of the array.
154 * \return
155 * - @b -1 if no magic word is found
156 * - @b position of the magic word otherwise
157 */
158 u_int32_t findMagicWord(const char * message, const unsigned length);
159
160 /**
161 * @brief getMessageSize get the message size of the entire message.
162 * @param message Raw data of the message.
163 * @param length Length of the raw data received.
164 * @param magicWordIndex First element of the message, used to get the size of the message.
165 * @return The @b size of the whole message.
166 *
167 * The size of the message is found inside the message thanks to an offset after the index of the Magic Word.
168 * - The first header of the message that contains the size of the message is in \b Big \b Endian format !
169 *
170 */
171 u_int32_t getMessageSize(const char * message, const unsigned length, const long magicWordIndex);
172
173 /**
174 * @brief isMessageComplete compare the size of the message read into the message and the length of the received data.
175 * @param length Length of the received data.
176 * @param size Size of the message read. See getMessageSize.
177 * @return @b true if the message is complete, @b false otherwise
178 */
179 bool isMessageComplete(const unsigned length, const long size);
180
181public Q_SLOTS:
182 /**
183 * @brief customEvent allows to receive the incoming data and store them into known structures.
184 * @param e Event that carries the Ethernet packets and receiving time.
185 */
186 void customEvent(QEvent * e);
187
188 /**
189 * @brief Configure the object, not used for the moment.
190 */
191 void configure();
192
193public:
194 /**
195 * @brief S_socket, used to receive and send data to the remote sensor.
196 */
197 SickSocket * S_socket;
198
199private:
200 /// Name is used to recognize between several sensors and store data into .dbt and utc files.
201 QString name_;
202
203 /// The IP address of the remote Sick LDMRS sensor we are connected to.
204 QString ipaddr_;
205
206 /// The SickLDMRS port
207 int port_;
208
209 /// Enable storing in DBT + UTC files
210 bool recording_;
211
212 /// List of messages to process.
213 std::list<MessageLDMRS> msgList;
214
215 /// Received raw data is appended into this MessagePacket.
216 MessagePacket pendingBytes;
217
218 /// Append new data into the private MessagePacket @b pendingBytes.
219 void storePendingBytes(road_time_t time);
220
221 /**
222 * @brief fillDataHeader fills the message header of the message
223 * @param msg
224 * - @b Warning:
225 * - the body field of the message have to be completed before
226 * - Data header format is @b Big @b Endian
227 */
228 void fillDataHeader(MessageLDMRS& msg);
229
230
231 /**
232 * @brief fillScanHeader fill the scan header of the message
233 * @param msg Raw data must be stored into the body field of the message
234 * - @b Warning
235 * - the body field of the message have to be completed before
236 * - Scan header format is @b Little @b Endian
237 */
238 void fillScanHeader(MessageLDMRS& msg);
239
240 /**
241 * @brief Write data into .dbt + .utc files.
242 * @param msg
243 *
244 * SickLDMRS_dbt structure is filled and stored into the DBT file.
245 * The array of ScanPoint is then stored into the UTC file.
246 */
247 void writeData(MessageLDMRS &msg);
248
249
250 std::string kSickDbtFileName; //!< Name of the DBT file.
251 std::string kSickUtcFileName; //!< Name of the UTC file.
252
253 pacpus::DbiteFile dbtFile_; //!< DBT file.
254 std::ofstream dataFile_; //!< UTC file.
255
256
257#ifdef SICKLDMRS_SH_MEM
258 ShMem * shmem_;
259 SickLDMRS_shMem sickData;
260#endif
261
262};
263
264} // namespace pacpus
265
266#endif // SICKLDMRSSENSOR_H
Note: See TracBrowser for help on using the repository browser.