sr_publisher.cpp
Go to the documentation of this file.
1 
30 // ROS include
31 #include <ros/ros.h>
32 
33 // messages
34 #include <sr_robot_msgs/joints_data.h>
35 #include <sr_robot_msgs/joint.h>
36 #include <sensor_msgs/JointState.h>
37 
38 // generic C/C++ include
39 #include <vector>
40 #include <string>
41 #include <sstream>
42 
43 #include "sr_hand/sr_publisher.h"
44 
45 namespace shadowrobot
46 {
48 // CONSTRUCTOR/DESTRUCTOR //
51  n_tilde("~"), publish_rate(0.0)
52  {
54 
55  // set publish frequency
56  double publish_freq;
57  n_tilde.param("publish_frequency", publish_freq, 50.0);
58  publish_rate = Rate(publish_freq);
59 
60  // publishes JointState messages for the robot_state_publisher
61  std::string prefix;
62  std::string searched_param;
63  n_tilde.searchParam("shadowhand_prefix", searched_param);
64  n_tilde.param(searched_param, prefix, std::string());
65  std::string full_topic = prefix + "position/joint_states";
66  sr_jointstate_pos_pub = node.advertise<sensor_msgs::JointState>(full_topic, 2);
67  full_topic = prefix + "target/joint_states";
68  sr_jointstate_target_pub = node.advertise<sensor_msgs::JointState>(full_topic, 2);
69 
70  // publishes standard joints data (pos, targets, temp, current, ...)
71  full_topic = prefix + "shadowhand_data";
72  sr_pub = node.advertise<sr_robot_msgs::joints_data>(full_topic, 2);
73  }
74 
76  {
77  // if( shadowhand != NULL )
78  // delete shadowhand;
79  }
80 
82 // PUBLISH METHOD //
85  {
86  SRArticulatedRobot::JointsMap joints_map = sr_articulated_robot->getAllJointsData();
87 
88  sr_robot_msgs::joints_data msg;
89  std::vector<sr_robot_msgs::joint> jointVector;
90 
91  sensor_msgs::JointState jointstate_pos_msg;
92  sensor_msgs::JointState jointstate_target_msg;
93 
94  ros::Time now = ros::Time::now();
95  jointstate_pos_msg.header.stamp = now;
96  jointstate_target_msg.header.stamp = now;
97 
98  for (SRArticulatedRobot::JointsMap::const_iterator it = joints_map.begin(); it != joints_map.end(); ++it)
99  {
100  sr_robot_msgs::joint joint;
101  JointData currentData = it->second;
102 
103  // compute the angular velocity of the joint
104  if (currentData.last_pos_time.toSec() != 0.0)
105  {
106  currentData.velocity = (currentData.position - currentData.last_pos);
107  currentData.velocity /= (now - currentData.last_pos_time).toSec();
108  ROS_DEBUG("Velocity = (%f - %f)/(%f) = %f", currentData.position, currentData.last_pos,
109  (now - currentData.last_pos_time).toSec(), currentData.velocity);
110  }
111 
112  joint.joint_name = it->first;
113  jointstate_pos_msg.name.push_back(it->first);
114  jointstate_target_msg.name.push_back(it->first);
115 
116  jointstate_target_msg.position.push_back(toRad(currentData.target));
117  jointstate_target_msg.velocity.push_back(0.0);
118  jointstate_target_msg.effort.push_back(0.0);
119 
120  jointstate_pos_msg.position.push_back(toRad(currentData.position));
121  jointstate_pos_msg.velocity.push_back(currentData.velocity);
122  jointstate_pos_msg.effort.push_back(currentData.force);
123 
124  joint.joint_position = currentData.position;
125  joint.joint_target = currentData.target;
126  joint.joint_torque = currentData.force;
127  joint.joint_temperature = currentData.temperature;
128  joint.joint_current = currentData.current;
129 
130  // update data for the velocity
131  currentData.last_pos_time = now;
132  currentData.last_pos = currentData.position;
133 
134  sr_articulated_robot->joints_map_mutex.lock();
135  sr_articulated_robot->joints_map[it->first] = JointData(currentData);
136  sr_articulated_robot->joints_map_mutex.unlock();
137 
138  ROS_DEBUG("last_pos_time[%s] = %f / %f", it->first.c_str(), currentData.last_pos_time.toSec(),
139  joints_map[it->first].last_pos_time.toSec());
140 
141  jointVector.push_back(joint);
142  }
143 
144  msg.joints_list_length = jointVector.size();
145  msg.joints_list = jointVector;
146 
147  // publish standard data (pos, target, current, temp, force, ...)
148  sr_pub.publish(msg);
149  // publish JointState position message
150  sr_jointstate_pos_pub.publish(jointstate_pos_msg);
151  // publish JointState target message
152  sr_jointstate_target_pub.publish(jointstate_target_msg);
153 
154  ros::spinOnce();
156  }
157 
158 } // namespace shadowrobot
159 
160 
msg
SRPublisher(boost::shared_ptr< SRArticulatedRobot > sr_art_robot)
void publish(const boost::shared_ptr< M > &message) const
std::map< std::string, JointData > JointsMap
NodeHandle node
ros node handle
Definition: sr_publisher.h:74
Publisher sr_jointstate_target_pub
The publisher which publishes the data to the \/{prefix}\/target\/joint_states topic.
Definition: sr_publisher.h:84
boost::shared_ptr< SRArticulatedRobot > sr_articulated_robot
The shadowhand object (can be either an object connected to the real robot or a virtual hand)...
Definition: sr_publisher.h:79
~SRPublisher()
Destructor.
bool searchParam(const std::string &key, std::string &result) const
bool param(const std::string &param_name, T &param_val, const T &default_val) const
This class reads and publishes data concerning the shadowhand / shadowarm. To publish those data...
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
bool sleep()
Publisher sr_jointstate_pos_pub
The publisher which publishes the data to the \/{prefix}\/position\/joint_states topic.
Definition: sr_publisher.h:82
Rate publish_rate
the rate at which the data will be published. This can be set by a parameter in the launch file...
Definition: sr_publisher.h:76
static Time now()
double toRad(double deg)
Definition: sr_publisher.h:93
ROSCPP_DECL void spinOnce()
Publisher sr_pub
The publisher which publishes the data to the \/{prefix}\/shadowhand_data topic.
Definition: sr_publisher.h:86
#define ROS_DEBUG(...)


sr_hand
Author(s): Ugo Cupcic
autogenerated on Tue Oct 13 2020 03:55:53