madgwick_filter “nonstatic member reference must be relative to a specific object” when passing member function

asked 2019-05-22 17:09:49 -0500

RayROS gravatar image

Hello, following my previous post I was able to successfully install and compile the imu_filter_madgwick on Ubuntu-18.04, ros-melodic.

I have been trying to solve a problem related to the imu_filters_madgwick tool. I am reading data from a .csv file and parse it. On the main() I am not able to access to the function void filter_function(const sensor_msgs::Imu &msg); currently present inside the class MADGWICK_reader as it is shown below. I know that in order to avoid the nonstatic member reference the function should be a static function but at the same time it is not so simple as changing or adding static in front of the function itself. However I decided to address this using the ampersend & symbol to pass it by reference. Unfortunately the main is still not seeing the function and I don't know what I am doing wrong. When it comes the following statement ros::Subscriber sub = n.subscribe("imu0", 10, lf); is when compilation stops. I thought that the subscriber could be able to see an external member function if passed by reference but it is not happening. The project I am developing is below:

madgwick_filter.h

#ifndef MADGWICK_H
#define MADGWICK_H
#include ......

namespace madgwick
{
    using timestamp_t = uint64_t;
    using timestampToDouble_t = double;

    struct IMU_MADGWICK_DATA
    {
        double magz;
        double magy;
        double magx;
        float accelz;
        float accelx;
        float accely;
        unsigned long timestamp; /// FIXME uint64_t?
        double phi;   // orientation
        double psi;   // orientation
        double theta; // orientation
        double gyrox; // angular velocity x
        double gyroy; // angular velocity y
        double gyroz; // angular velocity z
    };


    class Madgwick
    {
    public:
      Madgwick();

    };

    class MADGWICK_reader
    {
    public:
        MADGWICK_reader(std::string filename);
        bool nextLine();
        bool nextLineWithSupport();

        IMU_MADGWICK_DATA madgwick_data;

        sensor_msgs::Imu imuMadgwickMsg;
        sensor_msgs::MagneticField magMedgwickMsg;

        geometry_msgs::QuaternionStamped q;
        geometry_msgs::Vector3Stamped v;
        geometry_msgs::TransformStamped q_trans;
        float sampleFreq;
        double beta;
        double q0=1.0, q1=0.0, q2=0.0, q3=0.0;
        std_msgs::Header header;
        float ax, ay, az, gx, gy, gz;
        ros::Duration dtime;
        float dt;

        float invSqrt();
        void qua2Euler(geometry_msgs::QuaternionStamped);
        void madgwickIMU(float gx, float gy, float gz, float ax, float ay, float az);
        void filter_function(const sensor_msgs::Imu &msg);

    private:
        io::CSVReader<13> madg_imu_reader;
        unsigned int imuMadgNum;
        unsigned int magMadgNum;
        void packMadgMagMsg();
        void pack_Imu_Madgwick_Msg();
        void pack_Mag_Madgwick_Msg();
    };
}#endif // MADGWICK_H

madgwick_filter.cpp

namespace madgwick
{
    Madgwick::Madgwick()
    {

    }

    MADGWICK_reader::MADGWICK_reader(std::string filename):
        madg_imu_reader(filename)
    {
        // operations ...
    }

    void MADGWICK_reader::filter_function(const sensor_msgs::Imu &msg)
    {
        timestampToDouble_t currentTime = (madgwick_data.timestamp)/1e6;
        ros::Time stamp(currentTime);
        header = msg.header;
        ax = float(msg.linear_acceleration.x);
        ay = float(msg.linear_acceleration.y);
        az = float(msg.linear_acceleration.z);

        // more operations ...
    }
}

Lastly the node where I am testing the filter explained above: madgwick_filter_node.cpp

#include "../include/imu_filter_madgwick/madgwick_filter.h"
#include <iostream>

int main(int argc, char** argv)
{
    ros::init(argc, argv, "madgwick_filter_node");
    madgwick::MADGWICK_reader reader(std::string filename);

    ros::NodeHandle n;
    ros::Publisher pub1 = n.advertise<geometry_msgs::QuaternionStamped>("quaternion", 1);
    ros::Publisher pub2 = n.advertise<geometry_msgs::Vector3Stamped>("ypr", 1);

    tf::TransformBroadcaster q_brodecaster;

    madgwick::MADGWICK_reader obj(std::string file);
    auto lf = std::bind(&madgwick::MADGWICK_reader::filter_function, &obj, std::placeholders::_1);

    ros::Subscriber sub = n.subscribe("imu0", 10, lf); // <-- Error here

    ros::spin ...
(more)
edit retag flag offensive close merge delete

Comments

following my previous post I was able to successfully install and compile the imu_filter_madgwick on Ubuntu-18.04, ros-melodic.

I'm seriously confused here. Your last comment on #q323682 was:

I installed the binaries as you advised sudo apt install ros-melodic-imu-filter-madgwick

Can you please clarify whether you're still trying to compile it from sources, or whether this is some other problem after installing using apt?

gvdhoorn gravatar image gvdhoorn  ( 2019-05-23 02:53:00 -0500 )edit

Hello gvdhoorn, I am successfully using it through sudo apt install ros-melodic-imu-filter-madgwick as I commented on my previous post. However because I am not familiar with this specific tool offered by ROS in the past couple of days I have been going through the thesis and the paper published by Madgwick to understand the algorithm and how to use it in different situation. On the question I posted I created an exercise to convince myself about his paper and to better understand this specific tool. I am very interested in understanding the math behind it and the best way for me was to create a small example. The problem I have on the example I posted (you can copy and paste it on your computer) will not compile because it seems that the main can access to the class MADGWICK_reader but cannot access to its member functions. In my case ...(more)

RayROS gravatar image RayROS  ( 2019-05-23 08:34:39 -0500 )edit