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,
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.
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
.
You can resolve both issues by:
Init
member function and 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())
{
}