Main MRPT website > C++ reference for MRPT 1.9.9
CImpinjRFID.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 #include <mrpt/system/os.h>
14 
15 #include <iostream>
16 #include <thread>
17 
18 using namespace mrpt::hwdrivers;
19 using namespace mrpt::system;
20 
21 using namespace std::literals;
22 
24 
26 {
27  m_sensorLabel = "RFID";
28  connected = false;
29 }
30 
31 CImpinjRFID::~CImpinjRFID() { closeReader(); }
33 {
34  // start the driver
35  // use a separate thread so the connection can be established while the
36  // program starts. This is essential because the module will stall on the
37  // accept() call until the driver executable requests a connection, and, on
38  // the other hand, if the module is not listening, the driver will fail
39  std::thread(dummy_startDriver, this).detach();
40  // system::createThreadFromObjectMethod<CImpinjRFID>(this,startDriver);
41 
42  // start connection
43 
44  connect();
45 }
46 
49 {
50  // start the driver
51  std::stringstream cmdline;
52  std::cout << "Waiting for the driver to start ... ";
53 
54  // create the command line (executable path + parameters)
55  cmdline << driver_path << " " << reader_name.c_str() << " " << IPm.c_str()
56  << " " << port;
57 
58  // wait until the current module starts the sockets and listens to it
59  std::this_thread::sleep_for(2s);
60 
61  const int ret = ::system(cmdline.str().c_str());
62  if (0 != ret)
63  std::cerr << "[CImpinjRFID::startDriver] Error (" << ret
64  << ") invoking command:\n"
65  << cmdline.str() << std::endl;
66 }
67 
69  const mrpt::config::CConfigFileBase& configSource,
70  const std::string& iniSection)
71 {
73  // TEMPORARILY DISABLED
74  /* pose_x_1 = configSource.read_float(iniSection,"pose_x_1",0,true);
75  pose_y_1 = configSource.read_float(iniSection,"pose_y_1",0,true);
76  pose_z_1 = configSource.read_float(iniSection,"pose_z_1",0,true);
77  pose_roll_1 =
78  configSource.read_float(iniSection,"pose_roll_1",0,true);
79  pose_pitch_1 =
80  configSource.read_float(iniSection,"pose_pitch_1",0,true);
81  pose_yaw_1 =
82  configSource.read_float(iniSection,"pose_yaw_1",0,true);
83 
84  pose_x_2 = configSource.read_float(iniSection,"pose_x_2",0,true);
85  pose_y_2 = configSource.read_float(iniSection,"pose_y_2",0,true);
86  pose_z_2 = configSource.read_float(iniSection,"pose_z_2",0,true);
87  pose_roll_2 =
88  configSource.read_float(iniSection,"pose_roll_2",0,true);
89  pose_pitch_2 =
90  configSource.read_float(iniSection,"pose_pitch_2",0,true);
91  pose_yaw_2 =
92  configSource.read_float(iniSection,"pose_yaw_2",0,true);
93  */
94  IPm = configSource.read_string(iniSection, "local_IP", "127.0.0.1", false);
95  reader_name = configSource.read_string(iniSection, "reader_name", "", true);
96  port = configSource.read_int(iniSection, "listen_port", 0, true);
97  driver_path = configSource.read_string(iniSection, "driver_path", "", true);
98 
99  MRPT_END
100 }
101 
102 /*---------------------------------------------------------------
103  getObservation
104  Get the power of a given network as an observation
105  ---------------------------------------------------------------*/
107 {
108  if (!connected)
109  {
110  // Start the server
111  server = std::make_unique<mrpt::comms::CServerTCPSocket>(port);
112  }
113 
114  client = server->accept();
115 
116  std::this_thread::sleep_for(1s);
117  connected = true;
118 }
119 
120 /*---------------------------------------------------------------
121  getObservation
122  Get the power of a given network as an observation
123  ---------------------------------------------------------------*/
125 {
126  try
127  {
128  bool receivedSomething = false;
129  char msg[34];
130  char cmd[20];
131  char epc[24];
132  char rx_pwr[5];
133  char* tmp;
134  obs.tag_readings.clear();
135  // send an observation command to the device interface program
136  strcpy(cmd, "OBS\0");
137  client->writeAsync(cmd, 10);
138 
139  // receive a reading from the sensor through the socket
140  while (client->readAsync(msg, 34, 100) > 0)
141  {
142  receivedSomething = true;
143  // the received string is in the format: ANT_PORT EPC RX_PWR
144  // ZERO_FILL
145  const char* ant_port_ptr = mrpt::system::strtok(msg, " ", &tmp);
146  if (!ant_port_ptr)
147  {
148  std::cerr << "[CImpinjRFID::getObservation] Unexpected format "
149  "in sensor data! (skipping).\n";
150  continue;
151  }
152  const char ant_port = *ant_port_ptr;
153  strcpy(epc, mrpt::system::strtok(nullptr, " ", &tmp));
154  strcpy(rx_pwr, mrpt::system::strtok(nullptr, " ", &tmp));
155 
156  // Fill the observation
157  obs.tag_readings.resize(
158  obs.tag_readings.size() +
159  1); // Alloc space for one more tag obs
161  *obs.tag_readings.rbegin(); // Get a reference to the latest
162  // new tag structure
163 
164  // Fill in fields in "new_tag":
165  new_tag.antennaPort = mrpt::format("%c", ant_port);
166  new_tag.epc = std::string(epc);
167  new_tag.power = atof(rx_pwr);
168  obs.sensorLabel = m_sensorLabel;
169 
170  // std::cout << "mrpt::hwdrivers::CImpinjRFID::getObservation() " <<
171  // "\n\tRXPWR: " << atof(rx_pwr) << " PWR READ: " << rx_pwr <<
172  // std::endl;
173  }
174  if (receivedSomething)
175  return true;
176  else
177  return false;
178  }
179  catch (std::exception& e)
180  {
181  std::cerr << e.what() << std::endl;
182  return false;
183  }
184 }
185 
186 /*---------------------------------------------------------------
187  getObservation
188  Get the power of a given network as an observation
189  ---------------------------------------------------------------*/
191 {
192  char cmd[20];
193  // send a kill command to the device interface program
194  strcpy(cmd, "END\0");
195  client->writeAsync(cmd, 10); // JL->Emil: Why 10 not strlen(cmd)? Is the
196  // server expecting a fixed length data block?
197  client->close();
198 }
199 
201 {
203  mrpt::make_aligned_shared<mrpt::obs::CObservationRFID>();
204  if (getObservation(*obs)) appendObservation(obs);
205 }
os.h
mrpt::hwdrivers::CImpinjRFID::startDriver
void startDriver()
start the external driver
Definition: CImpinjRFID.cpp:48
mrpt::obs::CObservation::sensorLabel
std::string sensorLabel
An arbitrary label that can be used to identify the sensor.
Definition: CObservation.h:62
s
GLdouble s
Definition: glext.h:3676
mrpt::obs::CObservationRFID::TTagReading::antennaPort
std::string antennaPort
Port of the antenna that did the reading.
Definition: CObservationRFID.h:50
mrpt::obs::CObservationRFID::tag_readings
std::vector< TTagReading > tag_readings
The vector of individual tag observations.
Definition: CObservationRFID.h:54
mrpt::hwdrivers::CImpinjRFID::~CImpinjRFID
virtual ~CImpinjRFID()
Definition: CImpinjRFID.cpp:31
mrpt::hwdrivers
Contains classes for various device interfaces.
Definition: C2DRangeFinderAbstract.h:22
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:169
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:125
mrpt::hwdrivers::CImpinjRFID::dummy_startDriver
static void dummy_startDriver(CImpinjRFID *o)
Definition: CImpinjRFID.cpp:47
mrpt::hwdrivers::CImpinjRFID::getObservation
bool getObservation(mrpt::obs::CObservationRFID &obs)
Gets the information of the tags as a timestamped observation NOTE: Deprecated, use getObservations i...
Definition: CImpinjRFID.cpp:124
mrpt::hwdrivers::CImpinjRFID::connect
void connect()
Connect to the reader.
Definition: CImpinjRFID.cpp:106
mrpt::hwdrivers::CImpinjRFID::doProcess
void doProcess()
This method will be invoked at a minimum rate of "process_rate" (Hz)
Definition: CImpinjRFID.cpp:200
MRPT_START
#define MRPT_START
Definition: exceptions.h:262
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::system::strtok
char * strtok(char *str, const char *strDelimit, char **context) noexcept
An OS-independent method for tokenizing a string.
Definition: string_utils.cpp:197
mrpt::hwdrivers::CImpinjRFID::loadConfig_sensorSpecific
void loadConfig_sensorSpecific(const mrpt::config::CConfigFileBase &configSource, const std::string &section)
Loads specific configuration for the device from a given source of configuration parameters,...
Definition: CImpinjRFID.cpp:68
mrpt::system::os::strcpy
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
Definition: os.cpp:297
mrpt::hwdrivers::CImpinjRFID
This class implements an interface to an Impinj RFID reader.
Definition: CImpinjRFID.h:29
IMPLEMENTS_GENERIC_SENSOR
#define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace)
This must be inserted in all CGenericSensor classes implementation files:
Definition: CGenericSensor.h:330
CImpinjRFID.h
mrpt::obs::CObservationRFID
This represents one or more RFID tags observed by a receiver.
Definition: CObservationRFID.h:27
MRPT_END
#define MRPT_END
Definition: exceptions.h:266
mrpt::obs::CObservationRFID::TTagReading
Each of the individual readings of a RFID tag.
Definition: CObservationRFID.h:41
mrpt::hwdrivers::CImpinjRFID::initialize
void initialize()
This method can or cannot be implemented in the derived class, depending on the need for it.
Definition: CImpinjRFID.cpp:32
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::obs::CObservationRFID::TTagReading::power
double power
The power or signal strength as sensed by the RFID receiver (in dBm)
Definition: CObservationRFID.h:46
mrpt::obs::CObservationRFID::Ptr
std::shared_ptr< CObservationRFID > Ptr
Definition: CObservationRFID.h:29
mrpt::obs::CObservationRFID::TTagReading::epc
std::string epc
EPC code of the observed tag.
Definition: CObservationRFID.h:48
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25
mrpt::obs::utils::getObservation
OBSERVATION_T::Ptr getObservation(mrpt::obs::CSensoryFrame::Ptr &observations, mrpt::obs::CObservation::Ptr &observation, bool priority_to_sf=true)
Given an mrpt::obs::CSensoryFrame and a mrpt::obs::CObservation pointer if a OBSERVATION_T type obser...
Definition: obs_utils.h:36
hwdrivers-precomp.h
mrpt::hwdrivers::CImpinjRFID::closeReader
void closeReader()
Close the connection to the reader.
Definition: CImpinjRFID.cpp:190



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