0

I am new to C++ and try to publish sensor data from an ARM chipset over UART with ROSserial_stm32 framework. Original sources came from a demonstration code written in C which gather data and send it over UART in a "string format". My aim is to replace those string sendings to ROS sensor_msgs frame format.

To achieve it, I have created the following header file:

/*
 * ROSserial.h
 *
 *  Created on: May 3, 2020
 *      Author: fofolevrai
 */

#ifndef INC_ROSSERIAL_H_
#define INC_ROSSERIAL_H_

#include <ros.h>
#include <geometry_msgs/Vector3.h>
#include <sensor_msgs/FluidPressure.h>
#include <sensor_msgs/Temperature.h>
#include <sensor_msgs/RelativeHumidity.h>

class ROSserial
{
private:
    //  Node handler
    ros::NodeHandle nh;
    //  Temperature data
    sensor_msgs::Temperature *lps22hb_air_temperature_t_;

public:
    //  Temperature publisher/topic
    ros::Publisher lps22hb_air_temperature_publisher_t;

    //  Class constructor
    ROSserial(void);

    //  Methods
    void Init(void);

};

#endif /* INC_ROSSERIAL_H_ */

And its associated C++ source file:

/*
 * ROSserial.cpp
 *
 *  Created on: May 4, 2020
 *      Author: fofolevrai
 */
#include "ROSserial.h"

// [ISSUE 1 ON FOLLOWING]
ROSserial::ROSserial(void)
{
    this->lps22hb_air_temperature_t_ = new sensor_msgs::Temperature();
}

void ROSserial::Init(void)
{
    //  Initialize ROS publisher [ISSUE 2 ON FOLLOWING]
    this->lps22hb_air_temperature_publisher_t("LPS22HB_Temperature", this->lps22hb_air_temperature_t_);

    //  Initialize ROS node
     this->nh.initNode();
     this->nh.advertise(lps22hb_air_temperature_publisher_t);
}

However, I've get the following errors:

ISSUE 1

../Core/Src/ROSserial.cpp: In constructor 'ROSserial::ROSserial()':
../Core/Src/ROSserial.cpp:9:26: error: no matching function for call to 'ros::Publisher::Publisher()'
 ROSserial::ROSserial(void)

ISSUE 2

../Core/Src/ROSserial.cpp: In member function 'void ROSserial::Init()':
../Core/Src/ROSserial.cpp:17:99: error: no match for call to '(ros::Publisher) (const char [20], sensor_msgs::Temperature*&)'
  this->lps22hb_air_temperature_publisher_t("LPS22HB_Temperature", this->lps22hb_air_temperature_t_);

I've been around the web for couple of days (1) (2) (3), looping and re-manipulating the code and still not able to understand what's wrong? I would really appreciate an help on compiler outputs?

Thanks for your help,

fofolevrai
  • 95
  • 1
  • 6
  • The type ros::Publisher has no the default constructor It means that you need to call an appropriate constructor with parameters in the mem-initializer lis. of the constructor ROSserial to create its data member lps22hb_air_temperature_publisher_t – Vlad from Moscow May 06 '20 at 22:25
  • @ Vlad from Moscow Thanks for pointing out the issue. – fofolevrai May 08 '20 at 11:01

1 Answers1

0

Issue 1

When you don't initialize member variables (and base classes) using initializer list in a constructor, they are default initialized. I.e.

ROSserial::ROSserial(void)
{
    this->lps22hb_air_temperature_t_ = new sensor_msgs::Temperature();
}

is equivalent to

ROSserial::ROSserial(void) : nh(),
                             lps22hb_air_temperature_t_(),
                             lps22hb_air_temperature_publisher_t()
{
    this->lps22hb_air_temperature_t_ = new sensor_msgs::Temperature();
}

The error message from the compiler indicates that lps22hb_air_temperature_publisher_t cannot be default initialized. Hence, it needs to be appropriately initialized.

Issue 2

this->lps22hb_air_temperature_publisher_t("LPS22HB_Temperature", this->lps22hb_air_temperature_t_);

That syntax is valid in the initialization list of a constructor to initialize a member but that is not the right syntax to set the value of the object. You could use

this->lps22hb_air_temperature_publisher_t = ros::Publisher("LPS22HB_Temperature", this->lps22hb_air_temperature_t_));

to set the value of lps22hb_air_temperature_publisher_t.

Resolution

You can resolve both issues by:

  1. Removing Init member function and
  2. Using a delegating constructor instead.
class ROSserial
{
   private:
      //  Node handler
      ros::NodeHandle nh;

      //  Temperature data
      sensor_msgs::Temperature *lps22hb_air_temperature_t_;

      // Constructor, for private usage.
      ROSserial(std::string const& desc,
                sensor_msgs::Temperature* temp);

   public:

      //  Temperature publisher/topic
      ros::Publisher lps22hb_air_temperature_publisher_t;

      //  Class constructor
      ROSserial();
};

ROSserial::ROSserial(std::string const& desc,
                     sensor_msgs::Temperature* temp) :
      lps22hb_air_temperature_t_(temp),
      lps22hb_air_temperature_publisher_t(desc, temp)
{
   // Initialize ROS node
   this->nh.initNode();
   this->nh.advertise(lps22hb_air_temperature_publisher_t);
}

// Use delegating constructor.
ROSserial::ROSserial() : ROSserial("LPS22HB_Temperature", new sensor_msgs::Temperature())
{
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • @R Sahu Average temperature is 20°C in northern France! Thanks a lot for your clear & detail answer. I am really grateful for your time! – fofolevrai May 08 '20 at 10:41
  • For ROS users, just replace the `std::string const& desc` by `const char * desc` to be type compatible with Publisher constructor. – fofolevrai May 08 '20 at 10:51
  • @bn059, Enjoy the nice weather if you are not required to be quarantined. I am glad I was able to help. Happy programming. – R Sahu May 08 '20 at 15:55