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

Calling a function in a function

asked 2013-12-19 01:54:30 -0500

chao gravatar image

updated 2013-12-21 21:31:37 -0500

tfoote gravatar image

Hi, can help me to check what is my mistake in the following code please? I did try to refer to http://wiki.ros.org/roscpp_tutorials/Tutorials/UsingClassMethodsAsCallbacks but fail. Thank you.

#include "ros/ros.h"
#include <visualization_msgs/Marker.h>
#include <pcl/ros/conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl_ros/point_cloud.h>

ros::Subscriber sub_;
ros::Publisher markerpub_;

    class Listener{
      public:
        double publishBbox();
      };

    void cloudcb(const PointCloud::ConstPtr&  cloud){
        publishBbox();
    }

    double Listener::publishBbox(){
        double x = (min_x_ + max_x_)/2;
        double y = (min_y_ + max_y_)/2;
        double z = (0 + max_z_)/2;

        double scale_x = (max_x_ - x)*2;
        double scale_y = (max_y_ - y)*2;
        double scale_z = (max_z_ - z)*2;

        visualization_msgs::Marker marker;
        marker.header.frame_id = "/camera_rgb_optical_frame";
        marker.header.stamp = ros::Time();
        marker.ns = "my_namespace";
        marker.id = 1;
        marker.type = visualization_msgs::Marker::CUBE;
        marker.action = visualization_msgs::Marker::ADD;
        marker.pose.position.x = x;
        marker.pose.position.y = -y;
        marker.pose.position.z = z;
        marker.pose.orientation.x = 0.0;
        marker.pose.orientation.y = 0.0;
        marker.pose.orientation.z = 0.0;
        marker.pose.orientation.w = 1.0;
        marker.scale.x = scale_x;
        marker.scale.y = scale_y;
        marker.scale.z = scale_z;
        marker.color.a = 0.5;
        marker.color.r = 0.0;
        marker.color.g = 1.0;
        marker.color.b = 0.0;
        //only if using a MESH_RESOURCE marker type:
        bboxpub_.publish( marker );
    }

    int main(int argc, char** argv){
        ros::init(argc, argv, "my_tf_broadcaster");

        ros::NodeHandle nh;

        sub_= nh.subscribe("camera/depth/points", 1, cloudcb);  
        Listener bbox;
        bboxpub_ = nh.advertise<visualization_msgs::Marker>("bbox",1, &Listener::publishBbox, &bbox);

        ros::spin();
        return 0;
    };
edit retag flag offensive close merge delete

Comments

Could you post the error messages please?

VC gravatar image VC  ( 2013-12-19 01:58:59 -0500 )edit

hi VC, my apologies. the error message was "error: ‘publishBbox’ was not declared in this scope". hav tried the suggestion given by Ben_S and it worked. Thanks :)

chao gravatar image chao  ( 2013-12-19 18:11:57 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2013-12-19 02:17:02 -0500

Ben_S gravatar image

cloudcb is not a member of your class Listener. Thats why it won't be possible to call publishBbox() from within. Make it a class method and add your object reference to the subscriber like you did for the publisher.

You should also get your variable names right and think about moving the subscriber and publisher into your class, initializing them in the constructor.

edit flag offensive delete link more

Comments

Thanks, it worked once i added the publishBbox in the class :)

chao gravatar image chao  ( 2013-12-19 18:12:46 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2013-12-19 01:54:30 -0500

Seen: 246 times

Last updated: Dec 19 '13