Using an own .h file

asked 2021-02-05 11:09:36 -0500

freddy_14 gravatar image

Hey guys,

I'm trying to make a parrot like node in which I receive a message from a topic, modify it and publish it. I read the https://answers.ros.org/question/2298... thread and found the link to a very helpful video https://www.youtube.com/watch?v=lR3cK... in which a guy solves that by creating an own class.

So I did all that but, unlike it does in his video, my code isn't working. When trying catkin_make I get the errors:

    /home/plfr1011/ros_workspace/src/hska_neo/src/debug_cmd.cpp:8:25: error: expected initializer before ‘<’ token
 void PublisherSubscriber<std_msgs::Float64, geometry_msgs::TransformStamped>::subscriberCallback(const geometry_msgs::TransformStamped::ConstPtr& receivedMsg)
                         ^
/home/plfr1011/ros_workspace/src/hska_neo/src/debug_cmd.cpp: In function ‘int main(int, char**)’:
/home/plfr1011/ros_workspace/src/hska_neo/src/debug_cmd.cpp:29:5: error: ‘PublisherSubscriber’ was not declared in this scope
     PublisherSubscriber<std_msgs::Float64, geometry_msgs::TransformStamped> cmd_vel_parrot("cmd_vel/debug", "cmd_vel", 100);
     ^~~~~~~~~~~~~~~~~~~
/home/plfr1011/ros_workspace/src/hska_neo/src/debug_cmd.cpp:29:42: error: expected primary-expression before ‘,’ token
     PublisherSubscriber<std_msgs::Float64, geometry_msgs::TransformStamped> cmd_vel_parrot("cmd_vel/debug", "cmd_vel", 100);
                                          ^
/home/plfr1011/ros_workspace/src/hska_neo/src/debug_cmd.cpp:29:75: error: expected primary-expression before ‘>’ token
     PublisherSubscriber<std_msgs::Float64, geometry_msgs::TransformStamped> cmd_vel_parrot("cmd_vel/debug", "cmd_vel", 100);
                                                                           ^
/home/plfr1011/ros_workspace/src/hska_neo/src/debug_cmd.cpp:29:77: error: ‘cmd_vel_parrot’ was not declared in this scope
     PublisherSubscriber<std_msgs::Float64, geometry_msgs::TransformStamped> cmd_vel_parrot("cmd_vel/debug", "cmd_vel", 100);
                                                                             ^~~~~~~~~~~~~~
hska_neo/CMakeFiles/debug_cmd.dir/build.make:62: recipe for target 'hska_neo/CMakeFiles/debug_cmd.dir/src/debug_cmd.cpp.o' failed
make[2]: *** [hska_neo/CMakeFiles/debug_cmd.dir/src/debug_cmd.cpp.o] Error 1
CMakeFiles/Makefile2:2097: recipe for target 'hska_neo/CMakeFiles/debug_cmd.dir/all' failed
make[1]: *** [hska_neo/CMakeFiles/debug_cmd.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed

My node is:

#include <ros/ros.h>
#include <PublisherSubscriber.h>
#include <geometry_msgs/TransformStamped.h>
#include <geometry_msgs/Vector3.h>
#include <std_msgs/Float64.h>

template<>
void PublisherSubscriber<std_msgs::Float64, geometry_msgs::TransformStamped>::subscriberCallback(const geometry_msgs::TransformStamped::ConstPtr& receivedMsg)
{
    geometry_msgs::Vector3 v;

    v.x = receivedMsg.transform.translation.x;
    v.y = receivedMsg.transform.translation.y;
    v.z = receivedMsg.transform.translation.z;

    publisherObject.publish(length(v));
};

double length(geometry_msgs::Vector3 vector){
    double length;
    length = sqrt(pow(vector.x, 2)+pow(vector.y, 2)+pow(vector.z, 2));

    return(length);
};

int main(int argc, char **argv)
{
    ros::init(argc, argv, "debug_cmd");
    PublisherSubscriber<std_msgs::Float64, geometry_msgs::TransformStamped> cmd_vel_parrot("cmd_vel/debug", "cmd_vel", 100);
    ros::spin();

    return 0;
}

I think the problem is somewhere in the PublisherSubscriber.h file:

#ifdef PUBLISHER_SUBSCRIBER_H
#define PUBLISHER_SUBSCRIBER_H

#include <ros/ros.h>
#include <string>

template<typename PublishT,typename SubscribeT>
class PublisherSubscriber {
public:
    PublisherSubscriber() {}
    PublisherSubscriber(std::string publishTopicName, std::string subscribeTopicName, int queueSize)
    {
        publisherObject = nH.advertise<PublishT>(publishTopicName,queueSize);
        subscriberObject = nH.advertise<SubscribeT>(subscribeTopicName,queueSize,&PublisherSubscriber::subscriberCallback,this);
    }
    void subscriberCallback(const typename SubscribeT::ConstPtr& recievedMsg);

protected:
    ros::Subscriber subscriberObject;
    ros::Publisher publisherObject;
    ros::NodeHandle nH;
};

#endif

Thanks in advance for your help!

edit retag flag offensive close merge delete

Comments

There could be an error in debug_cmd.cpp. At least, that's what the cmake error is telling.

skpro19 gravatar image skpro19  ( 2021-02-06 01:46:19 -0500 )edit

I saw that as well but I couldn't find the error in the debug_cmd.cpp. I thought the error may be in the PublisherSubscriber.h file because when I do not #include <PublisherSubscriber.h> the same error is appearing.

freddy_14 gravatar image freddy_14  ( 2021-02-06 04:50:58 -0500 )edit
2

This is not an answer, but I wanted to make you aware of some built-in functionality:

I'm trying to make a parrot like node in which I receive a message from a topic, modify it and publish it.

For the specific example you show (ie: publish the 'length' of the translation part of a TransformStamped as a Float64), you can probably use topic_tools/transform.

See the numpy.linalg.norm(..) example on that page and #q261935 for a related question.

If possible, reuse.

gvdhoorn gravatar image gvdhoorn  ( 2021-02-06 04:55:31 -0500 )edit