紧急通知:禁止一切关于政治&VPN翻墙等话题,发现相关帖子会立马删除封号
小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
4.5.3 修改其他节点的参数
-
遇到问题:
在PatrolClient类中写入更新服务参数函数update_server_param_k,在main函数中调用(node->update_server_param_k(1.5);)code代码报错:
函数 "PatrolClient::update_server_param_k" (已声明 所在行数:77) 不可访问,我没有找到代码的毛病,请求老师帮助
代码如下:#include <chrono> #include <cstdlib> #include <ctime> #include "rclcpp/rclcpp.hpp" #include "chapt4_interfaces/srv/patrol.hpp" #include "rcl_interfaces/msg/parameter.hpp" #include "rcl_interfaces/msg/parameter_value.hpp" #include "rcl_interfaces/msg/parameter_type.hpp" #include "rcl_interfaces/srv/set_parameters.hpp" using SetP = rcl_interfaces::srv::SetParameters; using namespace std::chrono_literals; using Patrol = chapt4_interfaces::srv::Patrol; class PatrolClient : public rclcpp::Node { public: PatrolClient() : Node("patrol_client") { patrol_client_ = this->create_client<Patrol>("patrol"); timer_ = this->create_wall_timer(10s, std::bind(&PatrolClient::timer_callback, this)); srand(time(NULL)); } void timer_callback() { while (!patrol_client_->wait_for_service(std::chrono::seconds(1))) { if(!rclcpp::ok()) { RCLCPP_ERROR(this->get_logger(),"等待服务端过程中被打断..."); return; } RCLCPP_INFO(this->get_logger(),"等待服务端上线中"); } auto request = std::make_shared<Patrol::Request>(); request->target_x = rand() % 15; request->target_y = rand() % 15; RCLCPP_INFO(this->get_logger(), "请求巡逻:(%f,%f)",request->target_x, request->target_y); patrol_client_->async_send_request( request, [&](rclcpp::Client<Patrol>::SharedFuture result_future) -> void { auto response = result_future.get(); if (response->result == Patrol::Response::SUCCESS) { RCLCPP_INFO(this->get_logger(), "目标点处理成功"); } else if (response->result == Patrol::Response::FALL) { RCLCPP_INFO(this->get_logger(), "目标点处理失败"); } }); } private: rclcpp::TimerBase::SharedPtr timer_; rclcpp::Client<Patrol>::SharedPtr patrol_client_; std::shared_ptr<SetP::Response> call_set_parameters( rcl_interfaces::msg::Parameter ¶meter) { auto param_client = this->create_client<SetP>("/turtle_controller/set_parameters"); while (!param_client->wait_for_service(std::chrono::seconds(1))) { if (!rclcpp::ok()) { RCLCPP_ERROR(this->get_logger(),"等待服务的过程中被打断..."); return nullptr; } RCLCPP_INFO(this->get_logger(),"等待参数设置服务端上线中"); } auto request = std::make_shared<SetP::Request>(); request->parameters.push_back(parameter); auto future = param_client->async_send_request(request); rclcpp::spin_until_future_complete(this->get_node_base_interface(), future); auto response = future.get(); return response; } void update_server_param_k(double k) { auto param = rcl_interfaces::msg::Parameter(); param.name = "k"; auto param_value = rcl_interfaces::msg::ParameterValue(); param_value.type = rcl_interfaces::msg::ParameterType::PARAMETER_DOUBLE; param_value.double_value = k; param.value = param_value; auto response = call_set_parameters(param); if (response == nullptr) { RCLCPP_WARN(this->get_logger(),"参数修改失败"); return; } else { for (auto result : response->results) { if (result.successful) { RCLCPP_INFO(this->get_logger(),"参数k已修改为:%f", k); } else { RCLCPP_WARN(this->get_logger(),"参数k失败原因:%s", result.reason.c_str()); } } } } }; int main(int argc, char **argv) { rclcpp::init(argc, argv); auto node = std::make_shared<PatrolClient>(); node->update_server_param_k(1.5); rclcpp::spin(node); rclcpp::shutdown(); return 0; }
-
@43996173 在 4.5.3 修改其他节点的参数 中说:
private:
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Client<Patrol>::SharedPtr patrol_client_;std::shared_ptr<SetP::Response> call_set_parameters( rcl_interfaces::msg::Parameter ¶meter) { auto param_client = this->create_client<SetP>("/turtle_controller/set_parameters"); while (!param_client->wait_for_service(std::chrono::seconds(1))) { if (!rclcpp::ok()) { RCLCPP_ERROR(this->get_logger(),"等待服务的过程中被打断..."); return nullptr; } RCLCPP_INFO(this->get_logger(),"等待参数设置服务端上线中"); } auto request = std::make_shared<SetP::Request>(); request->parameters.push_back(parameter); auto future = param_client->async_send_request(request); rclcpp::spin_until_future_complete(this->get_node_base_interface(), future); auto response = future.get(); return response; } void update_server_param_k(double k) {
注意这里的private,表示私有成员,不能通过对象.函数名字访问,请将其修改为public,这是一个基础的C++类的语法问题,请注意。
-
@小鱼 收到,谢谢,