MRPT  1.9.9
CImpinjRFID.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-2019, 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 #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 (const 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 {
204  if (getObservation(*obs)) appendObservation(obs);
205 }
std::string antennaPort
Port of the antenna that did the reading.
#define MRPT_START
Definition: exceptions.h:241
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
This class implements an interface to an Impinj RFID reader.
Definition: CImpinjRFID.h:26
Contains classes for various device interfaces.
GLdouble s
Definition: glext.h:3682
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
void loadConfig_sensorSpecific(const mrpt::config::CConfigFileBase &configSource, const std::string &section) override
Loads specific configuration for the device from a given source of configuration parameters, for example, an ".ini" file, loading from the section "[iniSection]" (see config::CConfigFileBase and derived classes)
Definition: CImpinjRFID.cpp:68
This class allows loading and storing values and vectors of different types from a configuration text...
void initialize() override
This method can or cannot be implemented in the derived class, depending on the need for it...
Definition: CImpinjRFID.cpp:32
std::string epc
EPC code of the observed tag.
char * strtok(char *str, const char *strDelimit, char **context) noexcept
An OS-independent method for tokenizing a string.
void startDriver()
start the external driver
Definition: CImpinjRFID.cpp:48
double power
The power or signal strength as sensed by the RFID receiver (in dBm)
GLsizei const GLchar ** string
Definition: glext.h:4116
static Ptr Create(Args &&... args)
#define IMPLEMENTS_GENERIC_SENSOR(class_name, NameSpace)
This must be inserted in all CGenericSensor classes implementation files:
static void dummy_startDriver(CImpinjRFID *o)
Definition: CImpinjRFID.cpp:47
std::string sensorLabel
An arbitrary label that can be used to identify the sensor.
Definition: CObservation.h:62
void connect()
Connect to the reader.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
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:31
bool getObservation(mrpt::obs::CObservationRFID &obs)
Gets the information of the tags as a timestamped observation NOTE: Deprecated, use getObservations i...
#define MRPT_END
Definition: exceptions.h:245
std::vector< TTagReading > tag_readings
The vector of individual tag observations.
Each of the individual readings of a RFID tag.
void doProcess() override
This method will be invoked at a minimum rate of "process_rate" (Hz)
This represents one or more RFID tags observed by a receiver.
char * strcpy(char *dest, size_t destSize, const char *source) noexcept
An OS-independent version of strcpy.
Definition: os.cpp:299
void closeReader()
Close the connection to the reader.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019