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
#include <ros/ros.h>
#include <std_msgs/String.h>
#include <geometry_msgs/Pose.h>
#include <vector>

class TowelThrower{
    public:

    ros::NodeHandle node_handle;
    ros::Publisher  new_coors_pub;
    ros::Subscriber vision_sub;

    TowelThrower()
    {
        new_coors_pub = node_handle.advertise<std_msgs::String>("coords_requests", 1000);
        vision_sub = node_handle.subscribe("vision_coors", 1, &TowelThrower::callback, this);
    }

    std::vector<geometry_msgs::Pose> create_path(double coorx, double coory)
    {
        std::vector<geometry_msgs::Pose> waypoints;
        return waypoints;
    }

    void callback(const geometry_msgs::Pose msg)
    {
        std_msgs::String nsg;
        nsg.data = "GIVE";

        ROS_INFO("%s", nsg.data.c_str());

        new_coors_pub.publish(nsg);
    }
};

int main(int argc, char** argv)
{
    ros::init(argc, argv, "towel_thrower_node");
    TowelThrower tt;

    ros::spin();

    return 0;
}

There are number of problems with this code:

  1. The compiler problem you are referencing is in the subscriber. You did not specify a queue size for parameter 2.
  2. It would have been good to include the header file to make this compile.
  3. The new_coords_pub and vision_sub are member variables but you also declared variable with the same the names in the constructor so the constructor will use the constructor declared variables which go out of scope when the constructor finishes.
  4. You did not declare an instance of the TowelThrower class.
  5. Not sure the use for the ros::AsyncSpinner or ros::Rate variables.
  6. ros::spin works just fine.
  7. You don't need to callros::shutdown after ros::spin.

Here is a solution to your problem:

#include <ros/ros.h>
#include <std_msgs/String.h>
#include <geometry_msgs/Pose.h>
#include <vector>

class TowelThrower{
    public:

    ros::NodeHandle node_handle;
    ros::Publisher  new_coors_pub;
    ros::Subscriber vision_sub;

    TowelThrower()
    {
        new_coors_pub = node_handle.advertise<std_msgs::String>("coords_requests", 1000);
        vision_sub = node_handle.subscribe("vision_coors", 1, &TowelThrower::callback, this);
    }

    std::vector<geometry_msgs::Pose> create_path(double coorx, double coory)
    {
        std::vector<geometry_msgs::Pose> waypoints;
        return waypoints;
    }

    void callback(const geometry_msgs::Pose msg)
    {
        std_msgs::String nsg;
        nsg.data = "GIVE";

        ROS_INFO("%s", nsg.data.c_str());

        new_coors_pub.publish(nsg);
    }
};

int main(int argc, char** argv)
{
    ros::init(argc, argv, "towel_thrower_node");
    TowelThrower tt;

    ros::spin();

    return 0;
}

There are number of problems with this your original code:

  1. The compiler problem you are referencing is in the subscriber. You did not specify a queue size for parameter 2.
  2. It would have been good to include the header file to make this compile.
  3. The new_coords_pub and vision_sub are member variables but you also declared variable with the same the names in the constructor so the constructor will use the constructor declared variables which go out of scope when the constructor finishes.
  4. You did not declare an instance of the TowelThrower class.
  5. Not sure the use for the ros::AsyncSpinner or ros::Rate variables.
  6. ros::spin works just fine.
  7. You don't need to callros::shutdown after ros::spin.

Here is a solution to your problem:

#include <ros/ros.h>
#include <std_msgs/String.h>
#include <geometry_msgs/Pose.h>
#include <vector>

class TowelThrower{
    public:

    ros::NodeHandle node_handle;
    ros::Publisher  new_coors_pub;
    ros::Subscriber vision_sub;

    TowelThrower()
    {
        new_coors_pub = node_handle.advertise<std_msgs::String>("coords_requests", 1000);
        vision_sub = node_handle.subscribe("vision_coors", 1, &TowelThrower::callback, this);
    }

    private:

    ros::NodeHandle node_handle;
    ros::Publisher  new_coors_pub;
    ros::Subscriber vision_sub;

    std::vector<geometry_msgs::Pose> create_path(double coorx, double coory)
    {
        std::vector<geometry_msgs::Pose> waypoints;
        return waypoints;
    }

    void callback(const geometry_msgs::Pose msg)
    {
        std_msgs::String nsg;
        nsg.data = "GIVE";

        ROS_INFO("%s", nsg.data.c_str());

        new_coors_pub.publish(nsg);
    }
};

int main(int argc, char** argv)
{
    ros::init(argc, argv, "towel_thrower_node");
    TowelThrower tt;

    ros::spin();

    return 0;
}

There are number of problems with your original code:

  1. The compiler problem you are referencing is in the subscriber. You did not specify a queue size for parameter 2.
  2. It would have been good to include the header file to make this compile.
  3. The new_coords_pub and vision_sub are member variables but you also declared variable with the same the names in the constructor so the constructor will use the constructor declared variables which go out of scope when the constructor finishes.
  4. You did not declare an instance of the TowelThrower class.
  5. Not sure the use for the ros::AsyncSpinner or ros::Rate variables.
  6. ros::spin works just fine.
  7. You don't need to callros::shutdown after ros::spin.
  8. create_path is not used and just distracts from the problem.
  9. Only the constructor needs to be public.