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 }
filesystem.h
mrpt::comms::CSerialPort::isOpen
bool isOpen() const
Returns if port has been correctly open.
Definition: CSerialPort.cpp:190
CNTRIPEmitter.h
mrpt::hwdrivers::CNTRIPEmitter
This "virtual driver" encapsulates a NTRIP client (see CNTRIPClient) but adds the functionality of du...
Definition: CNTRIPEmitter.h:61
mrpt::comms::CSerialPort::close
void close()
Close the port.
Definition: CSerialPort.cpp:625
mrpt::system::dateTimeLocalToString
std::string dateTimeLocalToString(const mrpt::system::TTimeStamp t)
Convert a timestamp into this textual form (in local time): YEAR/MONTH/DAY,HH:MM:SS....
Definition: datetime.cpp:176
mrpt::hwdrivers::CNTRIPEmitter::m_raw_output_file_prefix
std::string m_raw_output_file_prefix
Definition: CNTRIPEmitter.h:79
string_utils.h
mrpt::hwdrivers::CNTRIPEmitter::doProcess
void doProcess() override
The main loop, which must be called in a timely fashion in order to process the incomming NTRIP data ...
Definition: CNTRIPEmitter.cpp:42
mrpt::hwdrivers::CNTRIPClient::stream_data
mrpt::containers::MT_buffer stream_data
The buffer with all the bytes so-far read from the NTRIP server stream.
Definition: CNTRIPClient.h:143
mrpt::hwdrivers::CNTRIPEmitter::~CNTRIPEmitter
~CNTRIPEmitter() override
Destructor
Definition: CNTRIPEmitter.cpp:32
mrpt::system::now
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:86
mrpt::hwdrivers::CNTRIPClient::NTRIPArgs::server
std::string server
Definition: CNTRIPClient.h:81
mrpt::config::CConfigFileBase::read_bool
bool read_bool(const std::string &section, const std::string &name, bool defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:155
mrpt::hwdrivers
Contains classes for various device interfaces.
Definition: C2DRangeFinderAbstract.h:19
mrpt::hwdrivers::CNTRIPClient::NTRIPArgs::mountpoint
std::string mountpoint
Definition: CNTRIPClient.h:85
mrpt::hwdrivers::CNTRIPClient::NTRIPArgs::password
std::string password
Definition: CNTRIPClient.h:84
mrpt::hwdrivers::CNTRIPEmitter::m_com_bauds
int m_com_bauds
Definition: CNTRIPEmitter.h:77
out
mrpt::vision::TStereoCalibResults out
Definition: chessboard_stereo_camera_calib_unittest.cpp:25
mrpt::comms::CSerialPort::Write
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CSerialPort.cpp:834
mrpt::containers::MT_buffer::readAndClear
void readAndClear(std::vector< uint8_t > &d)
Read the whole buffer and empty it.
Definition: MT_buffer.h:60
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:15
mrpt::hwdrivers::CNTRIPEmitter::m_client
CNTRIPClient m_client
The NTRIP comms object.
Definition: CNTRIPEmitter.h:69
mrpt::comms::CSerialPort::purgeBuffers
void purgeBuffers()
Purge tx and rx buffers.
Definition: CSerialPort.cpp:899
mrpt::obs
This namespace contains representation of robot actions and observations.
Definition: CParticleFilter.h:17
mrpt::hwdrivers::CNTRIPClient::open
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...
Definition: CNTRIPClient.cpp:64
mrpt::config::CConfigFileBase::read_string
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:171
mrpt::system::fileNameStripInvalidChars
std::string fileNameStripInvalidChars(const std::string &filename, const char replacement_to_invalid_chars='_')
Replace invalid filename chars by underscores ('_') or any other user-given char.
Definition: filesystem.cpp:329
mrpt::hwdrivers::CNTRIPEmitter::m_rate_count
size_t m_rate_count
Definition: CNTRIPEmitter.h:82
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:87
mrpt::config::CConfigFileBase::read_int
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
Definition: CConfigFileBase.cpp:130
mrpt::hwdrivers::CNTRIPClient::NTRIPArgs::user
std::string user
Definition: CNTRIPClient.h:83
mrpt::comms::CSerialPort::setConfig
void setConfig(int baudRate, int parity=0, int bits=8, int nStopBits=1, bool enableFlowControl=false)
Changes the configuration of the port.
Definition: CSerialPort.cpp:202
mrpt::hwdrivers::CNTRIPEmitter::m_ntrip_args
CNTRIPClient::NTRIPArgs m_ntrip_args
Definition: CNTRIPEmitter.h:66
mrpt::hwdrivers::CGenericSensor::m_verbose
bool m_verbose
Definition: CGenericSensor.h:149
mrpt::config::CConfigFileBase
This class allows loading and storing values and vectors of different types from a configuration text...
Definition: config/CConfigFileBase.h:44
mrpt::hwdrivers::CNTRIPEmitter::m_raw_output_file_stream
std::ofstream m_raw_output_file_stream
Definition: CNTRIPEmitter.h:80
mrpt::system::timeLocalToString
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
mrpt::hwdrivers::CNTRIPEmitter::m_out_COM
mrpt::comms::CSerialPort m_out_COM
The output serial port.
Definition: CNTRIPEmitter.h:71
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:76
mrpt::hwdrivers::CNTRIPClient::close
void close()
Closes the connection.
Definition: CNTRIPClient.cpp:53
mrpt::hwdrivers::CNTRIPEmitter::m_rate_timer
mrpt::system::CTicTac m_rate_timer
Definition: CNTRIPEmitter.h:81
ASSERTMSG_
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
IMPLEMENTS_GENERIC_SENSOR
#define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace)
This must be inserted in all CGenericSensor classes implementation files:
Definition: CGenericSensor.h:314
mrpt::hwdrivers::CNTRIPEmitter::loadConfig_sensorSpecific
void loadConfig_sensorSpecific(const mrpt::config::CConfigFileBase &configSource, const std::string &iniSection) override
See the class documentation at the top for expected parameters.
Definition: CNTRIPEmitter.cpp:151
mrpt::comms::CSerialPort::open
void open()
Open the port.
Definition: CSerialPort.cpp:93
mrpt::comms::CSerialPort::setTimeouts
void setTimeouts(int ReadIntervalTimeout, int ReadTotalTimeoutMultiplier, int ReadTotalTimeoutConstant, int WriteTotalTimeoutMultiplier, int WriteTotalTimeoutConstant)
Changes the timeouts of the port, in milliseconds.
Definition: CSerialPort.cpp:564
mrpt::hwdrivers::CNTRIPClient::NTRIPArgs::port
int port
Definition: CNTRIPClient.h:82
mrpt::system::trim
std::string trim(const std::string &str)
Removes leading and trailing spaces.
Definition: string_utils.cpp:270
mrpt::hwdrivers::CNTRIPEmitter::initialize
void initialize() override
Set up the NTRIP communications, raising an exception on fatal errors.
Definition: CNTRIPEmitter.cpp:110
mrpt::hwdrivers::CNTRIPEmitter::m_com_port
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
mrpt::hwdrivers::CNTRIPEmitter::m_transmit_to_server
bool m_transmit_to_server
Definition: CNTRIPEmitter.h:78
mrpt::format
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
mrpt::comms::CSerialPort::Read
size_t Read(void *Buffer, size_t Count) override
Implements the virtual method responsible for reading from the stream - Unlike CStream::ReadBuffer,...
Definition: CSerialPort.cpp:648
hwdrivers-precomp.h



Page generated by Doxygen 1.8.17 for MRPT 2.0.4 at Fri Jul 17 08:43:33 UTC 2020