0
$\begingroup$

Rosanswers logo

Hi,

I've written a small program which subscribes the topic "joy" from the joy_node (joystick) then it should process the date and publish it on a new topic "sub_dynamixel". Well but it doesn't, the code compiles but no published data available.

#include <sensor_msgs/Joy.h>
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "message/message_back.h"

#include <sstream>

#include <stdio.h>
#include <iostream>




void chatterCallback(const sensor_msgs::Joy::ConstPtr &joy)
{ 
  float position;
  int position_move;  

  ros::NodeHandle r;
  ros::Publisher chatter_pub = r.advertise<message::message_back>("sub_dynamixel", 1000);
  message::message_back msg; 
  position = joy->axes[0];


 if(position>0)
 {
 position_move=position*512;
 position_move+=512;
 }
 else if(position<0)
 position_move=512-(position*(-1)*512);
 else if(position==0)
 position_move=512;
    

 msg.position_move = position_move;      
 chatter_pub.publish(msg); 
}




int main(int argc, char **argv)
{
ros::init(argc, argv, "dynamixel_teleoperation");
ros::NodeHandle n;

ros::Subscriber sub = n.subscribe("joy", 1000, chatterCallback);
ros::spin();


return 0;
}

Originally posted by Ros451 on ROS Answers with karma: 18 on 2013-09-10

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

chatter_pub is being destroyed at the end of the function, before the message gets out. You should always create publishers at a more global scope, either as a global variable, or by wrapping your implementation up in a class, and having the chatter_pub as a member variable.

Similar problem to this question, although it was about a subscriber, not a publisher.


Originally posted by Bill Smart with karma: 1263 on 2013-09-10

This answer was ACCEPTED on the original site

Post score: 2


Original comments

Comment by Ros451 on 2013-09-10:
Thank you!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.