00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "thing_reader.h"
00023 #include <string>
00024
00025
00026
00027
00028
00029
00030 #ifndef USE_TRANSPORT_LISTENER
00031 #define USE_TRANSPORT_LISTENER 0
00032 #endif
00033
00034 #define usage() {errx(1, "Please provide reader URL, such as:\n"\
00035 "tmr:///com4 or tmr:///com4 --ant 1,2\n"\
00036 "tmr://my-reader.example.com or tmr://my-reader.example.com --ant 1,2\n");}
00037
00038 namespace TMR
00039 {
00040 ThingReader::ThingReader()
00041 {
00042
00043
00044 }
00045 ThingReader::~ThingReader()
00046 {
00047
00048 }
00049 int ThingReader::run()
00050 {
00051
00052
00053 ros::Time::init();
00054 ros::NodeHandle node;
00055 ros::NodeHandle private_nh("~");
00056
00057 ROS_INFO("Thingmagic Reader");
00058
00059
00060
00061 std::string port;
00062 private_nh.param("port", port, std::string("/dev/ttyACM0"));
00063 std::string uri = "tmr://"+port;
00064
00065 this->string_pub_ = node.advertise<std_msgs::String>("rfid",100);
00066
00067
00068 #ifndef TMR_ENABLE_BACKGROUND_READS
00069 errx(1, "This sample requires background read functionality.\n"
00070 "Please enable TMR_ENABLE_BACKGROUND_READS in tm_config.h\n"
00071 "to run this codelet\n");
00072 return -1;
00073 #else
00074 TMR_Reader r, *rp;
00075 TMR_Status ret;
00076 TMR_Region region;
00077 TMR_ReadPlan plan;
00078 TMR_ReadListenerBlock rlb;
00079 TMR_ReadExceptionListenerBlock reb;
00080 uint8_t *antennaList = NULL;
00081 uint8_t buffer[20];
00082 uint8_t i;
00083 uint8_t antennaCount = 0x0;
00084 TMR_String model;
00085 char str[64];
00086
00087 rp = &r;
00088
00089 ROS_INFO("Creating reader object with URI <%s>",uri.c_str());
00090 ret = TMR_create(rp,uri.c_str());
00091 checkerr(rp, ret, 1, "creating reader");
00092
00093
00094 ret = TMR_connect(rp);
00095 checkerr(rp, ret, 1, "connecting reader");
00096
00097 region = TMR_REGION_NONE;
00098 ret = TMR_paramGet(rp, TMR_PARAM_REGION_ID, ®ion);
00099 checkerr(rp, ret, 1, "getting region");
00100
00101 if (TMR_REGION_NONE == region){
00102 TMR_RegionList regions;
00103 TMR_Region _regionStore[32];
00104 regions.list = _regionStore;
00105 regions.max = sizeof(_regionStore)/sizeof(_regionStore[0]);
00106 regions.len = 0;
00107
00108 ret = TMR_paramGet(rp, TMR_PARAM_REGION_SUPPORTEDREGIONS, ®ions);
00109 checkerr(rp, ret, __LINE__, "getting supported regions");
00110
00111 if (regions.len < 1){
00112 checkerr(rp, TMR_ERROR_INVALID_REGION, __LINE__, "Reader doesn't supportany regions");
00113 }
00114 region = regions.list[0];
00115 ret = TMR_paramSet(rp, TMR_PARAM_REGION_ID, ®ion);
00116 checkerr(rp, ret, 1, "setting region");
00117 }
00118
00119 model.value = str;
00120 model.max = 64;
00121 TMR_paramGet(rp, TMR_PARAM_VERSION_MODEL, &model);
00122 if (((0 == strcmp("Sargas", model.value)) || (0 == strcmp("M6e Micro", model.value)) ||(0 == strcmp("M6e Nano", model.value)))
00123 && (NULL == antennaList))
00124 {
00125 ROS_ERROR("Module doesn't has antenna detection support please provide antenna list\n");
00126 usage();
00127 }
00128
00136
00137 ret = TMR_RP_init_simple(&plan, antennaCount, antennaList, TMR_TAG_PROTOCOL_GEN2, 1000);
00138 checkerr(rp, ret, 1, "initializing the read plan");
00139
00140
00141 ret = TMR_paramSet(rp, TMR_PARAM_READ_PLAN, &plan);
00142 checkerr(rp, ret, 1, "setting read plan");
00143
00144 rlb.listener = callback_wrapper;
00145 rlb.cookie = this;
00146
00147 reb.listener = exceptionCallback;
00148 reb.cookie = NULL;
00149
00150 ret = TMR_addReadListener(rp, &rlb);
00151 checkerr(rp, ret, 1, "adding read listener");
00152
00153 ret = TMR_addReadExceptionListener(rp, &reb);
00154 checkerr(rp, ret, 1, "adding exception listener");
00155
00156 ret = TMR_startReading(rp);
00157 checkerr(rp, ret, 1, "starting reading");
00158
00159
00160 ros::spin();
00161
00162 ret = TMR_stopReading(rp);
00163 checkerr(rp, ret, 1, "stopping reading");
00164
00165 TMR_destroy(rp);
00166 return 0;
00167
00168 #endif
00169
00170
00171 }
00172
00173 void errx(int exitval, const char *fmt, ...)
00174 {
00175 va_list ap;
00176 va_start(ap, fmt);
00177 vfprintf(stderr, fmt, ap);
00178 ROS_FATAL(fmt,ap);
00179 exit(exitval);
00180 }
00181
00182 void checkerr(TMR_Reader* rp, TMR_Status ret, int exitval, const char *msg)
00183 {
00184 if (TMR_SUCCESS != ret)
00185 {
00186 errx(exitval, "Error %s: %s\n", msg, TMR_strerr(rp, ret));
00187 }
00188 }
00189
00190
00191 void callback_wrapper(TMR_Reader *reader, const TMR_TagReadData *t, void *cookie)
00192 {
00193 ThingReader* treader = (ThingReader*) cookie;
00194 treader->rfid_callback(reader,t,cookie);
00195 }
00196 void ThingReader::rfid_callback(TMR_Reader *reader, const TMR_TagReadData *t, void *cookie)
00197 {
00198 char epcStr[128];
00199 TMR_bytesToHex(t->tag.epc, t->tag.epcByteCount, epcStr);
00200 ROS_DEBUG("Background read: %s\n", epcStr);
00201 string_msg_.data = std::string(epcStr);
00202 string_pub_.publish(string_msg_);
00203 }
00204
00205
00206 void exceptionCallback(TMR_Reader *reader, TMR_Status error, void *cookie)
00207 {
00208 ROS_ERROR("Error:%s\n", TMR_strerr(reader, error));
00209 }
00210
00211
00212 }
00213
00214
00215
00216
00217