Main MRPT website > C++ reference for MRPT 1.9.9
CNTRIPEmitter.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "hwdrivers-precomp.h" // Precompiled headers
11 
13 
14 #include <mrpt/system/filesystem.h>
16 
18 
19 using namespace std;
20 using namespace mrpt;
21 using namespace mrpt::utils;
22 using namespace mrpt::obs;
23 using namespace mrpt::hwdrivers;
24 
25 /*-------------------------------------------------------------
26  CNTRIPEmitter
27 -------------------------------------------------------------*/
28 CNTRIPEmitter::CNTRIPEmitter()
29  : m_client(),
30  m_com_port(""),
31  m_com_bauds(38400),
32  m_transmit_to_server(true),
33  m_rate_count(0)
34 {
35 }
36 
37 /*-------------------------------------------------------------
38  ~CNTRIPEmitter
39 -------------------------------------------------------------*/
41 {
42  m_client.close();
45 }
46 
47 /*-------------------------------------------------------------
48  doProcess
49 -------------------------------------------------------------*/
51 {
52  vector_byte buf;
54 
55  if (!buf.empty())
56  {
57  if (m_verbose)
58  {
59  const double At = m_rate_timer.Tac();
60  m_rate_count += buf.size();
61  if (At > 5.0)
62  {
63  const double estim_rate_Bps = m_rate_count / At;
64  cout << format(
65  "[NTRIP %s] Rate: %.02f B/s\n",
67  .c_str(),
68  estim_rate_Bps);
69  m_rate_timer.Tic();
70  m_rate_count = 0;
71  }
72 
73  cout << format(
74  "[NTRIP %s] RX (%u bytes)\n",
76  (unsigned int)buf.size());
77  }
78  if (m_out_COM.isOpen())
79  {
80  // Send through the serial port:
81  cout << format(
82  "[NTRIP %s] RX: %u bytes\n",
84  (unsigned)buf.size());
85  m_out_COM.WriteBuffer(&buf[0], buf.size());
86  }
87 
88  if (m_raw_output_file_stream.is_open())
89  {
91  reinterpret_cast<const char*>(&buf[0]), buf.size());
92  }
93  }
94 
95  // Try to read a msg from the receiver -> NTRIP caster
97  {
98  char rxbuf[50];
99  const size_t nReadActual = m_out_COM.Read(rxbuf, sizeof(rxbuf) - 1);
100  if (nReadActual)
101  {
102  rxbuf[nReadActual] = 0;
103  if (m_verbose)
104  cout << format(
105  "[NTRIP %s] TX (%u bytes)\n",
107  .c_str(),
108  (unsigned int)nReadActual);
109  }
110  }
111 
112  std::this_thread::sleep_for(1ms);
113 }
114 
115 /*-------------------------------------------------------------
116  initialize
117 -------------------------------------------------------------*/
119 {
120  if (m_out_COM.isOpen()) m_out_COM.close();
121 
122  if (!m_com_port.empty())
123  {
124  cout << format("[NTRIP] Opening %s...\n", m_com_port.c_str());
127  m_out_COM.setTimeouts(0, 0, 10, 0, 1);
129  cout << format("[NTRIP] Open %s Ok.\n", m_com_port.c_str());
130  }
131 
132  if (m_raw_output_file_stream.is_open())
133  {
134  m_raw_output_file_stream.close();
135  }
136 
137  if (!m_raw_output_file_prefix.empty())
138  {
139  const string fil = mrpt::system::fileNameStripInvalidChars(
142  string(".bin"));
144  fil.c_str(), std::ofstream::out | std::ofstream::binary);
145  if (!m_raw_output_file_stream.is_open())
147  "Error opening output raw file: `%s`", fil.c_str());
148  }
149 
150  string errstr;
151  if (!m_client.open(m_ntrip_args, errstr))
153  "ERROR trying to connect to NTRIP caster: %s", errstr.c_str());
154 }
155 
156 /* -----------------------------------------------------
157  loadConfig_sensorSpecific
158  ----------------------------------------------------- */
161 {
162 #ifdef MRPT_OS_WINDOWS
163  m_com_port = c.read_string(s, "COM_port_WIN", "");
164 #else
165  m_com_port = c.read_string(s, "COM_port_LIN", "");
166 #endif
167 
168  m_raw_output_file_prefix = c.read_string(s, "raw_output_file_prefix", "");
169 
170  ASSERTMSG_(
171  !m_raw_output_file_prefix.empty() || !m_com_port.empty(),
172  "At least one of either raw file output or serial COM file must be "
173  "specified in configuration file!");
174 
175  if (!m_com_port.empty())
176  {
177  m_com_bauds = c.read_int(s, "baudRate", m_com_bauds, true);
178  }
179 
181  c.read_bool(s, "transmit_to_server", m_transmit_to_server);
182 
184  mrpt::system::trim(c.read_string(s, "mountpoint", "", true));
186  mrpt::system::trim(c.read_string(s, "server", "", true));
187  m_ntrip_args.port = c.read_int(s, "port", 2101, true);
188 
189  m_ntrip_args.user = mrpt::system::trim(c.read_string(s, "user", ""));
191  mrpt::system::trim(c.read_string(s, "password", ""));
192 }
void initialize()
Set up the NTRIP communications, raising an exception on fatal errors.
bool open(const NTRIPArgs &params, std::string &out_errmsg)
Tries to open a given NTRIP stream and, if successful, launches a thread for continuously reading fro...
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
mrpt::utils::CTicTac m_rate_timer
Definition: CNTRIPEmitter.h:84
std::vector< uint8_t > vector_byte
Definition: types_simple.h:27
std::string m_com_port
If set to non-empty, the serial port will be attempted to be opened automatically when this class is ...
Definition: CNTRIPEmitter.h:79
CNTRIPClient m_client
The NTRIP comms object.
Definition: CNTRIPEmitter.h:72
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
void WriteBuffer(const void *Buffer, size_t Count)
Writes a block of bytes to the stream from Buffer.
Definition: CStream.cpp:64
void open()
Open the port.
Definition: CSerialPort.cpp:88
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:76
Contains classes for various device interfaces.
STL namespace.
std::string fileNameStripInvalidChars(const std::string &filename, const char replacement_to_invalid_chars='_')
Replace invalid filename chars by underscores (&#39;_&#39;) or any other user-given char. ...
Definition: filesystem.cpp:327
void close()
Closes the connection.
GLdouble s
Definition: glext.h:3676
void setConfig(int baudRate, int parity=0, int bits=8, int nStopBits=1, bool enableFlowControl=false)
Changes the configuration of the port.
void readAndClear(vector_byte &d)
Read the whole buffer and empty it.
Definition: MT_buffer.h:63
void Tic()
Starts the stopwatch.
Definition: CTicTac.cpp:82
This class allows loading and storing values and vectors of different types from a configuration text...
const GLubyte * c
Definition: glext.h:6313
bool isOpen() const
Returns if port has been correctly open.
This namespace contains representation of robot actions and observations.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
void purgeBuffers()
Purge tx and rx buffers.
GLsizei const GLchar ** string
Definition: glext.h:4101
#define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace)
This must be inserted in all CGenericSensor classes implementation files:
void close()
Close the port.
void setTimeouts(int ReadIntervalTimeout, int ReadTotalTimeoutMultiplier, int ReadTotalTimeoutConstant, int WriteTotalTimeoutMultiplier, int WriteTotalTimeoutConstant)
Changes the timeouts of the port, in milliseconds.
void loadConfig_sensorSpecific(const mrpt::utils::CConfigFileBase &configSource, const std::string &iniSection)
See the class documentation at the top for expected parameters.
CNTRIPClient::NTRIPArgs m_ntrip_args
Definition: CNTRIPEmitter.h:69
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::ofstream m_raw_output_file_stream
Definition: CNTRIPEmitter.h:83
void doProcess()
The main loop, which must be called in a timely fashion in order to process the incomming NTRIP data ...
virtual ~CNTRIPEmitter()
Destructor.
std::string timeLocalToString(const mrpt::system::TTimeStamp t, unsigned int secondFractionDigits=6)
Convert a timestamp into this textual form (in local time): HH:MM:SS.MMMMMM.
Definition: datetime.cpp:314
double Tac()
Stops the stopwatch.
Definition: CTicTac.cpp:97
std::string trim(const std::string &str)
Removes leading and trailing spaces.
mrpt::comms::CSerialPort m_out_COM
The output serial port.
Definition: CNTRIPEmitter.h:74
mrpt::synch::MT_buffer stream_data
The buffer with all the bytes so-far read from the NTRIP server stream.
Definition: CNTRIPClient.h:164
std::string dateTimeLocalToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (in local time): YEAR/MONTH/DAY,HH:MM:SS.MMM.
Definition: datetime.cpp:269
This "virtual driver" encapsulates a NTRIP client (see CNTRIPClient) but adds the functionality of du...
Definition: CNTRIPEmitter.h:64
#define ASSERTMSG_(f, __ERROR_MSG)
size_t Read(void *Buffer, size_t Count)
Implements the virtual method responsible for reading from the stream - Unlike CStream::ReadBuffer, this method will not raise an exception on zero bytes read, as long as there is not any fatal error in the communications.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019