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

Revision history [back]

The issue is that your timers are going out of scope and are destroyed. You need to make timerReadTemperature and timerPublishTemperature (private) class members.

The issue is that your timers are going out of scope and are destroyed. You need to make timerReadTemperature and timerPublishTemperature (private) class members.

class TemperatureSensor
{
public:
    TemperatureSensor(ros::NodeHandle *nh);
    double readTemperatureSensorData();
    void publishTemperature();

private:
    double temperature;
    ros::Publisher temperaturePublisher;
    ros::Timer timerReadTemperature;
};

and

TemperatureSensor::TemperatureSensor(ros::NodeHandle *nh)
{

    temperature = 0.0;
    temperaturePublisher =
            nh->advertise<std_msgs::Float64>("/temperature", 10);
    ROS_INFO("TempSensor Published.");

    timerReadTemperature =
            nh->createTimer(ros::Duration(1.0),
                    std::bind(&TemperatureSensor::readTemperatureSensorData, this));
    ROS_INFO("TempSensor Read timer started.");

    timerPublishTemperature =
            nh->createTimer(ros::Duration(1.0),
                    std::bind(&TemperatureSensor::publishTemperature, this));
    ROS_INFO("TempSensor Publish timer started.");
}