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-2018, 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 #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()
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  std::vector<uint8_t> 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.Write(&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 _WIN32
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 }
filesystem.h
mrpt::comms::CSerialPort::isOpen
bool isOpen() const
Returns if port has been correctly open.
Definition: CSerialPort.cpp:203
CNTRIPEmitter.h
mrpt::hwdrivers::CNTRIPEmitter
This "virtual driver" encapsulates a NTRIP client (see CNTRIPClient) but adds the functionality of du...
Definition: CNTRIPEmitter.h:64
mrpt::comms::CSerialPort::close
void close()
Close the port.
Definition: CSerialPort.cpp:638
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:270
mrpt::hwdrivers::CNTRIPEmitter::m_raw_output_file_prefix
std::string m_raw_output_file_prefix
Definition: CNTRIPEmitter.h:82
s
GLdouble s
Definition: glext.h:3676
string_utils.h
c
const GLubyte * c
Definition: glext.h:6313
mrpt::comms::CSerialPort::Read
size_t Read(void *Buffer, size_t Count)
Implements the virtual method responsible for reading from the stream - Unlike CStream::ReadBuffer,...
Definition: CSerialPort.cpp:661
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:163
mrpt::system::now
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:75
mrpt::hwdrivers::CNTRIPClient::NTRIPArgs::server
std::string server
Definition: CNTRIPClient.h:94
mrpt::hwdrivers
Contains classes for various device interfaces.
Definition: C2DRangeFinderAbstract.h:22
mrpt::hwdrivers::CNTRIPClient::NTRIPArgs::mountpoint
std::string mountpoint
Definition: CNTRIPClient.h:98
mrpt::hwdrivers::CNTRIPClient::NTRIPArgs::password
std::string password
Definition: CNTRIPClient.h:97
mrpt::hwdrivers::CNTRIPEmitter::m_com_bauds
int m_com_bauds
Definition: CNTRIPEmitter.h:80
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:847
mrpt::containers::MT_buffer::readAndClear
void readAndClear(std::vector< uint8_t > &d)
Read the whole buffer and empty it.
Definition: MT_buffer.h:62
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::hwdrivers::CNTRIPEmitter::loadConfig_sensorSpecific
void loadConfig_sensorSpecific(const mrpt::config::CConfigFileBase &configSource, const std::string &iniSection)
See the class documentation at the top for expected parameters.
Definition: CNTRIPEmitter.cpp:159
mrpt::hwdrivers::CNTRIPEmitter::m_client
CNTRIPClient m_client
The NTRIP comms object.
Definition: CNTRIPEmitter.h:72
mrpt::comms::CSerialPort::purgeBuffers
void purgeBuffers()
Purge tx and rx buffers.
Definition: CSerialPort.cpp:912
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:69
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:328
mrpt::hwdrivers::CNTRIPEmitter::m_rate_count
size_t m_rate_count
Definition: CNTRIPEmitter.h:85
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
mrpt::hwdrivers::CNTRIPClient::NTRIPArgs::user
std::string user
Definition: CNTRIPClient.h:96
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:215
mrpt::hwdrivers::CNTRIPEmitter::m_ntrip_args
CNTRIPClient::NTRIPArgs m_ntrip_args
Definition: CNTRIPEmitter.h:69
mrpt::hwdrivers::CGenericSensor::m_verbose
bool m_verbose
Definition: CGenericSensor.h:148
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::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::hwdrivers::CNTRIPEmitter::m_raw_output_file_stream
std::ofstream m_raw_output_file_stream
Definition: CNTRIPEmitter.h:83
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:315
mrpt::hwdrivers::CNTRIPEmitter::m_out_COM
mrpt::comms::CSerialPort m_out_COM
The output serial port.
Definition: CNTRIPEmitter.h:74
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::hwdrivers::CNTRIPClient::close
void close()
Closes the connection.
Definition: CNTRIPClient.cpp:58
mrpt::hwdrivers::CNTRIPEmitter::m_rate_timer
mrpt::system::CTicTac m_rate_timer
Definition: CNTRIPEmitter.h:84
ASSERTMSG_
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:101
IMPLEMENTS_GENERIC_SENSOR
#define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace)
This must be inserted in all CGenericSensor classes implementation files:
Definition: CGenericSensor.h:330
mrpt::comms::CSerialPort::open
void open()
Open the port.
Definition: CSerialPort.cpp:106
mrpt::hwdrivers::CNTRIPEmitter::initialize
void initialize()
Set up the NTRIP communications, raising an exception on fatal errors.
Definition: CNTRIPEmitter.cpp:118
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:577
mrpt::hwdrivers::CNTRIPClient::NTRIPArgs::port
int port
Definition: CNTRIPClient.h:95
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::system::trim
std::string trim(const std::string &str)
Removes leading and trailing spaces.
Definition: string_utils.cpp:270
mrpt::hwdrivers::CNTRIPEmitter::doProcess
void doProcess()
The main loop, which must be called in a timely fashion in order to process the incomming NTRIP data ...
Definition: CNTRIPEmitter.cpp:50
mrpt::hwdrivers::CNTRIPEmitter::~CNTRIPEmitter
virtual ~CNTRIPEmitter()
Destructor
Definition: CNTRIPEmitter.cpp:40
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:79
mrpt::hwdrivers::CNTRIPEmitter::m_transmit_to_server
bool m_transmit_to_server
Definition: CNTRIPEmitter.h:81
hwdrivers-precomp.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST