MRPT  2.0.4
CNTRIPEmitter.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 "hwdrivers-precomp.h" // Precompiled headers
11 
13 
14 #include <mrpt/system/filesystem.h>
16 #include <iostream>
17 
19 
20 using namespace std;
21 using namespace mrpt;
22 using namespace mrpt::obs;
23 using namespace mrpt::hwdrivers;
24 
25 /*-------------------------------------------------------------
26  CNTRIPEmitter
27 -------------------------------------------------------------*/
28 CNTRIPEmitter::CNTRIPEmitter() : m_client(), m_com_port("") {}
29 /*-------------------------------------------------------------
30  ~CNTRIPEmitter
31 -------------------------------------------------------------*/
33 {
34  m_client.close();
37 }
38 
39 /*-------------------------------------------------------------
40  doProcess
41 -------------------------------------------------------------*/
43 {
44  std::vector<uint8_t> buf;
46 
47  if (!buf.empty())
48  {
49  if (m_verbose)
50  {
51  const double At = m_rate_timer.Tac();
52  m_rate_count += buf.size();
53  if (At > 5.0)
54  {
55  const double estim_rate_Bps = m_rate_count / At;
56  cout << format(
57  "[NTRIP %s] Rate: %.02f B/s\n",
59  .c_str(),
60  estim_rate_Bps);
61  m_rate_timer.Tic();
62  m_rate_count = 0;
63  }
64 
65  cout << format(
66  "[NTRIP %s] RX (%u bytes)\n",
68  (unsigned int)buf.size());
69  }
70  if (m_out_COM.isOpen())
71  {
72  // Send through the serial port:
73  cout << format(
74  "[NTRIP %s] RX: %u bytes\n",
76  (unsigned)buf.size());
77  m_out_COM.Write(&buf[0], buf.size());
78  }
79 
80  if (m_raw_output_file_stream.is_open())
81  {
83  reinterpret_cast<const char*>(&buf[0]), buf.size());
84  }
85  }
86 
87  // Try to read a msg from the receiver -> NTRIP caster
89  {
90  char rxbuf[50];
91  const size_t nReadActual = m_out_COM.Read(rxbuf, sizeof(rxbuf) - 1);
92  if (nReadActual)
93  {
94  rxbuf[nReadActual] = 0;
95  if (m_verbose)
96  cout << format(
97  "[NTRIP %s] TX (%u bytes)\n",
99  .c_str(),
100  (unsigned int)nReadActual);
101  }
102  }
103 
104  std::this_thread::sleep_for(1ms);
105 }
106 
107 /*-------------------------------------------------------------
108  initialize
109 -------------------------------------------------------------*/
111 {
112  if (m_out_COM.isOpen()) m_out_COM.close();
113 
114  if (!m_com_port.empty())
115  {
116  cout << format("[NTRIP] Opening %s...\n", m_com_port.c_str());
119  m_out_COM.setTimeouts(0, 0, 10, 0, 1);
121  cout << format("[NTRIP] Open %s Ok.\n", m_com_port.c_str());
122  }
123 
124  if (m_raw_output_file_stream.is_open())
125  {
126  m_raw_output_file_stream.close();
127  }
128 
129  if (!m_raw_output_file_prefix.empty())
130  {
131  const string fil = mrpt::system::fileNameStripInvalidChars(
134  string(".bin"));
136  fil.c_str(), std::ofstream::out | std::ofstream::binary);
137  if (!m_raw_output_file_stream.is_open())
139  "Error opening output raw file: `%s`", fil.c_str());
140  }
141 
142  string errstr;
143  if (!m_client.open(m_ntrip_args, errstr))
145  "ERROR trying to connect to NTRIP caster: %s", errstr.c_str());
146 }
147 
148 /* -----------------------------------------------------
149  loadConfig_sensorSpecific
150  ----------------------------------------------------- */
152  const mrpt::config::CConfigFileBase& c, const std::string& s)
153 {
154 #ifdef _WIN32
155  m_com_port = c.read_string(s, "COM_port_WIN", "");
156 #else
157  m_com_port = c.read_string(s, "COM_port_LIN", "");
158 #endif
159 
160  m_raw_output_file_prefix = c.read_string(s, "raw_output_file_prefix", "");
161 
162  ASSERTMSG_(
163  !m_raw_output_file_prefix.empty() || !m_com_port.empty(),
164  "At least one of either raw file output or serial COM file must be "
165  "specified in configuration file!");
166 
167  if (!m_com_port.empty())
168  {
169  m_com_bauds = c.read_int(s, "baudRate", m_com_bauds, true);
170  }
171 
173  c.read_bool(s, "transmit_to_server", m_transmit_to_server);
174 
176  mrpt::system::trim(c.read_string(s, "mountpoint", "", true));
178  mrpt::system::trim(c.read_string(s, "server", "", true));
179  m_ntrip_args.port = c.read_int(s, "port", 2101, true);
180 
181  m_ntrip_args.user = mrpt::system::trim(c.read_string(s, "user", ""));
183  mrpt::system::trim(c.read_string(s, "password", ""));
184 }
void doProcess() override
The main loop, which must be called in a timely fashion in order to process the incomming NTRIP data ...
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:87
size_t Read(void *Buffer, size_t Count) override
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.
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...
void initialize() override
Set up the NTRIP communications, raising an exception on fatal errors.
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
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:76
CNTRIPClient m_client
The NTRIP comms object.
Definition: CNTRIPEmitter.h:69
void open()
Open the port.
Definition: CSerialPort.cpp:93
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:86
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:329
void close()
Closes the connection.
void setConfig(int baudRate, int parity=0, int bits=8, int nStopBits=1, bool enableFlowControl=false)
Changes the configuration of the port.
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
~CNTRIPEmitter() override
Destructor.
This class allows loading and storing values and vectors of different types from a configuration text...
void loadConfig_sensorSpecific(const mrpt::config::CConfigFileBase &configSource, const std::string &iniSection) override
See the class documentation at the top for expected parameters.
bool isOpen() const
Returns if port has been correctly open.
This namespace contains representation of robot actions and observations.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
void purgeBuffers()
Purge tx and rx buffers.
#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.
CNTRIPClient::NTRIPArgs m_ntrip_args
Definition: CNTRIPEmitter.h:66
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::ofstream m_raw_output_file_stream
Definition: CNTRIPEmitter.h:80
mrpt::vision::TStereoCalibResults out
bool read_bool(const std::string &section, const std::string &name, bool defaultValue, bool failIfNotFound=false) const
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:222
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:71
mrpt::system::CTicTac m_rate_timer
Definition: CNTRIPEmitter.h:81
void readAndClear(std::vector< uint8_t > &d)
Read the whole buffer and empty it.
Definition: MT_buffer.h:60
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:76
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:176
This "virtual driver" encapsulates a NTRIP client (see CNTRIPClient) but adds the functionality of du...
Definition: CNTRIPEmitter.h:61
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
mrpt::containers::MT_buffer stream_data
The buffer with all the bytes so-far read from the NTRIP server stream.
Definition: CNTRIPClient.h:143



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