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

Can't create constructor that creates a subscriber node inside

asked 2021-09-03 19:33:05 -0500

ModernNoob gravatar image

I am trying to program in C++ to get the GPS data from "/mavros/global_position/global". The code right now ( This is a header file ):

#pragma once

#include <sensor_msgs/NavSatFix.h>
#include <ros/ros.h>


class Localization{
public:
    // Functions

    void receiver(const sensor_msgs::NavSatFixConstPtr&);

    // Constructors
    Localization(ros::NodeHandle);
private:
    // Data types
    double latitiude;
    double longitude;
    double altitude;
    const std::string topic {"/mavros/global_position/global"};
};

void Localization::receiver(const sensor_msgs::NavSatFixConstPtr& fix){
    if (fix->status.status == sensor_msgs::NavSatStatus::STATUS_NO_FIX) {
        ROS_INFO("No fix.");
        return;
    }
    latitiude = (*fix).latitude;
    longitude = (*fix).longitude;
    altitude = (*fix).altitude;

    ROS_INFO("Latitude: %f | Longitude: %f | Altitude: %f", latitiude, longitude, altitude);
};

Localization::Localization(ros::NodeHandle nh){
    ros::Subscriber location = nh.subscribe(topic, 1, receiver);
};

However, when I tried to build it with catkin_make. I receive the following error:

error: invalid use of non-static member function ‘void Localization::receiver(const NavSatFixConstPtr&)’
     ros::Subscriber location = nh.subscribe(topic, 1, receiver);
                                                               ^
/home/kannachan/drone/src/quantum_drone/scripts/missions/gps.h:11:9: note: declared here
    void receiver(const sensor_msgs::NavSatFixConstPtr& fix){
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2021-09-04 10:15:12 -0500

Your function declaration is missing the argument name “fix”. You can put in that and try again

edit flag offensive delete link more

Comments

I figured it out. I need to rewrite ros::Subscriber location = nh.subscribe(topic, 1, receiver); as ros::Subscriber location = nh.subscribe(topic, 1, &Localization::receiver, this).

ModernNoob gravatar image ModernNoob  ( 2021-09-04 19:19:33 -0500 )edit

oh right, this is a class member, didn't see that..

Fetullah Atas gravatar image Fetullah Atas  ( 2021-09-05 10:03:50 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2021-09-03 19:33:05 -0500

Seen: 71 times

Last updated: Sep 04 '21