Hi i have done the python5 days and C++ intorduction course. and now i am working thrue the C++ course.
At the end of the python course there was an example of a class we can use and it was absolute geniouse, it made everything soooooooooooomuch more easy to understand. i have now started to program everything in classes,
but i must say that C++ is like 1 milion times harder to make a class where you can use a publisher and subscriber in the same class, there is not many examples and i am just lousing the will to live trying to figure this out.
Even to the point where i said right “i am stupid, i do not understand” so ive tried trial and error adding the & and * and even → operators to try and make the variables i read from the kobuki laser scan stay in a class variable so i can use it after the call back function but it just does not work…
can somone who know a bit more about C++ tell me what the hell i am dooing wrong
#include <ros/ros.h>
#include <nav_msgs/Odometry.h>
#include <geometry_msgs/Twist.h>
#include <sensor_msgs/LaserScan.h>
#include <list>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
class RobotControllClass{
float st10;
int st_counter = 0;
string vel_topic = "/cmd_vel";
const string odom_topic = "/odom";
const string laser_topic_test = "/kobuki/laser/scan";
const string laser_topic_test_publish= "/laser_publish";
public:
vector<float> st550;
nav_msgs::Odometry odom;
geometry_msgs::Twist cmd;
sensor_msgs::LaserScan::ConstPtr laser_scan_pointer;
sensor_msgs::LaserScan laser_scan;
ros::Publisher pub;
ros::Publisher pub_laser;
ros::Subscriber sub_laser;
ros::NodeHandle nh;
int counter;
RobotControllClass(ros::NodeHandle *outside_nh){
nh = *outside_nh;
pub = nh.advertise<geometry_msgs::Twist> (nh.resolveName(vel_topic), 1000);
pub_laser = nh.advertise<sensor_msgs::LaserScan> (nh.resolveName(laser_topic_test_publish), 1000);
sub_laser = nh.subscribe(nh.resolveName(laser_topic_test), 1000, &RobotControllClass::laser_callback_number, this);
usleep(2000000);
}
void laser_callback_number(const sensor_msgs::LaserScan::ConstPtr &msg);
void publish_to_topic();
void publish_laser_scan_info();
float get_laser_value();
};
void RobotControllClass::laser_callback_number(const sensor_msgs::LaserScan::ConstPtr &msg){
laser_scan = *msg;
st550 = laser_scan.ranges;
printf("number is: %f \n", st550[360]);
// it gets printed here
//st10 = msg -> range_min;
//ROS_INFO("hello and fuck you");
//pub_laser.publish(laser_scan);
}
void RobotControllClass::publish_to_topic(){
//pub.publish(counter_msg);
//ROS_INFO("message published was: %d", counter_msg.data);
}
float RobotControllClass::get_laser_value(){
return 0;
}
void RobotControllClass::publish_laser_scan_info(){
pub_laser.publish(laser_scan);
float value = get_laser_value();
cout << "hello: " << endl;
printf("fuck offff: %f \n",1514 );
//printf("number is: %f \n", st550[360]);
/////////////////////////////////////////////////
//but it does not work if i print it heere why???????????
}
int main (int argc, char** argv){
ros::init(argc, argv, "node_test_node");
ros::NodeHandle nh;
ROS_INFO("ros beeing started");
RobotControllClass st10(&nh);
ros::Rate rate(1);
while (ros::ok()){
ROS_INFO("hello adn fuckyou");
st10.publish_laser_scan_info();
ros::spinOnce();
rate.sleep();
}
}
The functions publish_laser_scan_info and laser_callback_number