Ros2 Subscriber

asked 2023-03-30 02:56:20 -0500

jaime0010 gravatar image

i have a publisher and a subscriber and i want to take the varibles to the main but it doesn’t work.

class MinimalSubscriber : public rclcpp::Node
 {    public:
// Constructor
float a;
// The name of the node is minimal_subscriber
MinimalSubscriber()
: Node("minimal_subscriber")
{
  // Create the subscription.
  // The topic_callback function executes whenever data is published
  // to the 'addison' topic.
  subscription_ = this->create_subscription<std_msgs::msg::Float32>(
  "addison", 10, std::bind(&MinimalSubscriber::topic_callback, this, _1));
}


// Receives the String message that is published over the topic
void topic_callback(const std_msgs::msg::Float32::SharedPtr msg) 
{
  // Write the message that was received on the console window
  RCLCPP_INFO(this->get_logger(), "I heard: '%f'", msg->data);
  a=msg->data;
  rclcpp::shutdown();
}
// Declare the subscription attribute
rclcpp::Subscription<std_msgs::msg::Float32>::SharedPtr subscription_;
  };

 int main(int argc, char * argv[])
{
// Launch ROS 2
 rclcpp::init(argc, argv);
 MinimalSubscriber1 subs;
 float c=MinimalSubscriber::a;
 // Prepare to receive messages that arrive on the topic
 rclcpp::spin(std::make_shared<MinimalSubscriber>());

  cout<<"number:__"<<c<<"___"<<endl;
  // Shutdown routine for ROS2
  rclcpp::shutdown();
   return 0;
 }

It print trash and i dont know why. Thanks if someone can help me.

edit retag flag offensive close merge delete