ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

OK, I've solved the problem. So, once again ('cause seemingly the original description was not clear enough or too long to read(?) ;), my problem was: I HAD TO BE ABLE TO READ OUT AND STORE THE POSITIONS FROM THE MODEL IN GAZEBO "FROM TIME TO TIME, WHEN NEEDED" INSIDE A "GLOBAL" PROGRAM, WHICH HAS PLENTY OF DIFFERENT TASKS (So, creating a separate node for subscribing for messages of joint_state topic was of course not the solution I needed). HERE'S THE CODE I USED inside my "global program":

using namespace std;
float joint_pos_global;
void callback(const sensor_msgs::JointState & msg)
{
joint_pos_global = msg.position[3];
}
int main(int argc, char** argv) {
//--------//
ros::init(argc, argv, "listener");
ros::NodeHandle nh;
ros::Subscriber sub; sub = nh.subscribe ("joint_states", 10, callback);
//--------//

smth....smth...smth...smth...

//--Here I suddenly need to know the current position of joint Numb. 3--//
ros::spinOnce();
cout << "Position of joint 3 is" << joint_pos_global << "\n";
//--------------------------------------------------------------------------------------------------//

                           }

OK, I've solved the problem. So, once again ('cause seemingly the original description was not clear enough or too long to read(?) ;), my problem was: I HAD TO BE ABLE TO READ OUT AND STORE THE POSITIONS FROM THE MODEL IN GAZEBO "FROM TIME TO TIME, WHEN NEEDED" INSIDE A "GLOBAL" PROGRAM, WHICH HAS PLENTY OF OTHER DIFFERENT TASKS (So, creating a separate node for subscribing for messages of joint_state topic was of course not the solution I needed). HERE'S THE CODE I USED inside my "global program":

using namespace std;
float joint_pos_global;
void callback(const sensor_msgs::JointState & msg)
{
joint_pos_global = msg.position[3];
}
int main(int argc, char** argv) {
//--------//
ros::init(argc, argv, "listener");
ros::NodeHandle nh;
ros::Subscriber sub; sub = nh.subscribe ("joint_states", 10, callback);
//--------//

smth....smth...smth...smth...

//--Here I suddenly need to know the current position of joint Numb. 3--//
ros::spinOnce();
cout << "Position of joint 3 is" << joint_pos_global << "\n";
//--------------------------------------------------------------------------------------------------//

                           }