robot_joint_state_publisher.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 Franka Emika GmbH
2 // Use of this source code is governed by the Apache-2.0 license, see LICENSE
3 #include <franka/exception.h>
4 #include <franka/robot.h>
5 #include <ros/ros.h>
6 #include <sensor_msgs/JointState.h>
7 
8 int main(int argc, char** argv) {
9  ros::init(argc, argv, "robot_joint_state_publisher");
10  ros::NodeHandle node_handle("~");
11 
12  std::vector<std::string> joint_names;
13  node_handle.getParam("joint_names", joint_names);
14 
15  std::string robot_ip;
16  node_handle.getParam("robot_ip", robot_ip);
17 
18  double publish_rate;
19  node_handle.getParam("publish_rate", publish_rate);
20 
21  ros::Rate rate(publish_rate);
22 
23  sensor_msgs::JointState states;
24  states.effort.resize(joint_names.size());
25  states.name.resize(joint_names.size());
26  states.position.resize(joint_names.size());
27  states.velocity.resize(joint_names.size());
28 
29  for (size_t i = 0; i < joint_names.size(); i++) {
30  states.name[i] = joint_names[i];
31  }
32 
33  ros::Publisher publisher = node_handle.advertise<sensor_msgs::JointState>("joint_states", 1);
34 
35  try {
36  franka::Robot robot(robot_ip);
37 
38  robot.read([&](const franka::RobotState& robot_state) {
39  states.header.stamp = ros::Time::now();
40  for (size_t i = 0; i < joint_names.size(); i++) {
41  states.position[i] = robot_state.q[i];
42  states.velocity[i] = robot_state.dq[i];
43  states.effort[i] = robot_state.tau_J[i];
44  }
45  publisher.publish(states);
46  ros::spinOnce();
47  rate.sleep();
48  return ros::ok();
49  });
50 
51  } catch (const franka::Exception& e) {
52  ROS_ERROR_STREAM("Exception: " << e.what());
53  return -1;
54  }
55 
56  return 0;
57 }
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)
std::array< double, 7 > q
int main(int argc, char **argv)
ROSCPP_DECL bool ok()
std::array< double, 7 > dq
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
void read(std::function< bool(const RobotState &)> read_callback)
bool sleep()
bool getParam(const std::string &key, std::string &s) const
static Time now()
#define ROS_ERROR_STREAM(args)
ROSCPP_DECL void spinOnce()
std::array< double, 7 > tau_J


franka_visualization
Author(s): Franka Emika GmbH
autogenerated on Fri Oct 23 2020 03:47:22