Reputation:
How to make a subscriber and publisher in ROS on C++ in one file? I tried this and publisher works but subscriber callback function is not called
#include <ros/ros.h>
#include <iostream>
#include <std_msgs/UInt16.h>
#include <math.h>
int error = 0;
void error_sub(const std_msgs::UInt16::ConstPtr& msg)
{
ROS_INFO("I heard: [%d]", msg->data);
error = msg->data;
}
int main(int argc, char** argv)
{
ros::init(argc, argv, "lighter");
ros::NodeHandle nh;
ros::Publisher connected =nh.advertise<std_msgs::UInt16>("/robot/sonar/head_sonar/lights/set_lights",1);
ros::Subscriber sub = nh.subscribe("/plc/error", 1000, error_sub);
std_msgs::UInt16 msg;
while(ros::ok())
{
if(error >= 0)
{
msg.data = 36863;
connected.publish(msg);
}
ros::spinOnce();
}
}
Upvotes: 1
Views: 2473
Reputation:
I does not work because of the bad type of the message. It should be Int8
instead of UInt16
in callback function.
Upvotes: 0