Main MRPT website > C++ reference for MRPT 1.5.5
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-2017, 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 using namespace mrpt::hwdrivers;
16 using namespace mrpt::system;
17 
19 
21 {
22  m_sensorLabel = "RFID";
23  connected = false;
24 }
25 
26 
28 {
29  closeReader();
30 }
31 
32 
34 {
35 
36 
37  // start the driver
38  //use a separate thread so the connection can be established while the program starts. This is essential because the module will stall on the accept() call until the driver executable requests a connection, and, on the other hand, if the module is not listening, the driver will fail
39  mrpt::system::createThread(dummy_startDriver,this);
40 // system::createThreadFromObjectMethod<CImpinjRFID>(this,startDriver);
41 
42  // start connection
43 
44  connect();
45 
46 }
47 
48 
49 
51 {
52  o->startDriver();
53 }
54 
56 {
57  // start the driver
58  std::stringstream cmdline;
59  std::cout << "Waiting for the driver to start ... ";
60 
61  // create the command line (executable path + parameters)
62  cmdline << driver_path << " " << reader_name.c_str() << " " << IPm.c_str() << " " << port;
63 
64  // wait until the current module starts the sockets and listens to it
65  system::sleep(2000);
66 
67  const int ret = ::system(cmdline.str().c_str());
68  if (0!=ret)
69  std::cerr << "[CImpinjRFID::startDriver] Error ("<< ret << ") invoking command:\n" << cmdline.str() << std::endl;
70 
71  system::exitThread(); // JL->Emil: Really needed? If not, just remove...
72 }
73 
75  const mrpt::utils::CConfigFileBase &configSource,
76  const std::string &iniSection )
77 {
78 
80  // TEMPORARILY DISABLED
81 /* pose_x_1 = configSource.read_float(iniSection,"pose_x_1",0,true);
82  pose_y_1 = configSource.read_float(iniSection,"pose_y_1",0,true);
83  pose_z_1 = configSource.read_float(iniSection,"pose_z_1",0,true);
84  pose_roll_1 = configSource.read_float(iniSection,"pose_roll_1",0,true);
85  pose_pitch_1 = configSource.read_float(iniSection,"pose_pitch_1",0,true);
86  pose_yaw_1 = configSource.read_float(iniSection,"pose_yaw_1",0,true);
87 
88  pose_x_2 = configSource.read_float(iniSection,"pose_x_2",0,true);
89  pose_y_2 = configSource.read_float(iniSection,"pose_y_2",0,true);
90  pose_z_2 = configSource.read_float(iniSection,"pose_z_2",0,true);
91  pose_roll_2 = configSource.read_float(iniSection,"pose_roll_2",0,true);
92  pose_pitch_2 = configSource.read_float(iniSection,"pose_pitch_2",0,true);
93  pose_yaw_2 = configSource.read_float(iniSection,"pose_yaw_2",0,true);
94 */
95  IPm = configSource.read_string(iniSection,"local_IP","127.0.0.1",false);
96  reader_name = configSource.read_string(iniSection,"reader_name","", true);
97  port = configSource.read_int(iniSection,"listen_port",0,true);
98  driver_path = configSource.read_string(iniSection,"driver_path","",true);
99 
100  MRPT_END
101 }
102 
103 /*---------------------------------------------------------------
104  getObservation
105  Get the power of a given network as an observation
106  ---------------------------------------------------------------*/
108 {
109  if (!connected)
110 
111  // Start the server
112  server = new mrpt::utils::CServerTCPSocket(port);
113 
114  client = server->accept();
115 
116  system::sleep(1000);
117  connected = true;
118 
119 }
120 
121 /*---------------------------------------------------------------
122  getObservation
123  Get the power of a given network as an observation
124  ---------------------------------------------------------------*/
126 {
127  try{
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 ZERO_FILL
144  const char *ant_port_ptr = mrpt::system::strtok(msg," ",&tmp);
145  if (!ant_port_ptr)
146  {
147  std::cerr << "[CImpinjRFID::getObservation] Unexpected format in sensor data! (skipping).\n";
148  continue;
149  }
150  const char ant_port = *ant_port_ptr;
151  strcpy(epc,mrpt::system::strtok(NULL," ",&tmp));
152  strcpy(rx_pwr,mrpt::system::strtok(NULL, " ",&tmp));
153 
154  // Fill the observation
155  obs.tag_readings.resize(obs.tag_readings.size()+1); // Alloc space for one more tag obs
156  mrpt::obs::CObservationRFID::TTagReading & new_tag = *obs.tag_readings.rbegin(); // Get a reference to the latest new tag structure
157 
158  // Fill in fields in "new_tag":
159  new_tag.antennaPort = mrpt::format("%c",ant_port);
160  new_tag.epc = std::string(epc);
161  new_tag.power = atof(rx_pwr);
162  obs.sensorLabel = m_sensorLabel;
163 
164  //std::cout << "mrpt::hwdrivers::CImpinjRFID::getObservation() " << "\n\tRXPWR: " << atof(rx_pwr) << " PWR READ: " << rx_pwr << std::endl;
165  }
166  if (receivedSomething)
167  return true;
168  else
169  return false;
170 
171  }
172  catch (std::exception &e)
173  {
174  std::cerr << e.what() << std::endl;
175  return false;
176  }
177 }
178 
179 /*---------------------------------------------------------------
180  getObservation
181  Get the power of a given network as an observation
182  ---------------------------------------------------------------*/
184 {
185  char cmd[20];
186  // send a kill command to the device interface program
187  strcpy(cmd, "END\0");
188  client->writeAsync(cmd,10); // JL->Emil: Why 10 not strlen(cmd)? Is the server expecting a fixed length data block?
189  client->close();
190 }
191 
192 
194 
195  mrpt::obs::CObservationRFIDPtr obs = mrpt::obs::CObservationRFID::Create();
196  if(getObservation(*obs))
197  appendObservation(obs);
198 }
std::string antennaPort
Port of the antenna that did the reading.
static CObservationRFIDPtr Create()
OBSERVATION_T::Ptr getObservation(mrpt::obs::CSensoryFramePtr &observations, mrpt::obs::CObservationPtr &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:32
This namespace provides a OS-independent interface to many useful functions: filenames manipulation...
Definition: math_frwds.h:29
This class implements an interface to an Impinj RFID reader.
Definition: CImpinjRFID.h:25
Contains classes for various device interfaces.
std::string read_string(const std::string &section, const std::string &name, const std::string &defaultValue, bool failIfNotFound=false) const
void initialize()
This method can or cannot be implemented in the derived class, depending on the need for it...
Definition: CImpinjRFID.cpp:33
char BASE_IMPEXP * strcpy(char *dest, size_t destSize, const char *source) MRPT_NO_THROWS
An OS-independent version of strcpy.
Definition: os.cpp:296
A TCP socket that can be wait for client connections to enter.
TThreadHandle createThread(void(*func)(T), T param)
Creates a new thread from a function (or static method) with one generic parameter.
Definition: threads.h:181
This class allows loading and storing values and vectors of different types from a configuration text...
int read_int(const std::string &section, const std::string &name, int defaultValue, bool failIfNotFound=false) const
#define MRPT_END
void BASE_IMPEXP sleep(int time_ms) MRPT_NO_THROWS
An OS-independent method for sending the current thread to "sleep" for a given period of time...
Definition: threads.cpp:57
std::string epc
EPC code of the observed tag.
void doProcess()
This method will be invoked at a minimum rate of "process_rate" (Hz)
void startDriver()
start the external driver
Definition: CImpinjRFID.cpp:55
double power
The power or signal strength as sensed by the RFID receiver (in dBm)
void loadConfig_sensorSpecific(const mrpt::utils::CConfigFileBase &configSource, const std::string &section)
Loads specific configuration for the device from a given source of configuration parameters, for example, an ".ini" file, loading from the section "[iniSection]" (see utils::CConfigFileBase and derived classes)
Definition: CImpinjRFID.cpp:74
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
GLsizei const GLchar ** string
Definition: glext.h:3919
#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:50
#define MRPT_START
std::string sensorLabel
An arbitrary label that can be used to identify the sensor.
void connect()
Connect to the reader.
bool getObservation(mrpt::obs::CObservationRFID &obs)
Gets the information of the tags as a timestamped observation NOTE: Deprecated, use getObservations i...
std::vector< TTagReading > tag_readings
The vector of individual tag observations.
Each of the individual readings of a RFID tag.
This represents one or more RFID tags observed by a receiver.
char BASE_IMPEXP * strtok(char *str, const char *strDelimit, char **context) MRPT_NO_THROWS
An OS-independent method for tokenizing a string.
void BASE_IMPEXP exitThread() MRPT_NO_THROWS
Explicit close of the current (running) thread.
Definition: threads.cpp:526
void closeReader()
Close the connection to the reader.



Page generated by Doxygen 1.8.14 for MRPT 1.5.5 Git: e06b63dbf Fri Dec 1 14:41:11 2017 +0100 at lun oct 28 01:31:35 CET 2019