Asigning class private variables trough callback not working
Hey,
I'm using ROS Melodic on Ubuntu 18.04 and I'm trying to make a object orientated system. One of the classes I defined is a vision class.
Within this vision class I've defined a subscriber and a callback and some private variables. I've put them in a .h file. This .h file is used in my .cpp where my main is located. In my main I declare a vision object and a node handler. No matter what I do. Or how I try to assign the new values to my private variables, I'll always end up with only printing the standard object value. I've tried directly assigning it through this ->objectID = msg->data[0], without the this and also through making a setter. What would be a fix for this? My .h file
#pragma once
#include <ros/ros.h>
#include <std_msgs/Float32MultiArray.h>
using namespace std;
// Vision Class
class Vision
{
private:
//ros variable
ros::Subscriber vision_sub;
// private variables
char cameraID = 'None';
int cameraXResolution = 720;
int cameraYResolution = 1080;
float objectID = 2;
float xpersonCoordinate = 0;
float ypersonCoordinate = 0;
float zpersonCoordinate = 0;
public:
// constructors
// Ros vision subscriber
Vision(ros::NodeHandle *nh)
{
vision_sub = nh->subscribe("/VisionData", 1000, &Vision::callback_data, this);
}
// Member functions for visionClass
//Setters
void setCameraID(char cameraID)
{
this-> cameraID = cameraID;
}
void setCameraResolution(int cameraXResolution, int cameraYResolution)
{
this-> cameraXResolution = cameraXResolution,
this-> cameraYResolution = cameraYResolution;
}
void setObjectID(float objectID)
{
this-> objectID = objectID;
}
void setXYZCoordinates(float xpersonCoordinate, float ypersonCoordinate, float zpersonCoordinate)
{
this-> xpersonCoordinate = xpersonCoordinate,
this-> ypersonCoordinate = ypersonCoordinate,
this-> zpersonCoordinate = zpersonCoordinate;
}
//Getters
void getCameraResolution(int& cameraXResolution, int& cameraYResolution)
{
cameraXResolution = this-> cameraXResolution;
cameraYResolution = this-> cameraYResolution;
}
void getObjectID(float& objectID)
{
objectID = this-> objectID;
}
void getXYZCoordinates(float& xpersonCoordinate, float& ypersonCoordinate, float& zpersonCoordinate)
{
xpersonCoordinate = this-> xpersonCoordinate;
ypersonCoordinate = this-> ypersonCoordinate;
zpersonCoordinate = this-> zpersonCoordinate;
}
//Callback for ROS
void callback_data(const std_msgs::Float32MultiArray::ConstPtr& msg)
{
this -> objectID = msg->data[0];
// ROS_INFO("I heard: [%f]", msg->data[0]);
}
};
My .cpp file
//includes
#include <ros/ros.h>
#include "visionClass.h"
////////////////////////////////////////////////////////////////////////////////
// namespaces
using namespace std;
////////////////////////////////////////////////////////////////////////////////
// testing
float testvalue = 1;
////////////////////////////////////////////////////////////////////////////////
// function prototypes
////////////////////////////////////////////////////////////////////////////////
// memberfunction run()
int main (int argc, char **argv)
{
ros::init(argc, argv, "visionSystem");
ros::NodeHandle nh;
while(ros::ok())
{
Vision oakD(&nh);
oakD.getObjectID(testvalue);
ROS_INFO("From main I post:[%f]",testvalue);
ros::spinOnce();
}
}
////////////////////////////////////////////////////////////////////////////////
// functions
Asked by RamonS on 2022-12-02 09:40:59 UTC
Comments