没有那个文件或目录 6 | #include "chapt4_interfaces/srv/patrol.hpp",哪位大神能否帮忙看看问题在哪里?非常感谢
-
Ubuntu 22.04.5 LTS
jammy在按教材练习4.3.2服务端代码实现时,出现以下报错信息:
--- stderr: demo_cpp_service
/home/d/document/ros2_ws/src/demo_cpp_service/src/turtle_control.cpp:6:10: fatal error: chapt4_interfaces/srv/patrol.hpp: 没有那个文件或目录
6 | #include "chapt4_interfaces/srv/patrol.hpp"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
gmake[2]: *** [CMakeFiles/turtle_control.dir/build.make:76:CMakeFiles/turtle_control.dir/src/turtle_control.cpp.o] 错误 1
gmake[1]: *** [CMakeFiles/Makefile2:139:CMakeFiles/turtle_control.dir/all] 错误 2
gmake[1]: *** 正在等待未完成的任务....
gmake: *** [Makefile:146:all] 错误 2turtle_control.cpp内容如下:
#include "geometry_msgs/msg/twist.hpp"
#include "rclcpp/rclcpp.hpp"
#include "turtlesim/msg/pose.hpp"//1.添加服务头文件,并创建别名
#include "chapt4_interfaces/srv/patrol.hpp"
using Patrol=chapt4_interfaces::srv::Patrol;class TurtleController:public rclcpp::Node{
public :
TurtleController():Node("turtle_controller"){
//调用继承来父类来创建发布者
velocity_publisher_ = this->create_publisher<geometry_msgs::msg::Twist>("/turtle1/cmd_vel",10);
//调用继承来父类来创建订阅者
pose_subscription_ = this->create_subscriptionturtleism::msg::pose("/turtle1/pose",10,std::bind(&TurtleController::on_pose_received_,this,std::placeholders::_1));//3.创建服务 patrol_server_=this->create_service<Patrol>( //通过调用create_service创建服务,create_service是通过模板定义的,<>内的 Patrol 表示接口类型 //该方法的第一个参数是服务的名称,第二个参数是回调函数,这里使用Lambda表达式作为回调函数,它的参数是请求和响应对象的共享指针,在回调函数里,首先判断 //目标点是否超过给定的逻辑边界,若超过,则给response的result赋值失败常量,若不超过,则获取并设置目标点,给response的result赋值成功常量 "patrol", [&](const std::shared_ptr<Patrol::Request>request, std::shared_ptr<Patrol::Response> response) ->void{ //判断逻辑点是否在模拟器边界内 if ((0<request->target_x && request->target_x<12.0f) && (0<request->target_y && request->target_y<12.0f)){ target_x_=request->target_x; target_y_=request->target_y; response->result=Patrol::Response::SUCCESS; }else{ response->result=Patrol::Response::FAIL; } } ); } private: //2.添加Patrol类型服务共享指针 patrol_server_ 为成员变量 rclcpp::Service<Patrol>::SharedPtr patrol_server_; private: void on_pose_received_(const turtlesim::msg::Pose::SharedPtr pose){ //TODO: 收到位置计算误差,发布速度指令 auto message=geometry_msgs::msg::Twist(); //1.记录当前位置 double current_x=pose->x; double current_y=pose->y; RCLCPP_INFO(this->get_logger(),"当前位置:{x=%f,y=%f}",current_x,current_y); //2.计算与目标之间的距离,以及与当前海龟的朝向的角度差 double distance = std::sqrt((target_x_-current_x)*(target_x_-current_x)+(target_y_-current_y)*(target_y_-current_y)); double angle= std::atan2(target_y_-current_y,target_x_-current_x)-pose->theta; //3.控制策略:距离>0.1继续运动,角度差大于0.2则原地旋转,否则直行 if (distance >0.1){ if(fabs(angle)>0.2) { message.angular.z=fabs(angle); }else{ //通过比例控制器计算输出速度 message.linear.x=k_*distance; } } //4.限制最大值并发布消息 if (message.linear.x>max_speed_){ message.linear.x=max_speed_; } velocity_publisher_->publish(message); } private: rclcpp::Service<Patrol>::SharedPtr patrol_service_; rclcpp::Subscription<turtlesim::msg::Pose>::SharedPtr pose_subscription_; rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr velocity_publisher_; double target_x{1.0}; //目标X位置,设置默认值1.0 double target_y{1.0}; //目标Y位置,设置默认值1.0 double k_{1.0}; //比例系数,控制输出=误差x比例系数 double max_speed_{3.0}; //最大线速度,设置默认值3.0
};
int main(int argc,char **argv){
rclcpp::init(argc,argv);
auto node=std::make_shared<TurtleController>();
rclcpp::spin(node);
rclcpp:shutdown();
return 0;
}chapt4_interfaces的文件结构如下所示,
/home/d/图片/截图/截图 2025-03-22 16-16-52.png
在include/chapt4_interfaces不管试几次colcon build都不会有Patrol.hpp文件。不知道报错信息是指的哪个文件路径?也不知道#include "chapt4_interfaces/srv/patrol.hpp" 是指向的哪个路径?
如何才能解决这个问题?