bta_tof_driver.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (c) 2016
3  * VoXel Interaction Design GmbH
4  *
5  * @author Angel Merino Sastre
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  ******************************************************************************/
26 
44 
45 namespace bta_tof_driver
46 {
47 
49  ros::NodeHandle nh_private,
50  std::string nodeName) :
51  nh_(nh_camera),
52  nh_private_(nh_private),
53  it_(nh_camera),
54  cim_tof_(nh_camera),
55  nodeName_(nodeName),
56  config_init_(false),
57  _xyz (new sensor_msgs::PointCloud2)
58 {
59  //Set log to debug to test capturing. Remove if not needed.
60  /*
61  if( ros::console::set_logger_level(
62  ROSCONSOLE_DEFAULT_NAME, ros::console::levels::Info) )
63  {
64  ros::console::notifyLoggerLevelsChanged();
65  }*/
66  transformStamped.header.stamp = ros::Time::now();
67  transformStamped.header.frame_id = "world";
68  transformStamped.child_frame_id = "cloud";
69  transformStamped.transform.translation.x = 0.0;
70  transformStamped.transform.translation.y = 0.0;
71  transformStamped.transform.translation.z = 0.0;
73  q.setRPY(0, 0, 0);
74  transformStamped.transform.rotation.x = q.x();
75  transformStamped.transform.rotation.y = q.y();
76  transformStamped.transform.rotation.z = q.z();
77  transformStamped.transform.rotation.w = q.w();
79 }
80 
82 {
83  ROS_INFO("closing.");
84  close();
85 }
86 
88 {
89  ROS_DEBUG("Close called");
90  if (BTAisConnected(handle_)) {
91  ROS_DEBUG("Closing..");
92  BTA_Status status;
93  status = BTAclose(&handle_);
94  printf("done: %d \n", status);
95  }
96 
97  return;
98 }
99 
100 
101 void BtaRos::callback(bta_tof_driver::bta_tof_driverConfig &config_, uint32_t level)
102 {
103  BTA_Status status;
104  // Check the configuretion parameters with those given in the initialization
105  int it;
106  double fr;
107  uint32_t usValue;
108 
109  if (!config_init_) {
110  if (nh_private_.getParam(nodeName_+"/integrationTime",it)) {
111  usValue = (uint32_t)it;
112  status = BTAsetIntegrationTime(handle_, usValue);
113  if (status != BTA_StatusOk)
114  ROS_WARN_STREAM("Error setting IntegrationTime:: " << status << "---------------");
115  } else {
116  status = BTAgetIntegrationTime(handle_, &usValue);
117  if (status != BTA_StatusOk)
118  ROS_WARN_STREAM("Error reading IntegrationTime: " << status << "---------------");
119  else
120  nh_private_.setParam(nodeName_+"/integrationTime", (int)usValue);
121  }
122  nh_private_.getParam(nodeName_+"/integrationTime",config_.Integration_Time);
123 
124  if (nh_private_.getParam(nodeName_+"/frameRate",fr)) {
125  status = BTAsetFrameRate(handle_, fr);
126  if (status != BTA_StatusOk)
127  ROS_WARN_STREAM("Error setting FrameRate: " << status << "---------------");
128  } else {
129  float fr_f;
130  status = BTAgetFrameRate(handle_, &fr_f);
131  fr = fr_f;
132  if (status != BTA_StatusOk)
133  ROS_WARN_STREAM("Error reading FrameRate: " << status << "---------------");
134  else
135  nh_private_.setParam(nodeName_+"/frameRate", fr);
136  }
137  nh_private_.getParam(nodeName_+"/frameRate",config_.Frame_rate);
138  config_init_ = true;
139  return;
140  }
141 
142 
143  nh_private_.getParam(nodeName_+"/integrationTime",it);
144  if(it != config_.Integration_Time) {
145  usValue = (uint32_t)config_.Integration_Time;
146  status = BTAsetIntegrationTime(handle_, usValue);
147  if (status != BTA_StatusOk)
148  ROS_WARN_STREAM("Error setting IntegrationTime: " << status << "---------------");
149  else
150  nh_private_.setParam(nodeName_+"/integrationTime", config_.Integration_Time);
151  }
152 
153  nh_private_.getParam(nodeName_+"/frameRate",fr);
154  if(fr != config_.Frame_rate) {
155  usValue = (uint32_t)config_.Frame_rate;
156  status = BTAsetFrameRate(handle_, usValue);
157  if (status != BTA_StatusOk)
158  ROS_WARN_STREAM("Error setting FrameRate: " << status << "---------------");
159  else
160  nh_private_.setParam(nodeName_+"/frameRate", config_.Frame_rate);
161  }
162 
163  if(config_.Read_reg) {
164  try {
165  std::stringstream ss;
166  it = strtoul(config_.Reg_addr.c_str(), NULL, 0);
167  status = BTAreadRegister(handle_, it, &usValue, 0);
168  if (status != BTA_StatusOk) {
169  ROS_WARN_STREAM("Could not read reg: " << config_.Reg_addr << ". Status: " << status);
170  }
171  ss<<"";
172  ss << "0x"<< std::hex << usValue;
173  ss >> config_.Reg_val;
174  ROS_INFO_STREAM("Read register: " << config_.Reg_addr << ". Value: " << ss.str());
175  } catch(const boost::bad_lexical_cast &) {
176  ROS_WARN_STREAM("Wrong address: " << config_.Reg_addr << " " << it);
177  }
178  config_.Read_reg = false;
179  }
180  if(config_.Write_reg) {
181  //std::stringstream ss;
182  it = strtoul(config_.Reg_addr.c_str(), NULL, 0);
183  usValue = strtoul(config_.Reg_val.c_str(), NULL, 0);
184 
185  status = BTAwriteRegister(handle_, it, &usValue, 0);
186  if (status != BTA_StatusOk) {
187  ROS_WARN_STREAM("Could not write reg: " <<
188  config_.Reg_addr <<
189  " with val: " <<
190  config_.Reg_val <<
191  ". Status: " << status);
192  }
193  ROS_INFO_STREAM("Written register: " << config_.Reg_addr << ". Value: " << config_.Reg_val);
194  BTAgetIntegrationTime(handle_, &usValue);
195  config_.Integration_Time = usValue;
196  BTAsetFrameRate(handle_, fr);
197  config_.Frame_rate = fr;
198  config_.Write_reg = false;
199 
200  }
201 
202 }
203 
204 size_t BtaRos::getDataSize(BTA_DataFormat dataFormat) {
205  switch (dataFormat) {
206  case BTA_DataFormatUInt16:
207  return sizeof(uint16_t);
208  break;
209  case BTA_DataFormatSInt16:
210  return sizeof(int16_t);
211  break;
212  case BTA_DataFormatFloat32:
213  return sizeof(float);
214  break;
215  }
216 }
217 
218 std::string BtaRos::getDataType(BTA_DataFormat dataFormat) {
219  switch (dataFormat) {
220  case BTA_DataFormatUInt16:
222  break;
223  case BTA_DataFormatSInt16:
225  break;
226  case BTA_DataFormatFloat32:
228  break;
229  }
230 }
231 
232 float BtaRos::getUnit2Meters(BTA_Unit unit) {
233  //ROS_INFO_STREAM("BTA_Unit: " << unit);
234  switch (unit) {
235  /*case BTA_UnitCentimeter:
236  return 1/100.;
237  break;*/
238  case BTA_UnitMillimeter:
239  return 1/1000.;
240  break;
241  default:
242  return 1.0;
243  break;
244  }
245 }
246 
248 {
249  if (
250  (pub_amp_.getNumSubscribers() == 0) &&
251  (pub_dis_.getNumSubscribers() == 0) &&
252  (pub_xyz_.getNumSubscribers() == 0)
253  ) return;
254 
255  BTA_Status status;
256 
257  BTA_Frame *frame;
258  status = BTAgetFrame(handle_, &frame, 3000);
259  if (status != BTA_StatusOk) {
260  return;
261  }
262 
263  ROS_DEBUG(" frameArrived FrameCounter %d", frame->frameCounter);
264 
265  BTA_DataFormat dataFormat;
266  BTA_Unit unit;
267  uint16_t xRes, yRes;
268  sensor_msgs::CameraInfoPtr ci_tof(new sensor_msgs::CameraInfo(cim_tof_.getCameraInfo()));
269  ci_tof->header.frame_id = nodeName_+"/tof_camera";
270 
271 
272  void *distances;
273  status = BTAgetDistances(frame, &distances, &dataFormat, &unit, &xRes, &yRes);
274  if (status == BTA_StatusOk) {
275  sensor_msgs::ImagePtr dis (new sensor_msgs::Image);
276  dis->header.seq = frame->frameCounter;
277  dis->header.stamp.sec = frame->timeStamp;
278  dis->height = yRes;
279  dis->width = xRes;
280  dis->encoding = getDataType(dataFormat);
281  dis->step = xRes*getDataSize(dataFormat);
282  dis->data.resize(xRes*yRes*getDataSize(dataFormat));
283  memcpy ( &dis->data[0], distances, xRes*yRes*getDataSize(dataFormat) );
284 
285  dis->header.frame_id = "distances";
286  pub_dis_.publish(dis,ci_tof);
287  }
288 
289  bool ampOk = false;
290  void *amplitudes;
291  BTA_DataFormat amDataFormat;
292  status = BTAgetAmplitudes(frame, &amplitudes,
293  &amDataFormat, &unit, &xRes, &yRes);
294  if (status == BTA_StatusOk) {
295  sensor_msgs::ImagePtr amp (new sensor_msgs::Image);
296  amp->header.seq = frame->frameCounter;
297  amp->header.stamp.sec = frame->timeStamp;
298  amp->height = yRes;
299  amp->width = xRes;
300  amp->encoding = getDataType(amDataFormat);
301  amp->step = xRes*getDataSize(amDataFormat);
302  amp->data.resize(xRes*yRes*getDataSize(amDataFormat));
303  memcpy ( &amp->data[0], amplitudes, xRes*yRes*getDataSize(amDataFormat) );
304 
305  amp->header.frame_id = "amplitudes";//nodeName_+"/tof_camera";
306  pub_amp_.publish(amp,ci_tof);
307  ampOk = true;
308  }
309 
310  void *xCoordinates, *yCoordinates, *zCoordinates;
311  status = BTAgetXYZcoordinates(frame, &xCoordinates, &yCoordinates, &zCoordinates, &dataFormat, &unit, &xRes, &yRes);
312  if (status == BTA_StatusOk) {
313  if (_xyz->width != xRes || _xyz->height != yRes || _xyz->fields.size() != 4) {
314  _xyz->width = xRes;
315  _xyz->height = yRes;
317  modifier.setPointCloud2Fields(4, "x", 1, sensor_msgs::PointField::FLOAT32,
318  "y", 1, sensor_msgs::PointField::FLOAT32,
319  "z", 1, sensor_msgs::PointField::FLOAT32,
320  "intensity", 1, sensor_msgs::PointField::UINT16);
321  modifier.resize(_xyz->height * _xyz->width);
322  _xyz->header.frame_id = "cloud";
323  _xyz->is_dense = true;
324  }
325  //if (_cloud.size() != yRes*xRes) {
326  // _cloud.resize(yRes*xRes);
327  //}
328  /* else {
329  pub_xyz_.publish(_xyz);
330  return;
331  }*/
332  float conv = getUnit2Meters(unit);
337  if (dataFormat == BTA_DataFormatSInt16) {
338  /*short *xC, *yC, *zC;
339  xC = (short *)xCoordinates;
340  yC = (short *)yCoordinates;
341  zC = (short *)zCoordinates;*/
342  for (size_t i = 0; i < yRes*xRes; i++, ++_x, ++_y, ++_z, ++_i) {
343  *_x = static_cast<short *>(xCoordinates)[i]*conv;
344  *_y = static_cast<short *>(yCoordinates)[i]*conv;
345  *_z = static_cast<short *>(zCoordinates)[i]*conv;
346  if (ampOk) {
347  if(amDataFormat == BTA_DataFormatUInt16) {
348  *_i = static_cast<unsigned short *>(amplitudes)[i];
349  } else if(amDataFormat == BTA_DataFormatFloat32) {
350  *_i = static_cast<float *>(amplitudes)[i];
351  }
352  } else
353  *_i = 255;
354  }
355  } else if (dataFormat == BTA_DataFormatFloat32) {
356  for (size_t i = 0; i < yRes*xRes; i++, ++_x, ++_y, ++_z, ++_i) {
357  *_x = static_cast<float *>(xCoordinates)[i]*conv;
358  *_y = static_cast<float *>(yCoordinates)[i]*conv;
359  *_z = static_cast<float *>(zCoordinates)[i]*conv;
360  if (ampOk) {
361  if(amDataFormat == BTA_DataFormatUInt16) {
362  *_i = static_cast<unsigned short *>(amplitudes)[i];
363  } else if(amDataFormat == BTA_DataFormatFloat32) {
364  *_i = static_cast<float *>(amplitudes)[i];
365  }
366  } else
367  *_i = 255;
368  }
369  } else {
370  ROS_WARN_STREAM("Unhandled BTA_DataFormat: " << dataFormat);
371  return;
372  }
373  //pcl::toROSMsg(_cloud, *_xyz);
374 
375  _xyz->header.seq = frame->frameCounter;
376  _xyz->header.stamp.sec = frame->timeStamp;
377 
378  //Keeping until resolving problem with rviz
379  /*
380  xyz->width = xRes;
381  xyz->height = yRes;
382  sensor_msgs::PointCloud2Modifier modifier(*xyz);
383  ROS_DEBUG_STREAM("MAL? " << unit);
384 
385  modifier.setPointCloud2Fields(3, "x", 1, sensor_msgs::PointField::INT16,
386  "y", 1, sensor_msgs::PointField::INT16,
387  "z", 1, sensor_msgs::PointField::INT16/*,
388  "intensity", 1, sensor_msgs::PointField::UINT16* /);
389  ROS_DEBUG_STREAM("MAL? " << modifier.size());
390  short *xC, *yC, *zC;
391  xC = (short *)xCoordinates;
392  yC = (short *)yCoordinates;
393  zC = (short *)zCoordinates;
394  std::vector<short> test;
395  for (size_t i = 0; i < yRes*xRes; i++) {
396  test.push_back(xC[i]);
397  test.push_back(yC[i]);
398  test.push_back(zC[i]);
399  //memcpy ( &xyz->data[i*xyz->point_step], &xCoordinates[i*getDataSize(dataFormat)], getDataSize(dataFormat) );
400  //memcpy ( &xyz->data[i*xyz->point_step+getDataSize(dataFormat) ], &yCoordinates[i*getDataSize(dataFormat)], getDataSize(dataFormat) );
401  //memcpy ( &xyz->data[i*xyz->point_step+2*getDataSize(dataFormat) ], &zCoordinates[i*getDataSize(dataFormat)], getDataSize(dataFormat) );
402  //memcpy ( &xyz->data[i*xyz->point_step+3*getDataSize(dataFormat)], &amplitudes[i*getDataSize(amDataFormat)], getDataSize(amDataFormat) );
403  //ROS_DEBUG_STREAM("ox: " << (short)xCoordinates[i*getDataSize(dataFormat)] << " y: " << (short)yCoordinates[i*getDataSize(dataFormat)] << " z: " << (short)zCoordinates[i*getDataSize(dataFormat)]);
404  //ROS_DEBUG_STREAM("x: " << (short)xyz->data[i*xyz->point_step] << " y: " << (short)xyz->data[i*xyz->point_step+getDataSize(dataFormat) ] << " z: " << (short)xyz->data[i*xyz->point_step+2*getDataSize(dataFormat) ]);
405  }
406  memcpy ( &xyz->data[0], &test[0], test.size());
407  ROS_DEBUG_STREAM("MAL? " << modifier.size());
408  */
409 
411  }
412 
413  BTAfreeFrame(&frame);
414 }
415 
416 /*void BtaRos::ampCb(const sensor_msgs::ImagePtr& amp)
417  {
418  sensor_msgs::CameraInfoPtr ci_tof(new sensor_msgs::CameraInfo(cim_tof_.getCameraInfo()));
419  ci_tof->header.frame_id = nodeName_+"/tof_camera";
420  amp->header.frame_id = nodeName_+"/tof_camera";
421 
422  pub_amp_.publish(amp,ci_tof);
423  }
424 
425  void BtaRos::disCb(const sensor_msgs::ImagePtr& dis)
426  {
427  sensor_msgs::CameraInfoPtr ci_tof(new sensor_msgs::CameraInfo(cim_tof_.getCameraInfo()));
428  ci_tof->header.frame_id = nodeName_+"/tof_camera";
429  dis->header.frame_id = nodeName_+"/tof_camera";
430 
431  pub_dis_.publish(dis,ci_tof);
432  }*/
433 
435  int iusValue;
436  if(nh_private_.getParam(nodeName_+"/udpDataIpAddrLen",iusValue)) {
437  config_.udpDataIpAddrLen = (uint8_t)iusValue;
438  ROS_DEBUG_STREAM("config_.udpDataIpAddrLen: " << (int)config_.udpDataIpAddrLen);
439  }
440  if (nh_private_.hasParam(nodeName_+"/udpDataIpAddr")) {
441  ROS_DEBUG_STREAM("config_.udpDataIpAddr:");
442  for (int i = 1; i <= config_.udpDataIpAddrLen; i++) {
443  std::ostringstream to_string;
444  to_string << "";
445  to_string << nodeName_ << "/udpDataIpAddr/n" << i;
446  nh_private_.getParam(to_string.str(),iusValue);
447  udpDataIpAddr_[i-1] = (uint8_t)iusValue;
448  ROS_DEBUG_STREAM((int)udpDataIpAddr_[i-1] << ",");
449  }
450  config_.udpDataIpAddr = udpDataIpAddr_;
451  }
452  if(nh_private_.getParam(nodeName_+"/udpDataPort",iusValue)) {
453  config_.udpDataPort = (uint16_t)iusValue;
454  ROS_DEBUG_STREAM("config_.udpDataPort: " << config_.udpDataPort);
455  }
456 
457  if(nh_private_.getParam(nodeName_+"/udpControlOutIpAddrLen",iusValue)) {
458  config_.udpControlOutIpAddrLen = (uint8_t)iusValue;
459  ROS_DEBUG_STREAM("config_.udpControlOutIpAddrLen: " << (int)config_.udpControlOutIpAddrLen);
460  }
461  if (nh_private_.hasParam(nodeName_+"/udpControlOutIpAddr")) {
462  ROS_DEBUG_STREAM("config_.udpControlOutIpAddr:");
463  for (int i = 1; i <= config_.udpControlOutIpAddrLen; i++) {
464  std::ostringstream to_string;
465  to_string << "";
466  to_string << nodeName_ << "/udpControlOutIpAddr/n" << i;
467  nh_private_.getParam(to_string.str(),iusValue);
468  udpControlOutIpAddr_[i-1] = (uint8_t)iusValue;
469  ROS_DEBUG_STREAM((int)udpControlOutIpAddr_[i-1] << ",");
470  }
471  config_.udpControlOutIpAddr = udpControlOutIpAddr_;
472  }
473  if(nh_private_.getParam(nodeName_+"/udpControlPort",iusValue)) {
474  config_.udpControlPort = (uint16_t)iusValue;
475  ROS_DEBUG_STREAM("config_.udpControlPort: " << (int)config_.udpControlPort);
476  }
477 
478  if(nh_private_.getParam(nodeName_+"/udpControlInIpAddrLen",iusValue)) {
479  config_.udpControlInIpAddrLen = (uint8_t)iusValue;
480  ROS_DEBUG_STREAM("config_.udpControlInIpAddrLen: " << (int)config_.udpControlInIpAddrLen);
481  }
482  if (nh_private_.hasParam(nodeName_+"/udpControlInIpAddr")) {
483 
484  ROS_DEBUG_STREAM("config_.udpControlInIpAddr:");
485  for (int i = 1; i <= config_.udpControlInIpAddrLen; i++) {
486  std::ostringstream to_string;
487  to_string << "";
488  to_string << nodeName_ << "/udpControlInIpAddr/n" << i;
489  nh_private_.getParam(to_string.str(),iusValue);
490  udpControlInIpAddr_[i-1] = (uint8_t)iusValue;
491  ROS_DEBUG_STREAM((int)udpControlInIpAddr_[i-1] << ",");
492  }
493  config_.udpControlInIpAddr = udpControlInIpAddr_;
494  }
495  if(nh_private_.getParam(nodeName_+"/udpControlCallbackPort",iusValue)) {
496  config_.udpControlCallbackPort = (uint16_t)iusValue;
497  ROS_DEBUG_STREAM("config_.udpControlCallbackPort: " << (int)config_.udpControlCallbackPort);
498  }
499 
500  if(nh_private_.getParam(nodeName_+"/tcpDeviceIpAddrLen",iusValue)) {
501  config_.tcpDeviceIpAddrLen = (uint8_t)iusValue;
502  ROS_DEBUG_STREAM("config_.tcpDeviceIpAddrLen: " << (int)config_.tcpDeviceIpAddrLen);
503  }
504  if (nh_private_.hasParam(nodeName_+"/tcpDeviceIpAddr")) {
505  ROS_DEBUG_STREAM("TCP address:");
506  for (int i = 1; i <= config_.tcpDeviceIpAddrLen; i++) {
507  std::ostringstream to_string;
508  to_string << "";
509  to_string << nodeName_ << "/tcpDeviceIpAddr/n" << i;
510  nh_private_.getParam(to_string.str(),iusValue);
511  tcpDeviceIpAddr_[i-1] = (uint8_t)iusValue;
512  ROS_DEBUG_STREAM((int)tcpDeviceIpAddr_[i-1] << ",");
513  }
514  config_.tcpDeviceIpAddr = tcpDeviceIpAddr_;
515  }
516  if(nh_private_.getParam(nodeName_+"/tcpControlPort",iusValue)) {
517  config_.tcpControlPort = (uint16_t)iusValue;
518  ROS_DEBUG_STREAM("config_.tcpControlPort: " << config_.tcpControlPort);
519  }
520 
521  if (nh_private_.getParam(nodeName_+"/tcpDataPort",iusValue))
522  config_.tcpDataPort = (uint16_t)iusValue;
523 
524  nh_private_.getParam(nodeName_+"/uartPortName",uartPortName_);
525  config_.uartPortName = (uint8_t *)uartPortName_.c_str();
526 
527  if(nh_private_.getParam(nodeName_+"/uartBaudRate",iusValue))
528  config_.uartBaudRate = (uint32_t)iusValue;
529  if(nh_private_.getParam(nodeName_+"/uartDataBits",iusValue))
530  config_.uartDataBits = (uint8_t)iusValue;
531  if(nh_private_.getParam(nodeName_+"/uartStopBits",iusValue))
532  config_.uartStopBits = (uint8_t)iusValue;
533  if(nh_private_.getParam(nodeName_+"/uartParity",iusValue))
534  config_.uartParity = (uint8_t)iusValue;
535  if(nh_private_.getParam(nodeName_+"/uartTransmitterAddress",iusValue))
536  config_.uartTransmitterAddress = (uint8_t)iusValue;
537  if(nh_private_.getParam(nodeName_+"/uartReceiverAddress",iusValue))
538  config_.uartReceiverAddress = (uint8_t)iusValue;
539  if(nh_private_.getParam(nodeName_+"/serialNumber",iusValue))
540  config_.serialNumber = (uint32_t)iusValue;
541 
542  nh_private_.getParam(nodeName_+"/calibFileName",calibFileName_);
543  config_.calibFileName = (uint8_t *)calibFileName_.c_str();
544 
545  int32_t frameMode;
546  if (nh_private_.getParam(nodeName_+"/frameMode",frameMode))
547  config_.frameMode = (BTA_FrameMode)frameMode;
548 
549  if (nh_private_.getParam(nodeName_+"/verbosity",iusValue))
550  config_.verbosity = (uint8_t)iusValue;
551 
552  if(nh_private_.getParam(nodeName_+"/frameQueueLength",iusValue))
553  config_.frameQueueLength = (uint16_t)iusValue;
554 
555  int32_t frameQueueMode;
556  if (nh_private_.getParam(nodeName_+"/frameQueueMode",frameQueueMode))
557  config_.frameQueueMode = (BTA_QueueMode)frameQueueMode;
558 
559 #if !defined(BTA_ETH) || !defined(BTA_P100)
560  int32_t deviceType;
561  if(nh_private_.getParam(nodeName_+"/deviceType",deviceType))
562  config_.deviceType = (BTA_DeviceType)deviceType;
563 #endif
564 
565  //config_.frameArrived = &frameArrived;
566  config_.infoEvent = &infoEventCb;
567 }
568 
569 
570 
572  BTA_Status status;
573  BTA_DeviceInfo *deviceInfo;
574 
575  // Init camera connection
576  //ros::Duration().sleep();
577  status = (BTA_Status)-1;
578  for (int i=0; i<10; i++) {
579  ROS_INFO_STREAM("Connecting... try " << i+1);
580  status = BTAopen(&config_, &handle_);
581  if (status != BTA_StatusOk) {
582  if (!nh_private_.ok() && ros::isShuttingDown())
583  return -1;
584  ROS_WARN_STREAM("Could not connect to the camera. status: " << status);
585  continue;
586  }
587  break;
588  }
589  if (!BTAisConnected(handle_)) {
590  ROS_WARN_STREAM("Could not connect to the camera.");
591  return -1;
592  }
593 
594  ROS_INFO_STREAM("Camera connected sucessfully. status: " << status);
595  status = BTAgetDeviceInfo(handle_, &deviceInfo);
596  if (status != BTA_StatusOk) {
597  ROS_WARN_STREAM("Could not get device info. status: " << status);
598  return status;
599  }
600 
601  ROS_INFO_STREAM("Retrieved device info: \n"
602  << "deviceType: " << deviceInfo->deviceType << "\n"
603  << "serialNumber: " << deviceInfo->serialNumber << "\n"
604  << "firmwareVersionMajor: " << deviceInfo->firmwareVersionMajor << "\n"
605  << "firmwareVersionMinor: " << deviceInfo->firmwareVersionMinor << "\n"
606  << "firmwareVersionNonFunc: " << deviceInfo->firmwareVersionNonFunc
607  << "\n");
608 
609  ROS_INFO_STREAM("Service running: " << (int)BTAisRunning(handle_));
610  ROS_INFO_STREAM("Connection up: " << (int)BTAisConnected(handle_));
611 
612  BTAfreeDeviceInfo(deviceInfo);
613  return 1;
614 }
615 
617 {
618 
619  /*
620  * Camera config
621  */
622 
623  BTAinitConfig(&config_);
624 
625  parseConfig();
626 
627  /*
628  * Camera Initialization
629  */
630  ROS_DEBUG_STREAM("Config Readed sucessfully");
631 
632  BTA_Status status;
633  if (connectCamera() < 0)
634  return -1;
635 
637  reconfigure_server_->setCallback(boost::bind(&BtaRos::callback, this, _1, _2));
638 
639  while (!config_init_)
640  {
641  ROS_DEBUG("Waiting for dynamic reconfigure configuration.");
642  boost::this_thread::sleep(boost::posix_time::seconds(0.1));
643  }
644  ROS_DEBUG("Dynamic reconfigure configuration received.");
645 
646  /*
647  * ROS Node Initialization
648  */
649  {
650  // Advertise all published topics
653  /*camera_info_url_*/"package://bta_tof_driver/calib.yml")) {
654  cim_tof_.loadCameraInfo("package://bta_tof_driver/calib.yml");
655  ROS_INFO_STREAM("Loaded camera calibration from " <<
656  "package://bta_tof_driver/calib.yml" );
657  } else {
658  ROS_WARN_STREAM("Camera info at: " <<
659  "package://bta_tof_driver/calib.yml" <<
660  " not found. Using an uncalibrated config_.");
661  }
662 
663  pub_amp_ = it_.advertiseCamera(nodeName_ + "/tof_camera/image_raw", 1);
664  pub_dis_ = it_.advertiseCamera(nodeName_ + "/tof_camera/compressedDepth", 1);
665  pub_xyz_ = nh_private_.advertise<sensor_msgs::PointCloud2> (nodeName_ + "/tof_camera/point_cloud_xyz", 1);
666 
667  //sub_amp_ = nh_private_.subscribe("bta_node_amp", 1, &BtaRos::ampCb, this);
668  //sub_dis_ = nh_private_.subscribe("bta_node_dis", 1, &BtaRos::disCb, this);
669  }
670 
671  while (nh_private_.ok() && !ros::isShuttingDown()) {
672  if (!BTAisConnected(handle_)) {
673  ROS_WARN_STREAM("The camera got disconnected." << BTAisConnected(handle_));
674  if (connectCamera() < 0)
675  break;
676  }
677 
678  publishData();
679  ros::spinOnce ();
680  }
681  return 0;
682 }
683 }
684 
image_transport::CameraPublisher pub_amp_
void publish(const sensor_msgs::Image &image, const sensor_msgs::CameraInfo &info) const
void close()
Closes the connection to the device.
sensor_msgs::CameraInfo getCameraInfo(void)
void publish(const boost::shared_ptr< M > &message) const
camera_info_manager::CameraInfoManager cim_tof_
bool loadCameraInfo(const std::string &url)
uint32_t getNumSubscribers() const
static void BTA_CALLCONV infoEventCb(BTA_EventId eventId, int8_t *msg)
ros::NodeHandle nh_private_
geometry_msgs::TransformStamped transformStamped
boost::shared_ptr< ReconfigureServer > reconfigure_server_
image_transport::CameraPublisher pub_dis_
void setPointCloud2Fields(int n_fields,...)
sensor_msgs::PointCloud2Ptr _xyz
virtual ~BtaRos()
Class destructor.
int connectCamera()
Helper for connect to the device.
bool validateURL(const std::string &url)
size_t getDataSize(BTA_DataFormat dataFormat)
Returns the size of the data based in BTA_DataFormat.
CameraPublisher advertiseCamera(const std::string &base_topic, uint32_t queue_size, bool latch=false)
void setRPY(const tfScalar &roll, const tfScalar &pitch, const tfScalar &yaw)
void publishData()
Publish the data based on set up parameters.
#define ROS_INFO(...)
const std::string TYPE_32FC1
const std::string TYPE_16UC1
ROSCPP_DECL bool isShuttingDown()
dynamic_reconfigure::Server< Config > ReconfigureServer
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
void parseConfig()
Reads configuration from the server parameters.
image_transport::ImageTransport it_
uint32_t getNumSubscribers() const
#define ROS_INFO_STREAM(args)
tf2_ros::StaticTransformBroadcaster pub_tf
bool getParam(const std::string &key, std::string &s) const
float getUnit2Meters(BTA_Unit unit)
Gives the conversion value to meters from the BTA_Unit.
static Time now()
bool ok() const
bool hasParam(const std::string &key) const
void sendTransform(const geometry_msgs::TransformStamped &transform)
ROSCPP_DECL void spinOnce()
BtaRos(ros::NodeHandle nh_camera, ros::NodeHandle nh_private, std::string nodeName)
Class constructor.
void setParam(const std::string &key, const XmlRpc::XmlRpcValue &v) const
bool setCameraName(const std::string &cname)
void callback(bta_tof_driver::bta_tof_driverConfig &config, uint32_t level)
Callback for rqt_reconfigure. It is called any time we change a parameter in the visual interface...
#define ROS_DEBUG(...)
const std::string TYPE_16SC1
std::string getDataType(BTA_DataFormat dataFormat)
Returns the data encoding flat based in BTA_DataFormat.
int initialize()
Initializes the device and parameters.


bta_tof_driver
Author(s): Angel Merino , Simon Vogl
autogenerated on Fri Jun 21 2019 19:57:15