netft_node.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2008, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
40 #include "ros/ros.h"
41 #include "netft_rdt_driver.h"
42 #include "geometry_msgs/WrenchStamped.h"
43 #include "diagnostic_msgs/DiagnosticArray.h"
45 #include <std_msgs/Bool.h>
46 #include <unistd.h>
47 #include <iostream>
48 #include <memory>
49 #include <boost/program_options.hpp>
50 
51 namespace po = boost::program_options;
52 using namespace std;
53 
54 
55 int main(int argc, char **argv)
56 {
57  ros::init(argc, argv, "netft_node");
58  ros::NodeHandle nh;
59 
60  float pub_rate_hz;
61  string address;
62 
63  po::options_description desc("Options");
64  desc.add_options()
65  ("help", "display help")
66  ("rate", po::value<float>(&pub_rate_hz)->default_value(500.0), "set publish rate (in hertz)")
67  ("wrench", "publish older Wrench message type instead of WrenchStamped")
68  ("address", po::value<string>(&address), "IP address of NetFT box")
69  ;
70 
71  po::positional_options_description p;
72  p.add("address", 1);
73 
74  po::variables_map vm;
75  po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
76  po::notify(vm);
77 
78  if (vm.count("help"))
79  {
80  cout << desc << endl;
81  //usage(progname);
82  exit(EXIT_SUCCESS);
83  }
84 
85  if (!vm.count("address"))
86  {
87  cout << desc << endl;
88  cerr << "Please specify address of NetFT" << endl;
89  exit(EXIT_FAILURE);
90  }
91 
92  bool publish_wrench = false;
93  if (vm.count("wrench"))
94  {
95  publish_wrench = true;
96  ROS_WARN("Publishing NetFT data as geometry_msgs::Wrench is deprecated");
97  }
98 
99  ros::Publisher ready_pub;
100  std_msgs::Bool is_ready;
101  ready_pub = nh.advertise<std_msgs::Bool>("netft_ready", 1);
102  std::shared_ptr<netft_rdt_driver::NetFTRDTDriver> netft;
103  try
104  {
105  netft = std::shared_ptr<netft_rdt_driver::NetFTRDTDriver>(new netft_rdt_driver::NetFTRDTDriver(address));
106  is_ready.data = true;
107  ready_pub.publish(is_ready);
108  }
109  catch(std::runtime_error)
110  {
111  is_ready.data = false;
112  ready_pub.publish(is_ready);
113  }
114 
115  ros::Publisher pub;
116  if (publish_wrench)
117  {
118  pub = nh.advertise<geometry_msgs::Wrench>("netft_data", 100);
119  }
120  else
121  {
122  pub = nh.advertise<geometry_msgs::WrenchStamped>("netft_data", 100);
123  }
124  ros::Rate pub_rate(pub_rate_hz);
125  geometry_msgs::WrenchStamped data;
126 
127  ros::Duration diag_pub_duration(1.0);
128  ros::Publisher diag_pub = nh.advertise<diagnostic_msgs::DiagnosticArray>("/diagnostics", 2);
129  diagnostic_msgs::DiagnosticArray diag_array;
130  diag_array.status.reserve(1);
132  ros::Time last_diag_pub_time(ros::Time::now());
133 
134  while (ros::ok())
135  {
136  if (netft->waitForNewData())
137  {
138  netft->getData(data);
139  if (publish_wrench)
140  {
141  //geometry_msgs::Wrench(data.wrench);
142  pub.publish(data.wrench);
143  }
144  else
145  {
146  pub.publish(data);
147  }
148  }
149 
150  ros::Time current_time(ros::Time::now());
151  if ( (current_time - last_diag_pub_time) > diag_pub_duration )
152  {
153  diag_array.status.clear();
154  netft->diagnostics(diag_status);
155  diag_array.status.push_back(diag_status);
156  diag_array.header.stamp = ros::Time::now();
157  diag_pub.publish(diag_array);
158  ready_pub.publish(is_ready);
159  last_diag_pub_time = current_time;
160  }
161 
162  ros::spinOnce();
163  pub_rate.sleep();
164  }
165 
166  return 0;
167 }
void publish(const boost::shared_ptr< M > &message) const
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
data
#define ROS_WARN(...)
options
ROSCPP_DECL bool ok()
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
int main(int argc, char **argv)
Definition: netft_node.cpp:55
static Time now()
ROSCPP_DECL void spinOnce()


netft_utils
Author(s): Alex von Sternberg , Derek King, Andy Zelenak
autogenerated on Tue Mar 2 2021 03:15:08