MRPT  2.0.4
CObservationGPS.h
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 #pragma once
10 
11 #include <mrpt/obs/CObservation.h>
12 #include <mrpt/obs/gnss_messages.h>
13 #include <mrpt/poses/CPose2D.h>
14 #include <mrpt/poses/CPose3D.h>
16 #include <map>
17 #include <typeinfo>
18 
19 namespace mrpt::obs
20 {
21 /** This class <b>stores messages</b> from GNSS or GNSS+IMU devices, from
22  * consumer-grade inexpensive GPS receivers to Novatel/Topcon/... advanced RTK
23  * solutions.
24  *
25  * See mrpt::hwdrivers::CGPSInterface for a class capable of reading from a
26  * serial port or any input stream and \b parsing the ASCII/binary stream into
27  * indivual messages \b stored in mrpt::obs::CObservationGPS objects.
28  *
29  * Supported message types are:
30  * - NMEA 0183 (ASCII): GGA, RMC
31  * - Topcon GRIL (Binary): PZS, SATS
32  * - Novatel GNSS/SPAN OEM6 (Binary): See list of log packets under namespace
33  * mrpt::obs::gnss and in enum type mrpt::obs::gnss::gnss_message_type_t
34  *
35  * Note that this object has \b two timestamp fields:
36  * - The standard CObservation::timestamp field in the base class, which should
37  * contain the accurate satellite-based UTC timestamp, and
38  * - the field CObservationGPS::originalReceivedTimestamp, with the local
39  * computer-based timestamp based on the reception of the message in the
40  * computer.
41  *
42  * Normally, users read and write messages by means of these methods:
43  * - CObservationGPS::getMsgByClass()
44  * - CObservationGPS::setMsg()
45  * - CObservationGPS::hasMsgType()
46  * - CObservationGPS::hasMsgClass()
47  *
48  * Example access to GPS datum:
49  * \code
50  * mrpt::obs::CObservationGPS obs;
51  * //...
52  * if (obs.hasMsgClass<mrpt::obs::gnss::Message_NMEA_GGA>()) {
53  * const mrpt::obs::gnss::Message_NMEA_GGA &gga =
54  * o.getMsgByClass<mrpt::obs::gnss::Message_NMEA_GGA>();
55  * //gga.fields.XXX ...
56  * }
57  * \endcode
58  *
59  * \note <b>[API changed in MRPT 1.4.0]</b> mrpt::obs::CObservationGPS now
60  * stores message objects in a more flexible way. API clean-up and extended so
61  * the number of GNSS message types is larger and more scalable.
62  * \note Porting old code: For example, replace `observation.GGA_datum.XXX` with
63  * `observation.getMsgByClass<gnss::Message_NMEA_GGA>().fields.XXX`, etc.
64  * \sa CObservation
65  * \ingroup mrpt_obs_grp
66  */
68 {
70 
71  public:
72  using message_list_t =
73  std::map<gnss::gnss_message_type_t, gnss::gnss_message_ptr>;
74 
75  /** @name GNSS (GPS) data fields
76  * @{ */
77  /** The sensor pose on the robot/vehicle */
79  /** The local computer-based timestamp based on the reception of the message
80  * in the computer. \sa CObservation::timestamp in the base class, which
81  * should contain the accurate satellite-based UTC timestamp. */
83  /** If true, CObservation::timestamp has been generated from accurate
84  * satellite clock. Otherwise, no GPS data is available and timestamps are
85  * based on the local computer clock. */
87  /** The main piece of data in this class: a list of GNNS messages.
88  * Normally users might prefer to access the list via the methods
89  * CObservationGPS::getMsgByClass() and CObservationGPS::setMsg()
90  * Typically only one message, may be multiple if all have the same
91  * timestamp. */
93  /** @} */
94 
95  /** @name Main API to access to the data fields
96  * @{ */
97  /** Stores a message in the list \a messages, making a copy of the passed
98  * object.
99  * Valid message classes are those derived from
100  * mrpt::obs::gnss::gnss_message. If another message of the same type
101  * exists, it is overwritten. */
102  template <class MSG_CLASS>
103  void setMsg(const MSG_CLASS& msg)
104  {
105  messages[static_cast<gnss::gnss_message_type_t>(MSG_CLASS::msg_type)]
106  .set(new MSG_CLASS(msg));
107  }
108  /** Returns true if the list \a CObservationGPS::messages contains one of
109  * the requested type. \sa mrpt::obs::gnss::gnss_message_type_t,
110  * CObservationGPS::getMsgByType() */
111  bool hasMsgType(const gnss::gnss_message_type_t type_id) const;
112  /** Like \a hasMsgType() but allows querying for message classes, from any
113  * of those derived from mrpt::obs::gnss::gnss_message \sa
114  * CObservationGPS::hasMsgType(), */
115  template <class MSG_CLASS>
116  bool hasMsgClass() const
117  {
118  return hasMsgType(
119  static_cast<gnss::gnss_message_type_t>(MSG_CLASS::msg_type));
120  }
121  /** Returns a pointer to the message in the list CObservationGPS::messages
122  * of the requested type. Users normally would prefer using
123  * CObservationGPS::getMsgByClass()
124  * to avoid having to perform a dynamic_cast<>() on the returned pointer.
125  * \exception std::runtime_error If there is no such a message in the list.
126  * Please, check existence before calling this method with
127  * CObservationGPS::hasMsgType()
128  * \sa mrpt::obs::gnss::gnss_message_type_t,
129  * CObservationGPS::getMsgByClass(), CObservationGPS::hasMsgType() */
131  const gnss::gnss_message_type_t type_id);
132  /** \overload */
134  const gnss::gnss_message_type_t type_id) const;
135 
136  /** Returns a reference to the message in the list CObservationGPS::messages
137  * of the requested class.
138  * \exception std::runtime_error If there is no such a message in the list.
139  * Please, check existence before calling this method with
140  * CObservationGPS::hasMsgClass()
141  * \sa mrpt::obs::gnss::gnss_message_type_t,
142  * CObservationGPS::getMsgByType(), CObservationGPS::hasMsgType() */
143  template <class MSG_CLASS>
144  MSG_CLASS& getMsgByClass()
145  {
146  auto it = messages.find(
147  static_cast<gnss::gnss_message_type_t>(MSG_CLASS::msg_type));
148  ASSERTMSG_(
149  it != messages.end(), mrpt::format(
150  "[CObservationGPS::getMsgByClass] Cannot "
151  "find any observation of type `%s`",
152  typeid(MSG_CLASS).name()));
153  ASSERT_(it->second.get());
154  return *dynamic_cast<MSG_CLASS*>(it->second.get());
155  }
156  /** \overload */
157  template <class MSG_CLASS>
158  const MSG_CLASS& getMsgByClass() const
159  {
160  auto it = messages.find(
161  static_cast<gnss::gnss_message_type_t>(MSG_CLASS::msg_type));
162  ASSERTMSG_(
163  it != messages.end(), mrpt::format(
164  "[CObservationGPS::getMsgByClass] Cannot "
165  "find any observation of type `%s`",
166  typeid(MSG_CLASS).name()));
167  ASSERT_(it->second.get());
168  return *dynamic_cast<const MSG_CLASS*>(it->second.get());
169  }
170 
171  /** Like CObservationGPS::getMsgByClass() but returns a nullptr pointer if
172  * message is not found, instead of launching an exception */
173  template <class MSG_CLASS>
174  MSG_CLASS* getMsgByClassPtr()
175  {
176  auto it = messages.find(
177  static_cast<gnss::gnss_message_type_t>(MSG_CLASS::msg_type));
178  return it == messages.end()
179  ? static_cast<MSG_CLASS*>(nullptr)
180  : dynamic_cast<MSG_CLASS*>(it->second.get());
181  }
182  /** \overload */
183  template <class MSG_CLASS>
184  const MSG_CLASS* getMsgByClassPtr() const
185  {
186  auto it = messages.find(
187  static_cast<gnss::gnss_message_type_t>(MSG_CLASS::msg_type));
188  return it == messages.end()
189  ? dynamic_cast<MSG_CLASS*>(nullptr)
190  : dynamic_cast<MSG_CLASS*>(it->second.get());
191  }
192 
193  /** Dumps the contents of the observation in a human-readable form to a
194  * given output stream \sa dumpToConsole(), getDescriptionAsText() */
195  void dumpToStream(std::ostream& out) const;
196  /** Dumps the contents of the observation in a human-readable form to an
197  * std::ostream (use std::cout to print to console) */
198  void dumpToConsole(std::ostream& o) const;
199  /** Empties this observation, clearing the container \a messages */
200  void clear();
201  void swap(CObservationGPS& o);
202 
203  void getSensorPose(mrpt::poses::CPose3D& out_sensorPose) const override
204  {
205  out_sensorPose = sensorPose;
206  } // See base class docs
207  void setSensorPose(const mrpt::poses::CPose3D& newSensorPose) override
208  {
209  sensorPose = newSensorPose;
210  } // See base class docs
212  std::ostream& o) const override; // See base class docs
213 
215  const override; // See base class docs
216  /** @} */
217 
218  /** true if the corresponding field exists in \a messages. */
219  bool has_GGA_datum() const
220  {
221  return messages.find(gnss::NMEA_GGA) != messages.end();
222  }
223  /** true if the corresponding field exists in \a messages. */
224  bool has_RMC_datum() const
225  {
226  return messages.find(gnss::NMEA_RMC) != messages.end();
227  }
228 
229  /** @name Utilities
230  * @{ */
231  static bool GPS_time_to_UTC(
232  uint16_t gps_week, double gps_sec,
233  const int leap_seconds_count /**< [in] GPS to UTC time number of leap
234  seconds (normally grabbed from
235  satellital live data) */
236  ,
237  /** Return false on invalid input data */
238  mrpt::system::TTimeStamp& utc_out /**< [out] UTC timestamp */);
239  /** \overload */
240  static bool GPS_time_to_UTC(
241  uint16_t gps_week, double gps_sec, const int leap_seconds_count,
242  mrpt::system::TTimeParts& utc_out);
243  /** @} */
244 }; // End of class def.
245 
246 } // namespace mrpt::obs
bool has_GGA_datum() const
true if the corresponding field exists in messages.
MSG_CLASS * getMsgByClassPtr()
Like CObservationGPS::getMsgByClass() but returns a nullptr pointer if message is not found...
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
void dumpToStream(std::ostream &out) const
Dumps the contents of the observation in a human-readable form to a given output stream.
mrpt::system::TTimeStamp originalReceivedTimestamp
The local computer-based timestamp based on the reception of the message in the computer.
void swap(CObservationGPS &o)
gnss_message_type_t
List of all known GNSS message types.
message_list_t messages
The main piece of data in this class: a list of GNNS messages.
void setSensorPose(const mrpt::poses::CPose3D &newSensorPose) override
A general method to change the sensor pose on the robot.
mrpt::obs::gnss::gnss_message * getMsgByType(const gnss::gnss_message_type_t type_id)
Returns a pointer to the message in the list CObservationGPS::messages of the requested type...
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
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
bool has_RMC_datum() const
true if the corresponding field exists in messages.
const MSG_CLASS * getMsgByClassPtr() const
void getDescriptionAsText(std::ostream &o) const override
Build a detailed, multi-line textual description of the observation contents and dump it to the outpu...
The parts of a date/time (it&#39;s like the standard &#39;tm&#39; but with fractions of seconds).
Definition: datetime.h:49
void getSensorPose(mrpt::poses::CPose3D &out_sensorPose) const override
A general method to retrieve the sensor pose on the robot.
This namespace contains representation of robot actions and observations.
bool has_satellite_timestamp
If true, CObservation::timestamp has been generated from accurate satellite clock.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
std::map< gnss::gnss_message_type_t, gnss::gnss_message_ptr > message_list_t
MSG_CLASS & getMsgByClass()
Returns a reference to the message in the list CObservationGPS::messages of the requested class...
void clear()
Empties this observation, clearing the container messages.
void setMsg(const MSG_CLASS &msg)
Stores a message in the list messages, making a copy of the passed object.
mrpt::system::TTimeStamp getOriginalReceivedTimeStamp() const override
By default, returns CObservation::timestamp but in sensors capable of satellite (or otherwise) accura...
mrpt::poses::CPose3D sensorPose
The sensor pose on the robot/vehicle.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
Declares a class that represents any robot&#39;s observation.
Definition: CObservation.h:43
Pure virtual base for all message types.
static bool GPS_time_to_UTC(uint16_t gps_week, double gps_sec, const int leap_seconds_count, mrpt::system::TTimeStamp &utc_out)
void dumpToConsole(std::ostream &o) const
Dumps the contents of the observation in a human-readable form to an std::ostream (use std::cout to p...
bool hasMsgClass() const
Like hasMsgType() but allows querying for message classes, from any of those derived from mrpt::obs::...
#define DEFINE_SERIALIZABLE(class_name, NS)
This declaration must be inserted in all CSerializable classes definition, within the class declarati...
bool hasMsgType(const gnss::gnss_message_type_t type_id) const
Returns true if the list CObservationGPS::messages contains one of the requested type.
This class stores messages from GNSS or GNSS+IMU devices, from consumer-grade inexpensive GPS receive...
const MSG_CLASS & getMsgByClass() const
#define INVALID_TIMESTAMP
Represents an invalid timestamp, where applicable.
Definition: datetime.h:43



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020