MRPT  2.0.4
gnss_messages_common.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "obs-precomp.h" // Precompiled headers
11 
12 #include <mrpt/io/CMemoryStream.h>
13 #include <mrpt/obs/gnss_messages.h> // Must include all message classes so we can implemente the class factory here
15 #include <iostream>
16 #include <map>
17 
18 using namespace std;
19 using namespace mrpt::obs::gnss;
20 
21 #define LIST_ALL_MSGS \
22  /* ====== NMEA ====== */ \
23  DOFOR(NMEA_GGA) \
24  DOFOR(NMEA_GSA) \
25  DOFOR(NMEA_RMC) \
26  DOFOR(NMEA_ZDA) \
27  DOFOR(NMEA_VTG) \
28  DOFOR(NMEA_GLL) \
29  /* ====== TopCon mmGPS ====== */ \
30  DOFOR(TOPCON_PZS) \
31  DOFOR(TOPCON_SATS) \
32  /* ====== Novatel OEM6 ====== */ \
33  DOFOR(NV_OEM6_GENERIC_FRAME) \
34  DOFOR(NV_OEM6_BESTPOS) \
35  /* ====== Novatel SPAN+OEM6 ====== */ \
36  DOFOR(NV_OEM6_GENERIC_SHORT_FRAME) \
37  DOFOR(NV_OEM6_INSPVAS) \
38  DOFOR(NV_OEM6_RANGECMP) \
39  DOFOR(NV_OEM6_RXSTATUS) \
40  DOFOR(NV_OEM6_RAWEPHEM) \
41  DOFOR(NV_OEM6_VERSION) \
42  DOFOR(NV_OEM6_RAWIMUS) \
43  DOFOR(NV_OEM6_MARKPOS) \
44  DOFOR(NV_OEM6_MARKTIME) \
45  DOFOR(NV_OEM6_MARK2TIME) \
46  DOFOR(NV_OEM6_IONUTC)
47 
48 // Class factory:
49 gnss_message* gnss_message::Factory(const gnss_message_type_t msg_id)
50 {
51 #define DOFOR(_MSG_ID) \
52  case _MSG_ID: \
53  return new Message_##_MSG_ID();
54  switch (msg_id)
55  {
57  default:
58  return nullptr;
59  };
60 #undef DOFOR
61 }
62 bool gnss_message::FactoryKnowsMsgType(const gnss_message_type_t msg_id)
63 {
64 #define DOFOR(_MSG_ID) \
65  case _MSG_ID: \
66  return true;
67  switch (msg_id)
68  {
70  default:
71  return false;
72  };
73 #undef DOFOR
74 }
75 
76 const std::string& gnss_message::getMessageTypeAsString() const
77 {
78  static bool first_call = true;
79  static std::map<gnss_message_type_t, std::string> gnss_type2str;
80  if (first_call)
81  {
82  first_call = false;
83 #define DOFOR(_MSG_ID) gnss_type2str[_MSG_ID] = #_MSG_ID;
85 #undef DOFOR
86  }
87 
88  return gnss_type2str[this->message_type];
89 }
90 
91 // Save to binary stream. Launches an exception upon error
92 void gnss_message::writeToStream(mrpt::serialization::CArchive& out) const
93 {
94  out.WriteAs<int32_t>(message_type);
95  this->internal_writeToStream(out);
96 }
97 
98 // Load from binary stream. Launches an exception upon error
99 void gnss_message::readFromStream(mrpt::serialization::CArchive& in)
100 {
101  int32_t msg_id;
102  in >> msg_id;
103  ASSERT_EQUAL_((int32_t)msg_id, this->message_type);
104  this->internal_readFromStream(in);
105 }
106 
107 void gnss_message::dumpToConsole(std::ostream& o) const
108 {
110  o << "\n";
112  o << "\n";
113 }
114 
115 // Load from binary stream and creates object detecting its type (class
116 // factory). Launches an exception upon error
117 gnss_message* gnss_message::readAndBuildFromStream(
119 {
120  int32_t msg_id;
121  in >> msg_id;
122  gnss_message* msg =
123  gnss_message::Factory(static_cast<gnss_message_type_t>(msg_id));
124  if (!msg)
126  "Error deserializing gnss_message: unknown message type '%i'",
127  static_cast<int>(msg_id));
128  msg->internal_readFromStream(in);
129  // internal_readFromStream() already calls fixEndianness().
130  return msg;
131 }
132 
133 // Ctor (default: nullptr pointer)
134 gnss_message_ptr::gnss_message_ptr() = default;
135 // Ctor:Makes a copy of the pointee
136 gnss_message_ptr::gnss_message_ptr(const gnss_message_ptr& o)
137 {
138  if (!o.ptr)
139  {
140  ptr = nullptr;
141  }
142  else
143  {
145  auto arch = mrpt::serialization::archiveFrom(buf);
146  o->writeToStream(arch);
147  buf.Seek(0);
148  ptr = gnss_message::readAndBuildFromStream(arch);
149  }
150 }
151 /** Assigns a pointer */
152 gnss_message_ptr::gnss_message_ptr(const gnss_message* p)
153  : ptr(const_cast<gnss_message*>(p))
154 {
155 }
157 {
158  if (ptr)
159  {
160  delete ptr;
161  ptr = nullptr;
162  }
163  ptr = p;
164 }
165 // Makes a copy of the pointee
167 {
169  auto arch = mrpt::serialization::archiveFrom(buf);
170  o->writeToStream(arch);
171  buf.Seek(0);
173  return *this;
174 }
176 {
177  if (ptr)
178  {
179  delete ptr;
180  ptr = nullptr;
181  }
182 }
183 
184 // ---------------------------------------
185 UTC_time::UTC_time() = default;
187 {
188  out << hour << minute << sec;
189 }
191 {
192  in >> hour >> minute >> sec;
193 }
194 
195 // Build an MRPT timestamp with the hour/minute/sec of this structure and the
196 // date from the given timestamp.
198  const mrpt::system::TTimeStamp& date) const
199 {
200  using namespace mrpt::system;
201 
202  TTimeParts parts;
203  timestampToParts(date, parts, false /* UTC, not local */);
204 
205  parts.hour = this->hour;
206  parts.minute = this->minute;
207  parts.second = this->sec;
208 
209  return buildTimestampFromParts(parts);
210 }
mrpt::io::CMemoryStream::Seek
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource.
Definition: CMemoryStream.cpp:126
LIST_ALL_MSGS
#define LIST_ALL_MSGS
Definition: gnss_messages_common.cpp:21
mrpt::obs::gnss::gnss_message_ptr::~gnss_message_ptr
virtual ~gnss_message_ptr()
Dtor: it frees the pointee memory.
Definition: gnss_messages_common.cpp:175
mrpt::obs::gnss::gnss_message_type_t
gnss_message_type_t
List of all known GNSS message types.
Definition: gnss_messages_type_list.h:22
mrpt::obs::gnss::gnss_message_ptr::ptr
gnss_message * ptr
Definition: gnss_messages_common.h:98
ASSERT_EQUAL_
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:137
mrpt::obs::gnss::UTC_time::readFromStream
void readFromStream(mrpt::serialization::CArchive &in)
Save to binary stream.
Definition: gnss_messages_common.cpp:190
mrpt::system::TTimeParts::hour
uint8_t hour
Day (1-31)
Definition: datetime.h:54
mrpt::system::buildTimestampFromParts
mrpt::system::TTimeStamp buildTimestampFromParts(const mrpt::system::TTimeParts &p)
Builds a timestamp from the parts (Parts are in UTC)
Definition: datetime.cpp:74
mrpt::system::TTimeParts::second
double second
Minute (0-59)
Definition: datetime.h:56
CMemoryStream.h
out
mrpt::vision::TStereoCalibResults out
Definition: chessboard_stereo_camera_calib_unittest.cpp:25
obs-precomp.h
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
mrpt::system::TTimeParts
The parts of a date/time (it's like the standard 'tm' but with fractions of seconds).
Definition: datetime.h:49
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
mrpt::obs::gnss::gnss_message_ptr
A smart pointer to a GNSS message.
Definition: gnss_messages_common.h:95
mrpt::system::timestampToParts
void timestampToParts(TTimeStamp t, TTimeParts &p, bool localTime=false)
Gets the individual parts of a date/time (days, hours, minutes, seconds) - UTC time or local time.
Definition: datetime.cpp:50
mrpt::obs::gnss::UTC_time::getAsTimestamp
mrpt::system::TTimeStamp getAsTimestamp(const mrpt::system::TTimeStamp &date) const
Build an MRPT timestamp with the hour/minute/sec of this structure and the date from the given timest...
Definition: gnss_messages_common.cpp:197
mrpt::obs::gnss::UTC_time::hour
uint8_t hour
Definition: gnss_messages_common.h:191
mrpt::obs::gnss::UTC_time::minute
uint8_t minute
Definition: gnss_messages_common.h:192
gnss_messages.h
mrpt::system::TTimeStamp
mrpt::Clock::time_point TTimeStamp
A system independent time type, it holds the the number of 100-nanosecond intervals since January 1,...
Definition: datetime.h:40
mrpt::io::CMemoryStream
This CStream derived class allow using a memory buffer as a CStream.
Definition: io/CMemoryStream.h:26
mrpt::serialization::archiveFrom
CArchiveStreamBase< STREAM > archiveFrom(STREAM &s)
Helper function to create a templatized wrapper CArchive object for a: MRPT's CStream,...
Definition: CArchive.h:592
mrpt::obs::gnss::getAllFieldValues
bool getAllFieldValues(std::ostream &o) const override
mrpt::obs::gnss::gnss_message::readAndBuildFromStream
static gnss_message * readAndBuildFromStream(mrpt::serialization::CArchive &in)
Load from binary stream and creates object detecting its type (class factory).
Definition: gnss_messages_common.cpp:117
mrpt::obs::gnss::gnss_message::writeToStream
void writeToStream(mrpt::serialization::CArchive &out) const
Save to binary stream.
Definition: gnss_messages_common.cpp:92
mrpt::obs::gnss::gnss_message_ptr::set
void set(gnss_message *p)
Replaces the pointee with a new pointer.
Definition: gnss_messages_common.cpp:156
mrpt::obs::gnss::getAllFieldDescriptions
bool getAllFieldDescriptions(std::ostream &o) const override
mrpt::obs::gnss::gnss_message::internal_readFromStream
virtual void internal_readFromStream(mrpt::serialization::CArchive &in)=0
Save to binary stream.
mrpt::obs::gnss
GNSS (GPS) data structures, mainly for use within mrpt::obs::CObservationGPS.
Definition: gnss_messages_ascii_nmea.h:13
mrpt::system::TTimeParts::minute
uint8_t minute
Hour (0-23)
Definition: datetime.h:55
mrpt::obs::gnss::gnss_message
Pure virtual base for all message types.
Definition: gnss_messages_common.h:26
mrpt::obs::gnss::gnss_message_ptr::operator=
gnss_message_ptr & operator=(const gnss_message_ptr &o)
Definition: gnss_messages_common.cpp:166
CArchive.h
mrpt::obs::gnss::UTC_time::UTC_time
UTC_time()
mrpt::obs::gnss::UTC_time::writeToStream
void writeToStream(mrpt::serialization::CArchive &out) const
Save to binary stream.
Definition: gnss_messages_common.cpp:186
mrpt::system
Definition: backtrace.h:14
mrpt::obs::gnss::UTC_time::sec
double sec
Definition: gnss_messages_common.h:193



Page generated by Doxygen 1.8.17 for MRPT 2.0.4 at Sun Jul 19 15:15:43 UTC 2020