gps.h
Go to the documentation of this file.
1 //==============================================================================
2 // Copyright (c) 2012, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND
19 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
22 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //==============================================================================
29 
30 #ifndef UBLOX_GPS_H
31 #define UBLOX_GPS_H
32 // STL
33 #include <map>
34 #include <vector>
35 #include <locale>
36 #include <stdexcept>
37 // Boost
38 #include <boost/asio/ip/tcp.hpp>
39 #include <boost/asio/ip/udp.hpp>
40 #include <boost/asio/serial_port.hpp>
41 #include <boost/asio/io_service.hpp>
42 #include <boost/atomic.hpp>
43 // ROS
44 #include <ros/console.h>
45 // Other u-blox packages
46 #include <ublox/serialization/ublox_msgs.h>
47 // u-blox gps
48 #include <ublox_gps/async_worker.h>
49 #include <ublox_gps/callback.h>
50 
56 namespace ublox_gps {
58 constexpr static unsigned int kBaudrates[] = { 4800,
59  9600,
60  19200,
61  38400,
62  57600,
63  115200,
64  230400,
65  460800 };
69 class Gps {
70  public:
72  constexpr static double kDefaultAckTimeout = 1.0;
74  constexpr static int kWriterSize = 2056;
75 
76  Gps();
77  virtual ~Gps();
78 
83  void setSaveOnShutdown(bool save_on_shutdown) {
84  save_on_shutdown_ = save_on_shutdown;
85  }
86 
91  void setConfigOnStartup(const bool config_on_startup) { config_on_startup_flag_ = config_on_startup; }
92 
98  void initializeTcp(std::string host, std::string port);
99 
105  void initializeUdp(std::string host, std::string port);
106 
114  void initializeSerial(std::string port, unsigned int baudrate,
115  uint16_t uart_in, uint16_t uart_out);
116 
124  void resetSerial(std::string port);
125 
129  bool sendRtcm(const std::vector<uint8_t> &message);
130 
135  void close();
136 
141  void reset(const boost::posix_time::time_duration& wait);
142 
149  bool configReset(uint16_t nav_bbr_mask, uint16_t reset_mode);
150 
158  bool configGnss(ublox_msgs::CfgGNSS gnss,
159  const boost::posix_time::time_duration& wait);
160 
166  bool clearBbr();
167 
175  bool configUart1(unsigned int baudrate, uint16_t in_proto_mask,
176  uint16_t out_proto_mask);
177 
185  bool disableUart1(ublox_msgs::CfgPRT& prev_cfg);
186 
194  bool configUsb(uint16_t tx_ready, uint16_t in_proto_mask,
195  uint16_t out_proto_mask);
196 
204  bool configRate(uint16_t meas_rate, uint16_t nav_rate);
205 
212  bool configRtcm(std::vector<uint8_t> ids, std::vector<uint8_t> rates);
213 
222  bool configSbas(bool enable, uint8_t usage, uint8_t max_sbas);
223 
238  bool configTmode3Fixed(bool lla_flag,
239  std::vector<float> arp_position,
240  std::vector<int8_t> arp_position_hp,
241  float fixed_pos_acc);
242 
249  bool configTmode3SurveyIn(unsigned int svin_min_dur, float svin_acc_limit);
250 
256  bool disableTmode3();
257 
265  bool setRate(uint8_t class_id, uint8_t message_id, uint8_t rate);
266 
272  bool setDynamicModel(uint8_t model);
273 
279  bool setFixMode(uint8_t mode);
280 
286  bool setDeadReckonLimit(uint8_t limit);
287 
296  bool setPpp(bool enable, float protocol_version);
297 
303  bool setDgnss(uint8_t mode);
304 
310  bool setUseAdr(bool enable, float protocol_version);
311 
319  bool setUTCtime();
320 
329  bool setTimtm2(uint8_t rate);
330 
337  template <typename T>
338  void subscribe(typename CallbackHandler_<T>::Callback callback,
339  unsigned int rate);
344  template <typename T>
345  void subscribe(typename CallbackHandler_<T>::Callback callback);
346 
351  void subscribe_nmea(boost::function<void(const std::string&)> callback) { callbacks_.set_nmea_callback(callback); }
352 
360  template <typename T>
361  void subscribeId(typename CallbackHandler_<T>::Callback callback,
362  unsigned int message_id);
363 
369  template <typename T>
370  bool read(T& message,
371  const boost::posix_time::time_duration& timeout = default_timeout_);
372 
373  bool isInitialized() const { return worker_ != 0; }
374  bool isConfigured() const { return isInitialized() && configured_; }
375  bool isOpen() const { return worker_->isOpen(); }
376 
384  template <typename ConfigT>
385  bool poll(ConfigT& message,
386  const std::vector<uint8_t>& payload = std::vector<uint8_t>(),
387  const boost::posix_time::time_duration& timeout = default_timeout_);
396  bool poll(uint8_t class_id, uint8_t message_id,
397  const std::vector<uint8_t>& payload = std::vector<uint8_t>());
398 
406  template <typename ConfigT>
407  bool configure(const ConfigT& message, bool wait = true);
408 
416  bool waitForAcknowledge(const boost::posix_time::time_duration& timeout,
417  uint8_t class_id, uint8_t msg_id);
418 
423  void setRawDataCallback(const Worker::Callback& callback);
424 
425  private:
427  enum AckType {
429  ACK,
431  };
432 
434  struct Ack {
436  uint8_t class_id;
437  uint8_t msg_id;
438  };
439 
444  void setWorker(const boost::shared_ptr<Worker>& worker);
445 
449  void subscribeAcks();
450 
455  void processAck(const ublox_msgs::Ack &m);
456 
461  void processNack(const ublox_msgs::Ack &m);
462 
467  void processUpdSosAck(const ublox_msgs::UpdSOS_Ack &m);
468 
479  bool saveOnShutdown();
480 
489 
490 
492  static const boost::posix_time::time_duration default_timeout_;
494  mutable boost::atomic<Ack> ack_;
495 
498 
499  std::string host_, port_;
500 };
501 
502 template <typename T>
504  typename CallbackHandler_<T>::Callback callback, unsigned int rate) {
505  if (!setRate(T::CLASS_ID, T::MESSAGE_ID, rate)) return;
506  subscribe<T>(callback);
507 }
508 
509 template <typename T>
511  callbacks_.insert<T>(callback);
512 }
513 
514 template <typename T>
516  unsigned int message_id) {
517  callbacks_.insert<T>(callback, message_id);
518 }
519 
520 template <typename ConfigT>
521 bool Gps::poll(ConfigT& message,
522  const std::vector<uint8_t>& payload,
523  const boost::posix_time::time_duration& timeout) {
524  if (!poll(ConfigT::CLASS_ID, ConfigT::MESSAGE_ID, payload)) return false;
525  return read(message, timeout);
526 }
527 
528 template <typename T>
529 bool Gps::read(T& message, const boost::posix_time::time_duration& timeout) {
530  if (!worker_) return false;
531  return callbacks_.read(message, timeout);
532 }
533 
534 template <typename ConfigT>
535 bool Gps::configure(const ConfigT& message, bool wait) {
536  if (!worker_) return false;
537 
538  // Reset ack
539  Ack ack;
540  ack.type = WAIT;
541  ack_.store(ack, boost::memory_order_seq_cst);
542 
543  // Encode the message
544  std::vector<unsigned char> out(kWriterSize);
545  ublox::Writer writer(out.data(), out.size());
546  if (!writer.write(message)) {
547  ROS_ERROR("Failed to encode config message 0x%02x / 0x%02x",
548  message.CLASS_ID, message.MESSAGE_ID);
549  return false;
550  }
551  // Send the message to the device
552  worker_->send(out.data(), writer.end() - out.data());
553 
554  if (!wait) return true;
555 
556  // Wait for an acknowledgment and return whether or not it was received
558  message.CLASS_ID,
559  message.MESSAGE_ID);
560 }
561 
562 } // namespace ublox_gps
563 
564 #endif // UBLOX_GPS_H
ublox_gps::Gps::save_on_shutdown_
bool save_on_shutdown_
Whether or not to save Flash BBR on shutdown.
Definition: gps.h:486
ublox_gps::Gps::isInitialized
bool isInitialized() const
Definition: gps.h:373
ublox_gps::Gps::reset
void reset(const boost::posix_time::time_duration &wait)
Reset I/O communications.
Definition: gps.cpp:296
ublox_gps::CallbackHandlers
Callback handlers for incoming u-blox messages.
Definition: callback.h:121
ublox_gps::Gps::kWriterSize
constexpr static int kWriterSize
Size of write buffer for output messages.
Definition: gps.h:74
ublox_gps::Gps::ACK
@ ACK
Not acknowledged.
Definition: gps.h:429
ublox_gps::Gps::NACK
@ NACK
Definition: gps.h:428
ublox_gps::Gps::host_
std::string host_
Definition: gps.h:499
ublox_gps::Gps::configRtcm
bool configRtcm(std::vector< uint8_t > ids, std::vector< uint8_t > rates)
Configure the RTCM messages with the given IDs to the set rate.
Definition: gps.cpp:428
boost::shared_ptr
async_worker.h
ublox_gps::Gps::configured_
bool configured_
Whether or not the I/O port has been configured.
Definition: gps.h:484
ublox_gps::Gps::setTimtm2
bool setTimtm2(uint8_t rate)
Enable or disable TIM-TM2 (time mark message).
Definition: gps.cpp:623
ublox_gps::Gps::processAck
void processAck(const ublox_msgs::Ack &m)
Callback handler for UBX-ACK message.
Definition: gps.cpp:70
ublox_gps::Gps::setSaveOnShutdown
void setSaveOnShutdown(bool save_on_shutdown)
If called, when the node shuts down, it will send a command to save the flash memory.
Definition: gps.h:83
ublox_gps::Gps::~Gps
virtual ~Gps()
Definition: gps.cpp:48
ublox_gps::Gps::configGnss
bool configGnss(ublox_msgs::CfgGNSS gnss, const boost::posix_time::time_duration &wait)
Configure the GNSS, cold reset the device, and reset the I/O.
Definition: gps.cpp:321
ublox_gps::Gps::ack_
boost::atomic< Ack > ack_
Stores last received ACK accessed by multiple threads.
Definition: gps.h:494
ublox_gps::Gps::isOpen
bool isOpen() const
Definition: gps.h:375
ublox_gps::Gps::setRate
bool setRate(uint8_t class_id, uint8_t message_id, uint8_t rate)
Set the rate at which the U-Blox device sends the given message.
Definition: gps.cpp:504
ublox_gps::Gps::clearBbr
bool clearBbr()
Send a message to the receiver to delete the BBR data stored in flash.
Definition: gps.cpp:350
ublox_gps::Gps::WAIT
@ WAIT
Acknowledge.
Definition: gps.h:430
ublox_gps::Gps::callbacks_
CallbackHandlers callbacks_
Callback handlers for u-blox messages.
Definition: gps.h:497
ublox_gps::Gps::configUsb
bool configUsb(uint16_t tx_ready, uint16_t in_proto_mask, uint16_t out_proto_mask)
Configure the USB Port.
Definition: gps.cpp:401
ublox_gps::Gps::setDynamicModel
bool setDynamicModel(uint8_t model)
Set the device dynamic model.
Definition: gps.cpp:514
wait
void wait(int seconds)
ublox_gps::Gps::setFixMode
bool setFixMode(uint8_t mode)
Set the device fix mode.
Definition: gps.cpp:523
callback.h
ublox_gps::Gps::Ack::msg_id
uint8_t msg_id
The message ID of the ACK.
Definition: gps.h:437
ublox_gps::Gps::configUart1
bool configUart1(unsigned int baudrate, uint16_t in_proto_mask, uint16_t out_proto_mask)
Configure the UART1 Port.
Definition: gps.cpp:358
ublox_gps::Gps::setUseAdr
bool setUseAdr(bool enable, float protocol_version)
Enable or disable ADR (automotive dead reckoning).
Definition: gps.cpp:559
ublox_gps::Gps::configTmode3Fixed
bool configTmode3Fixed(bool lla_flag, std::vector< float > arp_position, std::vector< int8_t > arp_position_hp, float fixed_pos_acc)
Set the TMODE3 settings to fixed.
Definition: gps.cpp:449
ublox_gps::Gps::config_on_startup_flag_
bool config_on_startup_flag_
Definition: gps.h:488
ublox_gps
Definition: async_worker.h:43
ublox_gps::Gps::disableUart1
bool disableUart1(ublox_msgs::CfgPRT &prev_cfg)
Disable the UART Port. Sets in/out protocol masks to 0. Does not modify other values.
Definition: gps.cpp:375
ublox_gps::CallbackHandler_::Callback
boost::function< void(const T &)> Callback
A callback function.
Definition: callback.h:70
ublox_gps::CallbackHandlers::read
bool read(T &message, const boost::posix_time::time_duration &timeout)
Read a u-blox message of the given type.
Definition: callback.h:206
console.h
ublox_gps::Gps::Gps
Gps()
Definition: gps.cpp:44
ublox_gps::Gps::initializeUdp
void initializeUdp(std::string host, std::string port)
Initialize UDP I/O.
Definition: gps.cpp:249
ublox_gps::CallbackHandlers::insert
void insert(typename CallbackHandler_< T >::Callback callback)
Definition: callback.h:129
ublox_gps::Gps::AckType
AckType
Types for ACK/NACK messages, WAIT is used when waiting for an ACK.
Definition: gps.h:427
ublox_gps::Gps::initializeTcp
void initializeTcp(std::string host, std::string port)
Initialize TCP I/O.
Definition: gps.cpp:213
ublox_gps::Gps::setDeadReckonLimit
bool setDeadReckonLimit(uint8_t limit)
Set the dead reckoning time limit.
Definition: gps.cpp:532
ublox_gps::Gps::subscribeAcks
void subscribeAcks()
Subscribe to ACK/NACK messages and UPD-SOS-ACK messages.
Definition: gps.cpp:58
ublox_gps::Gps::read
bool read(T &message, const boost::posix_time::time_duration &timeout=default_timeout_)
Definition: gps.h:529
ublox::Writer
ublox_gps::Gps::kDefaultAckTimeout
constexpr static double kDefaultAckTimeout
Default timeout for ACK messages in seconds.
Definition: gps.h:72
ublox_gps::Gps::port_
std::string port_
Definition: gps.h:499
ublox_gps::Gps::worker_
boost::shared_ptr< Worker > worker_
Processes I/O stream data.
Definition: gps.h:482
ublox_gps::Gps::sendRtcm
bool sendRtcm(const std::vector< uint8_t > &message)
Send rtcm correction message.
Definition: gps.cpp:571
ublox_node::meas_rate
uint16_t meas_rate
The measurement [ms], see CfgRate.msg.
Definition: node.h:121
ublox_gps::kBaudrates
constexpr static unsigned int kBaudrates[]
Possible baudrates for u-blox devices.
Definition: gps.h:58
ublox_gps::Gps::setUTCtime
bool setUTCtime()
Configure the U-Blox to UTC time.
Definition: gps.cpp:615
ublox_gps::Gps
Handles communication with and configuration of the u-blox device.
Definition: gps.h:69
ublox::Writer::write
bool write(const T &message, uint8_t class_id=T::CLASS_ID, uint8_t message_id=T::MESSAGE_ID)
ublox_gps::Gps::Ack::class_id
uint8_t class_id
The class ID of the ACK.
Definition: gps.h:436
ublox_gps::Gps::setPpp
bool setPpp(bool enable, float protocol_version)
Enable or disable PPP (precise-point-positioning).
Definition: gps.cpp:541
ublox_gps::Gps::subscribe
void subscribe(typename CallbackHandler_< T >::Callback callback, unsigned int rate)
Configure the U-Blox send rate of the message & subscribe to the given message.
Definition: gps.h:503
ublox_gps::Gps::disableTmode3
bool disableTmode3()
Set the TMODE3 settings to disabled. Should only be called for High Precision GNSS devices,...
Definition: gps.cpp:496
ublox_gps::Gps::initializeSerial
void initializeSerial(std::string port, unsigned int baudrate, uint16_t uart_in, uint16_t uart_out)
Initialize the Serial I/O port.
Definition: gps.cpp:108
ublox_gps::Gps::setWorker
void setWorker(const boost::shared_ptr< Worker > &worker)
Set the I/O worker.
Definition: gps.cpp:50
ublox_gps::Gps::isConfigured
bool isConfigured() const
Definition: gps.h:374
ublox_gps::Worker::Callback
boost::function< void(unsigned char *, std::size_t &)> Callback
Definition: worker.h:42
ublox_gps::Gps::subscribe_nmea
void subscribe_nmea(boost::function< void(const std::string &)> callback)
Subscribe to the given Ublox message.
Definition: gps.h:351
ublox::Writer::end
iterator end()
ublox_gps::Gps::Ack::type
AckType type
The ACK type.
Definition: gps.h:435
ublox_gps::Gps::configTmode3SurveyIn
bool configTmode3SurveyIn(unsigned int svin_min_dur, float svin_acc_limit)
Set the TMODE3 settings to survey-in.
Definition: gps.cpp:485
ublox_gps::Gps::setConfigOnStartup
void setConfigOnStartup(const bool config_on_startup)
Set the internal flag for enabling or disabling the initial configurations.
Definition: gps.h:91
ublox_gps::Gps::poll
bool poll(ConfigT &message, const std::vector< uint8_t > &payload=std::vector< uint8_t >(), const boost::posix_time::time_duration &timeout=default_timeout_)
Definition: gps.h:521
ublox_gps::Gps::resetSerial
void resetSerial(std::string port)
Reset the Serial I/O port after u-blox reset.
Definition: gps.cpp:172
ublox_gps::Gps::configSbas
bool configSbas(bool enable, uint8_t usage, uint8_t max_sbas)
Configure the SBAS settings.
Definition: gps.cpp:439
ublox_gps::CallbackHandlers::set_nmea_callback
void set_nmea_callback(boost::function< void(const std::string &)> callback)
Add a callback handler for nmea messages.
Definition: callback.h:160
ROS_ERROR
#define ROS_ERROR(...)
ublox_gps::Gps::default_timeout_
static const boost::posix_time::time_duration default_timeout_
The default timeout for ACK messages.
Definition: gps.h:492
ublox_gps::Gps::processUpdSosAck
void processUpdSosAck(const ublox_msgs::UpdSOS_Ack &m)
Callback handler for UBX-UPD-SOS-ACK message.
Definition: gps.cpp:93
ublox_node::nav_rate
uint16_t nav_rate
Navigation rate in measurement cycles, see CfgRate.msg.
Definition: node.h:123
ublox_gps::Gps::saveOnShutdown
bool saveOnShutdown()
Execute save on shutdown procedure.
Definition: gps.cpp:337
ublox_gps::Gps::setDgnss
bool setDgnss(uint8_t mode)
Set the DGNSS mode (see CfgDGNSS message for details).
Definition: gps.cpp:552
ublox_gps::Gps::Ack
Stores ACK/NACK messages.
Definition: gps.h:434
ublox_gps::Gps::setRawDataCallback
void setRawDataCallback(const Worker::Callback &callback)
Set the callback function which handles raw data.
Definition: gps.cpp:610
ublox_gps::Gps::subscribeId
void subscribeId(typename CallbackHandler_< T >::Callback callback, unsigned int message_id)
Subscribe to the message with the given ID. This is used for messages which have the same format but ...
Definition: gps.h:515
ublox_gps::Gps::configure
bool configure(const ConfigT &message, bool wait=true)
Send the given configuration message.
Definition: gps.h:535
ublox_gps::Gps::waitForAcknowledge
bool waitForAcknowledge(const boost::posix_time::time_duration &timeout, uint8_t class_id, uint8_t msg_id)
Wait for an acknowledge message until the timeout.
Definition: gps.cpp:589
ublox_gps::Gps::processNack
void processNack(const ublox_msgs::Ack &m)
Callback handler for UBX-NACK message.
Definition: gps.cpp:82
ublox_gps::Gps::configReset
bool configReset(uint16_t nav_bbr_mask, uint16_t reset_mode)
Send a reset message to the u-blox device.
Definition: gps.cpp:307
ublox_gps::Gps::close
void close()
Closes the I/O port, and initiates save on shutdown procedure if enabled.
Definition: gps.cpp:285
ublox_gps::Gps::configRate
bool configRate(uint16_t meas_rate, uint16_t nav_rate)
Configure the device navigation and measurement rate settings.
Definition: gps.cpp:417


ublox_gps
Author(s): Johannes Meyer
autogenerated on Wed Dec 7 2022 03:47:53