Hello .
I would like to ask a basic C++ question.
I am currently working on the final project of ROS Basics in 5 Days (C++), which is a maze search.
I am currently creating a service server for collision detection.
I would like to adjust the subscriber to get the laser values and run the assignment, but I am stumped on the assignment.
I would like to read the values from a callback function called laser_callback in a callback function called obstacle_callback for collision as shown in the following code, but I can’t think of a good way to do it.
I’ve tried using pointers, declaring it as a global variable, etc., but I get an error when compiling.
If you could give me some advice on how to solve this problem, I would appreciate it.
#include “ros/ros.h”
#include "std_srvs/Trigger.h"
#include "sensor_msgs/LaserScan.h"
#include "my_turtlebot_topics/laser_turtle_sub.h"
laser_turtle_sub::laser_turtle_sub()
{
sub = nh.subscribe("/kobuki/laser/scan",1000,&laser_turtle_sub::laser_callback,this);
my_service = nh.advertiseService("/move_bb8_in_circle", &laser_turtle_sub::obstacle_callback,this);
}
void laser_turtle_sub::laser_callback(const sensor_msgs::LaserScan::ConstPtr& laser_msg)
{
float laser_range_c = laser_msg->ranges[360];
float laser_range_r = laser_msg->ranges[0];
float laser_range_l = laser_msg->ranges[719];
//ROS_INFO("LaserData Center:%f Right:%f Left:%f\n",laser_range_c,laser_range_r,laser_range_l);
}
bool laser_turtle_sub::obstacle_callback(std_srvs::Trigger::Request &req, std_srvs::Trigger::Response &res)
{
//サービス要請を受けたとき各方向のレーザーの値を取得する
res.message = laser_range_c;
ROS_INFO("%f",res.message);
return true;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "service_move_bb8_in_circle_server");
ros::NodeHandle nh;
laser_turtle_sub *lasersub = new laser_turtle_sub();
//vel_pub = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1000);
ROS_INFO("Service /move_bb8_in_circle_custom Ready");
ros::spin();
return 0;
}
thank you.
Translated with www.DeepL.com/Translator (free version)