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

I'm trying to assign a value in a callback function and I get error for undendifined reference.

asked 2021-01-18 11:43:25 -0600

alexspi13 gravatar image

updated 2021-01-18 15:09:20 -0600

jayess gravatar image

I hava class UltrasonicSensors. I have my callback funtion as method and I try to assign the value of callback and then to use it in my method which has the subscriber. But I get this error:

/home/alex/catkin_ws/src/ros_ackerman_monster_truck/src/obstacle_avoidance/UltrasonicSensors.cpp:-1: error: undefined reference to `UltrasonicSensors::m_msg'
:-1: error: collect2: error: ld returned 1 exit status

Header file:

class UltrasonicSensors
        {

        public:
            UltrasonicSensors() {}
            ~UltrasonicSensors(){}

        static void u1_callback(const std_msgs::Int32& us_msg);
        static void u2_callback(const std_msgs::Int32& us_msg);
        static void u3_callback(const std_msgs::Int32& us_msg);
    int ultrasonic(const char* topic_name);
    static std_msgs::Int32 m_msg;

Cpp file::

    #include "UltrasonicSensors.h"
    #include <string.h>
    void UltrasonicSensors::u1_callback(const std_msgs::Int32& us_msg)
    {
     ROS_INFO("[%i]efefefe" , us_msg.data );
     m_msg = us_msg; // Here I want to assign the value of us_msg
    }
    void UltrasonicSensors::u2_callback(const std_msgs::Int32& us_msg)
    {ROS_INFO("[%i]" , us_msg.data );}


    void UltrasonicSensors::u3_callback(const std_msgs::Int32& us_msg)
    {ROS_INFO("[%i]" , us_msg.data );}

    int UltrasonicSensors::ultrasonic(const char* topic_name )
    {
        if(strcmp(topic_name,"ultrasonic_sensor_middle"))
            m_sub->subscribe(m_subscriber, "ultrasonic_sensor_middle", m_nh, u1_callback);
        else if(strcmp(topic_name,"ultrasonic_sensor_left"))
            m_sub->subscribe(m_subscriber, "ultrasonic_sensor_left", m_nh, u2_callback);
        else if(strcmp(topic_name,"ultrasonic_sensor_right"))
            m_sub->subscribe(m_subscriber, "ultrasonic_sensor_right", m_nh, u3_callback);
        else{ROS_ERROR("Invalid topic name for ultrasonic sensors!");}

      return m_msg.data;   //And return it here 
}
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-01-19 02:54:33 -0600

mgruhler gravatar image

The problem is that your member variable is declared static (why?).

You need to initialize a static member variable in the .cpp file, if you need that. See this SO question and maybe have a look at what static member variables are used for and how they are used here.

I suggest to NOT make this member variable static. In this use case this makes no sense as all instances of the UltrasonicSensors class would share the same variable and thus overwrite it. I guess this is not the intended behavior here.

edit flag offensive delete link more

Comments

I get error if the variable is non static. Compiler says that can't use non static variable in static method.

alexspi13 gravatar image alexspi13  ( 2021-01-19 09:57:25 -0600 )edit

Well, yes. Right. Forgot to circle around to that as well.

Basically, the same question here, why should those functions be static? I don't see a reason why they need to be static.

Looking again at your code, I'd also suggest that you revisit how subscribing and publishing works. Your ultrasonic function will probably not produce something meaningful. Typically you set up a subscriber to a topic once when the program starts, and tear it down when it stops. After you subscribe to a topic, there is a small delay until you start receiving information on the callbacks. Thus, I guess you'll

mgruhler gravatar image mgruhler  ( 2021-01-20 13:31:17 -0600 )edit

Is static because if it's not compiler send error. So you think it will be better idea to have subscriber in constructor? But how can I use the data in another class?

alexspi13 gravatar image alexspi13  ( 2021-01-21 12:47:00 -0600 )edit

Is static because if it's not compiler send error.

I don't think this qualifies as a good reason ;-) Please revisit for what you should use static member variables and functions, and where this doesn't make sense...

So you think it will be better idea to have subscriber in constructor?

Yes

But how can I use the data in another class?

You can store the data in a member variable and write a public getter function to access it. I.e. if the class then has an instance of that object.

Seeing what you have here, it might probably be even better to have the class subscribe to only one topic, that you set up during the initialization of that class, and store the data therein. Then have three instances (one for left, middle and right sensor respectively) and query them when you need to. Then you'll ...(more)

mgruhler gravatar image mgruhler  ( 2021-01-22 04:28:08 -0600 )edit

I change the pubslisher from arduino and now I'm sending from one subscriber, one Vector3 with all ultrasonic data. So now I need one callback for 3 ultrasonics. Can you have a look in my code please? Because I have issue with communication rate between arduino and Rpi, it's extremely slow. Maybe if changed something would be better? https://github.com/alexspirou/ROS_Ack...

alexspi13 gravatar image alexspi13  ( 2021-01-22 13:14:57 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2021-01-18 11:43:25 -0600

Seen: 380 times

Last updated: Jan 19 '21